history: use mapping protocol to access namespaces where appropriate

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

View File

@ -49,7 +49,7 @@ class History:
async def _handle_job_delete(self, web_request): async def _handle_job_delete(self, web_request):
all = web_request.get_boolean("all", False) all = web_request.get_boolean("all", False)
id = str(web_request.get_int("id", -1)) id = web_request.get_str("id", None)
if all: if all:
deljobs = [] deljobs = []
for job in self.history_ns.keys(): for job in self.history_ns.keys():
@ -59,7 +59,7 @@ class History:
self.metadata = [] self.metadata = []
return deljobs return deljobs
if id == -1: if id is None:
raise self.server.error("No ID to delete") raise self.server.error("No ID to delete")
if id not in self.history_ns.keys(): if id not in self.history_ns.keys():
@ -69,26 +69,26 @@ class History:
return [id] return [id]
async def _handle_jobs_list(self, web_request): async def _handle_jobs_list(self, web_request):
id = str(web_request.get_int("id", -1)) id = web_request.get_str("id", None)
if id != "-1": if id is not None:
if id not in self.history_ns: if id not in self.history_ns:
raise self.server.error(f"Invalid job id: {id}") raise self.server.error(f"Invalid job id: {id}")
return {id: self.history_ns.get(id, {})} return {id: self.history_ns[id]}
before = web_request.get_float("before", -1)
since = web_request.get_float("since", -1)
limit = web_request.get_int("limit", 50)
start = web_request.get_int("start", 0)
if start > (len(self.history_ns)-1) or len(self.history_ns) == 0:
return {"count": len(self.history_ns), "prints": {}}
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)
since = web_request.get_float("since", -1)
limit = web_request.get_int("limit", 50)
start = web_request.get_int("start", 0)
if start >= end_num or end_num == 0:
return {"count": 0, "jobs": {}}
for id in self.history_ns.keys(): for id in self.history_ns.keys():
job = self.history_ns.get(id) job = self.history_ns[id]
if since != -1 and since > job.get('start_time'): if since != -1 and since > job.get('start_time'):
start_num += 1 start_num += 1
continue continue
@ -134,17 +134,17 @@ class History:
def add_job(self, job): def add_job(self, job):
self.current_job_id = str( self.current_job_id = str(
self.database.get_item("moonraker", JOBS_AUTO_INC_KEY)) self.database.get_item("moonraker", JOBS_AUTO_INC_KEY))
self.database.update_item("moonraker", JOBS_AUTO_INC_KEY, self.database.insert_item("moonraker", JOBS_AUTO_INC_KEY,
int(self.current_job_id)+1) int(self.current_job_id)+1)
self.current_job = job self.current_job = job
self.grab_job_metadata() self.grab_job_metadata()
self.history_ns.insert(self.current_job_id, job.get_stats()) self.history_ns[self.current_job_id] = job.get_stats()
def delete_job(self, id): def delete_job(self, id):
id = str(id) id = str(id)
if id in self.history_ns.keys(): if id in self.history_ns.keys():
self.history_ns.delete(id) del self.history_ns[id]
return return
@ -161,25 +161,19 @@ class History:
def get_job(self, id): def get_job(self, id):
id = str(id) id = str(id)
if id not in self.history_ns.keys(): return self.history_ns.get(id, None)
return None
return self.history_ns.get(id)
def grab_job_metadata(self): def grab_job_metadata(self):
if self.current_job is None: if self.current_job is None:
return return
filename = self.current_job.get("filename") filename = self.current_job.get("filename")
if filename not in self.gcdb:
return
metadata = self.gcdb.get(filename, {}) metadata = self.gcdb.get(filename, {})
metadata.pop("thumbnails", None) metadata.pop("thumbnails", None)
self.current_job.set("metadata", metadata) self.current_job.set("metadata", metadata)
def save_current_job(self): def save_current_job(self):
self.history_ns.update_child(self.current_job_id, self.history_ns[self.current_job_id] = self.current_job.get_stats()
self.current_job.get_stats())
class PrinterJob: class PrinterJob:
def __init__(self, data={}): def __init__(self, data={}):