dsh-workbench-tasks 0.0.0-stage → 5.2.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +26 -2
- package/cordis.patch.yml +7 -0
- package/lib/client.js +11963 -0
- package/lib/db/db-core.js +432 -0
- package/lib/db/db-legacy.js +473 -0
- package/lib/db/db.js +97 -0
- package/lib/db/ownership-path.js +33 -0
- package/lib/db/timeline-schema.js +7 -0
- package/lib/db/workspace-schema.js +15 -0
- package/lib/index.js +232 -0
- package/lib/infra/backup.js +289 -0
- package/lib/infra/config.js +462 -0
- package/lib/infra/data.js +368 -0
- package/lib/infra/sync.js +189 -0
- package/lib/publisher/brief.js +76 -0
- package/lib/publisher/catalog.js +99 -0
- package/lib/publisher/command.js +81 -0
- package/lib/stats/activity-calendar.js +74 -0
- package/lib/stats/difficulty-bins.js +49 -0
- package/lib/stats/events.js +40 -0
- package/lib/stats/overview.js +99 -0
- package/lib/stats/stats-today.js +26 -0
- package/lib/stats/stats.js +589 -0
- package/lib/stats/timeline-stats.js +174 -0
- package/lib/tagging/source.js +89 -0
- package/lib/tagging/tag-options.js +99 -0
- package/lib/tagging/tagging.js +371 -0
- package/lib/task/model/task-difficulty.js +15 -0
- package/lib/task/model/task-model.js +256 -0
- package/lib/task/model/task-ownership-path.js +22 -0
- package/lib/task/model/task-priority.js +33 -0
- package/lib/task/read/task-discovery.js +123 -0
- package/lib/task/read/task-publish-filter.js +31 -0
- package/lib/task/read/task-query.js +351 -0
- package/lib/task/read/task-search-schema.js +34 -0
- package/lib/task/read/task-view.js +146 -0
- package/lib/task/write/task-bulk.js +247 -0
- package/lib/task/write/task-claim.js +229 -0
- package/lib/task/write/task-create-schema.js +34 -0
- package/lib/task/write/task-create.js +178 -0
- package/lib/task/write/task-operations.js +207 -0
- package/lib/task/write/task-target.js +56 -0
- package/lib/task/write/task-transaction.js +18 -0
- package/lib/task/write/task-update.js +104 -0
- package/lib/tasks.js +456 -0
- package/lib/timeline/task-timelines.js +110 -0
- package/lib/timeline/timeline-actions.js +69 -0
- package/lib/timeline/timeline-migration.js +70 -0
- package/lib/timeline/timeline-store.js +57 -0
- package/lib/timeline/timeline-sync.js +46 -0
- package/lib/tree/task-node-write.js +26 -0
- package/lib/tree/tree.js +517 -0
- package/lib/workspace/workspace-actions.js +217 -0
- package/lib/workspace/workspace-store.js +196 -0
- package/package.json +144 -4
- package/shared/tree/limits.cjs +3 -0
|
@@ -0,0 +1,589 @@
|
|
|
1
|
+
// 任务统计报表:只读聚合,不写库、不发事件。
|
|
2
|
+
// 契约见 docs/specs/18-统计报表设计.md §4。
|
|
3
|
+
|
|
4
|
+
import { configValues } from "../infra/config.js";
|
|
5
|
+
import { todayActivity } from "./stats-today.js";
|
|
6
|
+
import { EVENTS, EVENT_TYPES, PLAN, eventRange, planRange } from "./events.js";
|
|
7
|
+
import { difficultyBinsFor, normalizeDimension } from "./difficulty-bins.js";
|
|
8
|
+
|
|
9
|
+
const STATUS_LABELS = {
|
|
10
|
+
"0": "已开启",
|
|
11
|
+
"1": "已关闭",
|
|
12
|
+
"2": "已完成",
|
|
13
|
+
"3": "已重启",
|
|
14
|
+
"4": "已接取",
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
function pad(n) {
|
|
18
|
+
return String(n).padStart(2, "0");
|
|
19
|
+
}
|
|
20
|
+
function dayStr(d) {
|
|
21
|
+
return d.getFullYear() + "-" + pad(d.getMonth() + 1) + "-" + pad(d.getDate());
|
|
22
|
+
}
|
|
23
|
+
function startOfDay(d) {
|
|
24
|
+
return new Date(d.getFullYear(), d.getMonth(), d.getDate());
|
|
25
|
+
}
|
|
26
|
+
function addDays(d, n) {
|
|
27
|
+
const x = new Date(d.getFullYear(), d.getMonth(), d.getDate());
|
|
28
|
+
x.setDate(x.getDate() + n);
|
|
29
|
+
return x;
|
|
30
|
+
}
|
|
31
|
+
function parseDay(value) {
|
|
32
|
+
const s = String(value || "");
|
|
33
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(s)) return null;
|
|
34
|
+
const p = s.split("-").map(Number);
|
|
35
|
+
const d = new Date(p[0], p[1] - 1, p[2]);
|
|
36
|
+
if (d.getFullYear() !== p[0] || d.getMonth() !== p[1] - 1 || d.getDate() !== p[2]) return null;
|
|
37
|
+
return d;
|
|
38
|
+
}
|
|
39
|
+
function diffDays(from, to) {
|
|
40
|
+
const a = Date.UTC(from.getFullYear(), from.getMonth(), from.getDate());
|
|
41
|
+
const b = Date.UTC(to.getFullYear(), to.getMonth(), to.getDate());
|
|
42
|
+
return Math.round((b - a) / 86400000);
|
|
43
|
+
}
|
|
44
|
+
function round(n, digits) {
|
|
45
|
+
if (!Number.isFinite(n)) return 0;
|
|
46
|
+
const f = Math.pow(10, digits === undefined ? 1 : digits);
|
|
47
|
+
return Math.round(n * f) / f;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function resolveRange(db, args) {
|
|
51
|
+
const today = startOfDay(new Date());
|
|
52
|
+
let from;
|
|
53
|
+
let to = today;
|
|
54
|
+
if (args.from || args.to) {
|
|
55
|
+
if (!args.from || !args.to) throw new Error("from 与 to 必须同时提供");
|
|
56
|
+
from = parseDay(args.from);
|
|
57
|
+
to = parseDay(args.to);
|
|
58
|
+
if (!from || !to) throw new Error("from/to 必须是合法的 YYYY-MM-DD 日期");
|
|
59
|
+
if (from > to) throw new Error("from 不能晚于 to");
|
|
60
|
+
} else {
|
|
61
|
+
const range = String(args.range || "30d");
|
|
62
|
+
if (["7d", "30d", "90d", "365d", "all"].indexOf(range) < 0) {
|
|
63
|
+
throw new Error("不支持的时间范围:" + range);
|
|
64
|
+
}
|
|
65
|
+
if (range === "all") {
|
|
66
|
+
const row = db.prepare(
|
|
67
|
+
"SELECT date(MIN(created_at)/1000,'unixepoch','localtime') AS d FROM task WHERE deleted_at IS NULL",
|
|
68
|
+
).get();
|
|
69
|
+
const earliest = row && row.d ? parseDay(row.d) : null;
|
|
70
|
+
from = earliest || addDays(today, -29);
|
|
71
|
+
} else {
|
|
72
|
+
from = addDays(today, -(parseInt(range, 10) - 1));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
const days = diffDays(from, to) + 1;
|
|
76
|
+
const bucket = days <= 120 ? "day" : days <= 730 ? "week" : "month";
|
|
77
|
+
const cfg = configValues(db);
|
|
78
|
+
const weekStartsOn = Number(cfg["stats.weekStartsOn"]) === 0 ? 0 : 1;
|
|
79
|
+
return { from: dayStr(from), to: dayStr(to), days: days, bucket: bucket, weekStartsOn: weekStartsOn };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function bucketKeys(range) {
|
|
83
|
+
const from = parseDay(range.from);
|
|
84
|
+
const to = parseDay(range.to);
|
|
85
|
+
const keys = [];
|
|
86
|
+
const index = Object.create(null);
|
|
87
|
+
for (let d = from; d <= to; d = addDays(d, 1)) {
|
|
88
|
+
let key;
|
|
89
|
+
if (range.bucket === "week") {
|
|
90
|
+
const ws = Number(range.weekStartsOn) === 0 ? 0 : 1;
|
|
91
|
+
const wd = (d.getDay() - ws + 7) % 7;
|
|
92
|
+
key = dayStr(addDays(d, -wd));
|
|
93
|
+
} else if (range.bucket === "month") {
|
|
94
|
+
key = dayStr(d).slice(0, 7);
|
|
95
|
+
} else {
|
|
96
|
+
key = dayStr(d);
|
|
97
|
+
}
|
|
98
|
+
if (!(key in index)) {
|
|
99
|
+
index[key] = keys.length;
|
|
100
|
+
keys.push(key);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return { keys: keys, index: index };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function bucketOf(day, range) {
|
|
107
|
+
const d = parseDay(day);
|
|
108
|
+
if (!d) return "";
|
|
109
|
+
if (range.bucket === "week") {
|
|
110
|
+
const ws = Number(range.weekStartsOn) === 0 ? 0 : 1;
|
|
111
|
+
const wd = (d.getDay() - ws + 7) % 7;
|
|
112
|
+
return dayStr(addDays(d, -wd));
|
|
113
|
+
}
|
|
114
|
+
if (range.bucket === "month") return day.slice(0, 7);
|
|
115
|
+
return day;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function loadNodes(db) {
|
|
119
|
+
const rows = db.prepare(
|
|
120
|
+
"SELECT id,name,parent_id,path FROM task_node WHERE deleted_at IS NULL",
|
|
121
|
+
).all();
|
|
122
|
+
const map = Object.create(null);
|
|
123
|
+
for (const row of rows) map[String(row.id)] = row;
|
|
124
|
+
return map;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function nodePathTitle(node, map) {
|
|
128
|
+
if (!node) return "";
|
|
129
|
+
const segments = String(node.path || "").split("/").filter(Boolean);
|
|
130
|
+
const names = [];
|
|
131
|
+
for (const id of segments) {
|
|
132
|
+
const n = map[id];
|
|
133
|
+
if (n) names.push(String(n.name));
|
|
134
|
+
}
|
|
135
|
+
return names.length ? names.join(" / ") : String(node.name || "");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function groupOf(node, map) {
|
|
139
|
+
if (!node) return null;
|
|
140
|
+
const segments = String(node.path || "").split("/").filter(Boolean);
|
|
141
|
+
if (segments.length >= 2 && segments[0] === "1") {
|
|
142
|
+
const gid = segments.length >= 3 ? segments[1] : segments[1];
|
|
143
|
+
return map[gid] || null;
|
|
144
|
+
}
|
|
145
|
+
let cur = node;
|
|
146
|
+
let guard = 0;
|
|
147
|
+
while (cur && String(cur.parent_id) !== "1" && guard < 50) {
|
|
148
|
+
cur = map[String(cur.parent_id)];
|
|
149
|
+
guard += 1;
|
|
150
|
+
}
|
|
151
|
+
if (cur && String(cur.id) === "1") return null;
|
|
152
|
+
return cur || null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function zeroTrend(range) {
|
|
156
|
+
const keys = bucketKeys(range).keys;
|
|
157
|
+
return keys.map(function (key) {
|
|
158
|
+
return { bucket: key, created: 0, done: 0, due: 0 };
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function fillTrend(trend, range, field, rows) {
|
|
163
|
+
const index = bucketKeys(range).index;
|
|
164
|
+
for (const row of rows) {
|
|
165
|
+
const key = bucketOf(String(row.d || ""), range);
|
|
166
|
+
if (!(key in index)) continue;
|
|
167
|
+
trend[index[key]][field] += Number(row.n || 0);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// 事件趋势:五条曲线(四类事件 + 计划目标)与年度活动日历完全同口径,
|
|
172
|
+
// 只是按页首区间的日/周/月桶再聚合一次。旧 trend 的 created/done/due 语义不同,
|
|
173
|
+
// 因此另起 eventTrend 字段,避免把老卡片的三条线搅浑。
|
|
174
|
+
const EVENT_TREND_SERIES = EVENT_TYPES.concat([PLAN.key]);
|
|
175
|
+
|
|
176
|
+
function zeroEventTrend(range) {
|
|
177
|
+
return bucketKeys(range).keys.map(function (key) {
|
|
178
|
+
const row = { bucket: key };
|
|
179
|
+
for (const name of EVENT_TREND_SERIES) row[name] = 0;
|
|
180
|
+
return row;
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function fillEventTrend(trend, range, type, rows) {
|
|
185
|
+
const index = bucketKeys(range).index;
|
|
186
|
+
for (const row of rows) {
|
|
187
|
+
const key = bucketOf(String(row.d || ""), range);
|
|
188
|
+
if (!(key in index)) continue;
|
|
189
|
+
trend[index[key]][type] += Number(row.n || 0);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** 单系列按日计数:事件用 EVENTS 口径,target 用 PLAN 计划口径(同一份日期定义)。 */
|
|
194
|
+
function countSeriesByDay(db, type, from, to) {
|
|
195
|
+
const spec = type === PLAN.key ? planRange(from, to) : eventRange(type, from, to);
|
|
196
|
+
const date = type === PLAN.key ? PLAN.date : EVENTS[type].date;
|
|
197
|
+
return db.prepare(
|
|
198
|
+
"SELECT " + date + " AS d, COUNT(*) AS n FROM task t WHERE " + spec.sql + " GROUP BY 1",
|
|
199
|
+
).all(...spec.params);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// 难度分布的五维切换:all 保持「当前存量」语义,其余四维与年度活动日历同口径、按页首区间重算。
|
|
203
|
+
export function statsReport(db, args, api) {
|
|
204
|
+
const input = args || {};
|
|
205
|
+
const range = resolveRange(db, input);
|
|
206
|
+
const dimension = normalizeDimension(input.dimension);
|
|
207
|
+
const top = Math.max(1, Math.min(20, Number(input.top) || 10));
|
|
208
|
+
const today = dayStr(startOfDay(new Date()));
|
|
209
|
+
const tomorrow = dayStr(addDays(startOfDay(new Date()), 1));
|
|
210
|
+
const next7 = dayStr(addDays(startOfDay(new Date()), 7));
|
|
211
|
+
const warnings = [];
|
|
212
|
+
|
|
213
|
+
const total = Number(db.prepare("SELECT COUNT(*) AS n FROM task WHERE deleted_at IS NULL").get().n || 0);
|
|
214
|
+
const statusRows = db.prepare(
|
|
215
|
+
"SELECT status, COUNT(*) AS n FROM task WHERE deleted_at IS NULL GROUP BY status",
|
|
216
|
+
).all();
|
|
217
|
+
const statusCount = Object.create(null);
|
|
218
|
+
for (const row of statusRows) statusCount[String(row.status)] = Number(row.n || 0);
|
|
219
|
+
const active = statusCount["0"] || 0;
|
|
220
|
+
const shutdown = statusCount["1"] || 0;
|
|
221
|
+
const done = statusCount["2"] || 0;
|
|
222
|
+
const restarted = statusCount["3"] || 0;
|
|
223
|
+
// 已接取单列,不并入已开启:两个口径相加不会超过总数。
|
|
224
|
+
const claimed = statusCount["4"] || 0;
|
|
225
|
+
|
|
226
|
+
// 逾期/今日到期/未来 7 天都覆盖已接取(4),文案注明「含已接取」。
|
|
227
|
+
const overdue = Number(db.prepare(
|
|
228
|
+
"SELECT COUNT(*) AS n FROM task WHERE deleted_at IS NULL AND status IN ('0','4') AND target_date IS NOT NULL AND target_date<>'' AND target_date<?",
|
|
229
|
+
).get(today).n || 0);
|
|
230
|
+
const dueToday = Number(db.prepare(
|
|
231
|
+
"SELECT COUNT(*) AS n FROM task WHERE deleted_at IS NULL AND status IN ('0','4') AND target_date=?",
|
|
232
|
+
).get(today).n || 0);
|
|
233
|
+
const dueNext7 = Number(db.prepare(
|
|
234
|
+
"SELECT COUNT(*) AS n FROM task WHERE deleted_at IS NULL AND status IN ('0','4') AND target_date>=? AND target_date<=?",
|
|
235
|
+
).get(tomorrow, next7).n || 0);
|
|
236
|
+
|
|
237
|
+
const trend = zeroTrend(range);
|
|
238
|
+
fillTrend(trend, range, "created", db.prepare(
|
|
239
|
+
"SELECT date(created_at/1000,'unixepoch','localtime') AS d, COUNT(*) AS n FROM task " +
|
|
240
|
+
"WHERE deleted_at IS NULL AND date(created_at/1000,'unixepoch','localtime') BETWEEN ? AND ? GROUP BY d",
|
|
241
|
+
).all(range.from, range.to));
|
|
242
|
+
fillTrend(trend, range, "done", db.prepare(
|
|
243
|
+
"SELECT done_date AS d, COUNT(*) AS n FROM task WHERE deleted_at IS NULL AND done_date IS NOT NULL AND done_date<>'' " +
|
|
244
|
+
"AND done_date BETWEEN ? AND ? GROUP BY done_date",
|
|
245
|
+
).all(range.from, range.to));
|
|
246
|
+
fillTrend(trend, range, "due", db.prepare(
|
|
247
|
+
"SELECT target_date AS d, COUNT(*) AS n FROM task WHERE deleted_at IS NULL AND target_date IS NOT NULL AND target_date<>'' " +
|
|
248
|
+
"AND target_date BETWEEN ? AND ? GROUP BY target_date",
|
|
249
|
+
).all(range.from, range.to));
|
|
250
|
+
const eventTrend = zeroEventTrend(range);
|
|
251
|
+
for (const type of EVENT_TREND_SERIES) {
|
|
252
|
+
fillEventTrend(eventTrend, range, type, countSeriesByDay(db, type, range.from, range.to));
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const newInRange = trend.reduce(function (n, row) { return n + row.created; }, 0);
|
|
256
|
+
const doneInRange = trend.reduce(function (n, row) { return n + row.done; }, 0);
|
|
257
|
+
|
|
258
|
+
const cycleRow = db.prepare(
|
|
259
|
+
"SELECT AVG(julianday(done_date) - julianday(date(created_at/1000,'unixepoch','localtime'))) AS v FROM task " +
|
|
260
|
+
"WHERE deleted_at IS NULL AND status='2' AND done_date IS NOT NULL AND done_date<>'' AND done_date BETWEEN ? AND ?",
|
|
261
|
+
).get(range.from, range.to);
|
|
262
|
+
const leadRow = db.prepare(
|
|
263
|
+
"SELECT AVG(julianday(done_date) - julianday(publish_date)) AS v FROM task " +
|
|
264
|
+
"WHERE deleted_at IS NULL AND status='2' AND done_date IS NOT NULL AND done_date<>'' AND done_date BETWEEN ? AND ? " +
|
|
265
|
+
"AND publish_date IS NOT NULL AND publish_date<>''",
|
|
266
|
+
).get(range.from, range.to);
|
|
267
|
+
|
|
268
|
+
const doneMissing = Number(db.prepare(
|
|
269
|
+
"SELECT COUNT(*) AS n FROM task WHERE deleted_at IS NULL AND status='2' AND (done_date IS NULL OR done_date='')",
|
|
270
|
+
).get().n || 0);
|
|
271
|
+
if (doneMissing > 0) {
|
|
272
|
+
warnings.push({
|
|
273
|
+
code: "done_date_missing",
|
|
274
|
+
message: doneMissing + " 个已完成任务缺少完成日期,未计入完成趋势与周期均值",
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const openZero = db.prepare(
|
|
279
|
+
"SELECT COUNT(*) AS n FROM task WHERE deleted_at IS NULL AND status='0' AND priority=0",
|
|
280
|
+
).get().n || 0;
|
|
281
|
+
if (openZero > 0) {
|
|
282
|
+
warnings.push({
|
|
283
|
+
code: "priority_zero_open",
|
|
284
|
+
message: openZero + " 个已开启任务优先级为 0(应为 1..10),请用 tasks.update 修正或运行优先级回填",
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const priorityRows = db.prepare(
|
|
289
|
+
"SELECT priority, COUNT(*) AS n FROM task WHERE deleted_at IS NULL GROUP BY priority",
|
|
290
|
+
).all();
|
|
291
|
+
// 单值分桶:0..10 各自一桶,不再合并区间——区间会把「9 和 10」糊在一起,看不出真实分布。
|
|
292
|
+
// 0 桶只可能是历史数据或异常数据(已开启任务应为 1..10,关闭/完成为 0),
|
|
293
|
+
// 图表不画它,但数据照常下发并标 hidden,导出与数据质量检查仍能拿到全量口径。
|
|
294
|
+
const priorityBins = [];
|
|
295
|
+
for (let p = 0; p <= 10; p += 1) {
|
|
296
|
+
priorityBins.push({ key: String(p), label: String(p), count: 0, hidden: p === 0 });
|
|
297
|
+
}
|
|
298
|
+
for (const row of priorityRows) {
|
|
299
|
+
const n = Number(row.n || 0);
|
|
300
|
+
const p = Math.max(0, Math.min(10, Math.round(Number(row.priority) || 0)));
|
|
301
|
+
priorityBins[p].count += n;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const difficultyBins = difficultyBinsFor(db, dimension, range);
|
|
305
|
+
|
|
306
|
+
const overdueRows = db.prepare(
|
|
307
|
+
"SELECT id,title,target_date,table_id FROM task WHERE deleted_at IS NULL AND status IN ('0','4') " +
|
|
308
|
+
"AND target_date IS NOT NULL AND target_date<>'' AND target_date<? ORDER BY target_date ASC, id ASC LIMIT ?",
|
|
309
|
+
).all(today, Math.min(top, 10));
|
|
310
|
+
const nodeMap = loadNodes(db);
|
|
311
|
+
const overdueList = overdueRows.map(function (row) {
|
|
312
|
+
const table = nodeMap[String(row.table_id)];
|
|
313
|
+
return {
|
|
314
|
+
id: String(row.id),
|
|
315
|
+
title: String(row.title || ""),
|
|
316
|
+
targetDate: String(row.target_date || ""),
|
|
317
|
+
daysOverdue: Math.max(0, diffDays(parseDay(String(row.target_date)), startOfDay(new Date()))),
|
|
318
|
+
tableTitle: table ? String(table.name) : "已删除任务表",
|
|
319
|
+
};
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
function limitOf(value) {
|
|
323
|
+
const n = Number(value);
|
|
324
|
+
return Number.isFinite(n) ? Math.max(1, Math.min(50, Math.floor(n))) : 10;
|
|
325
|
+
}
|
|
326
|
+
const recentPublishLimit = limitOf(input.recentPublishLimit);
|
|
327
|
+
const recentDoneLimit = limitOf(input.recentDoneLimit);
|
|
328
|
+
const recentShutdownLimit = limitOf(input.recentShutdownLimit);
|
|
329
|
+
const recentClaimedLimit = limitOf(input.recentClaimedLimit);
|
|
330
|
+
const recentTable = function (tableId) {
|
|
331
|
+
const table = nodeMap[String(tableId)];
|
|
332
|
+
return {
|
|
333
|
+
tableId: table ? String(table.id) : null,
|
|
334
|
+
groupId: table ? String(table.parent_id || "1") : null,
|
|
335
|
+
tableTitle: table ? String(table.name) : "已删除任务表",
|
|
336
|
+
tableDeleted: !table,
|
|
337
|
+
};
|
|
338
|
+
};
|
|
339
|
+
const todayStart = startOfDay(new Date());
|
|
340
|
+
const recentPublishRows = db.prepare(
|
|
341
|
+
"SELECT id,title,publish_date,table_id,difficulty FROM task WHERE deleted_at IS NULL " +
|
|
342
|
+
"ORDER BY (publish_date IS NULL OR publish_date=''), publish_date DESC, updated_at DESC, id DESC LIMIT ?",
|
|
343
|
+
).all(recentPublishLimit);
|
|
344
|
+
const recentPublish = recentPublishRows.map(function (row) {
|
|
345
|
+
const day = parseDay(String(row.publish_date || ""));
|
|
346
|
+
return {
|
|
347
|
+
id: String(row.id),
|
|
348
|
+
title: String(row.title || ""),
|
|
349
|
+
date: String(row.publish_date || ""),
|
|
350
|
+
daysAgo: day ? Math.max(0, diffDays(day, todayStart)) : null,
|
|
351
|
+
difficulty: Number(row.difficulty) || 0,
|
|
352
|
+
...recentTable(row.table_id),
|
|
353
|
+
};
|
|
354
|
+
});
|
|
355
|
+
const recentDoneRows = db.prepare(
|
|
356
|
+
"SELECT id,title,done_date,table_id,difficulty FROM task WHERE deleted_at IS NULL AND status='2' " +
|
|
357
|
+
"ORDER BY (done_date IS NULL OR done_date=''), done_date DESC, updated_at DESC, id DESC LIMIT ?",
|
|
358
|
+
).all(recentDoneLimit);
|
|
359
|
+
const recentDone = recentDoneRows.map(function (row) {
|
|
360
|
+
const day = parseDay(String(row.done_date || ""));
|
|
361
|
+
return {
|
|
362
|
+
id: String(row.id),
|
|
363
|
+
title: String(row.title || ""),
|
|
364
|
+
date: String(row.done_date || ""),
|
|
365
|
+
daysAgo: day ? Math.max(0, diffDays(day, todayStart)) : null,
|
|
366
|
+
difficulty: Number(row.difficulty) || 0,
|
|
367
|
+
...recentTable(row.table_id),
|
|
368
|
+
};
|
|
369
|
+
});
|
|
370
|
+
const recentShutdownRows = db.prepare(
|
|
371
|
+
"SELECT id,title,shutdown_date,table_id,difficulty FROM task WHERE deleted_at IS NULL AND status='1' " +
|
|
372
|
+
"ORDER BY (shutdown_date IS NULL OR shutdown_date=''), shutdown_date DESC, updated_at DESC, id DESC LIMIT ?",
|
|
373
|
+
).all(recentShutdownLimit);
|
|
374
|
+
const recentShutdown = recentShutdownRows.map(function (row) {
|
|
375
|
+
const day = parseDay(String(row.shutdown_date || ""));
|
|
376
|
+
return {
|
|
377
|
+
id: String(row.id),
|
|
378
|
+
title: String(row.title || ""),
|
|
379
|
+
date: String(row.shutdown_date || ""),
|
|
380
|
+
daysAgo: day ? Math.max(0, diffDays(day, todayStart)) : null,
|
|
381
|
+
difficulty: Number(row.difficulty) || 0,
|
|
382
|
+
...recentTable(row.table_id),
|
|
383
|
+
};
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
// 最近接取:只列真实接取时间(claimed_at 非空)且已接取过(2/4)的任务,按接取时间倒序;
|
|
387
|
+
// 历史任务普遍缺 claimed_at,不按发布日近似,避免伪造接取记录。
|
|
388
|
+
const recentClaimedRows = db.prepare(
|
|
389
|
+
"SELECT id,title,claimed_at,table_id,difficulty FROM task WHERE deleted_at IS NULL " +
|
|
390
|
+
"AND status IN ('2','4') AND claimed_at IS NOT NULL AND claimed_at <> '' AND claimed_at > 0 " +
|
|
391
|
+
"ORDER BY claimed_at DESC, updated_at DESC, id DESC LIMIT ?",
|
|
392
|
+
).all(recentClaimedLimit);
|
|
393
|
+
const recentClaimed = recentClaimedRows.map(function (row) {
|
|
394
|
+
// claimed_at 是毫秒时间戳,清单里的 date 统一转成本地日期,与完成/关闭清单同格式。
|
|
395
|
+
const ms = Number(row.claimed_at);
|
|
396
|
+
const at = Number.isFinite(ms) && ms > 0 ? new Date(ms) : null;
|
|
397
|
+
const day = at ? startOfDay(at) : null;
|
|
398
|
+
return {
|
|
399
|
+
id: String(row.id),
|
|
400
|
+
title: String(row.title || ""),
|
|
401
|
+
date: day ? dayStr(day) : "",
|
|
402
|
+
daysAgo: day ? Math.max(0, diffDays(day, todayStart)) : null,
|
|
403
|
+
difficulty: Number(row.difficulty) || 0,
|
|
404
|
+
...recentTable(row.table_id),
|
|
405
|
+
};
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
const agingBins = [
|
|
409
|
+
{ key: "1-3", label: "1–3 天", count: 0 },
|
|
410
|
+
{ key: "4-7", label: "4–7 天", count: 0 },
|
|
411
|
+
{ key: "8-14", label: "8–14 天", count: 0 },
|
|
412
|
+
{ key: "15-30", label: "15–30 天", count: 0 },
|
|
413
|
+
{ key: "30+", label: ">30 天", count: 0 },
|
|
414
|
+
];
|
|
415
|
+
const agingRows = db.prepare(
|
|
416
|
+
"SELECT target_date FROM task WHERE deleted_at IS NULL AND status IN ('0','4') AND target_date IS NOT NULL AND target_date<>'' AND target_date<?",
|
|
417
|
+
).all(today);
|
|
418
|
+
for (const row of agingRows) {
|
|
419
|
+
const age = Math.max(1, diffDays(parseDay(String(row.target_date)), startOfDay(new Date())));
|
|
420
|
+
if (age <= 3) agingBins[0].count += 1;
|
|
421
|
+
else if (age <= 7) agingBins[1].count += 1;
|
|
422
|
+
else if (age <= 14) agingBins[2].count += 1;
|
|
423
|
+
else if (age <= 30) agingBins[3].count += 1;
|
|
424
|
+
else agingBins[4].count += 1;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// 工作区排行是「存量」口径:任务按存活关联在每个工作区各计一次,所以各行合计会大于任务总数。
|
|
428
|
+
// 只认「关联存活 + 工作区存活」,与 tasks.list 的 workspaceIds / noWorkspace 过滤保持同一条件。
|
|
429
|
+
const workspaceAgg = Object.create(null);
|
|
430
|
+
for (const ws of db.prepare(
|
|
431
|
+
"SELECT id,title,name,dir FROM task_workspace WHERE deleted_at IS NULL",
|
|
432
|
+
).all()) {
|
|
433
|
+
workspaceAgg[String(ws.id)] = {
|
|
434
|
+
id: String(ws.id),
|
|
435
|
+
title: String(ws.title || ""),
|
|
436
|
+
name: String(ws.name || ""),
|
|
437
|
+
dir: ws.dir === null || ws.dir === undefined ? "" : String(ws.dir),
|
|
438
|
+
total: 0, open: 0, overdue: 0, done: 0,
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
const workspaceOfTask = Object.create(null);
|
|
442
|
+
for (const link of db.prepare(
|
|
443
|
+
"SELECT workspace_id,task_id FROM task_workspace_link WHERE deleted_at IS NULL",
|
|
444
|
+
).all()) {
|
|
445
|
+
const wsId = String(link.workspace_id);
|
|
446
|
+
if (!workspaceAgg[wsId]) continue; // 工作区已删:任务回落到「未关联工作区」
|
|
447
|
+
const taskId = String(link.task_id);
|
|
448
|
+
if (!workspaceOfTask[taskId]) workspaceOfTask[taskId] = [];
|
|
449
|
+
workspaceOfTask[taskId].push(wsId);
|
|
450
|
+
}
|
|
451
|
+
const noWorkspaceAgg = {
|
|
452
|
+
id: "__none__", title: "未关联工作区", name: "", dir: "",
|
|
453
|
+
total: 0, open: 0, overdue: 0, done: 0,
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
const allTasks = db.prepare(
|
|
457
|
+
"SELECT id,status,target_date,table_id FROM task WHERE deleted_at IS NULL",
|
|
458
|
+
).all();
|
|
459
|
+
const tableAgg = Object.create(null);
|
|
460
|
+
const groupAgg = Object.create(null);
|
|
461
|
+
for (const row of allTasks) {
|
|
462
|
+
const tableId = row.table_id === null || row.table_id === undefined ? "" : String(row.table_id);
|
|
463
|
+
const tableNode = tableId ? nodeMap[tableId] : null;
|
|
464
|
+
const tableKey = tableNode ? tableId : "__missing__";
|
|
465
|
+
if (!tableAgg[tableKey]) {
|
|
466
|
+
tableAgg[tableKey] = {
|
|
467
|
+
id: tableKey,
|
|
468
|
+
title: tableNode ? String(tableNode.name) : (tableId ? "已删除任务表" : "未归档"),
|
|
469
|
+
path: tableNode ? nodePathTitle(tableNode, nodeMap) : "",
|
|
470
|
+
total: 0, open: 0, overdue: 0, done: 0,
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
const isOpen = String(row.status) === "0";
|
|
474
|
+
const isDone = String(row.status) === "2";
|
|
475
|
+
const isOverdue =
|
|
476
|
+
(isOpen || String(row.status) === "4") &&
|
|
477
|
+
!!row.target_date &&
|
|
478
|
+
String(row.target_date) < today;
|
|
479
|
+
|
|
480
|
+
const t = tableAgg[tableKey];
|
|
481
|
+
t.total += 1;
|
|
482
|
+
if (isOpen) t.open += 1;
|
|
483
|
+
if (isDone) t.done += 1;
|
|
484
|
+
if (isOverdue) t.overdue += 1;
|
|
485
|
+
|
|
486
|
+
const groupNode = groupOf(tableNode, nodeMap);
|
|
487
|
+
const groupKey = groupNode ? String(groupNode.id) : "__ungrouped__";
|
|
488
|
+
if (!groupAgg[groupKey]) {
|
|
489
|
+
groupAgg[groupKey] = {
|
|
490
|
+
id: groupKey,
|
|
491
|
+
title: groupNode ? String(groupNode.name) : "未归档",
|
|
492
|
+
total: 0, open: 0, overdue: 0, done: 0,
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
const g = groupAgg[groupKey];
|
|
496
|
+
g.total += 1;
|
|
497
|
+
if (isOpen) g.open += 1;
|
|
498
|
+
if (isDone) g.done += 1;
|
|
499
|
+
if (isOverdue) g.overdue += 1;
|
|
500
|
+
|
|
501
|
+
const wsIds = workspaceOfTask[String(row.id)] || [];
|
|
502
|
+
if (!wsIds.length) {
|
|
503
|
+
noWorkspaceAgg.total += 1;
|
|
504
|
+
if (isOpen) noWorkspaceAgg.open += 1;
|
|
505
|
+
if (isDone) noWorkspaceAgg.done += 1;
|
|
506
|
+
if (isOverdue) noWorkspaceAgg.overdue += 1;
|
|
507
|
+
} else {
|
|
508
|
+
for (const wsId of wsIds) {
|
|
509
|
+
const bucket = workspaceAgg[wsId];
|
|
510
|
+
if (!bucket) continue;
|
|
511
|
+
bucket.total += 1;
|
|
512
|
+
if (isOpen) bucket.open += 1;
|
|
513
|
+
if (isDone) bucket.done += 1;
|
|
514
|
+
if (isOverdue) bucket.overdue += 1;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
function withCompletion(item) {
|
|
519
|
+
item.completion = item.total > 0 ? round(item.done / item.total, 3) : 0;
|
|
520
|
+
return item;
|
|
521
|
+
}
|
|
522
|
+
function rank(list) {
|
|
523
|
+
return list.map(withCompletion).sort(function (a, b) {
|
|
524
|
+
return (b.open + b.overdue) - (a.open + a.overdue) || b.total - a.total;
|
|
525
|
+
}).slice(0, top);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
const totals = {
|
|
529
|
+
total: total,
|
|
530
|
+
active: active,
|
|
531
|
+
shutdown: shutdown,
|
|
532
|
+
done: done,
|
|
533
|
+
restarted: restarted,
|
|
534
|
+
claimed: claimed,
|
|
535
|
+
overdue: overdue,
|
|
536
|
+
dueToday: dueToday,
|
|
537
|
+
dueNext7: dueNext7,
|
|
538
|
+
newInRange: newInRange,
|
|
539
|
+
};
|
|
540
|
+
const rates = {
|
|
541
|
+
completion: total > 0 ? round(done / total, 3) : 0,
|
|
542
|
+
// 逾期率分母用「开启+已接取」,与逾期分子口径一致。
|
|
543
|
+
overdue: active + claimed > 0 ? round(overdue / (active + claimed), 3) : 0,
|
|
544
|
+
restart: total > 0 ? round(restarted / total, 3) : 0,
|
|
545
|
+
shutdown: total > 0 ? round(shutdown / total, 3) : 0,
|
|
546
|
+
avgLeadDays: leadRow && Number.isFinite(Number(leadRow.v)) ? round(Number(leadRow.v), 1) : null,
|
|
547
|
+
avgCycleDays: cycleRow && Number.isFinite(Number(cycleRow.v)) ? round(Number(cycleRow.v), 1) : null,
|
|
548
|
+
};
|
|
549
|
+
|
|
550
|
+
return {
|
|
551
|
+
generatedAt: Date.now(),
|
|
552
|
+
range: range,
|
|
553
|
+
today: todayActivity(db, today),
|
|
554
|
+
totals: totals,
|
|
555
|
+
rates: rates,
|
|
556
|
+
breakdowns: {
|
|
557
|
+
status: ["0", "1", "2", "4"].map(function (key) {
|
|
558
|
+
return { key: key, label: STATUS_LABELS[key], count: statusCount[key] || 0 };
|
|
559
|
+
}),
|
|
560
|
+
priority: priorityBins,
|
|
561
|
+
difficulty: difficultyBins,
|
|
562
|
+
// 客户端据此回显当前页签;维度只作用于难度分布,其余区块仍是存量口径。
|
|
563
|
+
difficultyDimension: dimension,
|
|
564
|
+
},
|
|
565
|
+
trend: trend,
|
|
566
|
+
// 事件趋势与年度活动日历同口径:四类事件 + 目标计划数,按日/周/月桶聚合。
|
|
567
|
+
eventTrend: eventTrend,
|
|
568
|
+
eventTrendTypes: EVENT_TREND_SERIES,
|
|
569
|
+
aging: agingBins,
|
|
570
|
+
rankings: {
|
|
571
|
+
tables: rank(Object.keys(tableAgg).map(function (k) { return tableAgg[k]; })),
|
|
572
|
+
groups: rank(Object.keys(groupAgg).map(function (k) { return groupAgg[k]; })),
|
|
573
|
+
workspaces: rank(Object.keys(workspaceAgg).map(function (k) { return workspaceAgg[k]; })),
|
|
574
|
+
noWorkspace: withCompletion(noWorkspaceAgg),
|
|
575
|
+
},
|
|
576
|
+
lists: { overdue: overdueList, recentPublish: recentPublish, recentDone: recentDone, recentShutdown: recentShutdown, recentClaimed: recentClaimed },
|
|
577
|
+
// 能力标记:前端据此区分「后端未返回某清单」与「清单确实为空」,旧后端缺该字段时按键存在性判断。
|
|
578
|
+
capabilities: { lists: ["overdue", "recentPublish", "recentDone", "recentShutdown", "recentClaimed"] },
|
|
579
|
+
warnings: warnings,
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
export function renderStats(r) {
|
|
584
|
+
if (!r || !r.totals) return "任务统计暂无数据";
|
|
585
|
+
const t = r.totals;
|
|
586
|
+
return "任务 " + t.total + " 个:已开启 " + t.active + "、已完成 " + t.done + "、已关闭 " + t.shutdown +
|
|
587
|
+
"、已重启 " + t.restarted + ";逾期 " + t.overdue + ";近 " + r.range.days + " 天完成 " +
|
|
588
|
+
r.trend.reduce(function (n, x) { return n + x.done; }, 0) + "、新增 " + t.newInRange + "。";
|
|
589
|
+
}
|