Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/team-query-language-settings.md
Original file line number Diff line number Diff line change
@@ -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
117 changes: 110 additions & 7 deletions packages/api/src/controllers/team.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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}`;
Expand All @@ -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;
},
Expand Down Expand Up @@ -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<string, any> = {};
const $unset: Record<string, any> = {};
function buildSetUnsetUpdate<T extends Record<string, unknown>>(settings: T) {
const $set: Record<string, unknown> = {};
const $unset: Record<string, unknown> = {};

for (const [key, value] of Object.entries(settings)) {
if (value === null) {
Expand All @@ -116,13 +119,113 @@ export function updateTeamClickhouseSettings(
}
}

const update: Record<string, any> = {};
const update: mongoose.UpdateQuery<ITeam> = {};
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) {
Comment thread
kunalbhatia2601 marked this conversation as resolved.
Resolved
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) {
Comment thread
kunalbhatia2601 marked this conversation as resolved.
Resolved
throw new Api400Error('At least one query language must be enabled');
}

Comment thread
kunalbhatia2601 marked this conversation as resolved.
Resolved
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',
);
}

Comment thread
kunalbhatia2601 marked this conversation as resolved.
Resolved
const update = buildSetUnsetUpdate(settings);

const queryFilter: Record<string, unknown> = { _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<mongoose.Model<unknown>, 'aggregate'>[] {
Expand Down
12 changes: 11 additions & 1 deletion packages/api/src/models/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,24 @@ export default mongoose.model<ITeam>(
type: Boolean,
default: false,
},
// TODO: maybe add these to a top level Mixed type
// CH Client Settings
metadataMaxRowsToRead: Number,
searchRowLimit: Number,
queryTimeout: Number,
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,
Expand Down
68 changes: 68 additions & 0 deletions packages/api/src/routers/api/__tests__/team.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Test uses avoidable casts The local-mode test casts the imported configuration to change it here and casts it again to restore it. The repository’s TypeScript guide says to avoid as casts when type inference or a typed alternative is possible. This repository requirement must be satisfied before merging.

Context Used: AGENTS.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Fix in Claude Code Fix in Conductor Fix in Cursor Fix in Codex

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);

Expand Down
34 changes: 34 additions & 0 deletions packages/api/src/routers/api/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -23,6 +25,7 @@ import {
rotateTeamApiKey,
setTeamName,
updateTeamClickhouseSettings,
updateTeamQueryLanguageSettings,
} from '@/controllers/team';
import {
deleteTeamMember,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -136,6 +141,35 @@ router.patch(
},
);

router.patch(
'/query-language-settings',
processRequest({
body: TeamQueryLanguageSettingsUpdateSchema,
}),
async (
req,
res: express.Response<UpdateQueryLanguageSettingsApiResponse>,
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({
Expand Down
20 changes: 20 additions & 0 deletions packages/app/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import type {
TeamClickHouseSettingsUpdate,
TeamInvitationsApiResponse,
TeamMembersApiResponse,
TeamQueryLanguageSettingsUpdate,
TeamTagsApiResponse,
UpdateClickHouseSettingsApiResponse,
UpdateQueryLanguageSettingsApiResponse,
WebhookCreateApiResponse,
WebhooksApiResponse,
WebhookTestApiResponse,
Expand Down Expand Up @@ -599,6 +601,24 @@ const api = {
}).json<UpdateClickHouseSettingsApiResponse>(),
});
},
useUpdateQueryLanguageSettings() {
const queryClient = useQueryClient();
return useMutation<
UpdateQueryLanguageSettingsApiResponse,
HTTPError,
TeamQueryLanguageSettingsUpdate
>({
mutationFn: async settings =>
hdxServer(`team/query-language-settings`, {
method: 'PATCH',
json: settings,
}).json<UpdateQueryLanguageSettingsApiResponse>(),
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. */
Expand Down
Loading
Loading