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:
34
pisa/api.py
34
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 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})
|
||||
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user