history: add an "exists" field to requested jobs

This field does not persist in the database as it is subject change if a file is moved or deleted when Moonraker is not running.

Also add thumbnail information to the metadata sans the base64 image data for each entry.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2021-03-14 18:25:35 -04:00
parent 62012726da
commit f09cd413ca
1 changed files with 15 additions and 9 deletions

View File

@ -9,6 +9,7 @@ HIST_NAMESPACE = "history"
class History: class History:
def __init__(self, config): def __init__(self, config):
self.server = config.get_server() self.server = config.get_server()
self.file_manager = self.server.lookup_plugin('file_manager')
database = self.server.lookup_plugin("database") database = self.server.lookup_plugin("database")
self.gcdb = database.wrap_namespace("gcode_metadata", parse_keys=False) self.gcdb = database.wrap_namespace("gcode_metadata", parse_keys=False)
@ -55,8 +56,7 @@ class History:
if job_id not in self.cached_job_ids: if job_id not in self.cached_job_ids:
raise self.server.error(f"Invalid job uid: {job_id}", 404) raise self.server.error(f"Invalid job uid: {job_id}", 404)
job = self.history_ns[job_id] job = self.history_ns[job_id]
job['job_id'] = job_id return {"job": self._prep_requested_job(job, job_id)}
return {"job": job}
if action == "DELETE": if action == "DELETE":
all = web_request.get_boolean("all", False) all = web_request.get_boolean("all", False)
if all: if all:
@ -99,8 +99,7 @@ class History:
if start != 0: if start != 0:
start -= 1 start -= 1
continue continue
job['job_id'] = job_id jobs.append(self._prep_requested_job(job, job_id))
jobs.append(job)
i += 1 i += 1
return {"count": end_num - start_num, "jobs": jobs} return {"count": end_num - start_num, "jobs": jobs}
@ -184,21 +183,28 @@ class History:
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")
metadata = self.gcdb.get(filename, {}) metadata = self.gcdb.get(filename, {})
metadata.pop("thumbnails", None) if "thumbnails" in metadata:
for thumb in metadata['thumbnails']:
thumb.pop('data', 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[self.current_job_id] = self.current_job.get_stats() self.history_ns[self.current_job_id] = self.current_job.get_stats()
def send_history_event(self, evt_action): def send_history_event(self, evt_action):
job = dict(self.current_job.get_stats()) job = self._prep_requested_job(
job['job_id'] = self.current_job_id self.current_job.get_stats(), self.current_job_id)
self.server.send_event("history:history_changed", self.server.send_event("history:history_changed",
{'action': evt_action, 'job': job}) {'action': evt_action, 'job': job})
def _prep_requested_job(self, job, job_id):
job['job_id'] = job_id
job['exists'] = self.file_manager.check_file_exists(
"gcodes", job['filename'])
return job
def on_exit(self): def on_exit(self):
self.finish_job("server_exit", self.print_stats) self.finish_job("server_exit", self.print_stats)
@ -225,7 +231,7 @@ class PrinterJob:
return getattr(self, name) return getattr(self, name)
def get_stats(self): def get_stats(self):
return self.__dict__ return self.__dict__.copy()
def set(self, name, val): def set(self, name, val):
if not hasattr(self, name): if not hasattr(self, name):