ccusage-tracker 0.1.5 → 0.1.7

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.
Files changed (2) hide show
  1. package/dist/index.js +39 -4
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -419,14 +419,22 @@ Hook: ${hookInstalled ? "installed" : "not installed"}`);
419
419
  const configDir = dirname(configPath);
420
420
  const lastErrorPath = join3(configDir, "last-error.txt");
421
421
  if (existsSync3(lastErrorPath)) {
422
+ const reason = readFileSync3(lastErrorPath, "utf-8").trim();
422
423
  console.log(`
423
- Last upload: FAILED - ${readFileSync3(lastErrorPath, "utf-8").trim()}`);
424
- console.log(" 用量目前並未上報。請量測 ccusage 耗時,若接近 25s 需再放寬 timeout:");
425
- console.log(" time ccusage daily --json --since $(date +%Y%m%d)");
424
+ Upload error: ${reason}`);
425
+ console.log(uploadFailureHint(reason));
426
426
  } else {
427
427
  console.log(`
428
- Last upload: no recorded failure`);
428
+ Upload error: none recorded`);
429
429
  }
430
+ const lastUploadPath = join3(configDir, "last-upload.txt");
431
+ let lastUploadTs = null;
432
+ if (existsSync3(lastUploadPath)) {
433
+ const parsed = parseInt(readFileSync3(lastUploadPath, "utf-8").trim(), 10);
434
+ if (!isNaN(parsed))
435
+ lastUploadTs = parsed;
436
+ }
437
+ console.log(uploadAgeLine(lastUploadTs, Date.now()));
430
438
  const lastFlushPath = join3(configDir, "last-flush.txt");
431
439
  if (existsSync3(lastFlushPath)) {
432
440
  const ts = parseInt(readFileSync3(lastFlushPath, "utf-8").trim(), 10);
@@ -438,6 +446,33 @@ Last upload: no recorded failure`);
438
446
  const probe = probeCcusage();
439
447
  console.log(probe ? `ccusage: installed (${probe})` : "ccusage: not found (install with: npx ccusage@latest)");
440
448
  }
449
+ var STALE_UPLOAD_MS = 24 * 60 * 60 * 1000;
450
+ function uploadAgeLine(ts, now) {
451
+ if (ts === null) {
452
+ return "Last successful upload: 尚無成功紀錄(剛安裝屬正常,跑完一次 session 後再看)";
453
+ }
454
+ const age = now - ts;
455
+ const when = new Date(ts).toISOString();
456
+ if (age >= STALE_UPLOAD_MS) {
457
+ const hours = Math.floor(age / 3600000);
458
+ return `Last successful upload: ${when} (${hours} 小時前) — 超過 24 小時,上報可能已停擺`;
459
+ }
460
+ return `Last successful upload: ${when} (${Math.floor(age / 60000)} 分鐘前)`;
461
+ }
462
+ function uploadFailureHint(reason) {
463
+ if (reason.includes("ccusage")) {
464
+ return [
465
+ " 用量目前並未上報。請量測 ccusage 耗時,若接近 25s 需再放寬 timeout:",
466
+ " time ccusage daily --json --since $(date +%Y%m%d)"
467
+ ].join(`
468
+ `);
469
+ }
470
+ return [
471
+ " 用量目前並未上報。ccusage 有取到數,卡在送出那一段 —— 先看上面的 Server 一行,",
472
+ " 再檢查網路 / VPN / proxy。當日快照是整日累計,下次 hook 跑成就會補齊"
473
+ ].join(`
474
+ `);
475
+ }
441
476
  function probeCcusage() {
442
477
  try {
443
478
  const r = spawnSync2("ccusage --version", { encoding: "utf8", shell: true, timeout: 1e4 });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ccusage-tracker",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "CLI for ccusage-tracker — install Claude Code SessionStart/SessionEnd/Stop hooks to report token usage to a self-hosted team tracker.",
5
5
  "type": "module",
6
6
  "bin": {