Skip to content

Commit b680c8f

Browse files
LeoRoccoBreedtclaude
authored andcommitted
refactor(growth-report): share window-growth math between KPIs
Factor the new_in/before/pct window calculation into a single _window_growth() helper taking a collection of creation timestamps; _workspace_growth_kpi and the Users-section growth KPI now both call it, so a growth-window change lives in one place. Behavior unchanged (154 tests pass). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 0d8d877 commit b680c8f

1 file changed

Lines changed: 24 additions & 23 deletions

File tree

cometx/cli/admin_growth_report.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,25 @@ def _num(value):
8989
return value
9090

9191

92+
def _window_growth(created_ms, window):
93+
"""Growth over the analysis window from a collection of creation timestamps
94+
(epoch ms): `new_in` = items created within `[window.start, window.end]`,
95+
`before` = items created before `window.start`, `pct` = new_in/before*100
96+
(0-guarded). Shared by the workspace- and user-growth KPIs so the window
97+
math lives in one place. `None` timestamps are skipped."""
98+
new_in = before = 0
99+
for c in created_ms:
100+
if c is None:
101+
continue
102+
dt = _ms_to_utc(c)
103+
if window.start <= dt <= window.end:
104+
new_in += 1
105+
elif dt < window.start:
106+
before += 1
107+
pct = round(new_in / before * 100, 1) if before else 0.0
108+
return {"new_in": new_in, "before": before, "pct": pct}
109+
110+
92111
def _extract_service_account_names(payload) -> "set[str] | None":
93112
"""Defensively unwrap the `/admin/service-accounts` response into a
94113
flat set of account names, tolerating several plausible response
@@ -431,14 +450,7 @@ def _workspace_growth_kpi(users, window):
431450
cur = ws_created.get(ws)
432451
if cur is None or u.created_at < cur:
433452
ws_created[ws] = u.created_at
434-
new_in = sum(
435-
1
436-
for c in ws_created.values()
437-
if window.start <= _ms_to_utc(c) <= window.end
438-
)
439-
before = sum(1 for c in ws_created.values() if _ms_to_utc(c) < window.start)
440-
pct = round(new_in / before * 100, 1) if before else 0.0
441-
return {"new_in": new_in, "before": before, "pct": pct}
453+
return _window_growth(ws_created.values(), window)
442454

443455
def _build_unified_section(
444456
self,
@@ -630,25 +642,14 @@ def _build_people_section(self, users, now_ms, window=None):
630642
]
631643

632644
# User growth over the analysis window: new accounts in window /
633-
# accounts before window (mirrors the workspace/project growth KPI).
645+
# accounts before window (shares _window_growth with the workspace KPI).
634646
if window is not None:
635-
new_in = sum(
636-
1
637-
for u in users
638-
if u.created_at is not None
639-
and window.start <= _ms_to_utc(u.created_at) <= window.end
640-
)
641-
before = sum(
642-
1
643-
for u in users
644-
if u.created_at is not None and _ms_to_utc(u.created_at) < window.start
645-
)
646-
pct = round(new_in / before * 100, 1) if before else 0.0
647+
growth = _window_growth((u.created_at for u in users), window)
647648
kpis.append(
648649
{
649650
"label": f"New in {self.window or '7d'} (% of base)",
650-
"value": f"{pct}%",
651-
"sub": f"+{new_in} new",
651+
"value": f"{growth['pct']}%",
652+
"sub": f"+{growth['new_in']} new",
652653
}
653654
)
654655

0 commit comments

Comments
 (0)