Skip to content

Commit 1af4e8b

Browse files
committed
Add experimental Metric option
1 parent a7b2b7a commit 1af4e8b

File tree

4 files changed

+76
-24
lines changed

4 files changed

+76
-24
lines changed

src/components/ExperimentalEditor.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ export const ExperimentalEditor = ({ query, onChange, onRunQuery, editorContext
2020
onRunQuery();
2121
};
2222

23+
// Metric
24+
const { metricField } = query;
25+
26+
const onMetricChange = (field?: string) => {
27+
onChange({ ...query, metricField: field });
28+
onRunQuery();
29+
};
30+
2331
// Variable label
2432
const { variableTextField } = query;
2533

@@ -80,6 +88,32 @@ export const ExperimentalEditor = ({ query, onChange, onRunQuery, editorContext
8088
/>
8189
</InlineField>
8290
</InlineFieldRow>
91+
<InlineFieldRow>
92+
<InlineField
93+
label="Metric"
94+
tooltip={
95+
<>
96+
<p>{'Set the display name of the selected field to the name of the query result.'}</p>
97+
<a
98+
href="https://github.com/marcusolsson/grafana-json-datasource/issues/36"
99+
target="_blank"
100+
rel="noreferrer"
101+
>
102+
Share feedback
103+
</a>
104+
</>
105+
}
106+
>
107+
<Select
108+
placeholder={'Field'}
109+
width={12}
110+
isClearable={true}
111+
value={fieldNames.find((v) => v.value === metricField)}
112+
options={fieldNames}
113+
onChange={(value) => onMetricChange(value?.value)}
114+
/>
115+
</InlineField>
116+
</InlineFieldRow>
83117
</>
84118
)}
85119
{editorContext === 'variables' && (

src/components/FieldEditor.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import React from 'react';
22

33
import { SelectableValue, FieldType } from '@grafana/data';
4-
import { Icon, InlineFieldRow, InlineField, Select } from '@grafana/ui';
4+
import { Icon, InlineFieldRow, InlineField, Select, Input } from '@grafana/ui';
55

66
import { JsonPathQueryField } from './JsonPathQueryField';
7+
import { JsonField } from 'types';
78

89
interface Props {
910
limit?: number;
10-
onChange: (value: any) => void;
11+
onChange: (value: JsonField[]) => void;
1112
onComplete: () => Promise<any>;
12-
value: any[];
13+
value: JsonField[];
1314
}
1415

1516
export const FieldEditor = ({ value, onChange, limit, onComplete }: Props) => {
@@ -25,6 +26,10 @@ export const FieldEditor = ({ value, onChange, limit, onComplete }: Props) => {
2526
);
2627
};
2728

29+
const onAliasChange = (i: number) => (e: any) => {
30+
onChange(value.map((field, n) => (i === n ? { ...value[i], name: e.currentTarget.value } : field)));
31+
};
32+
2833
const addField = (i: number) => () => {
2934
if (!limit || value.length < limit) {
3035
onChange([...value.slice(0, i + 1), { name: '', jsonPath: '' }, ...value.slice(i + 1)]);
@@ -72,6 +77,9 @@ export const FieldEditor = ({ value, onChange, limit, onComplete }: Props) => {
7277
]}
7378
/>
7479
</InlineField>
80+
<InlineField label="Alias" tooltip="If left blank, the field uses the name of the queried element.">
81+
<Input width={12} value={field.name} onChange={onAliasChange(index)} />
82+
</InlineField>
7583

7684
{(!limit || value.length < limit) && (
7785
<a className="gf-form-label" onClick={addField(index)}>

src/datasource.ts

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export class JsonDataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourc
129129
throw new Error('Query returned empty data');
130130
}
131131

132-
const fields = query.fields
132+
const fields: Field[] = query.fields
133133
.filter((field) => field.jsonPath)
134134
.map((field) => {
135135
const path = replaceWithVars(field.jsonPath);
@@ -144,9 +144,10 @@ export class JsonDataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourc
144144
const typedValues = parseValues(values, propertyType);
145145

146146
return {
147-
name: paths[paths.length - 1],
147+
name: field.name || paths[paths.length - 1],
148148
type: propertyType,
149-
values: typedValues,
149+
values: new ArrayVector(typedValues),
150+
config: {},
150151
};
151152
});
152153

@@ -158,24 +159,32 @@ export class JsonDataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourc
158159
throw new Error('Fields have different lengths');
159160
}
160161

161-
if (query.groupByField) {
162-
return groupBy(
163-
toDataFrame({
164-
name: query.refId,
165-
refId: query.refId,
166-
fields: fields,
167-
}),
168-
query.groupByField
169-
);
170-
}
162+
const frames = query.groupByField
163+
? groupBy(
164+
toDataFrame({
165+
name: query.refId,
166+
refId: query.refId,
167+
fields: fields,
168+
}),
169+
query.groupByField
170+
)
171+
: [
172+
toDataFrame({
173+
name: query.refId,
174+
refId: query.refId,
175+
fields: fields,
176+
}),
177+
];
178+
179+
const res = frames.map((frame) => ({
180+
...frame,
181+
fields: fields.map(
182+
(field: Field): Field =>
183+
field.name === query.metricField ? { ...field, config: { displayNameFromDS: frame.name } } : field
184+
),
185+
}));
171186

172-
return [
173-
toDataFrame({
174-
name: query.refId,
175-
refId: query.refId,
176-
fields: fields,
177-
}),
178-
];
187+
return res;
179188
}
180189

181190
async requestJson(query: JsonApiQuery, interpolate: (text: string) => string) {

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DataQuery, DataSourceJsonData, FieldType } from '@grafana/data';
22

3-
interface JsonField {
3+
export interface JsonField {
44
name?: string;
55
jsonPath: string;
66
type?: FieldType;
@@ -23,6 +23,7 @@ export interface JsonApiQuery extends DataQuery {
2323

2424
// Experimental
2525
groupByField?: string;
26+
metricField?: string;
2627
variableTextField?: string;
2728
variableValueField?: string;
2829
}

0 commit comments

Comments
 (0)