fix(system): sort recent tasks by started_at desc
「最近任务」原先按 kv 键名排序。改为后端 `_task_records` 按 started_at 倒序写入 dict(缺开始时间排末尾,含进行中); 前端不再参与排序。配套 test_system_tasks_sorted_by_started_at_desc。
This commit was merged in pull request #5.
This commit is contained in:
1 parent
8e7ead1669
commit
3254648849
2 files changed
+60
-4
No files matched your search
@@ -622,19 +622,32 @@ class DashboardQueries:
|
||||
continue
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def _task_started_sort_key(value: Any) -> tuple[int, int]:
|
||||
"""缺 started_at 的排末尾;有值的按开始时间倒序(含进行中)。"""
|
||||
if isinstance(value, Mapping):
|
||||
raw = value.get("started_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_started_sort_key(pair[1]))
|
||||
# 插入序即开始时间倒序;前端 Object.entries 保留该序,勿再排序。
|
||||
return dict(items)
|
||||
|
||||
def _task_running(self, conn: sqlite3.Connection) -> dict[str, Any]:
|
||||
rows = conn.execute(
|
||||
|
||||
Reference in new issue
Block a user