history: refactor the APIs for consistency

Add GET and DELETE `/server/history/job` APIs and remvoe the `DELETE /server/history/delete` API.  When returning a list of jobs, return it as an array rather than as a object.  The `/server/history/list` API no longer accepts a job id.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2021-03-12 06:50:34 -05:00
parent 9331031f55
commit 1367083f99
1 changed files with 29 additions and 28 deletions

View File

@ -27,7 +27,7 @@ class History:
"server:klippy_shutdown", self._save_job_on_error) "server:klippy_shutdown", self._save_job_on_error)
self.server.register_endpoint( self.server.register_endpoint(
"/server/history/delete", ['DELETE'], self._handle_job_delete) "/server/history/job", ['GET', 'DELETE'], self._handle_job_request)
self.server.register_endpoint( self.server.register_endpoint(
"/server/history/list", ['GET'], self._handle_jobs_list) "/server/history/list", ['GET'], self._handle_jobs_list)
@ -47,37 +47,37 @@ class History:
logging.info(f"Error subscribing to print_stats") logging.info(f"Error subscribing to print_stats")
self.print_stats = result.get("print_stats", {}) self.print_stats = result.get("print_stats", {})
async def _handle_job_delete(self, web_request): async def _handle_job_request(self, web_request):
all = web_request.get_boolean("all", False) action = web_request.get_action()
id = web_request.get_str("id", None) if action == "GET":
if all: id = web_request.get_str("id")
deljobs = [] if id not in self.history_ns:
for job in self.history_ns.keys(): raise self.server.error(f"Invalid job id: {id}", 404)
self.delete_job(job) job = self.history_ns[id]
deljobs.append(job) job['job_id'] = id
self.database.update_item("moonraker", JOBS_AUTO_INC_KEY, 0) return {"job": job}
self.metadata = [] if action == "DELETE":
return deljobs all = web_request.get_boolean("all", False)
if all:
deljobs = []
for job in self.history_ns.keys():
self.delete_job(job)
deljobs.append(job)
self.database.insert_item("moonraker", JOBS_AUTO_INC_KEY, 0)
self.metadata = []
return {'deleted_jobs': deljobs}
if id is None: id = web_request.get_str("id")
raise self.server.error("No ID to delete") if id not in self.history_ns.keys():
raise self.server.error(f"Invalid job id: {id}", 404)
if id not in self.history_ns.keys(): self.delete_job(id)
raise self.server.error(f"Invalid job id: {id}") return {'deleted_jobs': [id]}
self.delete_job(id)
return [id]
async def _handle_jobs_list(self, web_request): async def _handle_jobs_list(self, web_request):
id = web_request.get_str("id", None)
if id is not None:
if id not in self.history_ns:
raise self.server.error(f"Invalid job id: {id}")
return {id: self.history_ns[id]}
i = 0 i = 0
end_num = len(self.history_ns) end_num = len(self.history_ns)
jobs = {} jobs = []
start_num = 0 start_num = 0
before = web_request.get_float("before", -1) before = web_request.get_float("before", -1)
@ -100,10 +100,11 @@ class History:
if start != 0: if start != 0:
start -= 1 start -= 1
continue continue
jobs[id] = job job['job_id'] = id
jobs.append(job)
i += 1 i += 1
return {"count": end_num - start_num, "prints": jobs} return {"count": end_num - start_num, "jobs": jobs}
async def _status_update(self, data): async def _status_update(self, data):
if "print_stats" in data: if "print_stats" in data: