Skip to content

Commit 4ea57f3

Browse files
Merge pull request #45 from NeedleInAJayStack/fix/grafanaLt11_5
Fixes Grafana for v11.0.0 to v11.5.0
2 parents 7477c3e + 9af4af3 commit 4ea57f3

File tree

6 files changed

+41
-44
lines changed

6 files changed

+41
-44
lines changed

src/components/HaystackQueryInput.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function HaystackQueryInput({ query, onChange }: HaystackQueryInputProps)
2020
<AutoSizeInput
2121
minWidth={minWidth}
2222
prefix={<Icon name="angle-right" />}
23-
onChange={onQueryChange}
23+
onBlur={onQueryChange}
2424
value={query.eval}
2525
placeholder={DEFAULT_QUERY.eval}
2626
/>
@@ -32,7 +32,7 @@ export function HaystackQueryInput({ query, onChange }: HaystackQueryInputProps)
3232
<AutoSizeInput
3333
minWidth={minWidth}
3434
prefix={'@'}
35-
onChange={onQueryChange}
35+
onBlur={onQueryChange}
3636
value={query.hisRead}
3737
placeholder={DEFAULT_QUERY.hisRead}
3838
/>
@@ -44,7 +44,7 @@ export function HaystackQueryInput({ query, onChange }: HaystackQueryInputProps)
4444
<AutoSizeInput
4545
minWidth={minWidth}
4646
prefix={<Icon name="filter" />}
47-
onChange={onQueryChange}
47+
onBlur={onQueryChange}
4848
value={query.hisReadFilter}
4949
placeholder={DEFAULT_QUERY.hisReadFilter}
5050
/>
@@ -56,7 +56,7 @@ export function HaystackQueryInput({ query, onChange }: HaystackQueryInputProps)
5656
<AutoSizeInput
5757
minWidth={minWidth}
5858
prefix={<Icon name="filter" />}
59-
onChange={onQueryChange}
59+
onBlur={onQueryChange}
6060
value={query.read}
6161
placeholder={DEFAULT_QUERY.read}
6262
/>

src/components/HaystackQueryTypeSelector.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { DataSource, queryTypes } from '../datasource';
55

66
export interface HaystackQueryTypeSelectorProps {
77
datasource: DataSource | null;
8-
type: string;
8+
type?: string;
99
refId: string;
1010
onChange: (type: string) => void;
1111
}
@@ -16,7 +16,7 @@ export function HaystackQueryTypeSelector({ datasource, type, refId, onChange }:
1616
};
1717

