You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.query/ExamplesContent.tsx
+13Lines changed: 13 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -38,6 +38,19 @@ ORDER BY p50_duration_ms DESC
38
38
LIMIT 20`,
39
39
scope: "environment",
40
40
},
41
+
{
42
+
title: "Runs over time",
43
+
description:
44
+
"Count of runs bucketed over time. The bucket size adjusts automatically to the time range.",
45
+
query: `SELECT
46
+
timeBucket(),
47
+
count() AS run_count
48
+
FROM runs
49
+
GROUP BY timeBucket
50
+
ORDER BY timeBucket
51
+
LIMIT 1000`,
52
+
scope: "environment",
53
+
},
41
54
{
42
55
title: "Most expensive 100 runs (past 7d)",
43
56
description: "Top 100 runs by cost over the last 7 days.",
Copy file name to clipboardExpand all lines: apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.query/TRQLGuideContent.tsx
+5Lines changed: 5 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -488,6 +488,11 @@ ORDER BY run_count DESC`,
488
488
<FunctionCategory
489
489
title="Date/time functions"
490
490
functions={[
491
+
{
492
+
name: "timeBucket()",
493
+
desc: "Auto-bucket by time period. Uses the table's time column with an interval based on the query's time range.",
494
+
example: "SELECT timeBucket(), count() FROM runs GROUP BY timeBucket",
495
+
},
491
496
{name: "now()",desc: "Current date and time",example: "now()"},
Copy file name to clipboardExpand all lines: apps/webapp/app/v3/services/aiQueryService.server.ts
+43-14Lines changed: 43 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -414,13 +414,28 @@ HAVING cnt > 10
414
414
\`\`\`
415
415
416
416
### Date/Time Functions
417
+
- timeBucket() - automatically bucket by time. Uses the table's time column and picks the best interval based on the query's time range. Use in SELECT and reference as \`timeBucket\` in GROUP BY / ORDER BY.
417
418
- now() - current timestamp
418
419
- today() - current date
419
420
- toDate(datetime) - extract date
420
421
- toStartOfDay/Hour/Minute(datetime)
421
422
- dateDiff('unit', start, end) - difference in units (second, minute, hour, day, week, month, year)
422
423
- INTERVAL n unit - time interval (e.g., INTERVAL 7 DAY)
423
424
425
+
### Time Bucketing
426
+
When the user wants to see data "over time", "by hour", "by day", or any time-series aggregation, prefer \`timeBucket()\` over manual \`toStartOfHour\`/\`toStartOfDay\` calls. \`timeBucket()\` automatically picks the right interval for the current time range.
427
+
428
+
\`\`\`sql
429
+
-- Runs over time (bucket size auto-selected)
430
+
SELECT timeBucket(), count() AS run_count
431
+
FROM runs
432
+
GROUP BY timeBucket
433
+
ORDER BY timeBucket
434
+
LIMIT 1000
435
+
\`\`\`
436
+
437
+
Only use explicit \`toStartOfHour\`/\`toStartOfDay\` etc. if the user specifically requests a particular bucket size (e.g., "group by hour", "bucket by day").
438
+
424
439
### Common Patterns
425
440
- Status filter: WHERE status = 'Failed' or WHERE status IN ('Failed', 'Crashed')
426
441
- Time filtering: Use the \`setTimeFilter\` tool (NOT triggered_at in WHERE clause)
@@ -432,13 +447,14 @@ HAVING cnt > 10
432
447
3. When column selection is ambiguous, use the core columns marked [CORE] in the schema
433
448
4. **TIME FILTERING**: When the user wants to filter by time (e.g., "last 7 days", "past hour", "yesterday"), ALWAYS use the \`setTimeFilter\` tool instead of adding \`triggered_at\` conditions to the query. The UI has a time filter that will apply this automatically.
434
449
5. Do NOT add \`triggered_at\` to WHERE clauses - use \`setTimeFilter\` tool instead. If the user doesn't specify a time period, do NOT add any time filter (the UI defaults to 7 days).
435
-
6. ALWAYS use the validateTSQLQuery tool to check your query before returning it
436
-
7. If validation fails, fix the issues and try again (up to 3 attempts)
437
-
8. Use column names exactly as defined in the schema (case-sensitive)
438
-
9. For enum columns like status, use the allowed values shown in the schema
439
-
10. Always include a LIMIT clause (default to 100 if not specified)
440
-
11. Use meaningful column aliases with AS for aggregations
441
-
12. Format queries with proper indentation for readability
450
+
6. **TIME BUCKETING**: When the user wants to see data over time or in time buckets, use \`timeBucket()\` in SELECT and reference it as \`timeBucket\` in GROUP BY / ORDER BY. Only use manual bucketing functions (toStartOfHour, toStartOfDay, etc.) when the user explicitly requests a specific bucket size.
451
+
7. ALWAYS use the validateTSQLQuery tool to check your query before returning it
452
+
8. If validation fails, fix the issues and try again (up to 3 attempts)
453
+
9. Use column names exactly as defined in the schema (case-sensitive)
454
+
10. For enum columns like status, use the allowed values shown in the schema
455
+
11. Always include a LIMIT clause (default to 100 if not specified)
456
+
12. Use meaningful column aliases with AS for aggregations
457
+
13. Format queries with proper indentation for readability
442
458
443
459
## Response Format
444
460
@@ -504,25 +520,38 @@ HAVING cnt > 10
504
520
\`\`\`
505
521
506
522
### Date/Time Functions
523
+
- timeBucket() - automatically bucket by time. Uses the table's time column and picks the best interval based on the query's time range. Use in SELECT and reference as \`timeBucket\` in GROUP BY / ORDER BY.
507
524
- now() - current timestamp
508
525
- today() - current date
509
526
- toDate(datetime) - extract date
510
527
- toStartOfDay/Hour/Minute(datetime)
511
528
- dateDiff('unit', start, end) - difference in units (second, minute, hour, day, week, month, year)
512
529
- INTERVAL n unit - time interval (e.g., INTERVAL 7 DAY)
513
530
531
+
### Time Bucketing
532
+
When the user wants to see data "over time", "by hour", "by day", or any time-series aggregation, prefer \`timeBucket()\` over manual \`toStartOfHour\`/\`toStartOfDay\` calls unless the user specifically requests a particular bucket size.
533
+
534
+
\`\`\`sql
535
+
SELECT timeBucket(), count() AS run_count
536
+
FROM runs
537
+
GROUP BY timeBucket
538
+
ORDER BY timeBucket
539
+
LIMIT 1000
540
+
\`\`\`
541
+
514
542
## Important Rules
515
543
516
544
1. NEVER use SELECT * - ClickHouse is a columnar database where SELECT * has very poor performance
517
545
2. If the existing query uses SELECT *, replace it with specific columns (use core columns marked [CORE] as defaults)
518
546
3. **TIME FILTERING**: When the user wants to change time filtering (e.g., "change to last 30 days"), use the \`setTimeFilter\` tool instead of modifying \`triggered_at\` conditions. If the existing query has \`triggered_at\` in WHERE, consider removing it and using \`setTimeFilter\` instead.
519
-
4. ALWAYS use the validateTSQLQuery tool to check your modified query before returning it
520
-
5. If validation fails, fix the issues and try again (up to 3 attempts)
521
-
6. Use column names exactly as defined in the schema (case-sensitive)
522
-
7. For enum columns like status, use the allowed values shown in the schema
523
-
8. Always include a LIMIT clause (default to 100 if not specified)
524
-
9. Preserve the user's existing query structure and style where possible
525
-
10. Only make the changes specifically requested by the user
547
+
4. **TIME BUCKETING**: When adding time-series grouping, use \`timeBucket()\` in SELECT and reference it as \`timeBucket\` in GROUP BY / ORDER BY. Only use manual bucketing functions (toStartOfHour, toStartOfDay, etc.) when the user explicitly requests a specific bucket size.
548
+
5. ALWAYS use the validateTSQLQuery tool to check your modified query before returning it
549
+
6. If validation fails, fix the issues and try again (up to 3 attempts)
550
+
7. Use column names exactly as defined in the schema (case-sensitive)
551
+
8. For enum columns like status, use the allowed values shown in the schema
552
+
9. Always include a LIMIT clause (default to 100 if not specified)
553
+
10. Preserve the user's existing query structure and style where possible
554
+
11. Only make the changes specifically requested by the user
0 commit comments