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

Dev/moutyque/fix #7

Merged
merged 3 commits into from
Jan 21, 2025
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 15 additions & 12 deletions src/components/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { ChangeEvent, useState } from 'react';
import { InlineField, Input, Stack, Select, AsyncMultiSelect, useTheme2, CollapsableSection } from '@grafana/ui';
import { QueryEditorProps, SelectableValue, AppEvents } from '@grafana/data';
import { DataSource, queryTypes, queryUnits } from '../datasource';
import { Configuration, DEFAULT_LIMIT, MyDataSourceOptions, MyQuery } from '../types';
import { Configuration, DEFAULT_LIMIT, DEFAULT_QUERY, MyDataSourceOptions, MyQuery } from '../types';

import { getAppEvents } from '@grafana/runtime';
import CodeMirror, { EditorView, placeholder } from '@uiw/react-codemirror';
Expand All @@ -17,12 +17,13 @@ const appEvents = getAppEvents();
type Props = QueryEditorProps<DataSource, MyQuery, MyDataSourceOptions>;

export function QueryEditor({ query, onChange, datasource }: Props) {
const { limit, type, unit, dimensions } = query;
const { limit, type, unit, dimensions, expression } = query;
const [uiDimensions, setUIDimensions] = useState<Array<SelectableValue<string>>>(
dimensions?.map((v) => ({ label: v, value: v })) ?? [{ label: 'SrcAS', value: 'SrcAS' }]
);

const [expression, setExpression] = useState<string>('InIfBoundary = external');
const [uiExpression, setUIExpression] = useState<string>(expression || DEFAULT_QUERY.expression !!);


const getFilterTheme = (isDark: boolean) => [
syntaxHighlighting(
Expand Down Expand Up @@ -59,10 +60,10 @@ export function QueryEditor({ query, onChange, datasource }: Props) {
.sort((a, b) => a.label.localeCompare(b.label)) ?? []
);
} catch (error) {
appEvents.publish({
type: AppEvents.alertError.name,
payload: ['Failed to fetch dimensions:'+error],
});
appEvents.publish({
type: AppEvents.alertError.name,
payload: ['Failed to fetch dimensions:' + error],
});
return [];
}
};
Expand Down Expand Up @@ -155,9 +156,11 @@ export function QueryEditor({ query, onChange, datasource }: Props) {
width={10}
/>
</InlineField>
</Stack>
<Stack>
<InlineField label="Filters" tooltip="Filters for the query" grow={true} labelWidth={16}>
<CodeMirror
value={expression || ''}
value={uiExpression}
theme={getTheme(theme.isDark)}
extensions={[
filterLanguage(),
Expand All @@ -170,7 +173,7 @@ export function QueryEditor({ query, onChange, datasource }: Props) {
EditorView.lineWrapping,
EditorView.updateListener.of((viewUpdate) => {
if (viewUpdate.docChanged) {
setExpression(viewUpdate.state.doc.toString());
setUIExpression(viewUpdate.state.doc.toString());
onChange({ ...query, expression: viewUpdate.state.doc.toString() });
}
if (viewUpdate.focusChanged) {
Expand All @@ -194,9 +197,9 @@ export function QueryEditor({ query, onChange, datasource }: Props) {
</Stack>
<Stack>
<CollapsableSection label="Options" isOpen={false}>
<InlineField label="Legend" labelWidth={16} tooltip="Series name override or template. Ex {{hostname}} will be replaced with label values for hostname.">
<Select value={type} options={queryTypeOptions()} onChange={onTypeChange} width={20} />
</InlineField>
<InlineField label="Legend" labelWidth={16} tooltip="Series name override or template. Ex {{hostname}} will be replaced with label values for hostname.">
<Select value={type} options={queryTypeOptions()} onChange={onTypeChange} width={20} />
</InlineField>
</CollapsableSection>
</Stack>
</Stack >
Expand Down
4 changes: 2 additions & 2 deletions src/datasource.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FetchResponse, getBackendSrv, isFetchError } from '@grafana/runtime';
import { FetchResponse, getBackendSrv, getTemplateSrv, isFetchError } from '@grafana/runtime';
import {
CoreApp,
DataQueryRequest,
Expand Down Expand Up @@ -46,7 +46,7 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
bidirectional: false,
dimensions: target.dimensions,
end: to,
filter: target.expression,
filter: getTemplateSrv().replace(target.expression, options.scopedVars),
limit: target.limit,
points: 200,
'previous-period': false,
Expand Down
Loading