1818
const queryTypeDefault = queryTypes[0];
19-
function queryTypeFromValue(value: string): QueryType | null {
19+
function queryTypeFromValue(value?: string): QueryType | null {
2020
return queryTypes.find((queryType) => queryType.value === value) ?? null;
2121
}
2222

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,28 @@
1-
import React, { useState } from 'react';
2-
import { HaystackVariableQuery } from '../types';
1+
import React from 'react';
2+
import { HaystackDataSourceOptions, HaystackQuery, HaystackVariableQuery } from '../types';
33
import { HaystackQueryTypeSelector } from './HaystackQueryTypeSelector';
44
import { HaystackQueryInput } from './HaystackQueryInput';
5-
import { InlineField, Input } from '@grafana/ui';
5+
import { QueryEditorProps } from '@grafana/data';
6+
import { InlineField, Input, Stack } from '@grafana/ui';
7+
import { DataSource } from 'datasource';
68

7-
interface VariableQueryProps {
8-
query: HaystackVariableQuery;
9-
onChange: (query: HaystackVariableQuery, definition: string) => void;
10-
}
9+
type Props = QueryEditorProps<DataSource, HaystackQuery, HaystackDataSourceOptions, HaystackVariableQuery>;
1110

12-
export const VARIABLE_REF_ID = "variable";
13-
14-
export const VariableQueryEditor: React.FC<VariableQueryProps> = ({ onChange, query: variableQuery }) => {
11+
export const VariableQueryEditor = ({ onChange, query }: Props) => {
1512
let variableInputWidth = 30;
16-
const [query, setState] = useState(variableQuery);
17-
18-
const saveQuery = () => {
19-
// refId must match but doesn't get set originally so set should set it on every change
20-
setState({ ...query, refId: VARIABLE_REF_ID});
2113

14+
// Computes the query string and calls the onChange function. Should be used instead of onChange for all mutating functions.
15+
const onChangeAndSave = (query: HaystackVariableQuery) => {
2216
let type = query.type;
2317
let queryCmd = "";
2418
if (query.type === "eval") {
25-
queryCmd = query.eval
19+
queryCmd = query.eval ?? "";
2620
} else if (query.type === "hisRead") {
27-
queryCmd = query.hisRead
21+
queryCmd = query.hisRead ?? "";
2822
} else if (query.type === "hisReadFilter") {
29-
queryCmd = query.hisReadFilter
23+
queryCmd = query.hisReadFilter ?? "";
3024
} else if (query.type === "read") {
31-
queryCmd = query.read
25+
queryCmd = query.read ?? "";
3226
}
3327
let column = "none";
3428
if (query.column !== undefined && query.column !== '') {
@@ -39,39 +33,42 @@ export const VariableQueryEditor: React.FC<VariableQueryProps> = ({ onChange, qu
3933
displayColumn = `'${query.displayColumn}'`;
4034
}
4135
let displayString = `${type}: '${queryCmd}', Column: ${column}, Display: ${displayColumn}`
42-
onChange(query, displayString);
36+
onChange({ ...query, query: displayString });
4337
};
4438

4539
const onTypeChange = (newType: string) => {
46-
setState({ ...query, type: newType});
40+
onChangeAndSave({ ...query, type: newType});
4741
};
4842

4943
const onQueryChange = (newQuery: string) => {
5044
if (query.type === "eval") {
51-
setState({ ...query, eval: newQuery });
45+
onChangeAndSave({ ...query, eval: newQuery });
5246
} else if (query.type === "hisRead") {
53-
setState({ ...query, hisRead: newQuery });
47+
onChangeAndSave({ ...query, hisRead: newQuery });
5448
} else if (query.type === "hisReadFilter") {
55-
setState({ ...query, hisReadFilter: newQuery });
49+
onChangeAndSave({ ...query, hisReadFilter: newQuery });
5650
} else if (query.type === "read") {
57-
setState({ ...query, read: newQuery });
51+
onChangeAndSave({ ...query, read: newQuery });
5852
}
5953
};
6054

6155
const onColumnChange = (event: React.FormEvent<HTMLInputElement>) => {
62-
setState({...query, column: event.currentTarget.value,});
56+
onChangeAndSave({...query, column: event.currentTarget.value,});
6357
};
6458

6559
const onDisplayColumnChange = (event: React.FormEvent<HTMLInputElement>) => {
66-
setState({...query, displayColumn: event.currentTarget.value,});
60+
onChangeAndSave({...query, displayColumn: event.currentTarget.value,});
6761
};
6862

6963
return (
70-
<div onBlur={saveQuery}>
64+
<Stack
65+
direction="column"
66+
alignItems="flex-start"
67+
>
7168
<HaystackQueryTypeSelector
7269
datasource={null}
7370
type={query.type}
74-
refId={query.refId ?? VARIABLE_REF_ID}
71+
refId={query.refId}
7572
onChange={onTypeChange}
7673
/>
7774
<HaystackQueryInput
@@ -94,6 +91,6 @@ export const VariableQueryEditor: React.FC<VariableQueryProps> = ({ onChange, qu
9491
placeholder="Defaults to 'Column'"
9592
/>
9693
</InlineField>
97-
</div>
94+
</Stack>
9895
);
9996
};

src/datasource.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { HaystackQuery, OpsQuery, HaystackDataSourceOptions, HaystackVariableQue
1717
import { firstValueFrom, map, Observable } from 'rxjs';
1818
import { isRef, parseRef } from 'haystack';
1919
import { ComponentType } from 'react';
20-
import { VARIABLE_REF_ID, VariableQueryEditor } from 'components/VariableQueryEditor';
20+
import { VariableQueryEditor } from 'components/VariableQueryEditor';
2121

2222
export const queryTypes: QueryType[] = [
2323
{ label: 'Eval', value: 'eval', apiRequirements: ['eval'], description: 'Evaluate an Axon expression' },
@@ -130,7 +130,6 @@ export class HaystackVariableSupport extends CustomVariableSupport<DataSource, H
130130

131131
query(request: DataQueryRequest<HaystackVariableQuery>): Observable<DataQueryResponse> {
132132
let variableQuery = request.targets[0];
133-
variableQuery.refId = VARIABLE_REF_ID;
134133
let observable = this.onQuery(request);
135134
return observable.pipe(
136135
map((response) => {

src/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"updated": "%TODAY%"
4141
},
4242
"dependencies": {
43-
"grafanaDependency": ">=10.0.0",
43+
"grafanaDependency": ">=11.0.0",
4444
"plugins": []
4545
}
4646
}

src/types.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { DataSourceJsonData, SelectableValue } from '@grafana/data';
22
import { DataQuery } from '@grafana/schema';
33

44
export interface HaystackQuery extends DataQuery {
5-
type: string; // Defines the type of query that should be executed
6-
eval: string;
7-
hisRead: string;
8-
hisReadFilter: string;
9-
read: string;
5+
type?: string; // Defines the type of query that should be executed
6+
eval?: string;
7+
hisRead?: string;
8+
hisReadFilter?: string;
9+
read?: string;
1010
}
1111

1212
// OpsQuery is a query that is used to get the available ops from the datasource.
@@ -29,6 +29,7 @@ export interface QueryType extends SelectableValue<string> {
2929
}
3030

3131
export interface HaystackVariableQuery extends HaystackQuery {
32+
query: string; // Used to set 'definition' display string in the UI
3233
column: string;
3334
displayColumn: string;
3435
refId: string;

0 commit comments

Comments
 (0)