@d86e/hive-panel 1.0.13 → 1.0.15

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/src/data.js CHANGED
@@ -19,6 +19,40 @@ export function readMode(repo) {
19
19
  try { return JSON.parse(result.stdout); } catch { return { mode: "free" }; }
20
20
  }
21
21
 
22
+ export const EVENT_PAGE_LIMIT = 80;
23
+
24
+ export function readEventRows(repo) {
25
+ const file = path.join(repo, ".hive", "events.jsonl");
26
+ if (!fs.existsSync(file)) return [];
27
+ return fs.readFileSync(file, "utf8").split("\n").filter(Boolean).map((line) => {
28
+ try { return JSON.parse(line); } catch { return null; }
29
+ }).filter(Boolean).reverse();
30
+ }
31
+
32
+ export function readEventPage(repo, { cursor = 0, limit = EVENT_PAGE_LIMIT, kind = "" } = {}) {
33
+ const wantedKind = String(kind || "");
34
+ const rows = readEventRows(repo).filter((event) => !wantedKind || String(event.kind || "") === wantedKind);
35
+ const total = rows.length;
36
+ const offset = Math.max(0, Math.floor(Number(cursor) || 0));
37
+ const size = Math.max(1, Math.min(200, Math.floor(Number(limit) || EVENT_PAGE_LIMIT)));
38
+ const items = rows.slice(offset, offset + size);
39
+ const next = offset + items.length;
40
+ return { items, total, cursor: next, hasMore: next < total, kind: wantedKind };
41
+ }
42
+
43
+ export function readEventKinds(repo) {
44
+ const counts = new Map();
45
+ for (const event of readEventRows(repo)) {
46
+ const kind = String(event.kind || "");
47
+ if (!kind) continue;
48
+ counts.set(kind, (counts.get(kind) || 0) + 1);
49
+ }
50
+ return [...counts.entries()]
51
+ .map(([kind, count]) => ({ kind, count }))
52
+ .sort((left, right) => right.count - left.count || left.kind.localeCompare(right.kind))
53
+ .slice(0, 30);
54
+ }
55
+
22
56
  export function readEvents(repo, limit = 80) {
23
57
  const file = path.join(repo, ".hive", "events.jsonl");
24
58
  if (!fs.existsSync(file)) return [];
@@ -53,8 +87,91 @@ export function isInitialised(repo) {
53
87
  return INIT_MARKERS.some((marker) => fs.existsSync(path.join(repo, marker)));
54
88
  }
55
89
 
90
+ export const TASK_PAGE_LIMIT = 200;
91
+
92
+ function taskSortTime(task) {
93
+ const value = task && (task.stateChangedAt || task.updatedAt || task.createdAt);
94
+ const time = Date.parse(value || "");
95
+ if (Number.isFinite(time)) return time;
96
+ const id = String((task && task.id) || "");
97
+ const numeric = id.match(/(\d+)$/);
98
+ return numeric ? Number(numeric[1]) : Number.NaN;
99
+ }
100
+
101
+ export function compareTasksNewestFirst(left, right) {
102
+ const leftTime = taskSortTime(left);
103
+ const rightTime = taskSortTime(right);
104
+ if (Number.isFinite(leftTime) && Number.isFinite(rightTime) && leftTime !== rightTime) {
105
+ return rightTime - leftTime;
106
+ }
107
+ if (Number.isFinite(leftTime) !== Number.isFinite(rightTime)) return Number.isFinite(leftTime) ? -1 : 1;
108
+ return String((right && right.id) || "").localeCompare(String((left && left.id) || ""));
109
+ }
110
+
111
+ export function readTasks(repo, env = process.env) {
112
+ const tasks = env.HIVE_TASKS_BIN || "hive-tasks";
113
+ const rows = json(tasks, ["list", "--json"], repo, []);
114
+ return Array.isArray(rows) ? rows.filter((row) => row && typeof row === "object") : [];
115
+ }
116
+
117
+ export function readStats(repo, env = process.env) {
118
+ const tasks = env.HIVE_TASKS_BIN || "hive-tasks";
119
+ return json(tasks, ["stats", "--json"], repo, null);
120
+ }
121
+
122
+ function laneTotal(stats, state, rows) {
123
+ const byState = stats && stats.byState && typeof stats.byState === "object" ? stats.byState : null;
124
+ const value = byState ? Number(byState[state]) : Number.NaN;
125
+ return Number.isFinite(value) && value >= 0 ? value : rows.length;
126
+ }
127
+
128
+ export function paginateTasks(tasks, stats, limit = TASK_PAGE_LIMIT) {
129
+ const size = Number.isFinite(limit) && limit > 0 ? Math.min(200, Math.floor(limit)) : TASK_PAGE_LIMIT;
130
+ const lanePages = {};
131
+ for (const state of ["pending", "claimed", "done", "failed"]) {
132
+ const rows = (Array.isArray(tasks) ? tasks : [])
133
+ .filter((task) => task && (task.state || "pending") === state)
134
+ .sort(compareTasksNewestFirst);
135
+ const items = rows.slice(0, size);
136
+ const total = laneTotal(stats, state, rows);
137
+ lanePages[state] = {
138
+ items,
139
+ total,
140
+ cursor: items.length,
141
+ hasMore: items.length < total,
142
+ };
143
+ }
144
+ return lanePages;
145
+ }
146
+
147
+ export function snapshotTasksPage(repo, { state = "", cursor = 0, limit = TASK_PAGE_LIMIT, search = "" } = {}, env = process.env) {
148
+ let rows = readTasks(repo, env);
149
+ const query = String(search || "").trim().toLowerCase();
150
+ if (state) rows = rows.filter((task) => (task.state || "pending") === state);
151
+ if (query) {
152
+ rows = rows.filter((task) =>
153
+ String(task.id || "").toLowerCase().includes(query) ||
154
+ String(task.title || "").toLowerCase().includes(query),
155
+ );
156
+ }
157
+ rows.sort(compareTasksNewestFirst);
158
+ const total = rows.length;
159
+ const offset = Math.max(0, Math.floor(Number(cursor) || 0));
160
+ const size = Math.max(1, Math.min(200, Math.floor(Number(limit) || TASK_PAGE_LIMIT)));
161
+ const items = rows.slice(offset, offset + size);
162
+ const next = offset + items.length;
163
+ return { items, total, cursor: next, hasMore: next < total, state: String(state || ""), search: String(search || "") };
164
+ }
165
+
166
+ export function findTaskById(repo, id, env = process.env) {
167
+ const wanted = String(id || "");
168
+ if (!wanted) return null;
169
+ return readTasks(repo, env).find((task) => String(task.id || "") === wanted) || null;
170
+ }
171
+
56
172
  export function snapshot(repo, env = process.env) {
57
173
  const initialised = isInitialised(repo);
174
+ const eventPage = readEventPage(repo, { limit: EVENT_PAGE_LIMIT });
58
175
  const data = {
59
176
  at: new Date().toISOString(),
60
177
  repo,
@@ -63,15 +180,23 @@ export function snapshot(repo, env = process.env) {
63
180
  board: [],
64
181
  tasks: [],
65
182
  stats: null,
66
- events: readEvents(repo),
183
+ events: eventPage.items,
184
+ eventTotal: eventPage.total,
185
+ eventKinds: readEventKinds(repo),
67
186
  };
68
- if (!initialised) return data;
187
+ if (!initialised) {
188
+ data.lanePages = paginateTasks([], null);
189
+ return data;
190
+ }
69
191
  const hive = env.HIVE_BIN || "hive";
70
- const tasks = env.HIVE_TASKS_BIN || "hive-tasks";
192
+ const tasks = readTasks(repo, env);
193
+ const stats = readStats(repo, env);
194
+ const lanePages = paginateTasks(tasks, stats);
71
195
  return {
72
196
  ...data,
73
197
  board: json(hive, ["board", "--json"], repo, []),
74
- tasks: json(tasks, ["list", "--json"], repo, []),
75
- stats: json(tasks, ["stats", "--json"], repo, null),
198
+ tasks: Object.values(lanePages).flatMap((page) => page.items),
199
+ lanePages,
200
+ stats,
76
201
  };
77
202
  }
package/src/server.js CHANGED
@@ -2,7 +2,7 @@ import fs from "node:fs";
2
2
  import http from "node:http";
3
3
  import path from "node:path";
4
4
  import { execFile } from "node:child_process";
5
- import { snapshot } from "./data.js";
5
+ import { findTaskById, readEventPage, snapshot, snapshotTasksPage } from "./data.js";
6
6
  import { listActions, runAction } from "./actions.js";
7
7
 
8
8
  const MIME = { ".html": "text/html; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".css": "text/css; charset=utf-8", ".svg": "image/svg+xml" };
@@ -44,6 +44,34 @@ export function createServer({ repo, publicDir, env = process.env, writable = fa
44
44
  return response.end(JSON.stringify(data));
45
45
  }
46
46
 
47
+ if (request.method === "GET" && url.pathname === "/api/events") {
48
+ const page = readEventPage(repo, {
49
+ cursor: url.searchParams.get("cursor") || "0",
50
+ limit: url.searchParams.get("limit") || "80",
51
+ kind: url.searchParams.get("kind") || "",
52
+ });
53
+ response.writeHead(200, { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" });
54
+ return response.end(JSON.stringify(page));
55
+ }
56
+
57
+ if (request.method === "GET" && url.pathname === "/api/tasks") {
58
+ const page = snapshotTasksPage(repo, {
59
+ state: url.searchParams.get("state") || "",
60
+ cursor: url.searchParams.get("cursor") || "0",
61
+ limit: url.searchParams.get("limit") || "200",
62
+ search: url.searchParams.get("search") || url.searchParams.get("q") || "",
63
+ }, env);
64
+ response.writeHead(200, { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" });
65
+ return response.end(JSON.stringify(page));
66
+ }
67
+
68
+ if (request.method === "GET" && url.pathname.startsWith("/api/tasks/")) {
69
+ const id = decodeURIComponent(url.pathname.slice("/api/tasks/".length));
70
+ const task = findTaskById(repo, id, env);
71
+ response.writeHead(task ? 200 : 404, { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" });
72
+ return response.end(JSON.stringify(task ? { ok: true, task } : { ok: false, error: "task not found" }));
73
+ }
74
+
47
75
  if (url.pathname === "/api/actions") {
48
76
  response.writeHead(200, { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" });
49
77
  return response.end(JSON.stringify({ writable, actions: listActions() }));