LiamKhoaLe commited on
Commit
261e0ce
·
1 Parent(s): 478c2a1

Upd analytics tracker

Browse files
.DS_Store CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
 
helpers/coder.py CHANGED
@@ -20,7 +20,8 @@ async def generate_code_artifacts(
20
  context_text: str,
21
  web_context: str,
22
  gemini_rotator,
23
- nvidia_rotator
 
24
  ) -> str:
25
  """Generate code (files-by-files) with explanations using Gemini Pro.
26
 
@@ -51,6 +52,27 @@ async def generate_code_artifacts(
51
  selection = {"provider": "gemini", "model": "gemini-2.5-pro"}
52
 
53
  logger.info(f"[CODER] Generating code for subsection {subsection_id} (task='{task[:60]}...')")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  code_md = await generate_answer_with_model(selection, system_prompt, user_prompt, gemini_rotator, nvidia_rotator)
55
  code_md = (code_md or "").strip()
56
 
 
20
  context_text: str,
21
  web_context: str,
22
  gemini_rotator,
23
+ nvidia_rotator,
24
+ user_id: str = ""
25
  ) -> str:
26
  """Generate code (files-by-files) with explanations using Gemini Pro.
27
 
 
52
  selection = {"provider": "gemini", "model": "gemini-2.5-pro"}
53
 
54
  logger.info(f"[CODER] Generating code for subsection {subsection_id} (task='{task[:60]}...')")
55
+ # Track analytics
56
+ try:
57
+ from utils.analytics import get_analytics_tracker
58
+ tracker = get_analytics_tracker()
59
+ if tracker and user_id:
60
+ await tracker.track_agent_usage(
61
+ user_id=user_id,
62
+ agent_name="coding",
63
+ action="generate_code",
64
+ context="report_coding",
65
+ metadata={"subsection_id": subsection_id}
66
+ )
67
+ await tracker.track_model_usage(
68
+ user_id=user_id,
69
+ model_name=selection["model"],
70
+ provider=selection["provider"],
71
+ context="report_coding",
72
+ metadata={"subsection_id": subsection_id}
73
+ )
74
+ except Exception:
75
+ pass
76
  code_md = await generate_answer_with_model(selection, system_prompt, user_prompt, gemini_rotator, nvidia_rotator)
77
  code_md = (code_md or "").strip()
78
 
helpers/diagram.py CHANGED
@@ -27,7 +27,8 @@ async def generate_mermaid_diagram(
27
  nvidia_rotator,
28
  render_error: str = "",
29
  retry: int = 0,
30
- max_retries: int = 5
 
31
  ) -> str:
32
  from utils.api.router import generate_answer_with_model
33
 
@@ -70,6 +71,27 @@ async def generate_mermaid_diagram(
70
  selection = {"provider": "nvidia_large", "model": "openai/gpt-oss-120b"}
71
 
72
  logger.info(f"[DIAGRAM] Generating Mermaid (retry={retry}/{max_retries})")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  diagram = await generate_answer_with_model(selection, sys_prompt, user_prompt, gemini_rotator, nvidia_rotator)
74
  diagram = (diagram or "").strip()
75
 
 
27
  nvidia_rotator,
28
  render_error: str = "",
29
  retry: int = 0,
30
+ max_retries: int = 5,
31
+ user_id: str = ""
32
  ) -> str:
33
  from utils.api.router import generate_answer_with_model
34
 
 
71
  selection = {"provider": "nvidia_large", "model": "openai/gpt-oss-120b"}
72
 
73
  logger.info(f"[DIAGRAM] Generating Mermaid (retry={retry}/{max_retries})")
74
+ # Track analytics
75
+ try:
76
+ from utils.analytics import get_analytics_tracker
77
+ tracker = get_analytics_tracker()
78
+ if tracker and user_id:
79
+ await tracker.track_agent_usage(
80
+ user_id=user_id,
81
+ agent_name="diagram",
82
+ action="generate_mermaid",
83
+ context="report_diagram",
84
+ metadata={"retry": retry}
85
+ )
86
+ await tracker.track_model_usage(
87
+ user_id=user_id,
88
+ model_name=selection["model"],
89
+ provider=selection["provider"],
90
+ context="report_diagram",
91
+ metadata={"retry": retry}
92
+ )
93
+ except Exception:
94
+ pass
95
  diagram = await generate_answer_with_model(selection, sys_prompt, user_prompt, gemini_rotator, nvidia_rotator)
96
  diagram = (diagram or "").strip()
97
 
