Skip to content

Commit daab2ca

Browse files
authored
feat: support saved query/filter values in external api (#1814)
In #1584 we added saved default query/filter values support to dashboards. This PR extends that support to the external API. Fixes HDX-3519
1 parent f5828d1 commit daab2ca

9 files changed

Lines changed: 453 additions & 20 deletions

File tree

‎.changeset/angry-wasps-boil.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hyperdx/api": patch
3+
---
4+
5+
support saved query/filter values in external api

‎packages/api/openapi.json‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,26 @@
508508
],
509509
"description": "Query language for the where clause."
510510
},
511+
"SavedFilterValue": {
512+
"type": "object",
513+
"required": [
514+
"condition"
515+
],
516+
"properties": {
517+
"type": {
518+
"type": "string",
519+
"enum": [
520+
"sql"
521+
],
522+
"default": "sql"
523+
},
524+
"condition": {
525+
"type": "string",
526+
"description": "SQL filter condition. For example use expressions in the form \"column IN ('value')\".",
527+
"example": "ServiceName IN ('hdx-oss-dev-api')"
528+
}
529+
}
530+
},
511531
"MetricDataType": {
512532
"type": "string",
513533
"enum": [
@@ -1525,6 +1545,26 @@
15251545
"items": {
15261546
"$ref": "#/components/schemas/Filter"
15271547
}
1548+
},
1549+
"savedQuery": {
1550+
"type": "string",
1551+
"nullable": true,
1552+
"description": "Optional default dashboard query restored when loading the dashboard.",
1553+
"example": "service.name = 'api'"
1554+
},
1555+
"savedQueryLanguage": {
1556+
"$ref": "#/components/schemas/QueryLanguage",
1557+
"nullable": true,
1558+
"description": "Query language used by savedQuery.",
1559+
"default": "lucene",
1560+
"example": "sql"
1561+
},
1562+
"savedFilterValues": {
1563+
"type": "array",
1564+
"description": "Optional default dashboard filter values restored when loading the dashboard.",
1565+
"items": {
1566+
"$ref": "#/components/schemas/SavedFilterValue"
1567+
}
15281568
}
15291569
}
15301570
},
@@ -1563,6 +1603,26 @@
15631603
"items": {
15641604
"$ref": "#/components/schemas/FilterInput"
15651605
}
1606+
},
1607+
"savedQuery": {
1608+
"type": "string",
1609+
"nullable": true,
1610+
"description": "Optional default dashboard query to persist on the dashboard.",
1611+
"example": "service.name = 'api'"
1612+
},
1613+
"savedQueryLanguage": {
1614+
"$ref": "#/components/schemas/QueryLanguage",
1615+
"nullable": true,
1616+
"description": "Query language used by savedQuery.",
1617+
"default": "lucene",
1618+
"example": "sql"
1619+
},
1620+
"savedFilterValues": {
1621+
"type": "array",
1622+
"description": "Optional default dashboard filter values to persist on the dashboard.",
1623+
"items": {
1624+
"$ref": "#/components/schemas/SavedFilterValue"
1625+
}
15661626
}
15671627
}
15681628
},
@@ -1603,6 +1663,26 @@
16031663
"items": {
16041664
"$ref": "#/components/schemas/Filter"
16051665
}
1666+
},
1667+
"savedQuery": {
1668+
"type": "string",
1669+
"nullable": true,
1670+
"description": "Optional default dashboard query to persist on the dashboard.",
1671+
"example": "service.name = 'api'"
1672+
},
1673+
"savedQueryLanguage": {
1674+
"$ref": "#/components/schemas/QueryLanguage",
1675+
"nullable": true,
1676+
"description": "Query language used by savedQuery.",
1677+
"default": "lucene",
1678+
"example": "sql"
1679+
},
1680+
"savedFilterValues": {
1681+
"type": "array",
1682+
"description": "Optional default dashboard filter values to persist on the dashboard.",
1683+
"items": {
1684+
"$ref": "#/components/schemas/SavedFilterValue"
1685+
}
16061686
}
16071687
}
16081688
},

‎packages/api/src/routers/external-api/__tests__/dashboards.test.ts‎

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ describe('External API v2 Dashboards - old format', () => {
4040
name: 'Test External Dashboard',
4141
tiles: [makeExternalChart({ sourceId }), makeExternalChart({ sourceId })],
4242
tags: TEST_TAGS,
43+
savedQuery: null,
44+
savedQueryLanguage: null,
45+
savedFilterValues: [],
4346
...overrides,
4447
});
4548

