-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup_bq_views.py
More file actions
388 lines (370 loc) · 16.6 KB
/
setup_bq_views.py
File metadata and controls
388 lines (370 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import os
import argparse
from google.cloud import bigquery
def create_views(project_id, dataset_id, table_name):
client = bigquery.Client(project=project_id)
dataset_ref = f"{project_id}.{dataset_id}"
# 1. Model Pricing Table
pricing_sql = f"""
CREATE OR REPLACE TABLE `{dataset_ref}.model_pricing` AS
SELECT "gemini-1.5-flash" AS model_version, 0.075 / 1000000 AS input_cost_per_token, 0.30 / 1000000 AS output_cost_per_token UNION ALL
SELECT "gemini-1.5-pro", 1.25 / 1000000, 5.00 / 1000000 UNION ALL
SELECT "gemini-2.0-flash", 0.10 / 1000000, 0.40 / 1000000 UNION ALL
SELECT "gemini-2.0-pro", 1.50 / 1000000, 6.00 / 1000000 UNION ALL
SELECT "gemini-2.5-flash", 0.30 / 1000000, 2.50 / 1000000 UNION ALL
SELECT "gemini-2.5-pro", 1.25 / 1000000, 10.00 / 1000000 UNION ALL
SELECT "gemini-3.0-flash", 0.50 / 1000000, 3.00 / 1000000 UNION ALL
SELECT "gemini-3.0-pro", 2.00 / 1000000, 12.00 / 1000000;
"""
# 2. Master Session Summary
session_master_sql = f"""
CREATE OR REPLACE VIEW `{dataset_ref}.v_aaa_session_summary` AS
WITH session_costs AS (
SELECT
session_id,
SUM(CAST(JSON_VALUE(attributes, '$.usage_metadata.total_token_count') AS INT64)) as total_tokens,
SUM(CAST(JSON_VALUE(attributes, '$.usage_metadata.prompt_token_count') AS INT64)) as prompt_tokens,
SUM(CAST(JSON_VALUE(attributes, '$.usage_metadata.candidates_token_count') AS INT64)) as output_tokens,
SUM(
(CAST(JSON_VALUE(attributes, '$.usage_metadata.prompt_token_count') AS INT64) * COALESCE(p.input_cost_per_token, 0)) +
(CAST(JSON_VALUE(attributes, '$.usage_metadata.candidates_token_count') AS INT64) * COALESCE(p.output_cost_per_token, 0))
) as total_usd_cost
FROM `{dataset_ref}.{table_name}` e
LEFT JOIN `{dataset_ref}.model_pricing` p
ON JSON_VALUE(e.attributes, '$.model_version') = p.model_version
WHERE event_type = 'LLM_RESPONSE'
GROUP BY session_id
)
SELECT
e.session_id,
e.user_id,
MIN(e.timestamp) AS session_start,
MAX(e.timestamp) AS session_end,
TIMESTAMP_DIFF(MAX(e.timestamp), MIN(e.timestamp), MILLISECOND) AS session_duration_ms,
COUNT(DISTINCT e.invocation_id) AS total_turns,
COUNTIF(e.event_type = 'USER_MESSAGE_RECEIVED') AS human_messages,
COUNTIF(e.event_type = 'LLM_REQUEST') AS total_llm_calls,
COUNTIF(e.event_type = 'TOOL_COMPLETED') AS total_tools_executed,
COUNTIF(e.status = 'ERROR') AS total_errors,
MAX(c.total_tokens) AS session_total_tokens,
MAX(c.prompt_tokens) AS session_prompt_tokens,
MAX(c.output_tokens) AS session_completion_tokens,
MAX(c.total_usd_cost) AS session_total_cost_usd,
MAX(CAST(JSON_VALUE(e.latency_ms, '$.time_to_first_token_ms') AS INT64)) AS max_ttft_ms
FROM `{dataset_ref}.{table_name}` e
LEFT JOIN session_costs c ON e.session_id = c.session_id
GROUP BY e.session_id, e.user_id;
"""
# 3. Master Turn Summary
turn_master_sql = f"""
CREATE OR REPLACE VIEW `{dataset_ref}.v_aaa_turn_summary` AS
WITH turn_costs AS (
SELECT
session_id,
invocation_id,
SUM(CAST(JSON_VALUE(attributes, '$.usage_metadata.total_token_count') AS INT64)) as turn_tokens,
SUM(
(CAST(JSON_VALUE(attributes, '$.usage_metadata.prompt_token_count') AS INT64) * COALESCE(p.input_cost_per_token, 0)) +
(CAST(JSON_VALUE(attributes, '$.usage_metadata.candidates_token_count') AS INT64) * COALESCE(p.output_cost_per_token, 0))
) as turn_usd_cost
FROM `{dataset_ref}.{table_name}` e
LEFT JOIN `{dataset_ref}.model_pricing` p
ON JSON_VALUE(e.attributes, '$.model_version') = p.model_version
WHERE event_type = 'LLM_RESPONSE'
GROUP BY session_id, invocation_id
),
turn_intents AS (
SELECT
session_id,
invocation_id,
ANY_VALUE(JSON_VALUE(content, '$.text_summary')) as user_query
FROM `{dataset_ref}.{table_name}`
WHERE event_type = 'USER_MESSAGE_RECEIVED'
GROUP BY session_id, invocation_id
)
SELECT
e.session_id,
e.invocation_id,
e.user_id,
i.user_query,
ROW_NUMBER() OVER(PARTITION BY e.session_id ORDER BY MIN(e.timestamp)) as turn_index,
MIN(e.timestamp) AS turn_start,
MAX(e.timestamp) AS turn_end,
COUNTIF(e.event_type = 'LLM_REQUEST') AS llm_calls_in_turn,
COUNTIF(e.event_type = 'TOOL_COMPLETED') AS tools_in_turn,
COUNTIF(e.status = 'ERROR') AS errors_in_turn,
MAX(c.turn_tokens) AS tokens,
MAX(c.turn_usd_cost) AS cost,
SUM(IF(e.event_type = 'LLM_RESPONSE', CAST(JSON_VALUE(e.latency_ms, '$.total_ms') AS INT64), 0)) AS total_llm_latency_ms,
SUM(IF(e.event_type = 'TOOL_COMPLETED', CAST(JSON_VALUE(e.latency_ms, '$.total_ms') AS INT64), 0)) AS total_tool_latency_ms,
TIMESTAMP_DIFF(MAX(e.timestamp), MIN(e.timestamp), MILLISECOND) AS turn_duration_ms,
GREATEST(0, TIMESTAMP_DIFF(MAX(e.timestamp), MIN(e.timestamp), MILLISECOND) -
SUM(IF(e.event_type = 'LLM_RESPONSE', CAST(JSON_VALUE(e.latency_ms, '$.total_ms') AS INT64), 0)) -
SUM(IF(e.event_type = 'TOOL_COMPLETED', CAST(JSON_VALUE(e.latency_ms, '$.total_ms') AS INT64), 0))) AS turn_overhead_ms
FROM `{dataset_ref}.{table_name}` e
LEFT JOIN turn_costs c ON e.session_id = c.session_id AND e.invocation_id = c.invocation_id
LEFT JOIN turn_intents i ON e.session_id = i.session_id AND e.invocation_id = i.invocation_id
GROUP BY e.session_id, e.invocation_id, e.user_id, i.user_query;
"""
# 4. Master LLM & Prompt Tracing
llm_master_sql = f"""
CREATE OR REPLACE VIEW `{dataset_ref}.v_aaa_llm_calls` AS
WITH prompts AS (
SELECT
invocation_id,
session_id,
ANY_VALUE(COALESCE(
TO_JSON_STRING(JSON_QUERY(content, '$.prompt')),
TO_JSON_STRING(JSON_QUERY(content, '$.request.prompt')),
JSON_VALUE(content, '$.prompt'),
JSON_VALUE(content, '$.request.prompt')
)) as turn_prompt
FROM `{dataset_ref}.{table_name}`
WHERE event_type IN ('LLM_REQUEST', 'LLM_RESPONSE')
AND (JSON_QUERY(content, '$.prompt') IS NOT NULL OR JSON_QUERY(content, '$.request.prompt') IS NOT NULL)
GROUP BY invocation_id, session_id
)
SELECT
e.timestamp,
e.session_id,
e.invocation_id,
e.user_id,
e.agent,
JSON_VALUE(e.attributes, '$.model_version') AS model,
CAST(JSON_VALUE(e.attributes, '$.usage_metadata.prompt_token_count') AS INT64) AS prompt_tokens,
CAST(JSON_VALUE(e.attributes, '$.usage_metadata.candidates_token_count') AS INT64) AS completion_tokens,
CAST(JSON_VALUE(e.attributes, '$.usage_metadata.total_token_count') AS INT64) AS total_tokens,
(
(CAST(JSON_VALUE(e.attributes, '$.usage_metadata.prompt_token_count') AS INT64) * COALESCE(p.input_cost_per_token, 0)) +
(CAST(JSON_VALUE(e.attributes, '$.usage_metadata.candidates_token_count') AS INT64) * COALESCE(p.output_cost_per_token, 0))
) AS calculated_usd_cost,
COALESCE(pr.turn_prompt, TO_JSON_STRING(JSON_QUERY(e.content, '$.prompt'))) AS prompt,
COALESCE(
JSON_VALUE(e.content, '$.response.text'),
JSON_VALUE(e.content, '$.response'),
JSON_VALUE(e.content, '$.text'),
TO_JSON_STRING(JSON_QUERY(e.content, '$.response')),
TO_JSON_STRING(e.content)
) AS response
FROM `{dataset_ref}.{table_name}` e
LEFT JOIN `{dataset_ref}.model_pricing` p
ON JSON_VALUE(e.attributes, '$.model_version') = p.model_version
LEFT JOIN prompts pr
ON e.invocation_id = pr.invocation_id AND e.session_id = pr.session_id
WHERE e.event_type = 'LLM_RESPONSE';
"""
# 5. Master Tool Performance
tool_master_sql = f"""
CREATE OR REPLACE VIEW `{dataset_ref}.v_aaa_tool_usage` AS
WITH tool_starts AS (
SELECT
invocation_id, session_id, user_id, agent,
JSON_VALUE(content, '$.tool') AS tool_name,
COALESCE(
TO_JSON_STRING(JSON_QUERY(content, '$.args')), JSON_VALUE(content, '$.args'),
TO_JSON_STRING(JSON_QUERY(content, '$.arguments')), JSON_VALUE(content, '$.arguments'),
TO_JSON_STRING(JSON_QUERY(content, '$.parameters')), JSON_VALUE(content, '$.parameters'),
TO_JSON_STRING(JSON_QUERY(content, '$.input')), JSON_VALUE(content, '$.input')
) AS input_args
FROM `{dataset_ref}.{table_name}`
WHERE event_type = 'TOOL_STARTING'
),
tool_completes AS (
SELECT
invocation_id, timestamp, status, error_message,
JSON_VALUE(content, '$.tool') AS tool_name,
CAST(JSON_VALUE(latency_ms, '$.total_ms') AS INT64) AS latency_ms,
COALESCE(
TO_JSON_STRING(JSON_QUERY(content, '$.result')), JSON_VALUE(content, '$.result'),
TO_JSON_STRING(JSON_QUERY(content, '$.response')), JSON_VALUE(content, '$.response'),
TO_JSON_STRING(JSON_QUERY(content, '$.output')), JSON_VALUE(content, '$.output')
) AS output_result
FROM `{dataset_ref}.{table_name}`
WHERE event_type = 'TOOL_COMPLETED'
)
SELECT
tc.timestamp, ts.session_id, ts.invocation_id, ts.user_id, ts.agent, ts.tool_name,
tc.status, tc.error_message, tc.latency_ms, ts.input_args, tc.output_result
FROM tool_starts ts
JOIN tool_completes tc
ON ts.invocation_id = tc.invocation_id
AND ts.tool_name = tc.tool_name;
"""
# 6. Model Routing & Orchestrator Flow
routing_sql = f"""
CREATE OR REPLACE VIEW `{dataset_ref}.v_aaa_agent_routing` AS
SELECT
timestamp,
session_id,
user_id,
invocation_id,
JSON_VALUE(attributes, '$.root_agent_name') AS orchestrator,
agent as assigned_specialist,
event_type
FROM `{dataset_ref}.{table_name}`
WHERE event_type IN ('AGENT_COMPLETED', 'LLM_REQUEST')
ORDER BY timestamp DESC;
"""
# 7. User Context & Intent
intent_sql = f"""
CREATE OR REPLACE VIEW `{dataset_ref}.v_aaa_user_intent` AS
SELECT
timestamp,
session_id,
user_id,
JSON_VALUE(content, '$.text_summary') AS raw_user_prompt,
JSON_VALUE(attributes, '$.session_metadata.state.state."user:timezone"') AS user_timezone
FROM `{dataset_ref}.{table_name}`
WHERE event_type = 'USER_MESSAGE_RECEIVED'
ORDER BY timestamp DESC;
"""
# 8. Session Transcript (Chat Replay)
transcript_sql = f"""
CREATE OR REPLACE VIEW `{dataset_ref}.v_aaa_session_transcript` AS
SELECT
timestamp,
session_id,
user_id,
'Human' as speaker,
COALESCE(JSON_VALUE(content, '$.text'), JSON_VALUE(content, '$.text_summary')) as message
FROM `{dataset_ref}.{table_name}`
WHERE event_type = 'USER_MESSAGE_RECEIVED'
UNION ALL
SELECT
timestamp,
session_id,
user_id,
'Agent' as speaker,
COALESCE(JSON_VALUE(content, '$.response.text'), JSON_VALUE(content, '$.response'), JSON_VALUE(content, '$.text')) as message
FROM (
SELECT *, ROW_NUMBER() OVER(PARTITION BY session_id, invocation_id ORDER BY timestamp DESC) as rn
FROM `{dataset_ref}.{table_name}`
WHERE event_type = 'LLM_RESPONSE'
)
WHERE rn = 1;
"""
# 9. Unified Session Chronology
chronology_sql = f"""
CREATE OR REPLACE VIEW `{dataset_ref}.v_aaa_session_chronology` AS
WITH raw_data AS (
SELECT
timestamp as time,
session_id,
user_id,
invocation_id,
event_type,
status,
error_message,
content,
agent,
attributes,
latency_ms
FROM `{dataset_ref}.{table_name}`
),
prompts AS (
-- Extract prompts from any event in the turn (LLM_REQUEST or LLM_RESPONSE)
SELECT
invocation_id,
session_id,
ANY_VALUE(COALESCE(JSON_VALUE(content, '$.prompt'), JSON_VALUE(content, '$.request.prompt'))) as turn_prompt
FROM raw_data
WHERE event_type IN ('LLM_REQUEST', 'LLM_RESPONSE')
AND (JSON_VALUE(content, '$.prompt') IS NOT NULL OR JSON_VALUE(content, '$.request.prompt') IS NOT NULL)
GROUP BY invocation_id, session_id
),
base_events AS (
SELECT
r.*,
CASE
WHEN r.event_type = 'USER_MESSAGE_RECEIVED' THEN '👤 Human Input'
WHEN r.event_type = 'TOOL_STARTING' THEN '🔧 Tool Starting'
WHEN r.event_type = 'TOOL_COMPLETED' THEN '✅ Tool Execution Result'
WHEN r.event_type = 'LLM_REQUEST' THEN '🧠 Agent Reasoning (Start)'
WHEN r.event_type = 'LLM_RESPONSE' THEN '🗣️ Agent Output'
WHEN r.event_type = 'AGENT_STARTING' THEN '🔀 Sub-Agent Routing'
WHEN r.event_type = 'AGENT_COMPLETED' THEN '🏁 Sub-Agent Completed'
ELSE r.event_type
END as step_type,
COALESCE(
JSON_VALUE(r.content, '$.tool'),
r.agent,
JSON_VALUE(r.attributes, '$.root_agent_name'),
'System'
) as actor,
COALESCE(CAST(JSON_VALUE(r.latency_ms, '$.total_ms') AS INT64), 0) as duration_ms,
p.turn_prompt
FROM raw_data r
LEFT JOIN prompts p ON r.invocation_id = p.invocation_id AND r.session_id = p.session_id
WHERE r.event_type IN (
'USER_MESSAGE_RECEIVED',
'LLM_REQUEST',
'LLM_RESPONSE',
'TOOL_STARTING',
'TOOL_COMPLETED',
'AGENT_STARTING',
'AGENT_COMPLETED'
)
)
SELECT
time, session_id, user_id, invocation_id, event_type, step_type, actor, duration_ms, status, error_message,
-- 1. Conversation Message (Expanded UI - Identical to Transcripts)
CASE
WHEN event_type = 'USER_MESSAGE_RECEIVED' THEN
COALESCE(JSON_VALUE(content, '$.text'), JSON_VALUE(content, '$.text_summary'))
WHEN event_type IN ('LLM_RESPONSE', 'AGENT_COMPLETED') THEN
COALESCE(JSON_VALUE(content, '$.response.text'), JSON_VALUE(content, '$.response'), JSON_VALUE(content, '$.text'))
ELSE NULL
END as message,
-- 2. Technical Details (Collapsed UI with Eye Icon)
CASE
WHEN event_type = 'TOOL_STARTING' THEN
TO_JSON_STRING(COALESCE(JSON_QUERY(content, '$.args'), JSON_QUERY(content, '$.arguments'), JSON_QUERY(content, '$.parameters'), JSON_QUERY(content, '$.input'), content))
WHEN event_type = 'TOOL_COMPLETED' THEN
TO_JSON_STRING(COALESCE(JSON_QUERY(content, '$.result'), JSON_QUERY(content, '$.response'), JSON_QUERY(content, '$.output'), content))
WHEN event_type = 'LLM_REQUEST' THEN
CONCAT('🧠 Inference started (', actor, ')')
WHEN event_type IN ('LLM_RESPONSE', 'AGENT_COMPLETED') AND NOT (actor = JSON_VALUE(attributes, '$.root_agent_name') OR agent = JSON_VALUE(attributes, '$.root_agent_name')) THEN
TO_JSON_STRING(content)
ELSE NULL
END as technical_details,
-- 3. Always provide full details for the 'Inspect' side-drawer
COALESCE(TO_JSON_STRING(content), '(No data)') as _full_details
FROM base_events
ORDER BY time ASC;
"""
queries = [
("Pricing Table", pricing_sql),
("Session Summary View", session_master_sql),
("Turn Summary View", turn_master_sql),
("LLM Calls View", llm_master_sql),
("Tool Usage View", tool_master_sql),
("Agent Routing View", routing_sql),
("User Intent View", intent_sql),
("Session Transcript View", transcript_sql),
("Unified Session Chronology", chronology_sql)
]
table_success = 0
view_success = 0
fail_count = 0
for name, sql in queries:
try:
query_job = client.query(sql)
query_job.result()
print(f"✅ Created: {name}")
if "Table" in name:
table_success += 1
else:
view_success += 1
except Exception as e:
print(f"❌ Failed to create {name}: {e}")
fail_count += 1
print(f"\n📊 Summary: {table_success} Tables and {view_success} Views created successfully. ({fail_count} failed)")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Setup BigQuery Views for Agent Analytics Dashboard")
parser.add_argument("--project", required=True, help="GCP Project ID")
parser.add_argument("--dataset", required=True, help="BigQuery Dataset ID")
parser.add_argument("--table", required=True, help="Base BigQuery Table Name")
args = parser.parse_args()
print(f"🚀 Setting up views in {args.project}.{args.dataset} using base table {args.table}...")
create_views(args.project, args.dataset, args.table)
print("\n🎉 Setup complete! You can now import the Grafana JSONs and set the variables.")