Elasticsearch: Use generated types in data source (#62753)

* Use generated types in frontend

* Export missing MetricAggregationWithMeta

* Remove not needed changes

* Update

* Fix lint

* Update comments
This commit is contained in:
Ivana Huckova 2023-02-03 18:04:12 +01:00 committed by GitHub
parent bbb572e73f
commit 20ec54f1f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
49 changed files with 151 additions and 564 deletions

View File

@ -8,7 +8,7 @@ title: ElasticsearchDataQuery kind
## ElasticsearchDataQuery
#### Maturity: merged
#### Maturity: experimental
#### Version: 0.0
@ -18,15 +18,15 @@ It extends [DataQuery](#dataquery).
| Property | Type | Required | Description |
|--------------|-------------------------------------------|----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `refId` | string | **Yes** | *(Inherited from [DataQuery](#dataquery))*<br/>A - Z |
| `alias` | string | No | |
| `bucketAggs` | [BucketAggregation](#bucketaggregation)[] | No | |
| `alias` | string | No | Alias pattern |
| `bucketAggs` | [BucketAggregation](#bucketaggregation)[] | No | List of bucket aggregations |
| `datasource` | | No | *(Inherited from [DataQuery](#dataquery))*<br/>For mixed data sources the selected datasource is on the query level.<br/>For non mixed scenarios this is undefined.<br/>TODO find a better way to do this ^ that's friendly to schema<br/>TODO this shouldn't be unknown but DataSourceRef &#124; null |
| `hide` | boolean | No | *(Inherited from [DataQuery](#dataquery))*<br/>true if query is disabled (ie should not be returned to the dashboard) |
| `key` | string | No | *(Inherited from [DataQuery](#dataquery))*<br/>Unique, guid like, string used in explore mode |
| `metrics` | [MetricAggregation](#metricaggregation)[] | No | |
| `metrics` | [MetricAggregation](#metricaggregation)[] | No | List of metric aggregations |
| `queryType` | string | No | *(Inherited from [DataQuery](#dataquery))*<br/>Specify the query flavor<br/>TODO make this required and give it a default |
| `query` | string | No | |
| `timeField` | string | No | |
| `query` | string | No | Lucene query |
| `timeField` | string | No | Name of time field |
### BucketAggregation

View File

@ -444,7 +444,7 @@
"ts": "https://github.com/grafana/grafana/tree/main/public/app/plugins/datasource/elasticsearch/dataquery.gen.ts"
},
"machineName": "elasticsearchdataquery",
"maturity": "merged",
"maturity": "experimental",
"name": "ElasticsearchDataQuery",
"pluralMachineName": "elasticsearchdataquerys",
"pluralName": "ElasticsearchDataQuerys",
@ -1983,6 +1983,7 @@
"dashboard",
"dashboardlistpanelcfg",
"debugpanelcfg",
"elasticsearchdataquery",
"gaugepanelcfg",
"histogrampanelcfg",
"librarypanel",
@ -1999,7 +2000,7 @@
"textpanelcfg",
"xychartpanelcfg"
],
"count": 21
"count": 22
},
"mature": {
"name": "mature",
@ -2011,14 +2012,13 @@
"items": [
"alertgroupspanelcfg",
"azuremonitordataquery",
"elasticsearchdataquery",
"playlist",
"preferences",
"publicdashboard",
"serviceaccount",
"team"
],
"count": 8
"count": 7
},
"planned": {
"name": "planned",

View File

@ -644,12 +644,9 @@ type BaseMovingAverageModelSettingsModel string
// BasePipelineMetricAggregation defines model for BasePipelineMetricAggregation.
type BasePipelineMetricAggregation struct {
Field *string `json:"field,omitempty"`
Hide *bool `json:"hide,omitempty"`
Id string `json:"id"`
// TODO: Type is temporarily commented out as it causes a type error in the generated code. In the meantime, we decided to manually extend the type in types.ts.
// type: #PipelineMetricAggregationType
Field *string `json:"field,omitempty"`
Hide *bool `json:"hide,omitempty"`
Id string `json:"id"`
PipelineAgg *string `json:"pipelineAgg,omitempty"`
Type BasePipelineMetricAggregationType `json:"type"`
}
@ -676,11 +673,8 @@ type BucketAggregationWithFieldType string
// BucketScript defines model for BucketScript.
type BucketScript struct {
Hide *bool `json:"hide,omitempty"`
Id string `json:"id"`
// TODO: Type is temporarily commented out as it causes a type error in the generated code. In the meantime, we decided to manually extend the type in types.ts.
// type: #PipelineMetricAggregationType
Hide *bool `json:"hide,omitempty"`
Id string `json:"id"`
PipelineVariables *[]struct {
Name string `json:"name"`
PipelineAgg string `json:"pipelineAgg"`
@ -706,12 +700,9 @@ type CountType string
// CumulativeSum defines model for CumulativeSum.
type CumulativeSum struct {
Field *string `json:"field,omitempty"`
Hide *bool `json:"hide,omitempty"`
Id string `json:"id"`
// TODO: Type is temporarily commented out as it causes a type error in the generated code. In the meantime, we decided to manually extend the type in types.ts.
// type: #PipelineMetricAggregationType
Field *string `json:"field,omitempty"`
Hide *bool `json:"hide,omitempty"`
Id string `json:"id"`
PipelineAgg *string `json:"pipelineAgg,omitempty"`
Settings *struct {
Format *string `json:"format,omitempty"`
@ -750,12 +741,9 @@ type DateHistogramSettings struct {
// Derivative defines model for Derivative.
type Derivative struct {
Field *string `json:"field,omitempty"`
Hide *bool `json:"hide,omitempty"`
Id string `json:"id"`
// TODO: Type is temporarily commented out as it causes a type error in the generated code. In the meantime, we decided to manually extend the type in types.ts.
// type: #PipelineMetricAggregationType
Field *string `json:"field,omitempty"`
Hide *bool `json:"hide,omitempty"`
Id string `json:"id"`
PipelineAgg *string `json:"pipelineAgg,omitempty"`
Settings *struct {
Unit *string `json:"unit,omitempty"`
@ -768,7 +756,10 @@ type DerivativeType string
// ElasticsearchDataQuery defines model for ElasticsearchDataQuery.
type ElasticsearchDataQuery struct {
Alias *string `json:"alias,omitempty"`
// Alias pattern
Alias *string `json:"alias,omitempty"`
// List of bucket aggregations
BucketAggs *[]interface{} `json:"bucketAggs,omitempty"`
// For mixed data sources the selected datasource is on the query level.
@ -781,16 +772,22 @@ type ElasticsearchDataQuery struct {
Hide *bool `json:"hide,omitempty"`
// Unique, guid like, string used in explore mode
Key *string `json:"key,omitempty"`
Key *string `json:"key,omitempty"`
// List of metric aggregations
Metrics *[]interface{} `json:"metrics,omitempty"`
Query *string `json:"query,omitempty"`
// Lucene query
Query *string `json:"query,omitempty"`
// Specify the query flavor
// TODO make this required and give it a default
QueryType *string `json:"queryType,omitempty"`
// A - Z
RefId string `json:"refId"`
RefId string `json:"refId"`
// Name of time field
TimeField *string `json:"timeField,omitempty"`
}
@ -984,12 +981,9 @@ type MinType string
// #MovingAverage's settings are overridden in types.ts
type MovingAverage struct {
Field *string `json:"field,omitempty"`
Hide *bool `json:"hide,omitempty"`
Id string `json:"id"`
// TODO: Type is temporarily commented out as it causes a type error in the generated code. In the meantime, we decided to manually extend the type in types.ts.
// type: #PipelineMetricAggregationType
Field *string `json:"field,omitempty"`
Hide *bool `json:"hide,omitempty"`
Id string `json:"id"`
PipelineAgg *string `json:"pipelineAgg,omitempty"`
Settings map[string]interface{} `json:"settings,omitempty"`
Type MovingAverageType `json:"type"`
@ -1079,12 +1073,9 @@ type MovingAverageSimpleModelSettingsModel string
// MovingFunction defines model for MovingFunction.
type MovingFunction struct {
Field *string `json:"field,omitempty"`
Hide *bool `json:"hide,omitempty"`
Id string `json:"id"`
// TODO: Type is temporarily commented out as it causes a type error in the generated code. In the meantime, we decided to manually extend the type in types.ts.
// type: #PipelineMetricAggregationType
Field *string `json:"field,omitempty"`
Hide *bool `json:"hide,omitempty"`
Id string `json:"id"`
PipelineAgg *string `json:"pipelineAgg,omitempty"`
Settings *struct {
Script *interface{} `json:"script,omitempty"`
@ -1132,11 +1123,8 @@ type PipelineMetricAggregationType string
// PipelineMetricAggregationWithMultipleBucketPaths defines model for PipelineMetricAggregationWithMultipleBucketPaths.
type PipelineMetricAggregationWithMultipleBucketPaths struct {
Hide *bool `json:"hide,omitempty"`
Id string `json:"id"`
// TODO: Type is temporarily commented out as it causes a type error in the generated code. In the meantime, we decided to manually extend the type in types.ts.
// type: #PipelineMetricAggregationType
Hide *bool `json:"hide,omitempty"`
Id string `json:"id"`
PipelineVariables *[]struct {
Name string `json:"name"`
PipelineAgg string `json:"pipelineAgg"`
@ -1196,12 +1184,9 @@ type RawDocumentType string
// SerialDiff defines model for SerialDiff.
type SerialDiff struct {
Field *string `json:"field,omitempty"`
Hide *bool `json:"hide,omitempty"`
Id string `json:"id"`
// TODO: Type is temporarily commented out as it causes a type error in the generated code. In the meantime, we decided to manually extend the type in types.ts.
// type: #PipelineMetricAggregationType
Field *string `json:"field,omitempty"`
Hide *bool `json:"hide,omitempty"`
Id string `json:"id"`
PipelineAgg *string `json:"pipelineAgg,omitempty"`
Settings *struct {
Lag *string `json:"lag,omitempty"`

View File

@ -11,14 +11,10 @@ import {
import TableModel from 'app/core/TableModel';
import flatten from 'app/core/utils/flatten';
import {
ExtendedStatMetaType,
isMetricAggregationWithField,
TopMetrics,
} from './components/QueryEditor/MetricAggregationsEditor/aggregations';
import { isMetricAggregationWithField } from './components/QueryEditor/MetricAggregationsEditor/aggregations';
import { metricAggregationConfig } from './components/QueryEditor/MetricAggregationsEditor/utils';
import * as queryDef from './queryDef';
import { ElasticsearchAggregation, ElasticsearchQuery } from './types';
import { ElasticsearchAggregation, ElasticsearchQuery, TopMetrics, ExtendedStatMetaType } from './types';
import { describeMetric, getScriptValue } from './utils';
const HIGHLIGHT_TAGS_EXP = `${queryDef.highlightTags.pre}([^@]+)${queryDef.highlightTags.post}`;

View File

@ -1,22 +1,23 @@
import { AdHocVariableFilter, InternalTimeZones } from '@grafana/data';
import {
Filters,
Histogram,
DateHistogram,
Terms,
} from './components/QueryEditor/BucketAggregationsEditor/aggregations';
import {
isMetricAggregationWithField,
isMetricAggregationWithSettings,
isMovingAverageWithModelSettings,
isPipelineAggregation,
isPipelineAggregationWithMultipleBucketPaths,
MetricAggregation,
MetricAggregationWithInlineScript,
} from './components/QueryEditor/MetricAggregationsEditor/aggregations';
import { defaultBucketAgg, defaultMetricAgg, findMetricById, highlightTags } from './queryDef';
import { ElasticsearchQuery, TermsQuery } from './types';
import {
ElasticsearchQuery,
TermsQuery,
Filters,
Terms,
MetricAggregation,
MetricAggregationWithInlineScript,
Histogram,
DateHistogram,
} from './types';
import { convertOrderByToMetricId, getScriptValue } from './utils';
export class ElasticQueryBuilder {

View File

@ -4,10 +4,9 @@ import React from 'react';
import { SelectableValue } from '@grafana/data';
import { Segment } from '@grafana/ui';
import { MetricAggregation } from '../types';
import { describeMetric } from '../utils';
import { MetricAggregation } from './QueryEditor/MetricAggregationsEditor/aggregations';
const noWrap = css`
white-space: nowrap;
`;

View File

@ -7,8 +7,9 @@ import { useFields } from '../../../hooks/useFields';
import { useDispatch } from '../../../hooks/useStatelessReducer';
import { segmentStyles } from '../styles';
import { BucketAggregation, BucketAggregationType } from './../../../types';
import { SettingsEditor } from './SettingsEditor';
import { BucketAggregation, BucketAggregationType, isBucketAggregationWithField } from './aggregations';
import { isBucketAggregationWithField } from './aggregations';
import { changeBucketAggregationField, changeBucketAggregationType } from './state/actions';
import { bucketAggregationConfig } from './utils';

View File

@ -6,8 +6,8 @@ import { InternalTimeZones, SelectableValue } from '@grafana/data';
import { InlineField, Input, Select, TimeZonePicker } from '@grafana/ui';
import { useDispatch } from '../../../../hooks/useStatelessReducer';
import { DateHistogram } from '../../../../types';
import { useCreatableSelectPersistedBehaviour } from '../../../hooks/useCreatableSelectPersistedBehaviour';
import { DateHistogram } from '../aggregations';
import { changeBucketAggregationSetting } from '../state/actions';
import { bucketAggregationConfig } from '../utils';

View File

@ -5,8 +5,8 @@ import React, { useEffect, useRef } from 'react';
import { InlineField, Input, QueryField } from '@grafana/ui';
import { useDispatch, useStatelessReducer } from '../../../../../hooks/useStatelessReducer';
import { Filters } from '../../../../../types';
import { AddRemove } from '../../../../AddRemove';
import { Filters } from '../../aggregations';
import { changeBucketAggregationSetting } from '../../state/actions';
import { addFilter, changeFilter, removeFilter } from './state/actions';

View File

@ -1,6 +1,6 @@
import { createAction } from '@reduxjs/toolkit';
import { Filter } from '../../../aggregations';
import { Filter } from '../../../../../../types';
export const addFilter = createAction('@bucketAggregations/filter/add');
export const removeFilter = createAction<number>('@bucketAggregations/filter/remove');

View File

@ -1,6 +1,6 @@
import { reducerTester } from 'test/core/redux/reducerTester';
import { Filter } from '../../../aggregations';
import { Filter } from '../../../../../../types';
import { addFilter, changeFilter, removeFilter } from './actions';
import { reducer } from './reducer';

View File

@ -1,6 +1,6 @@
import { Action } from 'redux';
import { Filter } from '../../../aggregations';
import { Filter } from '../../../../../../types';
import { defaultFilter } from '../utils';
import { addFilter, changeFilter, removeFilter } from './actions';

View File

@ -1,3 +1,3 @@
import { Filter } from '../../aggregations';
import { Filter } from '../../../../../types';
export const defaultFilter = (): Filter => ({ label: '', query: '*' });

View File

@ -5,9 +5,7 @@ import selectEvent from 'react-select-event';
import { describeMetric } from 'app/plugins/datasource/elasticsearch/utils';
import { renderWithESProvider } from '../../../../test-helpers/render';
import { ElasticsearchQuery } from '../../../../types';
import { Average, Derivative, TopMetrics } from '../../MetricAggregationsEditor/aggregations';
import { Terms } from '../aggregations';
import { ElasticsearchQuery, Terms, Average, Derivative, TopMetrics } from '../../../../types';
import { TermsSettingsEditor } from './TermsSettingsEditor';

View File

@ -5,17 +5,11 @@ import { SelectableValue } from '@grafana/data';
import { InlineField, Select, Input } from '@grafana/ui';
import { useDispatch } from '../../../../hooks/useStatelessReducer';
import { MetricAggregation, Percentiles, ExtendedStatMetaType, ExtendedStats, Terms } from '../../../../types';
import { describeMetric } from '../../../../utils';
import { useCreatableSelectPersistedBehaviour } from '../../../hooks/useCreatableSelectPersistedBehaviour';
import { useQuery } from '../../ElasticsearchQueryContext';
import {
ExtendedStatMetaType,
ExtendedStats,
isPipelineAggregation,
MetricAggregation,
Percentiles,
} from '../../MetricAggregationsEditor/aggregations';
import { Terms } from '../aggregations';
import { isPipelineAggregation } from '../../MetricAggregationsEditor/aggregations';
import { changeBucketAggregationSetting } from '../state/actions';
import { bucketAggregationConfig, orderByOptions, orderOptions, sizeOptions } from '../utils';

View File

@ -4,8 +4,8 @@ import React, { ComponentProps, useRef } from 'react';
import { InlineField, Input } from '@grafana/ui';
import { useDispatch } from '../../../../hooks/useStatelessReducer';
import { BucketAggregation } from '../../../../types';
import { SettingsEditorContainer } from '../../SettingsEditorContainer';
import { BucketAggregation } from '../aggregations';
import { changeBucketAggregationSetting } from '../state/actions';
import { bucketAggregationConfig } from '../utils';

View File

@ -1,6 +1,6 @@
import { BucketAggregation } from '../../../../types';
import { describeMetric, convertOrderByToMetricId } from '../../../../utils';
import { useQuery } from '../../ElasticsearchQueryContext';
import { BucketAggregation } from '../aggregations';
import { bucketAggregationConfig, orderByOptions, orderOptions } from '../utils';
const hasValue = (value: string) => (object: { value?: string }) => object.value === value;

View File

@ -1,74 +1,7 @@
import { BucketAggregationType, BucketAggregationWithField, BucketAggregation } from '../../../types';
import { bucketAggregationConfig } from './utils';
export type BucketAggregationType = 'terms' | 'filters' | 'geohash_grid' | 'date_histogram' | 'histogram' | 'nested';
interface BaseBucketAggregation {
id: string;
type: BucketAggregationType;
settings?: Record<string, unknown>;
}
export interface BucketAggregationWithField extends BaseBucketAggregation {
field?: string;
}
export interface DateHistogram extends BucketAggregationWithField {
type: 'date_histogram';
settings?: {
interval?: string;
min_doc_count?: string;
trimEdges?: string;
offset?: string;
timeZone?: string;
};
}
export interface Histogram extends BucketAggregationWithField {
type: 'histogram';
settings?: {
interval?: string;
min_doc_count?: string;
};
}
type TermsOrder = 'desc' | 'asc';
export interface Terms extends BucketAggregationWithField {
type: 'terms';
settings?: {
order?: TermsOrder;
size?: string;
min_doc_count?: string;
orderBy?: string;
missing?: string;
};
}
export type Filter = {
query: string;
label: string;
};
export interface Filters extends BaseBucketAggregation {
type: 'filters';
settings?: {
filters?: Filter[];
};
}
interface GeoHashGrid extends BucketAggregationWithField {
type: 'geohash_grid';
settings?: {
precision?: string;
};
}
interface Nested extends BucketAggregationWithField {
type: 'nested';
settings?: {};
}
export type BucketAggregation = DateHistogram | Histogram | Terms | Filters | GeoHashGrid | Nested;
export const isBucketAggregationWithField = (
bucketAgg: BucketAggregation | BucketAggregationWithField
): bucketAgg is BucketAggregationWithField => bucketAggregationConfig[bucketAgg.type].requiresField;

View File

@ -5,8 +5,8 @@ import { IconButton } from '../../IconButton';
import { useQuery } from '../ElasticsearchQueryContext';
import { QueryEditorRow } from '../QueryEditorRow';
import { BucketAggregation } from './../../../types';
import { BucketAggregationEditor } from './BucketAggregationEditor';
import { BucketAggregation } from './aggregations';
import { addBucketAggregation, removeBucketAggregation } from './state/actions';
interface Props {

View File

@ -1,6 +1,6 @@
import { createAction } from '@reduxjs/toolkit';
import { BucketAggregation, BucketAggregationType, BucketAggregationWithField } from '../aggregations';
import { BucketAggregation, BucketAggregationType, BucketAggregationWithField } from '../../../../types';
export const addBucketAggregation = createAction<BucketAggregation['id']>('@bucketAggs/add');
export const removeBucketAggregation = createAction<BucketAggregation['id']>('@bucketAggs/remove');

View File

@ -3,9 +3,9 @@ import { reducerTester } from 'test/core/redux/reducerTester';
import { defaultBucketAgg } from 'app/plugins/datasource/elasticsearch/queryDef';
import { ElasticsearchQuery } from 'app/plugins/datasource/elasticsearch/types';
import { BucketAggregation, DateHistogram } from '../../../../types';
import { changeMetricType } from '../../MetricAggregationsEditor/state/actions';
import { initQuery } from '../../state';
import { BucketAggregation, DateHistogram } from '../aggregations';
import { bucketAggregationConfig } from '../utils';
import {

View File

@ -1,12 +1,11 @@
import { Action } from '@reduxjs/toolkit';
import { defaultBucketAgg } from '../../../../queryDef';
import { ElasticsearchQuery } from '../../../../types';
import { ElasticsearchQuery, Terms, BucketAggregation } from '../../../../types';
import { removeEmpty } from '../../../../utils';
import { changeMetricType } from '../../MetricAggregationsEditor/state/actions';
import { metricAggregationConfig } from '../../MetricAggregationsEditor/utils';
import { initQuery } from '../../state';
import { BucketAggregation, Terms } from '../aggregations';
import { bucketAggregationConfig } from '../utils';
import {

View File

@ -9,8 +9,8 @@ import { defaultBucketAgg, defaultMetricAgg } from '../../../queryDef';
import { ElasticsearchQuery } from '../../../types';
import { ElasticsearchProvider } from '../ElasticsearchQueryContext';
import { Average, UniqueCount } from './../../../types';
import { MetricEditor } from './MetricEditor';
import { Average, UniqueCount } from './aggregations';
describe('Metric Editor', () => {
it('Should display a "None" option for "field" if the metric supports inline script', async () => {

View File

@ -7,6 +7,7 @@ import { InlineSegmentGroup, Segment, SegmentAsync, useTheme2 } from '@grafana/u
import { useFields } from '../../../hooks/useFields';
import { useDispatch } from '../../../hooks/useStatelessReducer';
import { MetricAggregation, MetricAggregationType } from '../../../types';
import { MetricPicker } from '../../MetricPicker';
import { useDatasource, useQuery } from '../ElasticsearchQueryContext';
import { segmentStyles } from '../styles';
@ -18,8 +19,6 @@ import {
isMetricAggregationWithSettings,
isPipelineAggregation,
isPipelineAggregationWithMultipleBucketPaths,
MetricAggregation,
MetricAggregationType,
} from './aggregations';
import { changeMetricField, changeMetricType } from './state/actions';
import { getStyles } from './styles';

View File

@ -5,9 +5,9 @@ import React, { Fragment, useEffect } from 'react';
import { Input, InlineLabel } from '@grafana/ui';
import { useStatelessReducer, useDispatch } from '../../../../../hooks/useStatelessReducer';
import { BucketScript, MetricAggregation } from '../../../../../types';
import { AddRemove } from '../../../../AddRemove';
import { MetricPicker } from '../../../../MetricPicker';
import { BucketScript, MetricAggregation } from '../../aggregations';
import { changeMetricAttribute } from '../../state/actions';
import { SettingField } from '../SettingField';

View File

@ -1,6 +1,6 @@
import { reducerTester } from 'test/core/redux/reducerTester';
import { PipelineVariable } from '../../../aggregations';
import { PipelineVariable } from '../../../../../../types';
import {
addPipelineVariable,

View File

@ -1,6 +1,6 @@
import { Action } from '@reduxjs/toolkit';
import { PipelineVariable } from '../../../aggregations';
import { PipelineVariable } from '../../../../../../types';
import { defaultPipelineVariable, generatePipelineVariableName } from '../utils';
import {

View File

@ -1,4 +1,4 @@
import { PipelineVariable } from '../../aggregations';
import { PipelineVariable } from '../../../../../types';
export const defaultPipelineVariable = (name: string): PipelineVariable => ({ name, pipelineAgg: '' });

View File

@ -5,7 +5,8 @@ import { Input, InlineField, Select, InlineSwitch } from '@grafana/ui';
import { useDispatch } from '../../../../hooks/useStatelessReducer';
import { movingAvgModelOptions } from '../../../../queryDef';
import { isEWMAMovingAverage, isHoltMovingAverage, isHoltWintersMovingAverage, MovingAverage } from '../aggregations';
import { MovingAverage } from '../../../../types';
import { isEWMAMovingAverage, isHoltMovingAverage, isHoltWintersMovingAverage } from '../aggregations';
import { changeMetricSetting } from '../state/actions';
import { SettingField } from './SettingField';

View File

@ -5,8 +5,8 @@ import { InlineField, Input } from '@grafana/ui';
import { getScriptValue } from 'app/plugins/datasource/elasticsearch/utils';
import { useDispatch } from '../../../../hooks/useStatelessReducer';
import { MetricAggregationWithInlineScript, MetricAggregationWithSettings } from '../../../../types';
import { SettingKeyOf } from '../../../types';
import { MetricAggregationWithInlineScript, MetricAggregationWithSettings } from '../aggregations';
import { changeMetricSetting } from '../state/actions';
interface Props<T extends MetricAggregationWithSettings, K extends SettingKeyOf<T>> {

View File

@ -6,8 +6,8 @@ import { AsyncMultiSelect, InlineField, SegmentAsync, Select } from '@grafana/ui
import { useFields } from '../../../../hooks/useFields';
import { useDispatch } from '../../../../hooks/useStatelessReducer';
import { TopMetrics } from '../../../../types';
import { orderOptions } from '../../BucketAggregationsEditor/utils';
import { TopMetrics } from '../aggregations';
import { changeMetricSetting } from '../state/actions';
interface Props {

View File

@ -5,14 +5,10 @@ import { InlineField, Input, InlineSwitch, Select } from '@grafana/ui';
import { useDispatch } from '../../../../hooks/useStatelessReducer';
import { extendedStats } from '../../../../queryDef';
import { MetricAggregation, ExtendedStat } from '../../../../types';
import { useQuery } from '../../ElasticsearchQueryContext';
import { SettingsEditorContainer } from '../../SettingsEditorContainer';
import {
MetricAggregation,
isMetricAggregationWithInlineScript,
isMetricAggregationWithMissingSupport,
ExtendedStat,
} from '../aggregations';
import { isMetricAggregationWithInlineScript, isMetricAggregationWithMissingSupport } from '../aggregations';
import { changeMetricMeta, changeMetricSetting } from '../state/actions';
import { metricAggregationConfig } from '../utils';

View File

@ -1,5 +1,5 @@
import { extendedStats } from '../../../../queryDef';
import { MetricAggregation } from '../aggregations';
import { MetricAggregation } from '../../../../types';
const hasValue = (value: string) => (object: { value: string }) => object.value === value;

View File

@ -1,245 +1,18 @@
import {
PipelineMetricAggregationWithMultipleBucketPaths,
MetricAggregationWithMeta,
MovingAverage,
MetricAggregationType,
BaseMetricAggregation,
MetricAggregationWithField,
MetricAggregationWithMissingSupport,
MetricAggregationWithInlineScript,
PipelineMetricAggregation,
MetricAggregationWithSettings,
} from '../../../types';
import { metricAggregationConfig } from './utils';
export type PipelineMetricAggregationType =
| 'moving_avg'
| 'moving_fn'
| 'derivative'
| 'serial_diff'
| 'cumulative_sum'
| 'bucket_script';
export type MetricAggregationType =
| 'count'
| 'avg'
| 'sum'
| 'min'
| 'max'
| 'extended_stats'
| 'percentiles'
| 'cardinality'
| 'raw_document'
| 'raw_data'
| 'logs'
| 'rate'
| 'top_metrics'
| PipelineMetricAggregationType;
interface BaseMetricAggregation {
id: string;
type: MetricAggregationType;
hide?: boolean;
}
export interface PipelineVariable {
name: string;
pipelineAgg: string;
}
export interface MetricAggregationWithField extends BaseMetricAggregation {
field?: string;
}
export interface MetricAggregationWithMissingSupport extends BaseMetricAggregation {
settings?: {
missing?: string;
};
}
type InlineScript = string | { inline?: string };
export interface MetricAggregationWithInlineScript extends BaseMetricAggregation {
settings?: {
script?: InlineScript;
};
}
export interface Count extends BaseMetricAggregation {
type: 'count';
}
export interface Average
extends MetricAggregationWithField,
MetricAggregationWithMissingSupport,
MetricAggregationWithInlineScript {
type: 'avg';
settings?: {
script?: InlineScript;
missing?: string;
};
}
export interface Sum extends MetricAggregationWithField, MetricAggregationWithInlineScript {
type: 'sum';
settings?: {
script?: InlineScript;
missing?: string;
};
}
export interface Max extends MetricAggregationWithField, MetricAggregationWithInlineScript {
type: 'max';
settings?: {
script?: InlineScript;
missing?: string;
};
}
export interface Min extends MetricAggregationWithField, MetricAggregationWithInlineScript {
type: 'min';
settings?: {
script?: InlineScript;
missing?: string;
};
}
export type ExtendedStatMetaType =
| 'avg'
| 'min'
| 'max'
| 'sum'
| 'count'
| 'std_deviation'
| 'std_deviation_bounds_upper'
| 'std_deviation_bounds_lower';
export interface ExtendedStat {
label: string;
value: ExtendedStatMetaType;
}
export interface ExtendedStats extends MetricAggregationWithField, MetricAggregationWithInlineScript {
type: 'extended_stats';
settings?: {
script?: InlineScript;
missing?: string;
sigma?: string;
};
meta?: {
[P in ExtendedStatMetaType]?: boolean;
};
}
export interface Percentiles extends MetricAggregationWithField, MetricAggregationWithInlineScript {
type: 'percentiles';
settings?: {
percents?: string[];
script?: InlineScript;
missing?: string;
};
}
export interface UniqueCount extends MetricAggregationWithField {
type: 'cardinality';
settings?: {
precision_threshold?: string;
missing?: string;
};
}
export interface RawDocument extends BaseMetricAggregation {
type: 'raw_document';
settings?: {
size?: string;
};
}
export interface RawData extends BaseMetricAggregation {
type: 'raw_data';
settings?: {
size?: string;
};
}
export interface Logs extends BaseMetricAggregation {
type: 'logs';
settings?: {
limit?: string;
};
}
export interface Rate extends MetricAggregationWithField {
type: 'rate';
settings?: {
unit?: string;
mode?: string;
};
}
export interface BasePipelineMetricAggregation extends MetricAggregationWithField {
type: PipelineMetricAggregationType;
pipelineAgg?: string;
}
export interface PipelineMetricAggregationWithMultipleBucketPaths extends BaseMetricAggregation {
type: PipelineMetricAggregationType;
pipelineVariables?: PipelineVariable[];
}
export type MovingAverageModel = 'simple' | 'linear' | 'ewma' | 'holt' | 'holt_winters';
export interface MovingAverageModelOption {
label: string;
value: MovingAverageModel;
}
export interface BaseMovingAverageModelSettings {
model: MovingAverageModel;
window: string;
predict: string;
}
export interface MovingAverageSimpleModelSettings extends BaseMovingAverageModelSettings {
model: 'simple';
}
export interface MovingAverageLinearModelSettings extends BaseMovingAverageModelSettings {
model: 'linear';
}
export interface MovingAverageEWMAModelSettings extends BaseMovingAverageModelSettings {
model: 'ewma';
settings?: {
alpha?: string;
};
minimize: boolean;
}
export interface MovingAverageHoltModelSettings extends BaseMovingAverageModelSettings {
model: 'holt';
settings: {
alpha?: string;
beta?: string;
};
minimize: boolean;
}
export interface MovingAverageHoltWintersModelSettings extends BaseMovingAverageModelSettings {
model: 'holt_winters';
settings: {
alpha?: string;
beta?: string;
gamma?: string;
period?: string;
pad?: boolean;
};
minimize: boolean;
}
export type MovingAverageModelSettings<T extends MovingAverageModel = MovingAverageModel> = Partial<
Extract<
| MovingAverageSimpleModelSettings
| MovingAverageLinearModelSettings
| MovingAverageEWMAModelSettings
| MovingAverageHoltModelSettings
| MovingAverageHoltWintersModelSettings,
{ model: T }
>
>;
export interface MovingAverage<T extends MovingAverageModel = MovingAverageModel>
extends BasePipelineMetricAggregation {
type: 'moving_avg';
settings?: MovingAverageModelSettings<T>;
}
export const isEWMAMovingAverage = (metric: MovingAverage | MovingAverage<'ewma'>): metric is MovingAverage<'ewma'> =>
metric.settings?.model === 'ewma';
@ -255,78 +28,6 @@ export const isMovingAverageWithModelSettings = (
): metric is MovingAverage<'ewma'> | MovingAverage<'holt'> | MovingAverage<'holt_winters'> =>
['holt', 'ewma', 'holt_winters'].includes(metric.settings?.model || '');
export interface MovingFunction extends BasePipelineMetricAggregation {
type: 'moving_fn';
settings?: {
window?: string;
script?: InlineScript;
shift?: string;
};
}
export interface Derivative extends BasePipelineMetricAggregation {
type: 'derivative';
settings?: {
unit?: string;
};
}
export interface SerialDiff extends BasePipelineMetricAggregation {
type: 'serial_diff';
settings?: {
lag?: string;
};
}
export interface CumulativeSum extends BasePipelineMetricAggregation {
type: 'cumulative_sum';
settings?: {
format?: string;
};
}
export interface BucketScript extends PipelineMetricAggregationWithMultipleBucketPaths {
type: 'bucket_script';
settings?: {
script?: InlineScript;
};
}
export interface TopMetrics extends BaseMetricAggregation {
type: 'top_metrics';
settings?: {
order?: string;
orderBy?: string;
metrics?: string[];
};
}
type PipelineMetricAggregation = MovingAverage | Derivative | CumulativeSum | BucketScript;
export type MetricAggregationWithSettings =
| BucketScript
| CumulativeSum
| Derivative
| SerialDiff
| RawData
| RawDocument
| UniqueCount
| Percentiles
| ExtendedStats
| Min
| Max
| Sum
| Average
| MovingAverage
| MovingFunction
| Logs
| Rate
| TopMetrics;
export type MetricAggregationWithMeta = ExtendedStats;
export type MetricAggregation = Count | PipelineMetricAggregation | MetricAggregationWithSettings;
// Guards
// Given the structure of the aggregations (ie. `settings` field being always optional) we cannot
// determine types based solely on objects' properties, therefore we use `metricAggregationConfig` as the

View File

@ -5,8 +5,8 @@ import { IconButton } from '../../IconButton';
import { useQuery } from '../ElasticsearchQueryContext';
import { QueryEditorRow } from '../QueryEditorRow';
import { MetricAggregation } from './../../../types';
import { MetricEditor } from './MetricEditor';
import { MetricAggregation } from './aggregations';
import { addMetric, removeMetric, toggleMetricVisibility } from './state/actions';
import { metricAggregationConfig } from './utils';

View File

@ -1,6 +1,6 @@
import { createAction } from '@reduxjs/toolkit';
import { MetricAggregation, MetricAggregationWithMeta, MetricAggregationWithSettings } from '../aggregations';
import { MetricAggregationWithMeta, MetricAggregation, MetricAggregationWithSettings } from '../../../../types';
export const addMetric = createAction<MetricAggregation['id']>('@metrics/add');
export const removeMetric = createAction<MetricAggregation['id']>('@metrics/remove');

View File

@ -3,8 +3,8 @@ import { reducerTester } from 'test/core/redux/reducerTester';
import { ElasticsearchQuery } from 'app/plugins/datasource/elasticsearch/types';
import { defaultMetricAgg } from '../../../../queryDef';
import { Derivative, ExtendedStats, MetricAggregation } from '../../../../types';
import { initQuery } from '../../state';
import { Derivative, ExtendedStats, MetricAggregation } from '../aggregations';
import { metricAggregationConfig } from '../utils';
import {

View File

@ -1,15 +1,10 @@
import { Action } from '@reduxjs/toolkit';
import { defaultMetricAgg } from '../../../../queryDef';
import { ElasticsearchQuery } from '../../../../types';
import { ElasticsearchQuery, MetricAggregation } from '../../../../types';
import { removeEmpty } from '../../../../utils';
import { initQuery } from '../../state';
import {
isMetricAggregationWithMeta,
isMetricAggregationWithSettings,
isPipelineAggregation,
MetricAggregation,
} from '../aggregations';
import { isMetricAggregationWithMeta, isMetricAggregationWithSettings, isPipelineAggregation } from '../aggregations';
import { getChildren, metricAggregationConfig } from '../utils';
import {

View File

@ -1,15 +1,10 @@
import { MetricsConfiguration } from '../../../types';
import { MetricsConfiguration, MetricAggregation, PipelineMetricAggregationType } from '../../../types';
import {
defaultPipelineVariable,
generatePipelineVariableName,
} from './SettingsEditor/BucketScriptSettingsEditor/utils';
import {
isMetricAggregationWithField,
isPipelineAggregationWithMultipleBucketPaths,
MetricAggregation,
PipelineMetricAggregationType,
} from './aggregations';
import { isMetricAggregationWithField, isPipelineAggregationWithMultipleBucketPaths } from './aggregations';
export const metricAggregationConfig: MetricsConfiguration = {
count: {

View File

@ -23,7 +23,7 @@ import (
pfs.GrafanaPlugin
composableKinds: DataQuery: {
maturity: "merged"
maturity: "experimental"
lineage: {
seqs: [
@ -32,10 +32,15 @@ composableKinds: DataQuery: {
{
common.DataQuery
alias?: string
query?: string
// Alias pattern
alias?: string
// Lucene query
query?: string
// Name of time field
timeField?: string
// List of bucket aggregations
bucketAggs?: [...#BucketAggregation]
// List of metric aggregations
metrics?: [...#MetricAggregation]
#BucketAggregation: #DateHistogram | #Histogram | #Terms | #Filters | #GeoHashGrid | #Nested @cuetsy(kind="type")
@ -281,17 +286,11 @@ composableKinds: DataQuery: {
#BasePipelineMetricAggregation: {
#MetricAggregationWithField
//TODO: Type is temporarily commented out as it causes a type error in the generated code. In the meantime, we decided to manually extend the type in types.ts.
//type: #PipelineMetricAggregationType
pipelineAgg?: string
} @cuetsy(kind="interface")
#PipelineMetricAggregationWithMultipleBucketPaths: {
#BaseMetricAggregation
//TODO: Type is temporarily commented out as it causes a type error in the generated code. In the meantime, we decided to manually extend the type in types.ts.
//type: #PipelineMetricAggregationType
pipelineVariables?: [...#PipelineVariable]
} @cuetsy(kind="interface")

View File

@ -233,18 +233,10 @@ export interface Rate extends MetricAggregationWithField {
}
export interface BasePipelineMetricAggregation extends MetricAggregationWithField {
/**
* TODO: Type is temporarily commented out as it causes a type error in the generated code. In the meantime, we decided to manually extend the type in types.ts.
* type: #PipelineMetricAggregationType
*/
pipelineAgg?: string;
}
export interface PipelineMetricAggregationWithMultipleBucketPaths extends BaseMetricAggregation {
/**
* TODO: Type is temporarily commented out as it causes a type error in the generated code. In the meantime, we decided to manually extend the type in types.ts.
* type: #PipelineMetricAggregationType
*/
pipelineVariables?: Array<PipelineVariable>;
}
@ -361,10 +353,25 @@ export type PipelineMetricAggregation = (MovingAverage | Derivative | Cumulative
export type MetricAggregationWithSettings = (BucketScript | CumulativeSum | Derivative | SerialDiff | RawData | RawDocument | UniqueCount | Percentiles | ExtendedStats | Min | Max | Sum | Average | MovingAverage | MovingFunction | Logs | Rate | TopMetrics);
export interface Elasticsearch extends common.DataQuery {
/**
* Alias pattern
*/
alias?: string;
/**
* List of bucket aggregations
*/
bucketAggs?: Array<BucketAggregation>;
/**
* List of metric aggregations
*/
metrics?: Array<MetricAggregation>;
/**
* Lucene query
*/
query?: string;
/**
* Name of time field
*/
timeField?: string;
}

View File

@ -26,10 +26,9 @@ import { TemplateSrv } from 'app/features/templating/template_srv';
import { createFetchResponse } from '../../../../test/helpers/createFetchResponse';
import { Filters } from './components/QueryEditor/BucketAggregationsEditor/aggregations';
import { ElasticDatasource, enhanceDataFrame } from './datasource';
import { createElasticDatasource } from './mocks';
import { ElasticsearchOptions, ElasticsearchQuery } from './types';
import { Filters, ElasticsearchOptions, ElasticsearchQuery } from './types';
const ELASTICSEARCH_MOCK_URL = 'http://elasticsearch.local';

View File

@ -39,20 +39,16 @@ import { IndexPattern } from './IndexPattern';
import LanguageProvider from './LanguageProvider';
import { ElasticQueryBuilder } from './QueryBuilder';
import { ElasticsearchAnnotationsQueryEditor } from './components/QueryEditor/AnnotationQueryEditor';
import {
BucketAggregation,
isBucketAggregationWithField,
} from './components/QueryEditor/BucketAggregationsEditor/aggregations';
import { isBucketAggregationWithField } from './components/QueryEditor/BucketAggregationsEditor/aggregations';
import { bucketAggregationConfig } from './components/QueryEditor/BucketAggregationsEditor/utils';
import {
isMetricAggregationWithField,
isPipelineAggregationWithMultipleBucketPaths,
Logs,
} from './components/QueryEditor/MetricAggregationsEditor/aggregations';
import { metricAggregationConfig } from './components/QueryEditor/MetricAggregationsEditor/utils';
import { defaultBucketAgg, hasMetricOfType } from './queryDef';
import { trackQuery } from './tracking';
import { DataLinkConfig, ElasticsearchOptions, ElasticsearchQuery, TermsQuery } from './types';
import { Logs, BucketAggregation, DataLinkConfig, ElasticsearchOptions, ElasticsearchQuery, TermsQuery } from './types';
import { coerceESVersion, getScriptValue, isSupportedVersion } from './utils';
export const REF_ID_STARTER_LOG_VOLUME = 'log-volume-';

View File

@ -4,12 +4,10 @@ import { from } from 'rxjs';
import { getDefaultTimeRange } from '@grafana/data';
import { BucketAggregationType } from '../components/QueryEditor/BucketAggregationsEditor/aggregations';
import { ElasticsearchProvider } from '../components/QueryEditor/ElasticsearchQueryContext';
import { MetricAggregationType } from '../components/QueryEditor/MetricAggregationsEditor/aggregations';
import { ElasticDatasource } from '../datasource';
import { defaultBucketAgg, defaultMetricAgg } from '../queryDef';
import { ElasticsearchQuery } from '../types';
import { ElasticsearchQuery, MetricAggregationType, BucketAggregationType } from '../types';
import { useFields } from './useFields';

View File

@ -2,15 +2,10 @@ import { lastValueFrom } from 'rxjs';
import { MetricFindValue, SelectableValue } from '@grafana/data';
import {
BucketAggregationType,
isBucketAggregationType,
} from '../components/QueryEditor/BucketAggregationsEditor/aggregations';
import { isBucketAggregationType } from '../components/QueryEditor/BucketAggregationsEditor/aggregations';
import { useDatasource, useRange } from '../components/QueryEditor/ElasticsearchQueryContext';
import {
isMetricAggregationType,
MetricAggregationType,
} from '../components/QueryEditor/MetricAggregationsEditor/aggregations';
import { isMetricAggregationType } from '../components/QueryEditor/MetricAggregationsEditor/aggregations';
import { MetricAggregationType, BucketAggregationType } from '../types';
type AggregationType = BucketAggregationType | MetricAggregationType;

View File

@ -1,8 +1,7 @@
import { useMemo } from 'react';
import { BucketAggregation } from '../components/QueryEditor/BucketAggregationsEditor/aggregations';
import { useQuery } from '../components/QueryEditor/ElasticsearchQueryContext';
import { MetricAggregation } from '../components/QueryEditor/MetricAggregationsEditor/aggregations';
import { MetricAggregation, BucketAggregation } from '../types';
const toId = <T extends { id: unknown }>(e: T): T['id'] => e.id;

View File

@ -1,12 +1,12 @@
import { DateHistogram } from './components/QueryEditor/BucketAggregationsEditor/aggregations';
import { metricAggregationConfig, pipelineOptions } from './components/QueryEditor/MetricAggregationsEditor/utils';
import {
ElasticsearchQuery,
ExtendedStat,
MetricAggregation,
MovingAverageModelOption,
MetricAggregationType,
} from './components/QueryEditor/MetricAggregationsEditor/aggregations';
import { metricAggregationConfig, pipelineOptions } from './components/QueryEditor/MetricAggregationsEditor/utils';
import { ElasticsearchQuery } from './types';
DateHistogram,
} from './types';
export const extendedStats: ExtendedStat[] = [
{ label: 'Avg', value: 'avg' },

View File

@ -13,6 +13,7 @@ import {
MovingAverageSimpleModelSettings,
PipelineMetricAggregationType,
TermsOrder,
ExtendedStats,
BasePipelineMetricAggregation as SchemaBasePipelineMetricAggregation,
PipelineMetricAggregationWithMultipleBucketPaths as SchemaPipelineMetricAggregationWithMultipleBucketPaths,
MovingAverage as SchemaMovingAverage,
@ -27,7 +28,10 @@ import {
export * from './dataquery.gen';
export { Elasticsearch as ElasticsearchQuery } from './dataquery.gen';
export type MetricAggregationWithMeta = ExtendedStats;
// Start of temporary overrides because of incorrect type generation in dataquery.gen.ts
// TODO: Remove this once the type generation is fixed
export interface BasePipelineMetricAggregation extends SchemaBasePipelineMetricAggregation {
type: PipelineMetricAggregationType;
}

View File

@ -1,11 +1,8 @@
import { valid, gte } from 'semver';
import {
isMetricAggregationWithField,
MetricAggregation,
MetricAggregationWithInlineScript,
} from './components/QueryEditor/MetricAggregationsEditor/aggregations';
import { isMetricAggregationWithField } from './components/QueryEditor/MetricAggregationsEditor/aggregations';
import { metricAggregationConfig } from './components/QueryEditor/MetricAggregationsEditor/utils';
import { MetricAggregation, MetricAggregationWithInlineScript } from './types';
export const describeMetric = (metric: MetricAggregation) => {
if (!isMetricAggregationWithField(metric)) {