1import { DataQuery, ReducerID, SelectableValue } from '@grafana/data';
2import { EvalFunction } from '../alerting/state/alertDef';
3
4export enum ExpressionQueryType {
5  math = 'math',
6  reduce = 'reduce',
7  resample = 'resample',
8  classic = 'classic_conditions',
9}
10
11export const gelTypes: Array<SelectableValue<ExpressionQueryType>> = [
12  { value: ExpressionQueryType.math, label: 'Math' },
13  { value: ExpressionQueryType.reduce, label: 'Reduce' },
14  { value: ExpressionQueryType.resample, label: 'Resample' },
15  { value: ExpressionQueryType.classic, label: 'Classic condition' },
16];
17
18export const reducerTypes: Array<SelectableValue<string>> = [
19  { value: ReducerID.min, label: 'Min', description: 'Get the minimum value' },
20  { value: ReducerID.max, label: 'Max', description: 'Get the maximum value' },
21  { value: ReducerID.mean, label: 'Mean', description: 'Get the average value' },
22  { value: ReducerID.sum, label: 'Sum', description: 'Get the sum of all values' },
23  { value: ReducerID.count, label: 'Count', description: 'Get the number of values' },
24];
25
26export const downsamplingTypes: Array<SelectableValue<string>> = [
27  { value: ReducerID.min, label: 'Min', description: 'Fill with the minimum value' },
28  { value: ReducerID.max, label: 'Max', description: 'Fill with the maximum value' },
29  { value: ReducerID.mean, label: 'Mean', description: 'Fill with the average value' },
30  { value: ReducerID.sum, label: 'Sum', description: 'Fill with the sum of all values' },
31];
32
33export const upsamplingTypes: Array<SelectableValue<string>> = [
34  { value: 'pad', label: 'pad', description: 'fill with the last known value' },
35  { value: 'backfilling', label: 'backfilling', description: 'fill with the next known value' },
36  { value: 'fillna', label: 'fillna', description: 'Fill with NaNs' },
37];
38
39/**
40 * For now this is a single object to cover all the types.... would likely
41 * want to split this up by type as the complexity increases
42 */
43export interface ExpressionQuery extends DataQuery {
44  type: ExpressionQueryType;
45  reducer?: string;
46  expression?: string;
47  window?: string;
48  downsampler?: string;
49  upsampler?: string;
50  conditions?: ClassicCondition[];
51}
52export interface ClassicCondition {
53  evaluator: {
54    params: number[];
55    type: EvalFunction;
56  };
57  operator?: {
58    type: string;
59  };
60  query: {
61    params: string[];
62  };
63  reducer: {
64    params: [];
65    type: ReducerType;
66  };
67  type: 'query';
68}
69
70export type ReducerType =
71  | 'avg'
72  | 'min'
73  | 'max'
74  | 'sum'
75  | 'count'
76  | 'last'
77  | 'median'
78  | 'diff'
79  | 'diff_abs'
80  | 'percent_diff'
81  | 'percent_diff_abs'
82  | 'count_non_null';
83