@d86e/hive-panel 1.0.12 → 1.0.14

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
@@ -53,6 +53,88 @@ export function isInitialised(repo) {
53
53
  return INIT_MARKERS.some((marker) => fs.existsSync(path.join(repo, marker)));
54
54
  }
55
55
 
56
+ export const TASK_PAGE_LIMIT = 200;
57
+
58
+ function taskSortTime(task) {
59
+ const value = task && (task.stateChangedAt || task.updatedAt || task.createdAt);
60
+ const time = Date.parse(value || "");
61
+ if (Number.isFinite(time)) return time;
62
+ const id = String((task && task.id) || "");
63
+ const numeric = id.match(/(\d+)$/);
64
+ return numeric ? Number(numeric[1]) : Number.NaN;
65
+ }
66
+
67
+ export function compareTasksNewestFirst(left, right) {
68
+ const leftTime = taskSortTime(left);
69
+ const rightTime = taskSortTime(right);
70
+ if (Number.isFinite(leftTime) && Number.isFinite(rightTime) && leftTime !== rightTime) {
71
+ return rightTime - leftTime;
72
+ }
73
+ if (Number.isFinite(leftTime) !== Number.isFinite(rightTime)) return Number.isFinite(leftTime) ? -1 : 1;
74
+ return String((right && right.id) || "").localeCompare(String((left && left.id) || ""));
75
+ }
76
+
77
+ export function readTasks(repo, env = process.env) {
78
+ const tasks = env.HIVE_TASKS_BIN || "hive-tasks";
79
+ const rows = json(tasks, ["list", "--json"], repo, []);
80
+ return Array.isArray(rows) ? rows.filter((row) => row && typeof row === "object") : [];
81
+ }
82
+
83
+ export function readStats(repo, env = process.env) {
84
+ const tasks = env.HIVE_TASKS_BIN || "hive-tasks";
85
+ return json(tasks, ["stats", "--json"], repo, null);
86
+ }
87
+
88
+ function laneTotal(stats, state, rows) {
89
+ const byState = stats && stats.byState && typeof stats.byState === "object" ? stats.byState : null;
90
+ const value = byState ? Number(byState[state]) : Number.NaN;
91
+ return Number.isFinite(value) && value >= 0 ? value : rows.length;
92
+ }
93
+
94
+ export function paginateTasks(tasks, stats, limit = TASK_PAGE_LIMIT) {
95
+ const size = Number.isFinite(limit) && limit > 0 ? Math.min(200, Math.floor(limit)) : TASK_PAGE_LIMIT;
96
+ const lanePages = {};
97
+ for (const state of ["pending", "claimed", "done", "failed"]) {
98
+ const rows = (Array.isArray(tasks) ? tasks : [])
99
+ .filter((task) => task && (task.state || "pending") === state)
100
+ .sort(compareTasksNewestFirst);
101
+ const items = rows.slice(0, size);
102
+ const total = laneTotal(stats, state, rows);
103
+ lanePages[state] = {
104
+ items,
105
+ total,
106
+ cursor: items.length,
107
+ hasMore: items.length < total,
108
+ };
109
+ }
110
+ return lanePages;
111
+ }
112
+
113
+ export function snapshotTasksPage(repo, { state = "", cursor = 0, limit = TASK_PAGE_LIMIT, search = "" } = {}, env = process.env) {
114
+ let rows = readTasks(repo, env);
115
+ const query = String(search || "").trim().toLowerCase();
116
+ if (state) rows = rows.filter((task) => (task.state || "pending") === state);
117
+ if (query) {
118
+ rows = rows.filter((task) =>
119
+ String(task.id || "").toLowerCase().includes(query) ||
120
+ String(task.title || "").toLowerCase().includes(query),
121
+ );
122
+ }
123
+ rows.sort(compareTasksNewestFirst);
124
+ const total = rows.length;
125
+ const offset = Math.max(0, Math.floor(Number(cursor) || 0));
126
+ const size = Math.max(1, Math.min(200, Math.floor(Number(limit) || TASK_PAGE_LIMIT)));
127
+ const items = rows.slice(offset, offset + size);
128
+ const next = offset + items.length;
129
+ return { items, total, cursor: next, hasMore: next < total, state: String(state || ""), search: String(search || "") };
130
+ }
131
+
132
+ export function findTaskById(repo, id, env = process.env) {
133
+ const wanted = String(id || "");
134
+ if (!wanted) return null;
135
+ return readTasks(repo, env).find((task) => String(task.id || "") === wanted) || null;
136
+ }
137
+
56
138
  export function snapshot(repo, env = process.env) {
57
139
  const initialised = isInitialised(repo);
58
140
  const data = {
@@ -65,13 +147,19 @@ export function snapshot(repo, env = process.env) {
65
147
  stats: null,
66
148
  events: readEvents(repo),
67
149
  };
68
- if (!initialised) return data;
150
+ if (!initialised) {
151
+ data.lanePages = paginateTasks([], null);
152
+ return data;
153
+ }
69
154
  const hive = env.HIVE_BIN || "hive";
70
- const tasks = env.HIVE_TASKS_BIN || "hive-tasks";
155
+ const tasks = readTasks(repo, env);
156
+ const stats = readStats(repo, env);
157
+ const lanePages = paginateTasks(tasks, stats);
71
158
  return {
72
159
  ...data,
73
160
  board: json(hive, ["board", "--json"], repo, []),
74
- tasks: json(tasks, ["list", "--json"], repo, []),
75
- stats: json(tasks, ["stats", "--json"], repo, null),
161
+ tasks: Object.values(lanePages).flatMap((page) => page.items),
162
+ lanePages,
163
+ stats,
76
164
  };
77
165
  }
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, 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,24 @@ 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/tasks") {
48
+ const page = snapshotTasksPage(repo, {
49
+ state: url.searchParams.get("state") || "",
50
+ cursor: url.searchParams.get("cursor") || "0",
51
+ limit: url.searchParams.get("limit") || "200",
52
+ search: url.searchParams.get("search") || url.searchParams.get("q") || "",
53
+ }, env);
54
+ response.writeHead(200, { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" });
55
+ return response.end(JSON.stringify(page));
56
+ }
57
+
58
+ if (request.method === "GET" && url.pathname.startsWith("/api/tasks/")) {
59
+ const id = decodeURIComponent(url.pathname.slice("/api/tasks/".length));
60
+ const task = findTaskById(repo, id, env);
61
+ response.writeHead(task ? 200 : 404, { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" });
62
+ return response.end(JSON.stringify(task ? { ok: true, task } : { ok: false, error: "task not found" }));
63
+ }
64
+
47
65
  if (url.pathname === "/api/actions") {
48
66
  response.writeHead(200, { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" });
49
67
  return response.end(JSON.stringify({ writable, actions: listActions() }));