Spaces:
Running
Running
Fix _count_user_annotations: query task_completion DB directly, fix superuser token lookup
Browse files- ls_proxy_hf.py +38 -17
ls_proxy_hf.py
CHANGED
|
@@ -692,11 +692,31 @@ def proxy_me():
|
|
| 692 |
|
| 693 |
|
| 694 |
def _count_user_annotations(uid, proj):
|
| 695 |
-
"""Count distinct tasks annotated by uid in proj
|
|
|
|
| 696 |
try:
|
| 697 |
conn = sqlite3.connect(DB_PATH)
|
| 698 |
-
|
| 699 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 700 |
conn.close()
|
| 701 |
admin_token = row[0] if row else None
|
| 702 |
except Exception:
|
|
@@ -726,19 +746,7 @@ def _count_user_annotations(uid, proj):
|
|
| 726 |
except Exception:
|
| 727 |
pass
|
| 728 |
|
| 729 |
-
|
| 730 |
-
try:
|
| 731 |
-
conn = sqlite3.connect(DB_PATH)
|
| 732 |
-
done = conn.execute(
|
| 733 |
-
"SELECT COUNT(DISTINCT tc.task_id) FROM task_completion tc "
|
| 734 |
-
"JOIN task t ON tc.task_id=t.id "
|
| 735 |
-
"WHERE tc.completed_by_id=? AND t.project_id=? AND tc.was_cancelled=0",
|
| 736 |
-
(uid, int(proj))
|
| 737 |
-
).fetchone()[0]
|
| 738 |
-
conn.close()
|
| 739 |
-
return done
|
| 740 |
-
except Exception:
|
| 741 |
-
return 0
|
| 742 |
|
| 743 |
|
| 744 |
@app.route("/proxy/init")
|
|
@@ -798,9 +806,22 @@ def proxy_count_debug():
|
|
| 798 |
ls_status = str(e)
|
| 799 |
else:
|
| 800 |
done = -3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 801 |
result = {"uid": uid, "proj": proj, "admin_token_found": admin_token is not None,
|
| 802 |
"ls_status": ls_status, "ls_tasks": ls_tasks, "done": done,
|
| 803 |
-
"
|
|
|
|
| 804 |
return Response(json.dumps(result, indent=2), content_type="application/json; charset=utf-8")
|
| 805 |
|
| 806 |
|
|
|
|
| 692 |
|
| 693 |
|
| 694 |
def _count_user_annotations(uid, proj):
|
| 695 |
+
"""Count distinct tasks annotated by uid in proj — DB-first, LS API fallback."""
|
| 696 |
+
# Primary: query task_completion table directly (no network call needed)
|
| 697 |
try:
|
| 698 |
conn = sqlite3.connect(DB_PATH)
|
| 699 |
+
done = conn.execute(
|
| 700 |
+
"SELECT COUNT(DISTINCT tc.task_id) FROM task_completion tc "
|
| 701 |
+
"JOIN task t ON tc.task_id=t.id "
|
| 702 |
+
"WHERE tc.completed_by_id=? AND t.project_id=? AND tc.was_cancelled=0",
|
| 703 |
+
(uid, int(proj))
|
| 704 |
+
).fetchone()[0]
|
| 705 |
+
conn.close()
|
| 706 |
+
if done > 0:
|
| 707 |
+
return done
|
| 708 |
+
except Exception:
|
| 709 |
+
pass
|
| 710 |
+
|
| 711 |
+
# Fallback: use superuser token from DB to call LS API
|
| 712 |
+
try:
|
| 713 |
+
conn = sqlite3.connect(DB_PATH)
|
| 714 |
+
# Get token for any superuser
|
| 715 |
+
row = conn.execute(
|
| 716 |
+
"SELECT at.key FROM authtoken_token at "
|
| 717 |
+
"JOIN htx_user u ON at.user_id = u.id "
|
| 718 |
+
"WHERE u.is_superuser=1 ORDER BY u.id LIMIT 1"
|
| 719 |
+
).fetchone()
|
| 720 |
conn.close()
|
| 721 |
admin_token = row[0] if row else None
|
| 722 |
except Exception:
|
|
|
|
| 746 |
except Exception:
|
| 747 |
pass
|
| 748 |
|
| 749 |
+
return 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 750 |
|
| 751 |
|
| 752 |
@app.route("/proxy/init")
|
|
|
|
| 806 |
ls_status = str(e)
|
| 807 |
else:
|
| 808 |
done = -3
|
| 809 |
+
# Also check task_completion directly
|
| 810 |
+
try:
|
| 811 |
+
conn2 = sqlite3.connect(DB_PATH)
|
| 812 |
+
db_done = conn2.execute(
|
| 813 |
+
"SELECT COUNT(DISTINCT tc.task_id) FROM task_completion tc "
|
| 814 |
+
"JOIN task t ON tc.task_id=t.id "
|
| 815 |
+
"WHERE tc.completed_by_id=? AND t.project_id=? AND tc.was_cancelled=0",
|
| 816 |
+
(uid, int(proj))
|
| 817 |
+
).fetchone()[0]
|
| 818 |
+
conn2.close()
|
| 819 |
+
except Exception as e:
|
| 820 |
+
db_done = str(e)
|
| 821 |
result = {"uid": uid, "proj": proj, "admin_token_found": admin_token is not None,
|
| 822 |
"ls_status": ls_status, "ls_tasks": ls_tasks, "done": done,
|
| 823 |
+
"db_done": db_done, "ls_url": LS_URL,
|
| 824 |
+
"_count_result": _count_user_annotations(uid, proj)}
|
| 825 |
return Response(json.dumps(result, indent=2), content_type="application/json; charset=utf-8")
|
| 826 |
|
| 827 |
|