@@ -57,6 +60,9 @@ describe('External API v2 Dashboards - old format', () => {
5760
],
5861
tags: TEST_TAGS,
5962
filters: [],
63+
savedQuery: null,
64+
savedQueryLanguage: null,
65+
savedFilterValues: [],
6066
...overrides,
6167
});
6268

@@ -280,6 +286,9 @@ describe('External API v2 Dashboards - old format', () => {
280286
],
281287
tags: ['format-test'],
282288
filters: [],
289+
savedQuery: null,
290+
savedQueryLanguage: null,
291+
savedFilterValues: [],
283292
},
284293
});
285294

@@ -362,6 +371,80 @@ describe('External API v2 Dashboards - old format', () => {
362371
expect(dashboards[0].tiles).toHaveLength(2);
363372
});
364373

374+
it('should create a dashboard with saved query defaults', async () => {
375+
const mockDashboard = createMockDashboard(traceSource._id.toString(), {
376+
savedQuery: "service.name = 'api'",
377+
savedQueryLanguage: 'sql',
378+
savedFilterValues: [
379+
{
380+
type: 'sql',
381+
condition: "ServiceName IN ('hdx-oss-dev-api')",
382+
},
383+
],
384+
});
385+
386+
const response = await authRequest('post', BASE_URL)
387+
.send(mockDashboard)
388+
.expect(200);
389+
390+
expect(response.body.data.savedQuery).toBe("service.name = 'api'");
391+
expect(response.body.data.savedQueryLanguage).toBe('sql');
392+
expect(response.body.data.savedFilterValues).toEqual([
393+
{
394+
type: 'sql',
395+
condition: "ServiceName IN ('hdx-oss-dev-api')",
396+
},
397+
]);
398+
399+
const dashboardInDb = await Dashboard.findById(
400+
response.body.data.id,
401+
).lean();
402+
expect(dashboardInDb?.savedQuery).toBe("service.name = 'api'");
403+
expect(dashboardInDb?.savedQueryLanguage).toBe('sql');
404+
expect(dashboardInDb?.savedFilterValues).toEqual([
405+
{
406+
type: 'sql',
407+
condition: "ServiceName IN ('hdx-oss-dev-api')",
408+
},
409+
]);
410+
});
411+
412+
it('should default savedQueryLanguage to lucene when savedQuery is provided without a language', async () => {
413+
const mockDashboard = omit(
414+
createMockDashboard(traceSource._id.toString(), {
415+
savedQuery: "service.name = 'api'",
416+
}),
417+
'savedQueryLanguage',
418+
);
419+
420+
const response = await authRequest('post', BASE_URL)
421+
.send(mockDashboard)
422+
.expect(200);
423+
424+
expect(response.body.data.savedQuery).toBe("service.name = 'api'");
425+
expect(response.body.data.savedQueryLanguage).toBe('lucene');
426+
427+
const dashboardInDb = await Dashboard.findById(
428+
response.body.data.id,
429+
).lean();
430+
expect(dashboardInDb?.savedQueryLanguage).toBe('lucene');
431+
});
432+
433+
it('should return 400 when savedQueryLanguage is null and savedQuery is provided', async () => {
434+
const mockDashboard = createMockDashboard(traceSource._id.toString(), {
435+
savedQuery: "service.name = 'api'",
436+
savedQueryLanguage: null,
437+
});
438+
439+
const response = await authRequest('post', BASE_URL)
440+
.send(mockDashboard)
441+
.expect(400);
442+
443+
expect(response.body.message).toContain(
444+
'savedQueryLanguage cannot be null when savedQuery is provided',
445+
);
446+
});
447+
365448
it('can create all chart types', async () => {
366449
const dashboardWithAllCharts = {
367450
name: 'Test Dashboard with All Chart Types',
@@ -881,6 +964,56 @@ describe('External API v2 Dashboards - old format', () => {
881964
expect(updatedDashboardInDb?.tiles).toHaveLength(2);
882965
});
883966

967+
it('should update and clear saved query defaults', async () => {
968+
const dashboard = await createTestDashboard({
969+
savedQuery: 'service:api',
970+
savedQueryLanguage: 'lucene',
971+
savedFilterValues: [
972+
{
973+
type: 'lucene',
974+
condition: 'env:prod',
975+
},
976+
],
977+
});
978+
const updatedDashboard = createMockDashboardWithIds(
979+
traceSource._id.toString(),
980+
);
981+
982+
const response = await authRequest('put', `${BASE_URL}/${dashboard._id}`)
983+
.send(updatedDashboard)
984+
.expect(200);
985+
986+
expect(response.body.data.savedQuery).toBeNull();
987+
expect(response.body.data.savedQueryLanguage).toBeNull();
988+
expect(response.body.data.savedFilterValues).toEqual([]);
989+
990+
const updatedDashboardInDb = await Dashboard.findById(
991+
dashboard._id,
992+
).lean();
993+
expect(updatedDashboardInDb?.savedQuery).toBeNull();
994+
expect(updatedDashboardInDb?.savedQueryLanguage).toBeNull();
995+
expect(updatedDashboardInDb?.savedFilterValues).toEqual([]);
996+
});
997+
998+
it('should return 400 when savedQueryLanguage is null and savedQuery is provided on update', async () => {
999+
const dashboard = await createTestDashboard();
1000+
const updatedDashboard = createMockDashboardWithIds(
1001+
traceSource._id.toString(),
1002+
{
1003+
savedQuery: "service.name = 'api'",
1004+
savedQueryLanguage: null,
1005+
},
1006+
);
1007+
1008+
const response = await authRequest('put', `${BASE_URL}/${dashboard._id}`)
1009+
.send(updatedDashboard)
1010+
.expect(400);
1011+
1012+
expect(response.body.message).toContain(
1013+
'savedQueryLanguage cannot be null when savedQuery is provided',
1014+
);
1015+
});
1016+
8841017
it('should update dashboard filters when provided', async () => {
8851018
const dashboard = await createTestDashboard();
8861019
const filterId1 = new ObjectId().toString();
@@ -1814,6 +1947,9 @@ describe('External API v2 Dashboards - new format', () => {
18141947
],
18151948
tags: ['format-test'],
18161949
filters: [],
1950+
savedQuery: null,
1951+
savedQueryLanguage: null,
1952+
savedFilterValues: [],
18171953
},
18181954
});
18191955

@@ -1924,6 +2060,21 @@ describe('External API v2 Dashboards - new format', () => {
19242060
expect(dashboards[0].tiles).toHaveLength(2);
19252061
});
19262062

2063+
it('should return 400 when savedQueryLanguage is null and savedQuery is provided', async () => {
2064+
const mockDashboard = createMockDashboard(traceSource._id.toString(), {
2065+
savedQuery: "service.name = 'api'",
2066+
savedQueryLanguage: null,
2067+
});
2068+
2069+
const response = await authRequest('post', BASE_URL)
2070+
.send(mockDashboard)
2071+
.expect(400);
2072+
2073+
expect(response.body.message).toContain(
2074+
'savedQueryLanguage cannot be null when savedQuery is provided',
2075+
);
2076+
});
2077+
19272078
it('can create all chart types', async () => {
19282079
const dashboardWithAllCharts = {
19292080
name: 'Test Dashboard with All Chart Types',
@@ -2306,6 +2457,25 @@ describe('External API v2 Dashboards - new format', () => {
23062457
expect(updatedDashboardInDb?.tiles).toHaveLength(2);
23072458
});
23082459

2460+
it('should return 400 when savedQueryLanguage is null and savedQuery is provided on update', async () => {
2461+
const dashboard = await createTestDashboard();
2462+
const updatedDashboard = createMockDashboardWithIds(
2463+
traceSource._id.toString(),
2464+
{
2465+
savedQuery: "service.name = 'api'",
2466+
savedQueryLanguage: null,
2467+
},
2468+
);
2469+
2470+
const response = await authRequest('put', `${BASE_URL}/${dashboard._id}`)
2471+
.send(updatedDashboard)
2472+
.expect(400);
2473+
2474+
expect(response.body.message).toContain(
2475+
'savedQueryLanguage cannot be null when savedQuery is provided',
2476+
);
2477+
});
2478+
23092479
it('should update dashboard filters when provided', async () => {
23102480
const dashboard = await createTestDashboard();
23112481
const filterId1 = new ObjectId().toString();

0 commit comments

Comments
 (0)