From 3a1bf0cc8a257a66caebef4b412be4adb4ad7ce7 Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Thu, 14 Nov 2019 17:35:52 +0000 Subject: [PATCH] Updates API to use DB on get methods --- pisa/api.py | 38 +++++++++++++++++--------------------- pisa/db_manager.py | 11 +++++++++++ 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/pisa/api.py b/pisa/api.py index 562386f..131ce43 100644 --- a/pisa/api.py +++ b/pisa/api.py @@ -78,24 +78,26 @@ def get_appointment(): # ToDo: #15-add-system-monitor - appointment_in_watcher = watcher.locator_uuid_map.get(locator) + locator_map = watcher.db_manager.load_locator_map(locator) - if appointment_in_watcher: - for uuid in appointment_in_watcher: - appointment_data = watcher.appointments[uuid].to_dict() - appointment_data["status"] = "being_watched" - response.append(appointment_data) + if locator_map is not None: + for uuid in locator_map: + appointment_data = watcher.db_manager.load_watcher_appointment(uuid) - if watcher.responder: - responder_jobs = watcher.responder.jobs + if appointment_data is not None and appointment_data["triggered"] is False: + # Triggered is an internal flag that does not need to be send + del appointment_data["triggered"] - for job in responder_jobs.values(): - if job.locator == locator: - job_data = job.to_dict() + appointment_data["status"] = "being_watched" + response.append(appointment_data) + + job_data = watcher.db_manager.load_responder_job(uuid) + + if job_data is not None: job_data["status"] = "dispute_responded" response.append(job_data) - if not response: + else: response.append({"locator": locator, "status": "not_found"}) response = jsonify(response) @@ -105,18 +107,12 @@ def get_appointment(): @app.route("/get_all_appointments", methods=["GET"]) def get_all_appointments(): - watcher_appointments = {} - responder_jobs = {} - # ToDo: #15-add-system-monitor + response = None if request.remote_addr in request.host or request.remote_addr == "127.0.0.1": - for uuid, appointment in watcher.appointments.items(): - watcher_appointments[uuid] = appointment.to_dict() - - if watcher.responder: - for uuid, job in watcher.responder.jobs.items(): - responder_jobs[uuid] = job.to_dict() + watcher_appointments = watcher.db_manager.load_watcher_appointments() + responder_jobs = watcher.db_manager.load_responder_jobs() response = jsonify({"watcher_appointments": watcher_appointments, "responder_jobs": responder_jobs}) diff --git a/pisa/db_manager.py b/pisa/db_manager.py index d7ce42c..0b3d40a 100644 --- a/pisa/db_manager.py +++ b/pisa/db_manager.py @@ -52,6 +52,11 @@ class DBManager: self.db.put(key, value) + def load_entry(self, key): + data = self.db.get(key.encode("utf-8")) + data = json.loads(data) if data is not None else data + return data + def delete_entry(self, key, prefix=None): if isinstance(prefix, str): key = prefix + key @@ -60,6 +65,12 @@ class DBManager: self.db.delete(key) + def load_watcher_appointment(self, key): + return self.load_entry(WATCHER_PREFIX + key) + + def load_responder_job(self, key): + return self.load_entry(RESPONDER_PREFIX + key) + def load_watcher_appointments(self): all_appointments = self.load_appointments_db(prefix=WATCHER_PREFIX) non_triggered_appointments = {