Skip to content

Commit

Permalink
Some fixes and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
VladLasitsa committed Aug 17, 2022
1 parent 7f6959e commit 5405998
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,14 @@
* Side Public License, v 1.
*/

import { utc } from 'moment';
import { search } from '@kbn/data-plugin/public';
import dateMath from '@kbn/datemath';
import { VisualizeEditorLayersContext } from '@kbn/visualizations-plugin/public';
import { TimeRange, UI_SETTINGS } from '@kbn/data-plugin/common';
import { PaletteOutput } from '@kbn/coloring';
import { SUPPORTED_FORMATTERS } from '../formatters';
import { convertSplitFilters } from '../split_chart';
import { convertMetrics, convertFilter } from '../metrics';
import type { Panel, Series } from '../../../../common/types';
import { PANEL_TYPES, TIME_RANGE_DATA_MODES } from '../../../../common/enums';
import { getUISettings } from '../../../services';
import { VisSeries } from '../series';

function getWindow(interval?: string, timeRange?: TimeRange) {
let window = interval || '1h';

if (timeRange && !interval) {
const { from, to } = timeRange;
const timerange = utc(to).valueOf() - utc(from).valueOf();
const maxBars = getUISettings().get<number>(UI_SETTINGS.HISTOGRAM_BAR_TARGET);

const duration = search.aggs.calcAutoIntervalLessThan(maxBars, timerange);
const unit =
dateMath.units.find((u) => {
const value = duration.as(u);
return Number.isInteger(value);
}) || 'ms';

window = `${duration.as(unit)}${unit}`;
}

return window;
}

