Skip to content

Commit 9abf598

Browse files
committed
stop hardcoding the triggered_at column
1 parent 91df7cb commit 9abf598

File tree

2 files changed

+182
-9
lines changed

2 files changed

+182
-9
lines changed

apps/webapp/app/services/queryService.server.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,14 @@ export async function executeQuery<TOut extends z.ZodSchema>(
152152
return { success: false, error: new QueryError(errorMessage, { query: options.query }) };
153153
}
154154

155-
// Build time filter fallback for triggered_at column
155+
// Detect which table the query targets to determine the time column
156+
// Each table schema declares its primary time column via timeConstraint
157+
const matchedSchema = querySchemas.find((s) =>
158+
new RegExp(`\\bFROM\\s+${s.name}\\b`, "i").test(options.query)
159+
);
160+
const timeColumn = matchedSchema?.timeConstraint ?? "triggered_at";
161+
162+
// Build time filter fallback for the table's time column
156163
const defaultPeriod = await getDefaultPeriod(organizationId);
157164
const timeFilter = timeFilters({
158165
period: period ?? undefined,
@@ -173,15 +180,15 @@ export async function executeQuery<TOut extends z.ZodSchema>(
173180
}
174181

175182
// Build the fallback WHERE condition based on what the user specified
176-
let triggeredAtFallback: WhereClauseCondition;
183+
let timeFallback: WhereClauseCondition;
177184
if (timeFilter.from && timeFilter.to) {
178-
triggeredAtFallback = { op: "between", low: timeFilter.from, high: timeFilter.to };
185+
timeFallback = { op: "between", low: timeFilter.from, high: timeFilter.to };
179186
} else if (timeFilter.from) {
180-
triggeredAtFallback = { op: "gte", value: timeFilter.from };
187+
timeFallback = { op: "gte", value: timeFilter.from };
181188
} else if (timeFilter.to) {
182-
triggeredAtFallback = { op: "lte", value: timeFilter.to };
189+
timeFallback = { op: "lte", value: timeFilter.to };
183190
} else {
184-
triggeredAtFallback = { op: "gte", value: requestedFromDate! };
191+
timeFallback = { op: "gte", value: requestedFromDate! };
185192
}
186193

187194
const maxQueryPeriod = await getLimit(organizationId, "queryPeriodDays", 30);
@@ -196,7 +203,7 @@ export async function executeQuery<TOut extends z.ZodSchema>(
196203
project_id:
197204
scope === "project" || scope === "environment" ? { op: "eq", value: projectId } : undefined,
198205
environment_id: scope === "environment" ? { op: "eq", value: environmentId } : undefined,
199-
triggered_at: { op: "gte", value: maxQueryPeriodDate },
206+
[timeColumn]: { op: "gte", value: maxQueryPeriodDate },
200207
// Optional filters for tasks and queues
201208
task_identifier:
202209
taskIdentifiers && taskIdentifiers.length > 0
@@ -238,7 +245,7 @@ export async function executeQuery<TOut extends z.ZodSchema>(
238245
enforcedWhereClause,
239246
fieldMappings,
240247
whereClauseFallback: {
241-
triggered_at: triggeredAtFallback,
248+
[timeColumn]: timeFallback,
242249
},
243250
timeRange,
244251
clickhouseSettings: {

apps/webapp/app/v3/querySchemas.ts

Lines changed: 167 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,176 @@ export const runsSchema: TableSchema = {
434434
},
435435
};
436436

437+
/**
438+
* Schema definition for the metrics table (trigger_dev.metrics_v1)
439+
*/
440+
export const metricsSchema: TableSchema = {
441+
name: "metrics",
442+
clickhouseName: "trigger_dev.metrics_v1",
443+
description: "Host and runtime metrics collected during task execution",
444+
timeConstraint: "bucket_start",
445+
tenantColumns: {
446+
organizationId: "organization_id",
447+
projectId: "project_id",
448+
environmentId: "environment_id",
449+
},
450+
columns: {
451+
environment: {
452+
name: "environment",
453+
clickhouseName: "environment_id",
454+
...column("String", { description: "The environment slug", example: "prod" }),
455+
fieldMapping: "environment",
456+
customRenderType: "environment",
457+
},
458+
project: {
459+
name: "project",
460+
clickhouseName: "project_id",
461+
...column("String", {
462+
description: "The project reference, they always start with `proj_`.",
463+
example: "proj_howcnaxbfxdmwmxazktx",
464+
}),
465+
fieldMapping: "project",
466+
customRenderType: "project",
467+
},
468+
metric_name: {
469+
name: "metric_name",
470+
...column("LowCardinality(String)", {
471+
description: "The name of the metric (e.g. process.cpu.utilization, system.memory.usage)",
472+
example: "process.cpu.utilization",
473+
coreColumn: true,
474+
}),
475+
},
476+
metric_type: {
477+
name: "metric_type",
478+
...column("LowCardinality(String)", {
479+
description: "The type of metric",
480+
allowedValues: ["gauge", "sum", "histogram"],
481+
example: "gauge",
482+
}),
483+
},
484+
machine_id: {
485+
name: "machine_id",
486+
clickhouseName: "metric_subject",
487+
...column("String", {
488+
description: "The machine ID that produced this metric",
489+
example: "machine-abc123",
490+
}),
491+
},
492+
bucket_start: {
493+
name: "bucket_start",
494+
...column("DateTime", {
495+
description: "The start of the 10-second aggregation bucket",
496+
example: "2024-01-15 09:30:00",
497+
coreColumn: true,
498+
}),
499+
},
500+
count: {
501+
name: "count",
502+
...column("UInt64", {
503+
description: "Number of data points in this bucket",
504+
example: "6",
505+
}),
506+
},
507+
sum_value: {
508+
name: "sum_value",
509+
...column("Float64", {
510+
description: "Sum of values in this bucket",
511+
example: "0.45",
512+
}),
513+
},
514+
max_value: {
515+
name: "max_value",
516+
...column("Float64", {
517+
description: "Maximum value in this bucket",
518+
example: "0.85",
519+
coreColumn: true,
520+
}),
521+
},
522+
min_value: {
523+
name: "min_value",
524+
...column("Float64", {
525+
description: "Minimum value in this bucket",
526+
example: "0.12",
527+
}),
528+
},
529+
last_value: {
530+
name: "last_value",
531+
...column("Float64", {
532+
description: "Last recorded value in this bucket",
533+
example: "0.42",
534+
coreColumn: true,
535+
}),
536+
},
537+
538+
// Trigger context columns (from attributes.trigger.* JSON subpaths)
539+
run_id: {
540+
name: "run_id",
541+
...column("String", {
542+
description: "The run ID associated with this metric",
543+
customRenderType: "runId",
544+
example: "run_cm1a2b3c4d5e6f7g8h9i",
545+
}),
546+
expression: "attributes.trigger.run_id",
547+
},
548+
task_identifier: {
549+
name: "task_identifier",
550+
...column("String", {
551+
description: "Task identifier/slug",
552+
example: "my-background-task",
553+
coreColumn: true,
554+
}),
555+
expression: "attributes.trigger.task_slug",
556+
},
557+
attempt_number: {
558+
name: "attempt_number",
559+
...column("String", {
560+
description: "The attempt number for this metric",
561+
example: "1",
562+
}),
563+
expression: "attributes.trigger.attempt_number",
564+
},
565+
machine_name: {
566+
name: "machine_name",
567+
...column("String", {
568+
description: "The machine preset used for execution",
569+
allowedValues: [...MACHINE_PRESETS],
570+
example: "small-1x",
571+
}),
572+
expression: "attributes.trigger.machine_name",
573+
},
574+
environment_type: {
575+
name: "environment_type",
576+
...column("String", {
577+
description: "Environment type",
578+
allowedValues: [...ENVIRONMENT_TYPES],
579+
customRenderType: "environmentType",
580+
example: "PRODUCTION",
581+
}),
582+
expression: "attributes.trigger.environment_type",
583+
},
584+
worker_id: {
585+
name: "worker_id",
586+
...column("String", {
587+
description: "The worker ID that produced this metric",
588+
example: "worker-abc123",
589+
}),
590+
expression: "attributes.trigger.worker_id",
591+
},
592+
worker_version: {
593+
name: "worker_version",
594+
...column("String", {
595+
description: "The worker version that produced this metric",
596+
example: "20240115.1",
597+
}),
598+
expression: "attributes.trigger.worker_version",
599+
},
600+
},
601+
};
602+
437603
/**
438604
* All available schemas for the query editor
439605
*/
440-
export const querySchemas: TableSchema[] = [runsSchema];
606+
export const querySchemas: TableSchema[] = [runsSchema, metricsSchema];
441607

442608
/**
443609
* Default query for the query editor

0 commit comments

Comments
 (0)