diff --git a/.changeset/team-query-language-settings.md b/.changeset/team-query-language-settings.md new file mode 100644 index 0000000000..e87a8d77b9 --- /dev/null +++ b/.changeset/team-query-language-settings.md @@ -0,0 +1,7 @@ +--- +'@hyperdx/app': minor +'@hyperdx/api': minor +'@hyperdx/common-utils': minor +--- + +feat: Add team setting to change default query language and enable/disable query languages diff --git a/packages/api/src/controllers/team.ts b/packages/api/src/controllers/team.ts index 6fd5bba1d5..101c79ed40 100644 --- a/packages/api/src/controllers/team.ts +++ b/packages/api/src/controllers/team.ts @@ -1,6 +1,9 @@ import { + DEFAULT_QUERY_LANGUAGES, + QueryLanguage, TagResourceType, TeamClickHouseSettingsUpdate, + TeamQueryLanguageSettingsUpdate, } from '@hyperdx/common-utils/dist/types'; import mongoose from 'mongoose'; import { v4 as uuidv4 } from 'uuid'; @@ -11,6 +14,7 @@ import Alert from '@/models/alert'; import Dashboard from '@/models/dashboard'; import { SavedSearch } from '@/models/savedSearch'; import Team, { type ITeam, type TeamDocument } from '@/models/team'; +import { Api400Error } from '@/utils/errors'; export function getTeamInviteUrl(token: string) { return `${config.FRONTEND_URL}/join-team?token=${token}`; @@ -26,6 +30,8 @@ export const LOCAL_APP_TEAM = { apiKey: uuidv4(), collectorAuthenticationEnforced: false, isMetricsSeriesTableEnabled: false, + defaultQueryLanguage: 'lucene' as QueryLanguage, + allowedQueryLanguages: [...DEFAULT_QUERY_LANGUAGES] as QueryLanguage[], toJSON() { return this; }, @@ -101,12 +107,9 @@ export function setTeamName(teamId: ObjectId, name: string) { return Team.findByIdAndUpdate(teamId, { name }, { new: true }); } -export function updateTeamClickhouseSettings( - teamId: ObjectId, - settings: TeamClickHouseSettingsUpdate, -) { - const $set: Record = {}; - const $unset: Record = {}; +function buildSetUnsetUpdate>(settings: T) { + const $set: Record = {}; + const $unset: Record = {}; for (const [key, value] of Object.entries(settings)) { if (value === null) { @@ -116,13 +119,113 @@ export function updateTeamClickhouseSettings( } } - const update: Record = {}; + const update: mongoose.UpdateQuery = {}; if (Object.keys($set).length > 0) update.$set = $set; if (Object.keys($unset).length > 0) update.$unset = $unset; + return update; +} + +export function updateTeamClickhouseSettings( + teamId: ObjectId, + settings: TeamClickHouseSettingsUpdate, +) { + const update = buildSetUnsetUpdate(settings); return Team.findByIdAndUpdate(teamId, update, { new: true }); } +export async function updateTeamQueryLanguageSettings( + teamId: ObjectId, + settings: TeamQueryLanguageSettingsUpdate, +) { + if (config.IS_LOCAL_APP_MODE) { + const effectiveAllowed = + settings.allowedQueryLanguages ?? + LOCAL_APP_TEAM.allowedQueryLanguages ?? + DEFAULT_QUERY_LANGUAGES; + + if (effectiveAllowed.length === 0) { + throw new Api400Error('At least one query language must be enabled'); + } + + const effectiveDefault = + settings.defaultQueryLanguage ?? + LOCAL_APP_TEAM.defaultQueryLanguage ?? + 'lucene'; + + if (!effectiveAllowed.includes(effectiveDefault)) { + throw new Api400Error( + 'Default query language must be one of the allowed query languages', + ); + } + + if (settings.defaultQueryLanguage !== undefined) { + LOCAL_APP_TEAM.defaultQueryLanguage = settings.defaultQueryLanguage; + } + if (settings.allowedQueryLanguages !== undefined) { + LOCAL_APP_TEAM.allowedQueryLanguages = settings.allowedQueryLanguages; + } + return LOCAL_APP_TEAM; + } + + const currentTeam = await Team.findById(teamId); + if (!currentTeam) { + throw new Error(`Team ${teamId} not found`); + } + + const effectiveAllowed = + settings.allowedQueryLanguages ?? + currentTeam.allowedQueryLanguages ?? + DEFAULT_QUERY_LANGUAGES; + + if (effectiveAllowed.length === 0) { + throw new Api400Error('At least one query language must be enabled'); + } + + const effectiveDefault = + settings.defaultQueryLanguage ?? + currentTeam.defaultQueryLanguage ?? + 'lucene'; + + if (!effectiveAllowed.includes(effectiveDefault)) { + throw new Api400Error( + 'Default query language must be one of the allowed query languages', + ); + } + + const update = buildSetUnsetUpdate(settings); + + const queryFilter: Record = { _id: teamId }; + + if (settings.allowedQueryLanguages && !settings.defaultQueryLanguage) { + const includesDefaultLucene = + settings.allowedQueryLanguages.includes('lucene'); + queryFilter.$or = [ + { defaultQueryLanguage: { $in: settings.allowedQueryLanguages } }, + ...(includesDefaultLucene + ? [{ defaultQueryLanguage: { $exists: false } }] + : []), + ]; + } else if (settings.defaultQueryLanguage && !settings.allowedQueryLanguages) { + queryFilter.$or = [ + { allowedQueryLanguages: settings.defaultQueryLanguage }, + { allowedQueryLanguages: { $exists: false } }, + ]; + } + + const updatedTeam = await Team.findOneAndUpdate(queryFilter, update, { + new: true, + }); + + if (!updatedTeam) { + throw new Api400Error( + 'Default query language must be one of the allowed query languages', + ); + } + + return updatedTeam; +} + function getCollectionsWithTags( resourceType?: TagResourceType, ): Pick, 'aggregate'>[] { diff --git a/packages/api/src/models/team.ts b/packages/api/src/models/team.ts index 1954e0ac7a..5dddbc8ed9 100644 --- a/packages/api/src/models/team.ts +++ b/packages/api/src/models/team.ts @@ -38,7 +38,6 @@ export default mongoose.model( type: Boolean, default: false, }, - // TODO: maybe add these to a top level Mixed type // CH Client Settings metadataMaxRowsToRead: Number, searchRowLimit: Number, @@ -46,6 +45,17 @@ export default mongoose.model( fieldMetadataDisabled: Boolean, parallelizeWhenPossible: Boolean, filterKeysFetchLimit: Number, + // Query Language Settings + defaultQueryLanguage: { + type: String, + enum: ['lucene', 'sql'], + default: 'lucene', + }, + allowedQueryLanguages: { + type: [String], + enum: ['lucene', 'sql'], + default: ['lucene', 'sql'], + }, }, { timestamps: true, diff --git a/packages/api/src/routers/api/__tests__/team.int.test.ts b/packages/api/src/routers/api/__tests__/team.int.test.ts index 93344f53ac..a8c5451a8b 100644 --- a/packages/api/src/routers/api/__tests__/team.int.test.ts +++ b/packages/api/src/routers/api/__tests__/team.int.test.ts @@ -2,6 +2,11 @@ import _ from 'lodash'; import { ObjectId } from 'mongodb'; import mongoose from 'mongoose'; +import * as config from '@/config'; +import { + LOCAL_APP_TEAM, + updateTeamQueryLanguageSettings, +} from '@/controllers/team'; import { getLoggedInAgent, getServer } from '@/fixtures'; import Alert, { AlertSource, AlertThresholdType } from '@/models/alert'; import Team from '@/models/team'; @@ -34,12 +39,75 @@ describe('team router', () => { .toMatchInlineSnapshot(` { "allowedAuthMethods": [], + "allowedQueryLanguages": [ + "lucene", + "sql", + ], + "defaultQueryLanguage": "lucene", "isMetricsSeriesTableEnabled": false, "name": "fake@deploysentinel.com's Team", } `); }); + it('PATCH /team/query-language-settings - update query language settings', async () => { + const { agent } = await getLoggedInAgent(server); + + const patchResp = await agent + .patch('/team/query-language-settings') + .send({ + allowedQueryLanguages: ['sql'], + defaultQueryLanguage: 'sql', + }) + .expect(200); + + expect(patchResp.body).toEqual({ + allowedQueryLanguages: ['sql'], + defaultQueryLanguage: 'sql', + }); + + const getResp = await agent.get('/team').expect(200); + expect(getResp.body.allowedQueryLanguages).toEqual(['sql']); + expect(getResp.body.defaultQueryLanguage).toEqual('sql'); + }); + + it('PATCH /team/query-language-settings - rejects invalid configuration', async () => { + const { agent } = await getLoggedInAgent(server); + + // Rejects empty allowed languages + await agent + .patch('/team/query-language-settings') + .send({ + allowedQueryLanguages: [], + }) + .expect(400); + + // Rejects default language not in allowed languages + await agent + .patch('/team/query-language-settings') + .send({ + allowedQueryLanguages: ['sql'], + defaultQueryLanguage: 'lucene', + }) + .expect(400); + }); + + it('updateTeamQueryLanguageSettings works in local app mode', async () => { + const origLocalMode = config.IS_LOCAL_APP_MODE; + try { + (config as { IS_LOCAL_APP_MODE: boolean }).IS_LOCAL_APP_MODE = true; + const res = await updateTeamQueryLanguageSettings(LOCAL_APP_TEAM._id, { + allowedQueryLanguages: ['sql'], + defaultQueryLanguage: 'sql', + }); + expect(res.allowedQueryLanguages).toEqual(['sql']); + expect(res.defaultQueryLanguage).toEqual('sql'); + } finally { + (config as { IS_LOCAL_APP_MODE: boolean }).IS_LOCAL_APP_MODE = + origLocalMode; + } + }); + it('GET /team reflects isMetricsSeriesTableEnabled when set', async () => { const { agent, team } = await getLoggedInAgent(server); diff --git a/packages/api/src/routers/api/team.ts b/packages/api/src/routers/api/team.ts index 40fc199486..e9aa9708f8 100644 --- a/packages/api/src/routers/api/team.ts +++ b/packages/api/src/routers/api/team.ts @@ -5,10 +5,12 @@ import type { TeamMembersApiResponse, TeamTagsApiResponse, UpdateClickHouseSettingsApiResponse, + UpdateQueryLanguageSettingsApiResponse, } from '@hyperdx/common-utils/dist/types'; import { TagResourceTypeSchema, TeamClickHouseSettingsUpdateSchema, + TeamQueryLanguageSettingsUpdateSchema, } from '@hyperdx/common-utils/dist/types'; import crypto from 'crypto'; import express from 'express'; @@ -23,6 +25,7 @@ import { rotateTeamApiKey, setTeamName, updateTeamClickhouseSettings, + updateTeamQueryLanguageSettings, } from '@/controllers/team'; import { deleteTeamMember, @@ -56,6 +59,8 @@ router.get('/', async (req, res: TeamApiExpRes, next) => { 'name', 'createdAt', 'isMetricsSeriesTableEnabled', + 'defaultQueryLanguage', + 'allowedQueryLanguages', ] as const; const team = await getTeam(teamId, fields); if (team == null) { @@ -136,6 +141,35 @@ router.patch( }, ); +router.patch( + '/query-language-settings', + processRequest({ + body: TeamQueryLanguageSettingsUpdateSchema, + }), + async ( + req, + res: express.Response, + next, + ) => { + try { + const teamId = req.user?.team; + if (teamId == null) { + throw new Error(`User ${req.user?._id} not associated with a team`); + } + + if (Object.keys(req.body).length === 0) { + return res.json({}); + } + + const team = await updateTeamQueryLanguageSettings(teamId, req.body); + + res.json(pick(team, Object.keys(req.body))); + } catch (e) { + next(e); + } + }, +); + router.post( '/invitation', validateRequest({ diff --git a/packages/app/src/api.ts b/packages/app/src/api.ts index 08a83e0a32..2da9003612 100644 --- a/packages/app/src/api.ts +++ b/packages/app/src/api.ts @@ -23,8 +23,10 @@ import type { TeamClickHouseSettingsUpdate, TeamInvitationsApiResponse, TeamMembersApiResponse, + TeamQueryLanguageSettingsUpdate, TeamTagsApiResponse, UpdateClickHouseSettingsApiResponse, + UpdateQueryLanguageSettingsApiResponse, WebhookCreateApiResponse, WebhooksApiResponse, WebhookTestApiResponse, @@ -599,6 +601,24 @@ const api = { }).json(), }); }, + useUpdateQueryLanguageSettings() { + const queryClient = useQueryClient(); + return useMutation< + UpdateQueryLanguageSettingsApiResponse, + HTTPError, + TeamQueryLanguageSettingsUpdate + >({ + mutationFn: async settings => + hdxServer(`team/query-language-settings`, { + method: 'PATCH', + json: settings, + }).json(), + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ['team'] }); + queryClient.invalidateQueries({ queryKey: ['me'] }); + }, + }); + }, getTagsQueryKey: (resourceType?: TagResourceType) => [...TAGS_QUERY_KEY_PREFIX, resourceType ?? null] as const, /** Tags that have been added to resources, optionally narrowed to one resource type. */ diff --git a/packages/app/src/components/SearchInput/InputLanguageSwitch.tsx b/packages/app/src/components/SearchInput/InputLanguageSwitch.tsx index 9a63c12083..f9afbe772b 100644 --- a/packages/app/src/components/SearchInput/InputLanguageSwitch.tsx +++ b/packages/app/src/components/SearchInput/InputLanguageSwitch.tsx @@ -1,20 +1,28 @@ +import { + DEFAULT_QUERY_LANGUAGES, + formatQueryLanguageLabel, + type QueryLanguage, +} from '@hyperdx/common-utils/dist/types'; import { Select } from '@mantine/core'; import { IconChevronDown } from '@tabler/icons-react'; -type Language = 'sql' | 'lucene'; - -const DATA: { value: Language; label: string }[] = [ - { value: 'sql', label: 'SQL' }, - { value: 'lucene', label: 'Lucene' }, -]; +const DATA: { value: QueryLanguage; label: string }[] = + DEFAULT_QUERY_LANGUAGES.map(lang => ({ + value: lang, + label: formatQueryLanguageLabel(lang), + })); export default function InputLanguageSwitch({ language, onLanguageChange, + allowedLanguages = DEFAULT_QUERY_LANGUAGES, }: { - language: Language; - onLanguageChange: (language: Language) => void; + language: QueryLanguage; + onLanguageChange: (language: QueryLanguage) => void; + allowedLanguages?: QueryLanguage[]; }) { + const options = DATA.filter(d => allowedLanguages.includes(d.value)); + return ( { + if (val === 'sql' || val === 'lucene') { + setSelectedDefault(val); + } + }} + data={selectedAllowed.map((l: QueryLanguage) => ({ + value: l, + label: formatQueryLanguageLabel(l), + }))} + /> + + + + + + + + ) : ( + + + + + Enabled:{' '} + + {allowedLanguages.map(formatQueryLanguageLabel).join(', ')} + + + + Default:{' '} + + {formatQueryLanguageLabel(defaultLanguage)} + + + + + )} + + ); +} diff --git a/packages/app/src/components/TeamSettings/TeamQueryConfigSection.tsx b/packages/app/src/components/TeamSettings/TeamQueryConfigSection.tsx index e685112003..b45465691e 100644 --- a/packages/app/src/components/TeamSettings/TeamQueryConfigSection.tsx +++ b/packages/app/src/components/TeamSettings/TeamQueryConfigSection.tsx @@ -26,6 +26,8 @@ import { } from '@/defaults'; import { useBrandDisplayName } from '@/theme/ThemeProvider'; +import QueryLanguageSettingsForm from './QueryLanguageSettingsForm'; + type ClickhouseSettingType = 'number' | 'boolean'; interface ClickhouseSettingFormProps { @@ -281,7 +283,13 @@ export default function TeamQueryConfigSection() { return ( - ClickHouse Client Settings + Query language settings + + + + + + ClickHouse client settings diff --git a/packages/app/src/components/TeamSettings/__tests__/QueryLanguageSettingsForm.test.tsx b/packages/app/src/components/TeamSettings/__tests__/QueryLanguageSettingsForm.test.tsx new file mode 100644 index 0000000000..3c2b38d1f6 --- /dev/null +++ b/packages/app/src/components/TeamSettings/__tests__/QueryLanguageSettingsForm.test.tsx @@ -0,0 +1,95 @@ +import { MantineProvider } from '@mantine/core'; +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; + +import QueryLanguageSettingsForm from '@/components/TeamSettings/QueryLanguageSettingsForm'; + +const mockMutate = jest.fn(); + +jest.mock('@/api', () => ({ + __esModule: true, + default: { + useMe: () => ({ + data: { + team: { + allowedQueryLanguages: ['lucene', 'sql'], + defaultQueryLanguage: 'lucene', + }, + }, + }), + useUpdateQueryLanguageSettings: () => ({ + mutate: mockMutate, + isPending: false, + }), + }, +})); + +function renderForm() { + const queryClient = new QueryClient({ + defaultOptions: { queries: { retry: false } }, + }); + return render( + + + + + , + ); +} + +describe('QueryLanguageSettingsForm', () => { + beforeEach(() => { + mockMutate.mockReset(); + }); + + it('renders current configuration and enters edit mode', async () => { + const user = userEvent.setup(); + renderForm(); + + expect( + screen.getByText(/Query language configuration/i), + ).toBeInTheDocument(); + expect(screen.getByText(/Lucene, SQL/i)).toBeInTheDocument(); + + await user.click(screen.getByRole('button', { name: /Change/i })); + + expect(screen.getByRole('checkbox', { name: 'Lucene' })).toBeChecked(); + expect(screen.getByRole('checkbox', { name: 'SQL' })).toBeChecked(); + }); + + it('prevents unchecking all languages', async () => { + const user = userEvent.setup(); + renderForm(); + + await user.click(screen.getByRole('button', { name: /Change/i })); + + const luceneCheckbox = screen.getByRole('checkbox', { name: 'Lucene' }); + const sqlCheckbox = screen.getByRole('checkbox', { name: 'SQL' }); + + await user.click(luceneCheckbox); + expect(luceneCheckbox).not.toBeChecked(); + + await user.click(sqlCheckbox); + // SQL checkbox should stay checked because at least one language is required + expect(sqlCheckbox).toBeChecked(); + }); + + it('submits updated query language settings', async () => { + const user = userEvent.setup(); + renderForm(); + + await user.click(screen.getByRole('button', { name: /Change/i })); + + await user.click(screen.getByRole('checkbox', { name: 'Lucene' })); + await user.click(screen.getByRole('button', { name: /Save/i })); + + expect(mockMutate).toHaveBeenCalledWith( + { + allowedQueryLanguages: ['sql'], + defaultQueryLanguage: 'sql', + }, + expect.any(Object), + ); + }); +}); diff --git a/packages/cli/src/api/client.ts b/packages/cli/src/api/client.ts index 504401e670..f9bbee880c 100644 --- a/packages/cli/src/api/client.ts +++ b/packages/cli/src/api/client.ts @@ -415,9 +415,13 @@ export class ProxyClickhouseClient extends BaseClickhouseClient { // Response types (matching the internal API shapes) // ------------------------------------------------------------------ +import type { QueryLanguage } from '@hyperdx/common-utils/dist/types'; + export interface MeTeam { id: string; name: string; + defaultQueryLanguage?: QueryLanguage; + allowedQueryLanguages?: QueryLanguage[]; } interface MeResponse { diff --git a/packages/common-utils/src/types.ts b/packages/common-utils/src/types.ts index 69c26e4699..8f510dd71d 100644 --- a/packages/common-utils/src/types.ts +++ b/packages/common-utils/src/types.ts @@ -155,10 +155,18 @@ const SearchConditionRequiredLanguageSchema = z.enum([ 'lucene', 'promql', ]); +export const QueryLanguageSchema = + SearchConditionRequiredLanguageSchema.exclude(['promql']); +export type QueryLanguage = z.infer; +export const DEFAULT_QUERY_LANGUAGES: QueryLanguage[] = + QueryLanguageSchema.options; +export function formatQueryLanguageLabel(lang: QueryLanguage): string { + return lang === 'sql' ? 'SQL' : 'Lucene'; +} export const SearchConditionLanguageSchema = SearchConditionRequiredLanguageSchema.optional(); export const SearchConditionTrimmedLanguageSchema = - SearchConditionRequiredLanguageSchema.exclude(['promql']).optional(); + QueryLanguageSchema.optional(); export const AggregateFunctionSchema = z.enum([ 'avg', 'count', @@ -2338,6 +2346,27 @@ export type TeamClickHouseSettings = z.infer< typeof TeamClickHouseSettingsSchema >; +export const TeamQueryLanguageSettingsSchema = z.object({ + defaultQueryLanguage: QueryLanguageSchema.optional(), + allowedQueryLanguages: z.array(QueryLanguageSchema).min(1).optional(), +}); + +export const TeamQueryLanguageSettingsUpdateSchema = + TeamQueryLanguageSettingsSchema; + +export type TeamQueryLanguageSettings = z.infer< + typeof TeamQueryLanguageSettingsSchema +>; +export type TeamQueryLanguageSettingsUpdate = z.infer< + typeof TeamQueryLanguageSettingsUpdateSchema +>; + +export const UpdateQueryLanguageSettingsApiResponseSchema = + TeamQueryLanguageSettingsSchema.partial(); +export type UpdateQueryLanguageSettingsApiResponse = z.infer< + typeof UpdateQueryLanguageSettingsApiResponseSchema +>; + export const TeamSchema = z .object({ id: z.string(), @@ -2348,7 +2377,8 @@ export const TeamSchema = z collectorAuthenticationEnforced: z.boolean(), isMetricsSeriesTableEnabled: z.boolean(), }) - .merge(TeamClickHouseSettingsSchema); + .merge(TeamClickHouseSettingsSchema) + .merge(TeamQueryLanguageSettingsSchema); export type Team = z.infer; @@ -3078,7 +3108,9 @@ export const MeApiResponseSchema = z.object({ name: true, allowedAuthMethods: true, apiKey: true, - }).merge(TeamClickHouseSettingsSchema), + }) + .merge(TeamClickHouseSettingsSchema) + .merge(TeamQueryLanguageSettingsSchema), usageStatsEnabled: z.boolean(), aiAssistantEnabled: z.boolean(), });