routes/reports.py CHANGED
@@ -138,6 +138,19 @@ async def generate_report(
138
 
139
  # Use enhanced instructions for better CoT planning
140
  cot_plan = await generate_cot_plan(enhanced_instructions, file_summary, context_text, web_context_block, nvidia_rotator, gemini_rotator)
 
 
 
 
 
 
 
 
 
 
 
 
 
141
 
142
  # Step 2: Execute detailed subtasks based on CoT plan
143
  logger.info("[REPORT] Executing detailed subtasks")
@@ -151,6 +164,19 @@ async def generate_report(
151
  comprehensive_report = await synthesize_comprehensive_report(
152
  enhanced_instructions, cot_plan, detailed_analysis, eff_name, report_words, gemini_rotator, nvidia_rotator
153
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
154
 
155
  # Update status: Generating answer (final report generation)
156
  update_report_status(session_id, "generating", "Generating answer...", 80)
@@ -482,7 +508,8 @@ async def execute_detailed_subtasks(cot_plan: Dict[str, Any], context_text: str,
482
  context_text=context_text,
483
  web_context=web_context,
484
  gemini_rotator=gemini_rotator,
485
- nvidia_rotator=nvidia_rotator
 
486
  )
487
  subtask_result = subtask_result + "\n\n" + code_markdown
488
  try:
@@ -870,7 +897,7 @@ Create a comprehensive, authoritative report with proper hierarchical structure
870
  try:
871
  if should_generate_mermaid(instructions, report):
872
  logger.info("[REPORT] Considering Mermaid diagram generation")
873
- diagram = await generate_mermaid_diagram(instructions, detailed_analysis, gemini_rotator, nvidia_rotator)
874
  if diagram:
875
  report = ("## Diagrams\n\n" + "```mermaid\n" + diagram.strip() + "\n```\n\n" + report)
876
  else:
 
138
 
139
  # Use enhanced instructions for better CoT planning
140
  cot_plan = await generate_cot_plan(enhanced_instructions, file_summary, context_text, web_context_block, nvidia_rotator, gemini_rotator)
141
+ # Track planning agent
142
+ try:
143
+ tracker = get_analytics_tracker()
144
+ if tracker:
145
+ await tracker.track_agent_usage(
146
+ user_id=user_id,
147
+ agent_name="planning",
148
+ action="plan_report",
149
+ context="report_planning",
150
+ metadata={"project_id": project_id, "session_id": session_id}
151
+ )
152
+ except Exception:
153
+ pass
154
 
155
  # Step 2: Execute detailed subtasks based on CoT plan
156
  logger.info("[REPORT] Executing detailed subtasks")
 
164
  comprehensive_report = await synthesize_comprehensive_report(
165
  enhanced_instructions, cot_plan, detailed_analysis, eff_name, report_words, gemini_rotator, nvidia_rotator
166
  )
167
+ # Track synthesis (report agent)
168
+ try:
169
+ tracker = get_analytics_tracker()
170
+ if tracker:
171
+ await tracker.track_agent_usage(
172
+ user_id=user_id,
173
+ agent_name="report",
174
+ action="synthesize_report",
175
+ context="report_synthesis",
176
+ metadata={"project_id": project_id, "session_id": session_id}
177
+ )
178
+ except Exception:
179
+ pass
180
 
181
  # Update status: Generating answer (final report generation)
182
  update_report_status(session_id, "generating", "Generating answer...", 80)
 
508
  context_text=context_text,
509
  web_context=web_context,
510
  gemini_rotator=gemini_rotator,
511
+ nvidia_rotator=nvidia_rotator,
512
+ user_id=user_id
513
  )
514
  subtask_result = subtask_result + "\n\n" + code_markdown
515
  try:
 
897
  try:
898
  if should_generate_mermaid(instructions, report):
899
  logger.info("[REPORT] Considering Mermaid diagram generation")
900
+ diagram = await generate_mermaid_diagram(instructions, detailed_analysis, gemini_rotator, nvidia_rotator, user_id=user_id)
901
  if diagram:
902
  report = ("## Diagrams\n\n" + "```mermaid\n" + diagram.strip() + "\n```\n\n" + report)
903
  else:
static/index.html CHANGED
@@ -211,7 +211,7 @@
211
  </div>
212
  <div class="analytics-actions">
213
  <button id="refresh-analytics" class="btn-primary">Refresh Data</button>
214
- <button id="debug-analytics" class="btn-secondary">Debug Load</button>
215
  </div>
216
  </div>
217
 
 
211
  </div>
212
  <div class="analytics-actions">
213
  <button id="refresh-analytics" class="btn-primary">Refresh Data</button>
214
+ <!-- <button id="debug-analytics" class="btn-secondary">Debug Load</button> -->
215
  </div>
216
  </div>
217
 
utils/analytics.py CHANGED
@@ -114,11 +114,12 @@ class AnalyticsTracker:
114
  # Daily usage trends
115
  daily_pipeline = [
116
  {"$match": {"user_id": user_id, "timestamp": {"$gte": cutoff_time}}},
 
117
  {"$group": {
118
  "_id": {
119
- "year": {"$year": {"$dateFromTimestamp": {"$multiply": ["$timestamp", 1000]}}},
120
- "month": {"$month": {"$dateFromTimestamp": {"$multiply": ["$timestamp", 1000]}}},
121
- "day": {"$dayOfMonth": {"$dateFromTimestamp": {"$multiply": ["$timestamp", 1000]}}}
122
  },
123
  "total_requests": {"$sum": 1},
124
  "model_requests": {"$sum": {"$cond": [{"$eq": ["$type", "model"]}, 1, 0]}},
@@ -161,12 +162,20 @@ class AnalyticsTracker:
161
  model_pipeline = [
162
  {"$match": {"type": "model", "timestamp": {"$gte": cutoff_time}}},
163
  {"$group": {
164
- "_id": "$model_name",
 
 
 
165
  "count": {"$sum": 1},
166
- "unique_users": {"$addToSet": "$user_id"},
167
- "provider": {"$first": "$provider"}
 
 
 
 
 
 
168
  }},
169
- {"$addFields": {"unique_user_count": {"$size": "$unique_users"}}},
170
  {"$sort": {"count": -1}}
171
  ]
172
 
@@ -176,12 +185,25 @@ class AnalyticsTracker:
176
  agent_pipeline = [
177
  {"$match": {"type": "agent", "timestamp": {"$gte": cutoff_time}}},
178
  {"$group": {
179
- "_id": "$agent_name",
 
 
 
180
  "count": {"$sum": 1},
181
- "unique_users": {"$addToSet": "$user_id"},
182
- "actions": {"$addToSet": "$action"}
 
 
 
 
 
 
 
 
 
 
 
183
  }},
184
- {"$addFields": {"unique_user_count": {"$size": "$unique_users"}}},
185
  {"$sort": {"count": -1}}
186
  ]
187
 
 
114
  # Daily usage trends
115
  daily_pipeline = [
116
  {"$match": {"user_id": user_id, "timestamp": {"$gte": cutoff_time}}},
117
+ {"$addFields": {"date": {"$toDate": {"$multiply": ["$timestamp", 1000]}}}},
118
  {"$group": {
119
  "_id": {
120
+ "year": {"$year": "$date"},
121
+ "month": {"$month": "$date"},
122
+ "day": {"$dayOfMonth": "$date"}
123
  },
124
  "total_requests": {"$sum": 1},
125
  "model_requests": {"$sum": {"$cond": [{"$eq": ["$type", "model"]}, 1, 0]}},
 
162
  model_pipeline = [
163
  {"$match": {"type": "model", "timestamp": {"$gte": cutoff_time}}},
164
  {"$group": {
165
+ "_id": {
166
+ "provider": "$provider",
167
+ "model": "$model_name"
168
+ },
169
  "count": {"$sum": 1},
170
+ "unique_users": {"$addToSet": "$user_id"}
171
+ }},
172
+ {"$project": {
173
+ "_id": 0,
174
+ "model_name": "$_id.model",
175
+ "provider": "$_id.provider",
176
+ "count": 1,
177
+ "unique_user_count": {"$size": "$unique_users"}
178
  }},
 
179
  {"$sort": {"count": -1}}
180
  ]
181
 
 
185
  agent_pipeline = [
186
  {"$match": {"type": "agent", "timestamp": {"$gte": cutoff_time}}},
187
  {"$group": {
188
+ "_id": {
189
+ "agent": "$agent_name",
190
+ "action": "$action"
191
+ },
192
  "count": {"$sum": 1},
193
+ "unique_users": {"$addToSet": "$user_id"}
194
+ }},
195
+ {"$group": {
196
+ "_id": "$_id.agent",
197
+ "count": {"$sum": "$count"},
198
+ "unique_users": {"$addToSet": "$unique_users"},
199
+ "actions": {"$addToSet": "$_id.action"}
200
+ }},
201
+ {"$project": {
202
+ "_id": 1,
203
+ "count": 1,
204
+ "actions": 1,
205
+ "unique_user_count": {"$size": {"$setUnion": "$unique_users"}}
206
  }},
 
207
  {"$sort": {"count": -1}}
208
  ]
209