Skip to content

Commit a31aea5

Browse files
committed
feat: add ClickHouse LIMIT AFTER/UNTIL as ChartConfig clauses for Table and Search.
1 parent 442d6df commit a31aea5

21 files changed

Lines changed: 561 additions & 23 deletions

File tree

‎packages/api/src/mcp/tools/dashboards/schemas.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,22 @@ const mcpTableTileSchema = mcpTileLayoutSchema.extend({
841841
'When ordering by an alias that contains spaces or special characters, ' +
842842
`wrap the alias in quotes: e.g. '"P95 Latency" DESC'.`,
843843
),
844+
limitAfter: z
845+
.string()
846+
.max(10000)
847+
.optional()
848+
.describe(
849+
'ClickHouse LIMIT AFTER start expression. Mutually exclusive with OFFSET. ' +
850+
'Example: "service_id >= 3". Requires ORDER BY.',
851+
),
852+
limitUntil: z
853+
.string()
854+
.max(10000)
855+
.optional()
856+
.describe(
857+
'ClickHouse LIMIT UNTIL end expression. Used with LIMIT AFTER. ' +
858+
'Example: "service_id >= 8".',
859+
),
844860
asRatio: z.boolean().optional().describe(asRatioDescription),
845861
groupByColumnsOnLeft: z
846862
.boolean()

‎packages/api/src/mcp/tools/savedSearches/schemas.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ export const mcpSaveSavedSearchSchema = z.object({
7878
.string()
7979
.optional()
8080
.describe('Sort expression. Example: "Timestamp DESC"'),
81+
limitAfter: z
82+
.string()
83+
.optional()
84+
.describe(
85+
'ClickHouse LIMIT AFTER start expression. Mutually exclusive with OFFSET pagination.',
86+
),
87+
limitUntil: z
88+
.string()
89+
.optional()
90+
.describe('ClickHouse LIMIT UNTIL end expression. Used with LIMIT AFTER.'),
8191
sourceId: objectIdSchema.describe(
8292
'Source ID — call clickstack_list_sources to find available sources.',
8393
),

‎packages/api/src/models/savedSearch.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ type ExternalSavedSearch = {
1111
where?: string;
1212
whereLanguage?: string;
1313
orderBy?: string;
14+
limitAfter?: string;
15+
limitUntil?: string;
1416
sourceId?: string;
1517
tags?: string[];
1618
filters?: unknown[];
@@ -52,6 +54,8 @@ const savedSearchSchema = new Schema<
5254
where: String,
5355
whereLanguage: String,
5456
orderBy: String,
57+
limitAfter: String,
58+
limitUntil: String,
5559
source: {
5660
type: mongoose.Schema.Types.ObjectId,
5761
required: true,
@@ -88,6 +92,8 @@ savedSearchSchema.methods.toExternalJSON = function (): ExternalSavedSearch {
8892
where: this.where,
8993
whereLanguage: this.whereLanguage,
9094
orderBy: this.orderBy,
95+
limitAfter: this.limitAfter,
96+
limitUntil: this.limitUntil,
9197
sourceId: this.source ? String(this.source) : undefined,
9298
tags: this.tags,
9399
filters: this.filters,

‎packages/api/src/routers/external-api/v2/savedSearches.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ const savedSearchRequestSchema = z.object({
9898
.default(''),
9999
whereLanguage: z.enum(['lucene', 'sql']).optional().default('lucene'),
100100
orderBy: z.string().max(1024).optional().default(''),
101+
limitAfter: z.string().max(4096).optional().default(''),
102+
limitUntil: z.string().max(4096).optional().default(''),
101103
tags: tagsSchema.default([]),
102104
filters: z
103105
.array(savedSearchFilterSchema)

‎packages/api/src/routers/external-api/v2/utils/dashboards.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,8 @@ export const convertToExternalTileChartConfig = (
587587
? config.select.map(convertToExternalSelectItem)
588588
: [DEFAULT_SELECT_ITEM],
589589
orderBy: stringValueOrDefault(config.orderBy, undefined),
590+
limitAfter: config.limit?.after || undefined,
591+
limitUntil: config.limit?.until || undefined,
590592
...externalFormulaFields,
591593
...externalShowOperandSeriesField,
592594
};
@@ -597,6 +599,8 @@ export const convertToExternalTileChartConfig = (
597599
select: stringValueOrDefault(config.select, ''),
598600
where: config.where,
599601
whereLanguage: config.whereLanguage ?? 'lucene',
602+
limitAfter: config.limit?.after || undefined,
603+
limitUntil: config.limit?.until || undefined,
600604
};
601605
case DisplayType.Markdown:
602606
return {
@@ -917,6 +921,23 @@ const convertExternalPromqlConfigToInternal = (
917921
}
918922
};
919923

924+
function limitAfterUntilFields(externalConfig: {
925+
limitAfter?: string;
926+
limitUntil?: string;
927+
}): { limit?: { after?: string; until?: string } } {
928+
const after = externalConfig.limitAfter?.trim();
929+
const until = externalConfig.limitUntil?.trim();
930+
if (!after && !until) {
931+
return {};
932+
}
933+
return {
934+
limit: {
935+
...(after ? { after } : {}),
936+
...(until ? { until } : {}),
937+
},
938+
};
939+
}
940+
920941
export function convertToInternalTileConfig(
921942
externalTile: ConfigTile,
922943
): DashboardDocument['tiles'][number] {
@@ -1049,6 +1070,7 @@ export function convertToInternalTileConfig(
10491070
source: externalConfig.sourceId,
10501071
where: '',
10511072
seriesReturnType: externalConfig.asRatio ? 'ratio' : undefined,
1073+
...limitAfterUntilFields(externalConfig),
10521074
name,
10531075
description,
10541076
} satisfies BuilderSavedChartConfig;
@@ -1190,6 +1212,7 @@ export function convertToInternalTileConfig(
11901212
name,
11911213
description,
11921214
whereLanguage: externalConfig.whereLanguage ?? 'lucene',
1215+
...limitAfterUntilFields(externalConfig),
11931216
} satisfies BuilderSavedChartConfig;
11941217
break;
11951218
case 'event_patterns':

‎packages/api/src/utils/zod.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@ const externalDashboardTableChartConfigSchema = z.object({
422422
groupBy: z.string().max(10000).optional(),
423423
having: z.string().max(10000).optional(),
424424
orderBy: z.string().max(10000).optional(),
425+
limitAfter: z.string().max(10000).optional(),
426+
limitUntil: z.string().max(10000).optional(),
425427
asRatio: z.boolean().optional(),
426428
numberFormat: NumberFormatSchema.optional(),
427429
groupByColumnsOnLeft: z.boolean().optional(),
@@ -571,6 +573,8 @@ const externalDashboardSearchChartConfigSchema = z.object({
571573
select: z.string().max(10000),
572574
where: z.string().max(10000).optional().default(''),
573575
whereLanguage: whereLanguageSchema,
576+
limitAfter: z.string().max(10000).optional(),
577+
limitUntil: z.string().max(10000).optional(),
574578
});
575579

576580
// Extended schema for the /api/v2/search endpoint — adds orderBy which is not

0 commit comments

Comments
 (0)