mirror of
https://github.com/aljazceru/python-teos.git
synced 2025-12-17 22:24:23 +01:00
Updates API to use DB on get methods
This commit is contained in:
38
pisa/api.py
38
pisa/api.py
@@ -78,24 +78,26 @@ def get_appointment():
|
|||||||
|
|
||||||
# ToDo: #15-add-system-monitor
|
# 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:
|
if locator_map is not None:
|
||||||
for uuid in appointment_in_watcher:
|
for uuid in locator_map:
|
||||||
appointment_data = watcher.appointments[uuid].to_dict()
|
appointment_data = watcher.db_manager.load_watcher_appointment(uuid)
|
||||||
appointment_data["status"] = "being_watched"
|
|
||||||
response.append(appointment_data)
|
|
||||||
|
|
||||||
if watcher.responder:
|
if appointment_data is not None and appointment_data["triggered"] is False:
|
||||||
responder_jobs = watcher.responder.jobs
|
# Triggered is an internal flag that does not need to be send
|
||||||
|
del appointment_data["triggered"]
|
||||||
|
|
||||||
for job in responder_jobs.values():
|
appointment_data["status"] = "being_watched"
|
||||||
if job.locator == locator:
|
response.append(appointment_data)
|
||||||
job_data = job.to_dict()
|
|
||||||
|
job_data = watcher.db_manager.load_responder_job(uuid)
|
||||||
|
|
||||||
|
if job_data is not None:
|
||||||
job_data["status"] = "dispute_responded"
|
job_data["status"] = "dispute_responded"
|
||||||
response.append(job_data)
|
response.append(job_data)
|
||||||
|
|
||||||
if not response:
|
else:
|
||||||
response.append({"locator": locator, "status": "not_found"})
|
response.append({"locator": locator, "status": "not_found"})
|
||||||
|
|
||||||
response = jsonify(response)
|
response = jsonify(response)
|
||||||
@@ -105,18 +107,12 @@ def get_appointment():
|
|||||||
|
|
||||||
@app.route("/get_all_appointments", methods=["GET"])
|
@app.route("/get_all_appointments", methods=["GET"])
|
||||||
def get_all_appointments():
|
def get_all_appointments():
|
||||||
watcher_appointments = {}
|
|
||||||
responder_jobs = {}
|
|
||||||
|
|
||||||
# ToDo: #15-add-system-monitor
|
# ToDo: #15-add-system-monitor
|
||||||
|
response = None
|
||||||
|
|
||||||
if request.remote_addr in request.host or request.remote_addr == "127.0.0.1":
|
if request.remote_addr in request.host or request.remote_addr == "127.0.0.1":
|
||||||
for uuid, appointment in watcher.appointments.items():
|
watcher_appointments = watcher.db_manager.load_watcher_appointments()
|
||||||
watcher_appointments[uuid] = appointment.to_dict()
|
responder_jobs = watcher.db_manager.load_responder_jobs()
|
||||||
|
|
||||||
if watcher.responder:
|
|
||||||
for uuid, job in watcher.responder.jobs.items():
|
|
||||||
responder_jobs[uuid] = job.to_dict()
|
|
||||||
|
|
||||||
response = jsonify({"watcher_appointments": watcher_appointments, "responder_jobs": responder_jobs})
|
response = jsonify({"watcher_appointments": watcher_appointments, "responder_jobs": responder_jobs})
|
||||||
|
|
||||||
|
|||||||
@@ -52,6 +52,11 @@ class DBManager:
|
|||||||
|
|
||||||
self.db.put(key, value)
|
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):
|
def delete_entry(self, key, prefix=None):
|
||||||
if isinstance(prefix, str):
|
if isinstance(prefix, str):
|
||||||
key = prefix + key
|
key = prefix + key
|
||||||
@@ -60,6 +65,12 @@ class DBManager:
|
|||||||
|
|
||||||
self.db.delete(key)
|
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):
|
def load_watcher_appointments(self):
|
||||||
all_appointments = self.load_appointments_db(prefix=WATCHER_PREFIX)
|
all_appointments = self.load_appointments_db(prefix=WATCHER_PREFIX)
|
||||||
non_triggered_appointments = {
|
non_triggered_appointments = {
|
||||||
|
|||||||
Reference in New Issue
Block a user