Skip to content

Commit

Permalink
Merge pull request #10 from 9bTFfT3qrJ/main
Browse files Browse the repository at this point in the history
changed the routes that give info about count and age of sessions gro…
  • Loading branch information
dulvui authored Aug 20, 2024
2 parents 803a022 + 9357557 commit 1790709
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
16 changes: 8 additions & 8 deletions web/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,26 @@ def finish_job():

'''
watchdog: if preshared secret matches, return the number
of sessions that have a pending question
of sessions for each state
'''
@app.route("/get_pending_count")
def get_pending_count():
@app.route("/get_state_count")
def get_state_count():
secret = str(request.args.get("secret"))
if secret != preshared_secret:
abort(403)
ret = sql_get_pending_count()
ret = sql_get_state_count()
return jsonify(ret)

'''
watchdog: if preshared secret matches, return the
age of the latest job in seconds
age of the latest session in seconds for each state
'''
@app.route("/get_latest_job_age")
def get_latest_job_age():
@app.route("/get_state_latest_age")
def get_state_latest_age():
secret = str(request.args.get("secret"))
if secret != preshared_secret:
abort(403)
ret = sql_get_latest_job_age()
ret = sql_get_state_latest_age()
return jsonify(ret)

if __name__ == '__main__':
Expand Down
29 changes: 19 additions & 10 deletions web/libsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,28 +130,37 @@ def sql_claim_job() -> Dict:
return ret


def sql_get_pending_count() -> Dict:
def sql_get_state_count() -> Dict:
conn = sqlite3.connect(sql_settings.get("db_path"))
curs = conn.cursor()
curs.execute('''
SELECT count(*) as cnt FROM session
WHERE state = 'question-queued' or state = 'processing-question';
SELECT state, count(*) as cnt FROM session
GROUP BY state;
''')
res = curs.fetchone()
ret = {"count": res[0]}
ret = {}
while True:
res = curs.fetchone()
if res is None:
break
ret[res[0]] = res[1]
conn.commit()
conn.close()
return ret


def sql_get_latest_job_age() -> Dict:
def sql_get_state_latest_age() -> Dict:
conn = sqlite3.connect(sql_settings.get("db_path"))
curs = conn.cursor()
curs.execute('''
select STRFTIME('%s', 'now') - max(modified) from session;
select state, STRFTIME('%s', 'now') - max(modified) as age from session
GROUP BY state;
''')
res = curs.fetchone()
ret = {"age": res[0]}
ret = {}
while True:
res = curs.fetchone()
if res is None:
break
ret[res[0]] = res[1]
conn.commit()
conn.close()
return ret
Expand All @@ -171,7 +180,7 @@ def sql_finish_job(unique_id: str, conversation_llm: str, conversation: str, sou
''', [conversation_llm, conversation, source, unique_id])
conn.commit()
conn.close()
print("success finished job for uuid = %s" % unique_id)
print("success finished job for uuid = %s" % unique_id)


def sql_heartbeat():
Expand Down

0 comments on commit 1790709

Please sign in to comment.