Skip to content

Commit 18bb47d

Browse files
committed
Cap the cache hit rate at 99.99% so rounding cannot show 100%
With a non-zero input the hit rate is necessarily below 100%, but rounding to two decimals turned 99.99x% into 100%. Both the overview and the per-model figures now go through pct_hit_rate(), which caps at 99.99 whenever input > 0 and only returns 100 for fabricated data with a zero input. The per-model figure previously kept one decimal and had the same latent issue.
1 parent 38dd578 commit 18bb47d

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

  • plugins/yanhy2000/mcode-usage-monitor/miniapp/node

‎plugins/yanhy2000/mcode-usage-monitor/miniapp/node/api.py‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,22 @@ def fetch_all(db_path: Path):
115115
cleanup()
116116

117117

118+
def pct_hit_rate(cache_read: int, inp: int) -> float:
119+
"""缓存命中率(%), 保留两位小数。
120+
121+
真实数据里只要输入不为零, 命中率必然小于 100%; 但四舍五入会把 99.99x%
122+
顶成 100%, 造成"完全命中"的错觉。因此 input > 0 时封顶 99.99,
123+
只有 input 为 0 的构造数据才可能返回 100。
124+
"""
125+
tot = cache_read + inp
126+
if not tot:
127+
return 0.0
128+
v = cache_read / tot * 100
129+
if inp > 0 and v >= 99.99:
130+
v = 99.99
131+
return round(v, 2)
132+
133+
118134
RANGE_MS = {"1h": 3600_000, "12h": 12 * 3600_000, "24h": 86400_000,
119135
"7d": 7 * 86400_000, "30d": 30 * 86400_000, "all": None}
120136
CUSTOM_RANGE_RE = re.compile(r"(\d+)h")
@@ -300,7 +316,7 @@ def build_payload(db_path: Path, range_key: str, models_filter=None,
300316
sum_out = sum((r[5] or 0) for r in rows)
301317
sum_cr = sum((r[6] or 0) for r in rows)
302318
sum_dur = sum((r[7] or 0) for r in rows)
303-
hit_rate = (sum_cr / (sum_cr + sum_in) * 100) if (sum_cr + sum_in) else 0.0
319+
hit_rate = pct_hit_rate(sum_cr, sum_in)
304320
tok_s = (sum_out / (sum_dur / 1000)) if sum_dur else 0.0
305321
sessions = {r[2] for r in rows}
306322

@@ -351,12 +367,11 @@ def build_payload(db_path: Path, range_key: str, models_filter=None,
351367
m["dur"] += r[7] or 0
352368
model_list = []
353369
for m in sorted(models.values(), key=lambda x: -(x["output"] + x["input"] + x["cache_read"])):
354-
tot_in = m["cache_read"] + m["input"]
355370
model_list.append({
356371
"model": m["model"], "calls": m["calls"],
357372
"input": m["input"], "output": m["output"],
358373
"cache_read": m["cache_read"],
359-
"hit_rate": round(m["cache_read"] / tot_in * 100, 1) if tot_in else 0,
374+
"hit_rate": pct_hit_rate(m["cache_read"], m["input"]),
360375
"avg_dur_ms": round(m["dur"] / m["calls"]) if m["calls"] else 0,
361376
"tok_s": round(m["output"] / (m["dur"] / 1000), 1) if m["dur"] else 0,
362377
})
@@ -421,7 +436,7 @@ def build_payload(db_path: Path, range_key: str, models_filter=None,
421436
"output_tokens": sum_out,
422437
"cache_read_tokens": sum_cr,
423438
"total_tokens": sum_in + sum_cr + sum_out,
424-
"hit_rate_pct": round(hit_rate, 2),
439+
"hit_rate_pct": hit_rate,
425440
"avg_tok_s": round(tok_s, 1),
426441
"sum_dur_s": round(sum_dur / 1000, 1),
427442
},

0 commit comments

Comments
 (0)