function getTermParams(layer: Series) {
return {
size: layer.terms_size ?? 10,
Expand Down Expand Up @@ -71,22 +44,14 @@ export const getLayerConfiguration = (
xFieldName?: string,
xMode?: string,
splitWithDateHistogram?: boolean,
timeRange?: TimeRange
window?: string
): VisualizeEditorLayersContext => {
const layer = model.series[layerIdx];
const palette = layer.palette as PaletteOutput;
const splitFilters = convertSplitFilters(layer);
const { metrics: metricsArray, seriesAgg } = series;
const filter = convertFilter(layer);
const metrics = convertMetrics(
layer,
metricsArray,
filter,
model.time_range_mode === TIME_RANGE_DATA_MODES.LAST_VALUE &&
model.type !== PANEL_TYPES.TIMESERIES
? getWindow(model.interval, timeRange)
: undefined
);
const metrics = convertMetrics(layer, metricsArray, filter, window);
return {
indexPatternId,
xFieldName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@
*/

import type { Query } from '@kbn/es-query';
import { addTimeRangeToFormula } from '.';
import type { Metric } from '../../../../common/types';
import { SUPPORTED_METRICS } from './supported_metrics';

const escapeQuotes = (str: string) => {
return str?.replace(/'/g, "\\'");
};

const constructFilterRationFormula = (operation: string, metric?: Query) => {
const constructFilterRationFormula = (operation: string, metric?: Query, window?: string) => {
return `${operation}${metric?.language === 'lucene' ? 'lucene' : 'kql'}='${
metric?.query && typeof metric?.query === 'string'
? escapeQuotes(metric?.query)
: metric?.query ?? '*'
}')`;
}'${addTimeRangeToFormula(window)})`;
};

export const getFilterRatioFormula = (currentMetric: Metric) => {
export const getFilterRatioFormula = (currentMetric: Metric, window?: string) => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const { numerator, denominator, metric_agg, field } = currentMetric;
let aggregation = SUPPORTED_METRICS.count;
Expand All @@ -38,16 +39,18 @@ export const getFilterRatioFormula = (currentMetric: Metric) => {
if (aggregation.name === 'counter_rate') {
const numeratorFormula = constructFilterRationFormula(
`${aggregation.name}(max('${field}',`,
numerator
numerator,
window
);
const denominatorFormula = constructFilterRationFormula(
`${aggregation.name}(max('${field}',`,
denominator
denominator,
window
);
return `${numeratorFormula}) / ${denominatorFormula})`;
} else {
const numeratorFormula = constructFilterRationFormula(operation, numerator);
const denominatorFormula = constructFilterRationFormula(operation, denominator);
const numeratorFormula = constructFilterRationFormula(operation, numerator, window);
const denominatorFormula = constructFilterRationFormula(operation, denominator, window);
return `${numeratorFormula} / ${denominatorFormula}`;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * from './parent_pipeline_formula';
export * from './sibling_pipeline_formula';
export * from './filter_ratio_formula';
export * from './parent_pipeline_series';
export * from './validate_metrics';
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
* Side Public License, v 1.
*/

import { utc } from 'moment';
import { search } from '@kbn/data-plugin/public';
import dateMath from '@kbn/datemath';
import { TimeRange, UI_SETTINGS } from '@kbn/data-plugin/common';
import { getUISettings } from '../../../services';
import type { Metric } from '../../../../common/types';
import { SUPPORTED_METRICS } from './supported_metrics';
import { getFilterRatioFormula } from './filter_ratio_formula';
Expand Down Expand Up @@ -47,6 +52,27 @@ export const getPercentileRankSeries = (
});
};

export const getWindow = (interval?: string, timeRange?: TimeRange) => {
let window = interval || '1h';

if (timeRange && !interval) {
const { from, to } = timeRange;
const timerange = utc(to).valueOf() - utc(from).valueOf();
const maxBars = getUISettings().get<number>(UI_SETTINGS.HISTOGRAM_BAR_TARGET);

const duration = search.aggs.calcAutoIntervalLessThan(maxBars, timerange);
const unit =
dateMath.units.find((u) => {
const value = duration.as(u);
return Number.isInteger(value);
}) || 'ms';

window = `${duration.as(unit)}${unit}`;
}

return window;
};

export const getTimeScale = (metric: Metric) => {
const supportedTimeScales = ['1s', '1m', '1h', '1d'];
let timeScale;
Expand All @@ -67,6 +93,10 @@ export const getFormulaSeries = (script: string) => {
];
};

export const addTimeRangeToFormula = (window?: string) => {
return window ? `, timeRange='${window}'` : '';
};

export const getPipelineAgg = (subFunctionMetric: Metric) => {
const pipelineAggMap = SUPPORTED_METRICS[subFunctionMetric.type];
if (!pipelineAggMap) {
Expand All @@ -78,7 +108,8 @@ export const getPipelineAgg = (subFunctionMetric: Metric) => {
export const getFormulaEquivalent = (
currentMetric: Metric,
metrics: Metric[],
metaValue?: number
metaValue?: number,
window?: string
) => {
const aggregation = SUPPORTED_METRICS[currentMetric.type]?.name;
switch (currentMetric.type) {
Expand All @@ -87,18 +118,20 @@ export const getFormulaEquivalent = (
case 'min_bucket':
case 'sum_bucket':
case 'positive_only': {
return getSiblingPipelineSeriesFormula(currentMetric.type, currentMetric, metrics);
return getSiblingPipelineSeriesFormula(currentMetric.type, currentMetric, metrics, window);
}
case 'count': {
return `${aggregation}()`;
}
case 'percentile': {
return `${aggregation}(${currentMetric.field}${
metaValue ? `, percentile=${metaValue}` : ''
})`;
}${addTimeRangeToFormula(window)})`;
}
case 'percentile_rank': {
return `${aggregation}(${currentMetric.field}${metaValue ? `, value=${metaValue}` : ''})`;
return `${aggregation}(${currentMetric.field}${
metaValue ? `, value=${metaValue}` : ''
}${addTimeRangeToFormula(window)})`;
}
case 'cumulative_sum':
case 'derivative':
Expand All @@ -117,33 +150,34 @@ export const getFormulaEquivalent = (
subFunctionMetric,
pipelineAgg,
currentMetric.type,
metaValue
metaValue,
window
);
}
case 'positive_rate': {
return `${aggregation}(max(${currentMetric.field}))`;
return `${aggregation}(max(${currentMetric.field}${addTimeRangeToFormula(window)}))`;
}
case 'filter_ratio': {
return getFilterRatioFormula(currentMetric);
return getFilterRatioFormula(currentMetric, window);
}
case 'static': {
return `${currentMetric.value}`;
}
case 'std_deviation': {
if (currentMetric.mode === 'lower') {
return `average(${currentMetric.field}) - ${currentMetric.sigma || 1.5} * ${aggregation}(${
currentMetric.field
})`;
return `average(${currentMetric.field}${addTimeRangeToFormula(window)}) - ${
currentMetric.sigma || 1.5
} * ${aggregation}(${currentMetric.field}${addTimeRangeToFormula(window)})`;
}
if (currentMetric.mode === 'upper') {
return `average(${currentMetric.field}) + ${currentMetric.sigma || 1.5} * ${aggregation}(${
currentMetric.field
})`;
return `average(${currentMetric.field}${addTimeRangeToFormula(window)}) + ${
currentMetric.sigma || 1.5
} * ${aggregation}(${currentMetric.field}${addTimeRangeToFormula(window)})`;
}
return `${aggregation}(${currentMetric.field})`;
}
default: {
return `${aggregation}(${currentMetric.field})`;
return `${aggregation}(${currentMetric.field}${addTimeRangeToFormula(window)})`;
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export const getParentPipelineSeriesFormula = (
subFunctionMetric: Metric,
pipelineAgg: string,
aggregation: MetricType,
percentileValue?: number
percentileValue?: number,
window?: string
) => {
let formula = '';
const aggregationMap = SUPPORTED_METRICS[aggregation];
Expand All @@ -42,7 +43,7 @@ export const getParentPipelineSeriesFormula = (
additionalSubFunction.field ?? ''
}${additionalFunctionArgs ?? ''})))`;
} else {
const subFormula = getFormulaEquivalent(subFunctionMetric, metrics, percentileValue);
const subFormula = getFormulaEquivalent(subFunctionMetric, metrics, percentileValue, window);

if (!subFormula) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ export const computeParentSeries = (
currentMetric: Metric,
subFunctionMetric: Metric,
pipelineAgg: string,
meta?: number
meta?: number,
window?: string
) => {
const aggregationMap = SUPPORTED_METRICS[aggregation];
if (subFunctionMetric.type === 'filter_ratio') {
const script = getFilterRatioFormula(subFunctionMetric);
const script = getFilterRatioFormula(subFunctionMetric, window);
if (!script) {
return null;
}
Expand Down Expand Up @@ -49,7 +50,8 @@ export const computeParentSeries = (
export const getParentPipelineSeries = (
aggregation: MetricType,
currentMetricIdx: number,
metrics: Metric[]
metrics: Metric[],
window?: string
) => {
const currentMetric = metrics[currentMetricIdx];
// percentile value is derived from the field Id. It has the format xxx-xxx-xxx-xxx[percentile]
Expand All @@ -73,7 +75,8 @@ export const getParentPipelineSeries = (
subFunctionMetric,
pipelineAgg,
aggregation,
metaValue
metaValue,
window
);
if (!formula) {
return null;
Expand All @@ -85,7 +88,8 @@ export const getParentPipelineSeries = (
currentMetric,
subFunctionMetric,
pipelineAgg,
metaValue
metaValue,
window
);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { SUPPORTED_METRICS } from './supported_metrics';
export const getSiblingPipelineSeriesFormula = (
aggregation: MetricType,
currentMetric: Metric,
metrics: Metric[]
metrics: Metric[],
window?: string
) => {
const [nestedFieldId, nestedMeta] = currentMetric.field?.split('[') ?? [];
const subFunctionMetric = metrics.find((metric) => metric.id === nestedFieldId);
Expand Down Expand Up @@ -46,7 +47,7 @@ export const getSiblingPipelineSeriesFormula = (
} else {
const nestedMetaValue = Number(nestedMeta?.replace(']', ''));

const subFormula = getFormulaEquivalent(subFunctionMetric, metrics, nestedMetaValue);
const subFormula = getFormulaEquivalent(subFunctionMetric, metrics, nestedMetaValue, window);

if (!subFormula) {
return null;
Expand Down
Loading

0 comments on commit 5405998

Please sign in to comment.