dsh-usage-overview 0.0.0-stage → 1.1.0
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.en.md +176 -0
- package/README.md +153 -2
- package/cordis.patch.yml +3 -0
- package/lib/client.js +2185 -0
- package/lib/index.js +178 -0
- package/lib/log-fold.js +196 -0
- package/lib/protocol.js +104 -0
- package/lib/usage.js +425 -0
- package/package.json +44 -4
package/lib/index.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { homedir } from 'node:os';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
PANEL_ID,
|
|
6
|
+
PROVIDER_SOURCE_LABELS,
|
|
7
|
+
USAGE_STATS_PATH,
|
|
8
|
+
} from './protocol.js';
|
|
9
|
+
import { buildUsageDocument, collectSessions, mergeLive } from './usage.js';
|
|
10
|
+
|
|
11
|
+
export { PANEL_ID, NS, USAGE_STATS_PATH } from './protocol.js';
|
|
12
|
+
|
|
13
|
+
export const inject = ['webServer'];
|
|
14
|
+
|
|
15
|
+
function resolveHome() {
|
|
16
|
+
const configured = process.env.DSH_HOME;
|
|
17
|
+
if (typeof configured === 'string' && configured.length > 0) return configured;
|
|
18
|
+
return join(homedir(), '.dsh');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function loopbackRequest(req) {
|
|
22
|
+
const host = req.headers.host;
|
|
23
|
+
if (typeof host !== 'string' || host.length === 0) return false;
|
|
24
|
+
const hostname = host.startsWith('[') ? host.slice(0, host.indexOf(']') + 1) : host.split(':')[0];
|
|
25
|
+
const normalized = hostname.toLowerCase();
|
|
26
|
+
return (
|
|
27
|
+
normalized === '127.0.0.1' ||
|
|
28
|
+
normalized === 'localhost' ||
|
|
29
|
+
normalized === '[::1]' ||
|
|
30
|
+
normalized === '::1'
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function trustedRequest(ctx, req) {
|
|
35
|
+
if (loopbackRequest(req)) return true;
|
|
36
|
+
const connection = ctx.get('connection');
|
|
37
|
+
if (connection !== undefined && typeof connection.requestRejection === 'function') {
|
|
38
|
+
try {
|
|
39
|
+
return connection.requestRejection({ headers: req.headers }) === undefined;
|
|
40
|
+
} catch {
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function json(res, status, body) {
|
|
47
|
+
const text = JSON.stringify(body);
|
|
48
|
+
res.writeHead(status, {
|
|
49
|
+
'content-type': 'application/json; charset=utf-8',
|
|
50
|
+
'content-length': Buffer.byteLength(text),
|
|
51
|
+
'cache-control': 'no-store',
|
|
52
|
+
});
|
|
53
|
+
res.end(text);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function collectLive(ctx) {
|
|
57
|
+
const live = new Map();
|
|
58
|
+
const sessions = ctx.get('sessions');
|
|
59
|
+
const registry = ctx.get('sessionProjections');
|
|
60
|
+
if (sessions === undefined) return live;
|
|
61
|
+
|
|
62
|
+
let list;
|
|
63
|
+
try {
|
|
64
|
+
list = typeof sessions.list === 'function' ? sessions.list() : undefined;
|
|
65
|
+
} catch {
|
|
66
|
+
return live;
|
|
67
|
+
}
|
|
68
|
+
if (!Array.isArray(list)) return live;
|
|
69
|
+
|
|
70
|
+
for (const session of list) {
|
|
71
|
+
try {
|
|
72
|
+
const id = session?.id;
|
|
73
|
+
if (typeof id !== 'string' || id.length === 0) continue;
|
|
74
|
+
|
|
75
|
+
let values;
|
|
76
|
+
if (registry !== undefined && typeof registry.snapshot === 'function') {
|
|
77
|
+
try {
|
|
78
|
+
values = registry.snapshot(session)?.values;
|
|
79
|
+
} catch {
|
|
80
|
+
values = undefined;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const usage = values?.tokenUsage;
|
|
85
|
+
const totals = {
|
|
86
|
+
uncachedInputTokens: numberOr(usage?.totals?.uncachedInputTokens, 0),
|
|
87
|
+
cacheReadTokens: numberOr(usage?.totals?.cacheReadTokens, 0),
|
|
88
|
+
cacheWriteTokens: numberOr(usage?.totals?.cacheWriteTokens, 0),
|
|
89
|
+
outputTokens: numberOr(usage?.totals?.outputTokens, 0),
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const stats = values?.sessionStats;
|
|
93
|
+
const selection = values?.modelSelection?.lastUsed;
|
|
94
|
+
|
|
95
|
+
live.set(id, {
|
|
96
|
+
totals,
|
|
97
|
+
provider: typeof selection?.provider === 'string' ? selection.provider : undefined,
|
|
98
|
+
model: typeof selection?.model === 'string' ? selection.model : undefined,
|
|
99
|
+
createdAt: numberOr(session.createdAt, undefined),
|
|
100
|
+
sessionStats:
|
|
101
|
+
typeof stats === 'object' && stats !== null
|
|
102
|
+
? {
|
|
103
|
+
turns: numberOr(stats.turns, 0),
|
|
104
|
+
steps: numberOr(stats.steps, 0),
|
|
105
|
+
llmMs: numberOr(stats.llmMs, 0),
|
|
106
|
+
toolMs: numberOr(stats.toolMs, 0),
|
|
107
|
+
}
|
|
108
|
+
: undefined,
|
|
109
|
+
});
|
|
110
|
+
} catch {
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return live;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function numberOr(value, fallback) {
|
|
117
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : fallback;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function usageDocument(ctx, rangeDays) {
|
|
121
|
+
const home = resolveHome();
|
|
122
|
+
const sessions = await collectSessions(home);
|
|
123
|
+
const live = collectLive(ctx);
|
|
124
|
+
const merged = mergeLive(sessions, live);
|
|
125
|
+
|
|
126
|
+
return buildUsageDocument(merged, {
|
|
127
|
+
now: Date.now(),
|
|
128
|
+
rangeDays,
|
|
129
|
+
providerLabels: PROVIDER_SOURCE_LABELS,
|
|
130
|
+
liveIds: new Set(live.keys()),
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function parseRange(url) {
|
|
135
|
+
const raw = url.searchParams.get('range');
|
|
136
|
+
if (raw === null || raw === 'all') return null;
|
|
137
|
+
if (raw === '7d') return 7;
|
|
138
|
+
if (raw === '30d') return 30;
|
|
139
|
+
if (raw === '90d') return 90;
|
|
140
|
+
return 30;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function apply(ctx) {
|
|
144
|
+
const mount = (path, handler, label) => {
|
|
145
|
+
ctx.effect(() => {
|
|
146
|
+
const webServer = ctx.get('webServer');
|
|
147
|
+
if (webServer === undefined) return () => {};
|
|
148
|
+
const dispose = webServer.register({ kind: 'exact', path, handler });
|
|
149
|
+
return () => {
|
|
150
|
+
dispose();
|
|
151
|
+
};
|
|
152
|
+
}, `dsh-usage-overview: ${label}`);
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
mount(
|
|
156
|
+
USAGE_STATS_PATH,
|
|
157
|
+
(req, res) => {
|
|
158
|
+
void (async () => {
|
|
159
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
160
|
+
json(res, 405, { error: 'method-not-allowed' });
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (!trustedRequest(ctx, req)) {
|
|
164
|
+
json(res, 403, { error: 'request-not-trusted' });
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
try {
|
|
168
|
+
const url = new URL(req.url ?? '/', 'http://127.0.0.1');
|
|
169
|
+
json(res, 200, await usageDocument(ctx, parseRange(url)));
|
|
170
|
+
} catch (error) {
|
|
171
|
+
ctx.logger?.warn?.(`dsh-usage-overview: stats read failed: ${String(error)}`);
|
|
172
|
+
json(res, 500, { error: 'usage-unavailable' });
|
|
173
|
+
}
|
|
174
|
+
})();
|
|
175
|
+
},
|
|
176
|
+
'stats route',
|
|
177
|
+
);
|
|
178
|
+
}
|
package/lib/log-fold.js
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { zstdDecompressSync } from 'node:zlib';
|
|
3
|
+
|
|
4
|
+
const FRAME_MAGIC = Buffer.from([0x28, 0xb5, 0x2f, 0xfd]);
|
|
5
|
+
|
|
6
|
+
export function readLogText(path) {
|
|
7
|
+
const compressed = readFileSync(path);
|
|
8
|
+
const offsets = [];
|
|
9
|
+
let index = compressed.indexOf(FRAME_MAGIC);
|
|
10
|
+
while (index !== -1) {
|
|
11
|
+
offsets.push(index);
|
|
12
|
+
index = compressed.indexOf(FRAME_MAGIC, index + FRAME_MAGIC.length);
|
|
13
|
+
}
|
|
14
|
+
if (offsets.length === 0) return zstdDecompressSync(compressed).toString('utf8');
|
|
15
|
+
|
|
16
|
+
const parts = [];
|
|
17
|
+
for (const [position, start] of offsets.entries()) {
|
|
18
|
+
const end = offsets[position + 1] ?? compressed.length;
|
|
19
|
+
try {
|
|
20
|
+
parts.push(zstdDecompressSync(compressed.subarray(start, end)).toString('utf8'));
|
|
21
|
+
} catch {
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return parts.join('');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function parseLogEvents(text) {
|
|
28
|
+
const events = [];
|
|
29
|
+
for (const line of text.split('\n')) {
|
|
30
|
+
const trimmed = line.trim();
|
|
31
|
+
if (trimmed.length === 0) continue;
|
|
32
|
+
try {
|
|
33
|
+
events.push(JSON.parse(trimmed));
|
|
34
|
+
} catch {
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return events;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function localDayKey(ms) {
|
|
41
|
+
const date = new Date(ms);
|
|
42
|
+
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
43
|
+
const day = String(date.getDate()).padStart(2, '0');
|
|
44
|
+
return `${String(date.getFullYear())}-${month}-${day}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function emptyBuckets() {
|
|
48
|
+
return {
|
|
49
|
+
uncachedInputTokens: 0,
|
|
50
|
+
cacheReadTokens: 0,
|
|
51
|
+
cacheWriteTokens: 0,
|
|
52
|
+
outputTokens: 0,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function bucketsOf(usage) {
|
|
57
|
+
return {
|
|
58
|
+
uncachedInputTokens: num(usage?.inputTokens),
|
|
59
|
+
cacheReadTokens: num(usage?.cacheReadTokens),
|
|
60
|
+
cacheWriteTokens: num(usage?.cacheWriteTokens),
|
|
61
|
+
outputTokens: num(usage?.outputTokens),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function num(value) {
|
|
66
|
+
return typeof value === 'number' && Number.isFinite(value) && value > 0 ? value : 0;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function bucketTotal(buckets) {
|
|
70
|
+
return (
|
|
71
|
+
buckets.uncachedInputTokens + buckets.cacheReadTokens + buckets.cacheWriteTokens + buckets.outputTokens
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function addBuckets(target, source) {
|
|
76
|
+
target.uncachedInputTokens += source.uncachedInputTokens;
|
|
77
|
+
target.cacheReadTokens += source.cacheReadTokens;
|
|
78
|
+
target.cacheWriteTokens += source.cacheWriteTokens;
|
|
79
|
+
target.outputTokens += source.outputTokens;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function foldSessionEvents(events) {
|
|
83
|
+
const totals = emptyBuckets();
|
|
84
|
+
const byDay = new Map();
|
|
85
|
+
const byModel = new Map();
|
|
86
|
+
let firstAt;
|
|
87
|
+
let lastAt;
|
|
88
|
+
let turns = 0;
|
|
89
|
+
let steps = 0;
|
|
90
|
+
let samples = 0;
|
|
91
|
+
|
|
92
|
+
for (const event of events) {
|
|
93
|
+
const at = typeof event.time === 'number' ? event.time : undefined;
|
|
94
|
+
if (at !== undefined) {
|
|
95
|
+
if (firstAt === undefined || at < firstAt) firstAt = at;
|
|
96
|
+
if (lastAt === undefined || at > lastAt) lastAt = at;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (event.type === 'step/end') {
|
|
100
|
+
steps += 1;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (event.type === 'turn/end') {
|
|
104
|
+
turns += 1;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (event.type !== 'assistant/message') continue;
|
|
108
|
+
|
|
109
|
+
const usage = event.data?.usage;
|
|
110
|
+
if (usage === undefined || usage === null) continue;
|
|
111
|
+
|
|
112
|
+
const buckets = bucketsOf(usage);
|
|
113
|
+
const total = bucketTotal(buckets);
|
|
114
|
+
if (total <= 0) continue;
|
|
115
|
+
samples += 1;
|
|
116
|
+
addBuckets(totals, buckets);
|
|
117
|
+
|
|
118
|
+
const source = event.data?.message?.source;
|
|
119
|
+
const model = typeof source?.model === 'string' && source.model.length > 0 ? source.model : 'unknown';
|
|
120
|
+
const provider =
|
|
121
|
+
typeof source?.provider === 'string' && source.provider.length > 0 ? source.provider : 'unknown';
|
|
122
|
+
|
|
123
|
+
const routeKey = `${provider}\u0000${model}`;
|
|
124
|
+
const turn = typeof event.data?.turn === 'number' ? event.data.turn : undefined;
|
|
125
|
+
if (at !== undefined) {
|
|
126
|
+
const key = localDayKey(at);
|
|
127
|
+
let day = byDay.get(key);
|
|
128
|
+
if (day === undefined) {
|
|
129
|
+
day = { totals: emptyBuckets(), samples: 0, byRoute: new Map() };
|
|
130
|
+
byDay.set(key, day);
|
|
131
|
+
}
|
|
132
|
+
addBuckets(day.totals, buckets);
|
|
133
|
+
day.samples += 1;
|
|
134
|
+
let dayRoute = day.byRoute.get(routeKey);
|
|
135
|
+
if (dayRoute === undefined) {
|
|
136
|
+
dayRoute = { provider, model, total: 0, samples: 0, totals: emptyBuckets(), turns: new Set() };
|
|
137
|
+
day.byRoute.set(routeKey, dayRoute);
|
|
138
|
+
}
|
|
139
|
+
addBuckets(dayRoute.totals, buckets);
|
|
140
|
+
dayRoute.total += total;
|
|
141
|
+
dayRoute.samples += 1;
|
|
142
|
+
if (turn !== undefined) dayRoute.turns.add(turn);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
let route = byModel.get(routeKey);
|
|
146
|
+
if (route === undefined) {
|
|
147
|
+
route = { provider, model, totals: emptyBuckets(), samples: 0, turns: new Set() };
|
|
148
|
+
byModel.set(routeKey, route);
|
|
149
|
+
}
|
|
150
|
+
addBuckets(route.totals, buckets);
|
|
151
|
+
route.samples += 1;
|
|
152
|
+
if (turn !== undefined) route.turns.add(turn);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
totals,
|
|
157
|
+
total: bucketTotal(totals),
|
|
158
|
+
byDay,
|
|
159
|
+
byModel,
|
|
160
|
+
firstAt,
|
|
161
|
+
lastAt,
|
|
162
|
+
wallMs: firstAt !== undefined && lastAt !== undefined ? Math.max(0, lastAt - firstAt) : 0,
|
|
163
|
+
turns,
|
|
164
|
+
steps,
|
|
165
|
+
samples,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const cache = new Map();
|
|
170
|
+
|
|
171
|
+
export function foldSessionLog(path) {
|
|
172
|
+
let revision;
|
|
173
|
+
try {
|
|
174
|
+
const info = statSync(path);
|
|
175
|
+
revision = `${String(info.size)}:${String(info.mtimeMs)}`;
|
|
176
|
+
} catch {
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const cached = cache.get(path);
|
|
181
|
+
if (cached !== undefined && cached.revision === revision) return cached.value;
|
|
182
|
+
|
|
183
|
+
let value;
|
|
184
|
+
try {
|
|
185
|
+
value = foldSessionEvents(parseLogEvents(readLogText(path)));
|
|
186
|
+
} catch {
|
|
187
|
+
return undefined;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
cache.set(path, { revision, value });
|
|
191
|
+
return value;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function clearLogCache() {
|
|
195
|
+
cache.clear();
|
|
196
|
+
}
|
package/lib/protocol.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
export const USAGE_SCHEMA_VERSION = 1;
|
|
2
|
+
|
|
3
|
+
export const PANEL_ID = 'usage-overview';
|
|
4
|
+
|
|
5
|
+
export const NS = 'usage-overview';
|
|
6
|
+
|
|
7
|
+
export const USAGE_STATS_PATH = '/plugins/dsh-usage-overview/stats';
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
export const RANGE_PRESETS = Object.freeze([
|
|
11
|
+
{ id: '7d', days: 7 },
|
|
12
|
+
{ id: '30d', days: 30 },
|
|
13
|
+
{ id: '90d', days: 90 },
|
|
14
|
+
{ id: 'all', days: null },
|
|
15
|
+
]);
|
|
16
|
+
|
|
17
|
+
export const PROVIDER_SOURCE_LABELS = Object.freeze({
|
|
18
|
+
'workbuddy-ai': 'WorkBuddy AI',
|
|
19
|
+
'workbuddy': 'WorkBuddy',
|
|
20
|
+
'opencode-go': 'OpenCode Go',
|
|
21
|
+
'deepseek-official': 'DeepSeek Official',
|
|
22
|
+
'deepseek': 'DeepSeek',
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export const SERIES_COLORS = Object.freeze([
|
|
26
|
+
'#4f8ef7',
|
|
27
|
+
'#39c07a',
|
|
28
|
+
'#f2a33c',
|
|
29
|
+
'#c471f5',
|
|
30
|
+
'#f2557a',
|
|
31
|
+
'#3cc8d6',
|
|
32
|
+
'#9aa7ff',
|
|
33
|
+
'#e0c341',
|
|
34
|
+
]);
|
|
35
|
+
|
|
36
|
+
export const TOKEN_BUCKETS = Object.freeze([
|
|
37
|
+
'uncachedInputTokens',
|
|
38
|
+
'cacheReadTokens',
|
|
39
|
+
'cacheWriteTokens',
|
|
40
|
+
'outputTokens',
|
|
41
|
+
]);
|
|
42
|
+
|
|
43
|
+
export function cacheHitRate(buckets) {
|
|
44
|
+
const read = numberOr0(buckets?.cacheReadTokens);
|
|
45
|
+
const write = numberOr0(buckets?.cacheWriteTokens);
|
|
46
|
+
const uncached = numberOr0(buckets?.uncachedInputTokens);
|
|
47
|
+
const input = read + write + uncached;
|
|
48
|
+
if (input <= 0) return null;
|
|
49
|
+
return (read / input) * 100;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function numberOr0(value) {
|
|
53
|
+
return typeof value === 'number' && Number.isFinite(value) && value > 0 ? value : 0;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function isUsageDocument(value) {
|
|
57
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) return false;
|
|
58
|
+
if (value.schemaVersion !== USAGE_SCHEMA_VERSION) return false;
|
|
59
|
+
if (typeof value.generatedAt !== 'number') return false;
|
|
60
|
+
if (typeof value.totals !== 'object' || value.totals === null) return false;
|
|
61
|
+
if (!Array.isArray(value.days)) return false;
|
|
62
|
+
if (!Array.isArray(value.models)) return false;
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function formatTokens(value) {
|
|
67
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) return '—';
|
|
68
|
+
if (value < 1000) return String(Math.round(value));
|
|
69
|
+
if (value < 10000) return `${(value / 1000).toFixed(2)}K`;
|
|
70
|
+
if (value < 1000000) return `${(value / 1000).toFixed(1)}K`;
|
|
71
|
+
if (value < 100000000) return `${(value / 1000000).toFixed(1)}M`;
|
|
72
|
+
return `${(value / 100000000).toFixed(2)}亿`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function formatTokensZh(value) {
|
|
76
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) return '—';
|
|
77
|
+
if (value < 10000) return String(Math.round(value));
|
|
78
|
+
if (value < 100000000) return `${(value / 10000).toFixed(1)}万`;
|
|
79
|
+
return `${(value / 100000000).toFixed(1)}亿`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function formatDuration(ms) {
|
|
83
|
+
if (typeof ms !== 'number' || !Number.isFinite(ms) || ms < 0) return '—';
|
|
84
|
+
const seconds = Math.round(ms / 1000);
|
|
85
|
+
if (seconds < 60) return `${String(seconds)}秒`;
|
|
86
|
+
const minutes = Math.floor(seconds / 60);
|
|
87
|
+
if (minutes < 60) return `${String(minutes)}分${String(seconds % 60)}秒`;
|
|
88
|
+
const hours = Math.floor(minutes / 60);
|
|
89
|
+
if (hours < 24) return `${String(hours)}小时${String(minutes % 60)}分`;
|
|
90
|
+
const days = Math.floor(hours / 24);
|
|
91
|
+
return `${String(days)}天${String(hours % 24)}小时`;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function formatPercent(value) {
|
|
95
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) return '—';
|
|
96
|
+
return `${value.toFixed(2)}%`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function dayKeyOf(ms) {
|
|
100
|
+
const date = new Date(ms);
|
|
101
|
+
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
102
|
+
const day = String(date.getDate()).padStart(2, '0');
|
|
103
|
+
return `${String(date.getFullYear())}-${month}-${day}`;
|
|
104
|
+
}
|