Skip to content

Commit

Permalink
add tags list
Browse files Browse the repository at this point in the history
  • Loading branch information
lunaticusgreen committed Oct 28, 2024
1 parent de275d8 commit 85e9085
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/datasource/sql-query/sql_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class SqlQuery {
let intervalMs = SqlQueryHelper.convertInterval(i, this.target.intervalFactor || 1, true);
let adhocCondition: any[] = [];

adhocFilters = this.target.adHocFilters;
adhocFilters = this.target.adHocFilters
try {
let ast = scanner.toAST();
let topQueryAST = ast;
Expand Down
13 changes: 13 additions & 0 deletions src/spec/sql_query_specs.jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ describe('$rateColumns and subquery + $conditionalTest + SqlQuery.replace + adho
dateTimeColDataType: 'event_time',
round: '1m',
rawQuery: '',
adHocFilters: adhocFilters,
};
const options = {
rangeRaw: {
Expand Down Expand Up @@ -421,6 +422,7 @@ describe('$rateColumnsAggregated and subquery + $conditionalTest + SqlQuery.repl
dateTimeColDataType: 'event_time',
round: '1m',
rawQuery: '',
adHocFilters: adhocFilters,
};
const options = {
rangeRaw: {
Expand Down Expand Up @@ -552,6 +554,7 @@ describe('$perSecondColumnsAggregated and subquery + $conditionalTest + SqlQuery
dateTimeColDataType: 'event_time',
round: '1m',
rawQuery: '',
adHocFilters: adhocFilters,
};
const options = {
rangeRaw: {
Expand Down Expand Up @@ -683,6 +686,7 @@ describe('$increaseColumnsAggregated and subquery + $conditionalTest + SqlQuery.
dateTimeColDataType: 'event_time',
round: '1m',
rawQuery: '',
adHocFilters: adhocFilters,
};
const options = {
rangeRaw: {
Expand Down Expand Up @@ -814,6 +818,7 @@ describe('$deltaColumnsAggregated and subquery + $conditionalTest + SqlQuery.rep
dateTimeColDataType: 'event_time',
round: '1m',
rawQuery: '',
adHocFilters: adhocFilters,
};
const options = {
rangeRaw: {
Expand Down Expand Up @@ -893,6 +898,13 @@ describe('check replace with $adhoc macros', () => {
dateTimeColDataType: 'TimeFlowStart',
round: '1m',
rawQuery: '',
adHocFilters: [
{
key: 'default.flows_raw.SrcAS',
operator: '=',
value: '1299',
},
]
};
const options = {
rangeRaw: {
Expand Down Expand Up @@ -955,6 +967,7 @@ describe('check replace with $columns and concat and ARRAY JOIN', () => {
dateTimeColDataType: 'dateTimeColumn',
round: '1m',
rawQuery: '',
adHocFilters: adhocFilters,
};
const options = {
rangeRaw: {
Expand Down
7 changes: 7 additions & 0 deletions src/views/QueryEditor/QueryEditor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.adhoc-filters-tags {
margin: 5px 0;
}

.adhoc-filters-tags > div {
display: none;
}
3 changes: 3 additions & 0 deletions src/views/QueryEditor/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Alert } from '@grafana/ui';
import { useQueryState } from './hooks/useQueryState';
import { useFormattedData } from './hooks/useFormattedData';
import { initializeQueryDefaults } from './helpers/initializeQueryDefaults';
import './QueryEditor.css';

export function QueryEditor(props: QueryEditorProps<CHDataSource, CHQuery, CHDataSourceOptions>) {
const {
Expand All @@ -30,6 +31,7 @@ export function QueryEditor(props: QueryEditorProps<CHDataSource, CHQuery, CHDat

// @ts-ignore
const adHocFilters = datasource.templateSrv.getAdhocFilters(datasource.name);
const areAdHocFiltersAvailable = !!adHocFilters.length

if (adHocFilters?.length) {
// eslint-disable-next-line
Expand Down Expand Up @@ -65,6 +67,7 @@ export function QueryEditor(props: QueryEditorProps<CHDataSource, CHQuery, CHDat
{editorMode === EditorMode.SQL && (
<QueryTextEditor
adhocFilters={initializedQuery.adHocFilters}
areAdHocFiltersAvailable={areAdHocFiltersAvailable}
query={initializedQuery}
height={200}
onSqlChange={onSqlChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
InlineSwitch,
Input,
Select,
TagList,
TagsInput,
ToolbarButton,
} from '@grafana/ui';
import QueryMacrosInfo from './QueryMacrosInfo';
Expand Down Expand Up @@ -41,6 +41,7 @@ export const QueryTextEditor = ({
datasource,
isAnnotationView,
adhocFilters,
areAdHocFiltersAvailable,
}: any) => {
const [sqlFormattedData, setSqlFormattedData] = useState(formattedData);

Expand Down Expand Up @@ -82,16 +83,24 @@ export const QueryTextEditor = ({
onEditorMount={onEditorMount}
onRunQuery={onRunQuery}
/>
<div style={{ margin: '5px', display: 'flex' }}>
<p>Used Filters:</p>
<TagList
tags={adhocFilters.map((filter: any, index: number) => `${filter.key}${filter.operator}${filter.value}`)}
icon={'minus'}
onClick={() => {
console.log('remove');
}}
/>
</div>
{!areAdHocFiltersAvailable && <TagsInput
className={'adhoc-filters-tags'}
tags={adhocFilters.map((filter: any, index: number) => `${filter.key} ${filter.operator} ${filter.value}`)}
onChange={(tagsList) => {
onFieldChange({
fieldName: 'adHocFilters',
value: tagsList.map((item: string) => {
const [
key,
operator,
value
] = item.split(' ');

return { key, operator, value };
}),
});
}}
/>}
<div className="gf-form" style={{ display: 'flex', flexDirection: 'column', marginTop: '10px' }}>
<InlineFieldRow>
<InlineField
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export const initiateEditor = (
autocompletionData: any,
systemDatabasesData: any
) => {
const { Method, Variable, Constant, Keyword, TypeParameter, Text } = monacoInstance.languages.CompletionItemKind;
const {
Method, Variable, Constant, Keyword, TypeParameter, Text
} = monacoInstance.languages.CompletionItemKind;

let dynamicIdentifier: string[];
let dynamicKeyword: string[];
Expand Down Expand Up @@ -140,6 +142,7 @@ export const initiateEditor = (
// @ts-ignore
const createCompletionItem = (
label: string,
// @ts-ignore
kind: monacoInstance.languages.CompletionItemKind,
insertText: string,
range: any,
Expand Down

0 comments on commit 85e9085

Please sign in to comment.