Updates API to use DB on get methods

This commit is contained in:
Sergi Delgado Segura
2019-11-14 17:35:52 +00:00
parent f33d2d61ac
commit 3a1bf0cc8a
2 changed files with 28 additions and 21 deletions

View File

@@ -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 locator_map is not None:
for uuid in locator_map:
appointment_data = watcher.db_manager.load_watcher_appointment(uuid)
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"]
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 watcher.responder:
responder_jobs = watcher.responder.jobs
job_data = watcher.db_manager.load_responder_job(uuid)
for job in responder_jobs.values():
if job.locator == locator:
job_data = job.to_dict()
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})

View File

@@ -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 = {