fix(system): sort recent tasks by finished_at desc

「最近任务」原先按 kv 键名排序;改为按 finished_at 倒序,
缺完成时间的排末尾。前端渲染侧同步按 finished_at 排序兜底。
This commit is contained in:
bot-ci committed 2026-11-02 19:30:08 +08:00
1 parent 8e7ead1669
commit e7b48ba5d9
3 files changed
+70 -5

No files matched your search

+17 -4
View File
@@ -622,19 +622,32 @@ class DashboardQueries:
continue
return None
@staticmethod
def _task_finished_sort_key(value: Any) -> tuple[int, int]:
"""缺 finished_at 的排末尾;有值的按完成时间倒序。"""
if isinstance(value, Mapping):
raw = value.get("finished_at")
try:
return (0, -int(raw))
except (TypeError, ValueError):
pass
return (1, 0)
def _task_records(self, conn: sqlite3.Connection) -> dict[str, Any]:
rows = conn.execute(
"SELECT k, v FROM kv WHERE k LIKE 'task:last:%' ORDER BY k"
"SELECT k, v FROM kv WHERE k LIKE 'task:last:%'"
).fetchall()
result: dict[str, Any] = {}
items: list[tuple[str, Any]] = []
for row in rows:
name = str(row["k"]).removeprefix("task:last:")
try:
value = json.loads(str(row["v"]))
except (TypeError, json.JSONDecodeError):
value = {"raw": row["v"]}
result[name] = value
return result
items.append((name, value))
items.sort(key=lambda pair: self._task_finished_sort_key(pair[1]))
# 插入序即完成时间倒序;JSON/Object.entries 会保留。
return dict(items)
def _task_running(self, conn: sqlite3.Connection) -> dict[str, Any]:
rows = conn.execute(