Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SEARCH-8309 Allow savedSearchId to be composed using variable(s) #27

Merged
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
11 changes: 9 additions & 2 deletions src/components/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export function QueryEditor({ datasource, query, onChange, onRunQuery }: Props)
const onSavedQueryIdChange = useCallback((sv: SelectableValue<string>) => {
const newSavedSearchId = sv.value?.replace(/\s+/g, '') ?? ''; // auto-trim/remove any whitespace
setSavedSearchId(newSavedSearchId);
if (newSavedSearchId !== savedSearchId && newSavedSearchId.match(/^[a-zA-Z0-9_]+$/)) {
// May contain ${...} style variables from the dashboard, but otherwise may contain only valid ID characters.
// May contain any combination of valid ID chars and dashboard variable(s).
if (newSavedSearchId !== savedSearchId && newSavedSearchId.match(/^([a-zA-Z0-9_]+|\$\{.+?\})+$/)) {
onChange({ ...query, type: 'saved', savedSearchId: newSavedSearchId });
debouncedOnRunQuery();
}
Expand All @@ -74,7 +76,12 @@ export function QueryEditor({ datasource, query, onChange, onRunQuery }: Props)
if (queryType === 'saved') {
return (
<InlineField label="Saved Search" labelWidth={16} tooltip="ID of the Cribl saved search">
<Select onChange={onSavedQueryIdChange} options={savedSearchIdOptions} value={savedSearchId} width={24} />
<Select
allowCustomValue
onChange={onSavedQueryIdChange}
options={savedSearchIdOptions}
value={savedSearchIdOptions.find((o) => o.value === savedSearchId) || { label: savedSearchId, value: savedSearchId }}
width={24} />
</InlineField>
);
} else {
Expand Down
21 changes: 15 additions & 6 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ export class CriblDataSource extends DataSourceWithBackend<CriblQuery, CriblData
}

applyTemplateVariables(criblQuery: CriblQuery, scopedVars: ScopedVars) {
if (criblQuery.type !== 'adhoc') {
return criblQuery;
switch (criblQuery.type) {
case 'adhoc':
return {
...criblQuery,
// You can use dashboard variables in your query
query: getTemplateSrv().replace(criblQuery.query, scopedVars),
};
case 'saved':
return {
...criblQuery,
// The savedSearchId can also be composed using dashboard variable(s)
savedSearchId: getTemplateSrv().replace(criblQuery.savedSearchId, scopedVars),
};
default: // exhaustive check
throw new Error(`Unexpected query type`);
}
return {
...criblQuery,
query: getTemplateSrv().replace(criblQuery.query, scopedVars),
};
}

filterQuery(query: CriblQuery): boolean {
Expand Down
Loading