HamidOmarov commited on
Commit
089198a
·
1 Parent(s): af48d88

Add in-memory metrics + /stats, /get_history and include router

Browse files
Files changed (2) hide show
  1. app/metrics.py +42 -0
  2. app/routes_stats.py +13 -0
app/metrics.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from collections import deque, defaultdict
2
+ from datetime import datetime, timedelta, timezone
3
+
4
+ class StatsTracker:
5
+ def __init__(self, max_recent=200):
6
+ self.questions_answered = 0
7
+ self.total_chunks = 0
8
+ self._latencies_ms = deque(maxlen=max_recent)
9
+ self._recent_questions = deque(maxlen=max_recent)
10
+ self._by_day = defaultdict(int)
11
+
12
+ def record_upload(self, chunks_added:int):
13
+ self.total_chunks += int(chunks_added)
14
+
15
+ def record_question(self, question:str, latency_ms:float, top_k:int):
16
+ self.questions_answered += 1
17
+ self._latencies_ms.append(float(latency_ms))
18
+ now = datetime.now(timezone.utc)
19
+ self._recent_questions.append({
20
+ "timestamp": now.isoformat(),
21
+ "question": question,
22
+ "latency_ms": round(latency_ms, 2),
23
+ "top_k": top_k
24
+ })
25
+ self._by_day[now.date()] += 1
26
+
27
+ def get_stats(self):
28
+ avg_ms = round(sum(self._latencies_ms)/len(self._latencies_ms), 2) if self._latencies_ms else 0.0
29
+ today = datetime.now(timezone.utc).date()
30
+ last7 = []
31
+ for i in range(6, -1, -1):
32
+ d = today - timedelta(days=i)
33
+ last7.append({ "date": d.isoformat(), "questions": int(self._by_day.get(d, 0)) })
34
+ return {
35
+ "total_chunks": int(self.total_chunks),
36
+ "questions_answered": int(self.questions_answered),
37
+ "avg_ms": avg_ms,
38
+ "last7": last7,
39
+ "lastN_questions": list(self._recent_questions)[-20:],
40
+ }
41
+
42
+ tracker = StatsTracker()
app/routes_stats.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter
2
+ from .metrics import tracker
3
+
4
+ router = APIRouter()
5
+
6
+ @router.get("/stats")
7
+ def get_stats():
8
+ return tracker.get_stats()
9
+
10
+ @router.get("/get_history")
11
+ def get_history():
12
+ s = tracker.get_stats()
13
+ return {"history": s["lastN_questions"], "total_chunks": s["total_chunks"]}