Skip to content

Commit b20e7e5

Browse files
committed
some legit devin fixes
1 parent 80ab373 commit b20e7e5

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.models.$modelId/route.tsx

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
9595
});
9696
};
9797

98+
/** Escape a value for safe interpolation into a TSQL single-quoted string. */
99+
function escapeTSQL(value: string): string {
100+
return value.replace(/'/g, "''");
101+
}
102+
98103
function bignumberConfig(column: string, opts?: { aggregation?: "sum" | "avg" | "first"; suffix?: string; abbreviate?: boolean }): QueryWidgetConfig {
99104
return { type: "bignumber", column, aggregation: opts?.aggregation ?? "sum", abbreviate: opts?.abbreviate ?? false, suffix: opts?.suffix };
100105
}
@@ -425,7 +430,7 @@ function GlobalMetricsTab({
425430
<MetricWidget
426431
widgetKey={`${modelName}-calls`}
427432
title="Total Calls"
428-
query={`SELECT sum(call_count) AS total_calls FROM llm_models WHERE response_model = '${modelName}'`}
433+
query={`SELECT sum(call_count) AS total_calls FROM llm_models WHERE response_model = '${escapeTSQL(modelName)}'`}
429434
config={bignumberConfig("total_calls", { abbreviate: true })}
430435
{...widgetProps}
431436
/>
@@ -434,7 +439,7 @@ function GlobalMetricsTab({
434439
<MetricWidget
435440
widgetKey={`${modelName}-ttfc-p50`}
436441
title="p50 TTFC"
437-
query={`SELECT round(quantilesMerge(0.5)(ttfc_quantiles)[1], 0) AS ttfc_p50 FROM llm_models WHERE response_model = '${modelName}'`}
442+
query={`SELECT round(quantilesMerge(0.5)(ttfc_quantiles)[1], 0) AS ttfc_p50 FROM llm_models WHERE response_model = '${escapeTSQL(modelName)}'`}
438443
config={bignumberConfig("ttfc_p50", { aggregation: "avg", suffix: "ms" })}
439444
{...widgetProps}
440445
/>
@@ -443,7 +448,7 @@ function GlobalMetricsTab({
443448
<MetricWidget
444449
widgetKey={`${modelName}-ttfc-p90`}
445450
title="p90 TTFC"
446-
query={`SELECT round(quantilesMerge(0.9)(ttfc_quantiles)[1], 0) AS ttfc_p90 FROM llm_models WHERE response_model = '${modelName}'`}
451+
query={`SELECT round(quantilesMerge(0.9)(ttfc_quantiles)[1], 0) AS ttfc_p90 FROM llm_models WHERE response_model = '${escapeTSQL(modelName)}'`}
447452
config={bignumberConfig("ttfc_p90", { aggregation: "avg", suffix: "ms" })}
448453
{...widgetProps}
449454
/>
@@ -452,7 +457,7 @@ function GlobalMetricsTab({
452457
<MetricWidget
453458
widgetKey={`${modelName}-tps`}
454459
title="Tokens/sec (p50)"
455-
query={`SELECT round(quantilesMerge(0.5)(tps_quantiles)[1], 0) AS tps_p50 FROM llm_models WHERE response_model = '${modelName}'`}
460+
query={`SELECT round(quantilesMerge(0.5)(tps_quantiles)[1], 0) AS tps_p50 FROM llm_models WHERE response_model = '${escapeTSQL(modelName)}'`}
456461
config={bignumberConfig("tps_p50", { aggregation: "avg" })}
457462
{...widgetProps}
458463
/>
@@ -465,7 +470,7 @@ function GlobalMetricsTab({
465470
<MetricWidget
466471
widgetKey={`${modelName}-calls-time`}
467472
title="Calls over time"
468-
query={`SELECT timeBucket(), sum(call_count) AS calls FROM llm_models WHERE response_model = '${modelName}' GROUP BY timeBucket ORDER BY timeBucket`}
473+
query={`SELECT timeBucket(), sum(call_count) AS calls FROM llm_models WHERE response_model = '${escapeTSQL(modelName)}' GROUP BY timeBucket ORDER BY timeBucket`}
469474
config={chartConfig({ chartType: "bar", xAxisColumn: "timebucket", yAxisColumns: ["calls"] })}
470475
{...widgetProps}
471476
/>
@@ -474,7 +479,7 @@ function GlobalMetricsTab({
474479
<MetricWidget
475480
widgetKey={`${modelName}-ttfc-time`}
476481
title="TTFC over time"
477-
query={`SELECT timeBucket(), round(quantilesMerge(0.5)(ttfc_quantiles)[1], 0) AS ttfc_p50, round(quantilesMerge(0.9)(ttfc_quantiles)[1], 0) AS ttfc_p90 FROM llm_models WHERE response_model = '${modelName}' GROUP BY timeBucket ORDER BY timeBucket`}
482+
query={`SELECT timeBucket(), round(quantilesMerge(0.5)(ttfc_quantiles)[1], 0) AS ttfc_p50, round(quantilesMerge(0.9)(ttfc_quantiles)[1], 0) AS ttfc_p90 FROM llm_models WHERE response_model = '${escapeTSQL(modelName)}' GROUP BY timeBucket ORDER BY timeBucket`}
478483
config={chartConfig({ chartType: "line", xAxisColumn: "timebucket", yAxisColumns: ["ttfc_p50", "ttfc_p90"], aggregation: "avg" })}
479484
{...widgetProps}
480485
/>
@@ -519,7 +524,7 @@ function YourUsageTab({
519524
<MetricWidget
520525
widgetKey={`${modelName}-user-calls`}
521526
title="Your Calls"
522-
query={`SELECT count() AS total_calls FROM llm_metrics WHERE response_model = '${modelName}'`}
527+
query={`SELECT count() AS total_calls FROM llm_metrics WHERE response_model = '${escapeTSQL(modelName)}'`}
523528
config={bignumberConfig("total_calls", { abbreviate: true })}
524529
{...widgetProps}
525530
/>
@@ -528,7 +533,7 @@ function YourUsageTab({
528533
<MetricWidget
529534
widgetKey={`${modelName}-user-cost`}
530535
title="Your Cost"
531-
query={`SELECT sum(total_cost) AS total_cost FROM llm_metrics WHERE response_model = '${modelName}'`}
536+
query={`SELECT sum(total_cost) AS total_cost FROM llm_metrics WHERE response_model = '${escapeTSQL(modelName)}'`}
532537
config={bignumberConfig("total_cost", { aggregation: "sum" })}
533538
{...widgetProps}
534539
/>
@@ -537,7 +542,7 @@ function YourUsageTab({
537542
<MetricWidget
538543
widgetKey={`${modelName}-user-ttfc`}
539544
title="Avg TTFC"
540-
query={`SELECT round(avg(ms_to_first_chunk), 0) AS avg_ttfc FROM llm_metrics WHERE response_model = '${modelName}' AND ms_to_first_chunk > 0`}
545+
query={`SELECT round(avg(ms_to_first_chunk), 0) AS avg_ttfc FROM llm_metrics WHERE response_model = '${escapeTSQL(modelName)}' AND ms_to_first_chunk > 0`}
541546
config={bignumberConfig("avg_ttfc", { aggregation: "avg", suffix: "ms" })}
542547
{...widgetProps}
543548
/>
@@ -546,7 +551,7 @@ function YourUsageTab({
546551
<MetricWidget
547552
widgetKey={`${modelName}-user-tps`}
548553
title="Avg Tokens/sec"
549-
query={`SELECT round(avg(tokens_per_second), 0) AS avg_tps FROM llm_metrics WHERE response_model = '${modelName}' AND tokens_per_second > 0`}
554+
query={`SELECT round(avg(tokens_per_second), 0) AS avg_tps FROM llm_metrics WHERE response_model = '${escapeTSQL(modelName)}' AND tokens_per_second > 0`}
550555
config={bignumberConfig("avg_tps", { aggregation: "avg" })}
551556
{...widgetProps}
552557
/>
@@ -559,7 +564,7 @@ function YourUsageTab({
559564
<MetricWidget
560565
widgetKey={`${modelName}-user-cost-time`}
561566
title="Cost over time"
562-
query={`SELECT timeBucket(), sum(total_cost) AS cost FROM llm_metrics WHERE response_model = '${modelName}' GROUP BY timeBucket ORDER BY timeBucket`}
567+
query={`SELECT timeBucket(), sum(total_cost) AS cost FROM llm_metrics WHERE response_model = '${escapeTSQL(modelName)}' GROUP BY timeBucket ORDER BY timeBucket`}
563568
config={chartConfig({ chartType: "bar", xAxisColumn: "timebucket", yAxisColumns: ["cost"] })}
564569
{...widgetProps}
565570
/>
@@ -568,7 +573,7 @@ function YourUsageTab({
568573
<MetricWidget
569574
widgetKey={`${modelName}-user-tokens-time`}
570575
title="Tokens over time"
571-
query={`SELECT timeBucket(), sum(input_tokens) AS input_tokens, sum(output_tokens) AS output_tokens FROM llm_metrics WHERE response_model = '${modelName}' GROUP BY timeBucket ORDER BY timeBucket`}
576+
query={`SELECT timeBucket(), sum(input_tokens) AS input_tokens, sum(output_tokens) AS output_tokens FROM llm_metrics WHERE response_model = '${escapeTSQL(modelName)}' GROUP BY timeBucket ORDER BY timeBucket`}
572577
config={chartConfig({ chartType: "bar", xAxisColumn: "timebucket", yAxisColumns: ["input_tokens", "output_tokens"] })}
573578
{...widgetProps}
574579
/>
@@ -580,7 +585,7 @@ function YourUsageTab({
580585
<MetricWidget
581586
widgetKey={`${modelName}-user-tasks`}
582587
title="Cost by task"
583-
query={`SELECT task_identifier, count() AS calls, sum(total_cost) AS cost FROM llm_metrics WHERE response_model = '${modelName}' GROUP BY task_identifier ORDER BY cost DESC LIMIT 20`}
588+
query={`SELECT task_identifier, count() AS calls, sum(total_cost) AS cost FROM llm_metrics WHERE response_model = '${escapeTSQL(modelName)}' GROUP BY task_identifier ORDER BY cost DESC LIMIT 20`}
584589
config={{ type: "table", prettyFormatting: true, sorting: [] }}
585590
{...widgetProps}
586591
/>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ export async function executeQuery<TOut extends z.ZodSchema>(
214214

215215
// Force tenant isolation and time period limits
216216
// Global tables (no tenantColumns) skip tenant isolation — they contain anonymized cross-tenant data
217-
const isGlobalTable = !matchedSchema?.tenantColumns;
217+
const isGlobalTable = matchedSchema != null && !matchedSchema.tenantColumns;
218218
const enforcedWhereClause = {
219219
...(isGlobalTable
220220
? {}

internal-packages/clickhouse/schema/027_create_llm_model_aggregates_v1.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ AS SELECT
4444
sum(input_tokens) AS total_input_tokens,
4545
sum(output_tokens) AS total_output_tokens,
4646
sum(total_cost) AS total_cost,
47-
quantilesState(0.5, 0.9, 0.95, 0.99)(ms_to_first_chunk) AS ttfc_quantiles,
48-
quantilesState(0.5, 0.9, 0.95, 0.99)(tokens_per_second) AS tps_quantiles,
47+
quantilesStateIf(0.5, 0.9, 0.95, 0.99)(ms_to_first_chunk, ms_to_first_chunk > 0) AS ttfc_quantiles,
48+
quantilesStateIf(0.5, 0.9, 0.95, 0.99)(tokens_per_second, tokens_per_second > 0) AS tps_quantiles,
4949
quantilesState(0.5, 0.9, 0.95, 0.99)(duration) AS duration_quantiles,
5050
sumMap(map(finish_reason, toUInt64(1))) AS finish_reason_counts
5151
FROM trigger_dev.llm_metrics_v1
5252
WHERE response_model != ''
5353
GROUP BY response_model, base_response_model, gen_ai_system, minute;
5454

5555
-- +goose Down
56-
DROP VIEW IF EXISTS trigger_dev.llm_model_aggregates_mv_v1;
56+
DROP TABLE IF EXISTS trigger_dev.llm_model_aggregates_mv_v1;
5757
DROP TABLE IF EXISTS trigger_dev.llm_model_aggregates_v1;

0 commit comments

Comments
 (0)