2023-10-13 11:00:42 +03:00
|
|
|
import React, { useCallback } from 'react';
|
2023-03-10 14:41:06 +02:00
|
|
|
|
2023-10-27 10:30:49 -05:00
|
|
|
import {
|
|
|
|
|
PluginState,
|
|
|
|
|
TransformerRegistryItem,
|
|
|
|
|
TransformerUIProps,
|
|
|
|
|
ReducerID,
|
|
|
|
|
isReducerID,
|
|
|
|
|
SelectableValue,
|
2023-11-02 10:25:48 -05:00
|
|
|
Field,
|
|
|
|
|
FieldType,
|
2023-10-27 10:30:49 -05:00
|
|
|
} from '@grafana/data';
|
2023-11-02 10:25:48 -05:00
|
|
|
import { InlineFieldRow, InlineField, StatsPicker, Select, InlineLabel } from '@grafana/ui';
|
2023-03-10 14:41:06 +02:00
|
|
|
|
2023-10-27 10:30:49 -05:00
|
|
|
import {
|
|
|
|
|
timeSeriesTableTransformer,
|
|
|
|
|
TimeSeriesTableTransformerOptions,
|
|
|
|
|
getRefData,
|
|
|
|
|
} from './timeSeriesTableTransformer';
|
2023-03-10 14:41:06 +02:00
|
|
|
|
2023-10-13 11:00:42 +03:00
|
|
|
export function TimeSeriesTableTransformEditor({
|
|
|
|
|
input,
|
|
|
|
|
options,
|
|
|
|
|
onChange,
|
|
|
|
|
}: TransformerUIProps<TimeSeriesTableTransformerOptions>) {
|
2023-10-27 10:30:49 -05:00
|
|
|
const refIdMap = getRefData(input);
|
|
|
|
|
|
|
|
|
|
const onSelectTimefield = useCallback(
|
|
|
|
|
(refId: string, value: SelectableValue<string>) => {
|
|
|
|
|
const val = value?.value !== undefined ? value.value : '';
|
|
|
|
|
onChange({
|
|
|
|
|
...options,
|
|
|
|
|
[refId]: {
|
|
|
|
|
...options[refId],
|
|
|
|
|
timeField: val,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
[onChange, options]
|
|
|
|
|
);
|
2023-03-10 14:41:06 +02:00
|
|
|
|
2023-10-13 11:00:42 +03:00
|
|
|
const onSelectStat = useCallback(
|
|
|
|
|
(refId: string, stats: string[]) => {
|
|
|
|
|
const reducerID = stats[0];
|
|
|
|
|
if (reducerID && isReducerID(reducerID)) {
|
|
|
|
|
onChange({
|
2023-10-27 10:30:49 -05:00
|
|
|
...options,
|
|
|
|
|
[refId]: {
|
|
|
|
|
...options[refId],
|
|
|
|
|
stat: reducerID,
|
2023-10-13 11:00:42 +03:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[onChange, options]
|
|
|
|
|
);
|
2023-03-10 14:41:06 +02:00
|
|
|
|
2023-10-27 10:30:49 -05:00
|
|
|
let configRows = [];
|
|
|
|
|
for (const refId of Object.keys(refIdMap)) {
|
2023-11-02 10:25:48 -05:00
|
|
|
// Get time fields for the current refId
|
|
|
|
|
const timeFields: Record<string, Field<FieldType.time>> = {};
|
|
|
|
|
const timeValues: Array<SelectableValue<string>> = [];
|
|
|
|
|
|
|
|
|
|
// Get a map of time fields, we map
|
|
|
|
|
// by field name and assume that time fields
|
|
|
|
|
// in the same query with the same name
|
|
|
|
|
// are the same
|
|
|
|
|
for (const frame of input) {
|
|
|
|
|
if (frame.refId === refId) {
|
|
|
|
|
for (const field of frame.fields) {
|
|
|
|
|
if (field.type === 'time') {
|
|
|
|
|
timeFields[field.name] = field;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const timeField of Object.values(timeFields)) {
|
|
|
|
|
const { name } = timeField;
|
|
|
|
|
timeValues.push({ label: name, value: name });
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-27 10:30:49 -05:00
|
|
|
configRows.push(
|
|
|
|
|
<InlineFieldRow key={refId}>
|
2023-11-02 10:25:48 -05:00
|
|
|
<InlineField>
|
|
|
|
|
<InlineLabel>{`Trend #${refId}`}</InlineLabel>
|
|
|
|
|
</InlineField>
|
2023-10-27 10:30:49 -05:00
|
|
|
<InlineField
|
|
|
|
|
label="Time field"
|
|
|
|
|
tooltip="The time field that will be used for the time series. If not selected the first found will be used."
|
|
|
|
|
>
|
|
|
|
|
<Select
|
|
|
|
|
onChange={onSelectTimefield.bind(null, refId)}
|
2023-11-02 10:25:48 -05:00
|
|
|
options={timeValues}
|
2023-10-27 10:30:49 -05:00
|
|
|
value={options[refId]?.timeField}
|
2023-11-02 10:25:48 -05:00
|
|
|
isClearable={true}
|
2023-10-27 10:30:49 -05:00
|
|
|
/>
|
|
|
|
|
</InlineField>
|
|
|
|
|
<InlineField label="Stat" tooltip="The statistic that should be calculated for this time series.">
|
|
|
|
|
<StatsPicker
|
|
|
|
|
stats={[options[refId]?.stat ?? ReducerID.lastNotNull]}
|
|
|
|
|
onChange={onSelectStat.bind(null, refId)}
|
|
|
|
|
filterOptions={(ext) => ext.id !== ReducerID.allValues && ext.id !== ReducerID.uniqueValues}
|
|
|
|
|
/>
|
|
|
|
|
</InlineField>
|
|
|
|
|
</InlineFieldRow>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <>{configRows}</>;
|
2023-03-10 14:41:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const timeSeriesTableTransformRegistryItem: TransformerRegistryItem<TimeSeriesTableTransformerOptions> = {
|
|
|
|
|
id: timeSeriesTableTransformer.id,
|
|
|
|
|
editor: TimeSeriesTableTransformEditor,
|
|
|
|
|
transformation: timeSeriesTableTransformer,
|
|
|
|
|
name: timeSeriesTableTransformer.name,
|
|
|
|
|
description: timeSeriesTableTransformer.description,
|
|
|
|
|
state: PluginState.beta,
|
|
|
|
|
help: ``,
|
|
|
|
|
};
|