Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions daemon/claude_usage_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,56 @@ async def discover_target(skip_addr: str | None = None):
return address


CLAUDE_PROJECTS = Path.home() / ".claude" / "projects"


def seven_day_usage() -> list:
"""Total tokens per local day for the last 7 days, normalized 0-100
(busiest day = 100). Index 6 = today. Parsed from Claude Code's local
JSONL logs under ~/.claude/projects. Returns [0]*7 on any failure."""
import datetime
today = datetime.date.today()
idx = {(today - datetime.timedelta(days=6 - i)).isoformat(): i for i in range(7)}
totals = [0] * 7
cutoff = time.time() - 8 * 86400
try:
for f in CLAUDE_PROJECTS.rglob("*.jsonl"):
try:
if f.stat().st_mtime < cutoff:
continue
except OSError:
continue
with f.open(errors="ignore") as fh:
for line in fh:
if '"usage"' not in line:
continue
try:
rec = json.loads(line)
except ValueError:
continue
ts = rec.get("timestamp")
usage = (rec.get("message") or {}).get("usage")
if not ts or not usage:
continue
try:
day = datetime.datetime.fromisoformat(
ts.replace("Z", "+00:00")).astimezone().date().isoformat()
except ValueError:
continue
i = idx.get(day)
if i is None:
continue
totals[i] += (usage.get("input_tokens", 0)
+ usage.get("output_tokens", 0)
+ usage.get("cache_creation_input_tokens", 0)
+ usage.get("cache_read_input_tokens", 0))
except Exception as e:
log(f"7-day scan failed: {e}")
return [0] * 7
mx = max(totals) or 1
return [round(t * 100 / mx) for t in totals]


async def poll_api(token: str) -> dict | None:
headers = dict(API_HEADERS_TEMPLATE)
headers["Authorization"] = f"Bearer {token}"
Expand Down Expand Up @@ -311,6 +361,8 @@ def pct(util: str) -> int:
"st": hdr("anthropic-ratelimit-unified-5h-status", "unknown"),
"ok": True,
}
payload["t"] = time.strftime("%H:%M:%S", time.localtime()) # seconds → device anchors minute flip correctly
payload["d7"] = seven_day_usage()
return payload


Expand Down
2 changes: 1 addition & 1 deletion firmware/src/boards/waveshare_lcd_183/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@
#define BOARD_HAS_SECONDARY_BUTTON 0
#define BOARD_HAS_ROTATION 0
#define BOARD_HAS_IMU 1
#define BOARD_HAS_BATTERY 1
#define BOARD_HAS_BATTERY 0
#define BOARD_HAS_IO_EXPANDER 0
2 changes: 1 addition & 1 deletion firmware/src/boards/waveshare_lcd_183/caps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ static const BoardCaps caps = {
.height = LCD_HEIGHT,
.button_count = 1,
.has_rotation = false,
.has_battery = true,
.has_battery = false, // "Without BAT" SKU — PMU populated, no cell. Set true if you add one.
.has_imu = true,
};

Expand Down
2 changes: 2 additions & 0 deletions firmware/src/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ struct UsageData {
float weekly_pct; // 7-day window utilization (0-100)
int weekly_reset_mins; // minutes until weekly resets
char status[16]; // "allowed" or "limited"
char clock[9]; // "HH:MM:SS" host wall-clock (from daemon), or "" if absent
uint8_t day7[7]; // last 7 days usage, 0-100 normalized (index 6 = today)
bool ok; // data parse succeeded
bool valid; // false until first successful parse
};
Loading