Skip to content

Commit

Permalink
Fix rate aggregation and offset
Browse files Browse the repository at this point in the history
  • Loading branch information
Zacqary committed May 13, 2021
1 parent a16b364 commit 390bf91
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface Aggregation {
aggregatedValue: { value: number; values?: Array<{ key: number; value: number }> };
doc_count: number;
to_as_string: string;
key_as_string: string;
}>;
};
}
Expand Down Expand Up @@ -60,6 +61,7 @@ export const evaluateAlert = <Params extends EvaluatedAlertParams = EvaluatedAle
filterQuery,
timeframe
);

const { threshold, warningThreshold, comparator, warningComparator } = criterion;
const pointsEvaluator = (points: any[] | typeof NaN | null, t?: number[], c?: Comparator) => {
if (!t || !c) return [false];
Expand Down Expand Up @@ -179,7 +181,10 @@ const getValuesFromAggregations = (
const { buckets } = aggregations.aggregatedIntervals;
if (!buckets.length) return null; // No Data state
if (aggType === Aggregators.COUNT) {
return buckets.map((bucket) => ({ key: bucket.to_as_string, value: bucket.doc_count }));
return buckets.map((bucket) => ({
key: bucket.to_as_string,
value: bucket.doc_count,
}));
}
if (aggType === Aggregators.P95 || aggType === Aggregators.P99) {
return buckets.map((bucket) => {
Expand All @@ -190,7 +195,7 @@ const getValuesFromAggregations = (
});
}
return buckets.map((bucket) => ({
key: bucket.to_as_string,
key: bucket.key_as_string ?? bucket.to_as_string,
value: bucket.aggregatedValue?.value ?? null,
}));
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { MetricExpressionParams, Aggregators } from '../types';
import { getIntervalInSeconds } from '../../../../utils/get_interval_in_seconds';
import { roundTimestamp } from '../../../../utils/round_timestamp';
import { createPercentileAggregation } from './create_percentile_aggregation';
import { calculateDateHistogramOffset } from '../../../metrics/lib/calculate_date_histogram_offset';

const MINIMUM_BUCKETS = 5;
const COMPOSITE_RESULTS_PER_PAGE = 100;
Expand Down Expand Up @@ -48,6 +49,9 @@ export const getElasticsearchMetricQuery = (
timeUnit
);

const offset = calculateDateHistogramOffset({ from, to, interval, field: timefield });
const offsetInMS = parseInt(offset, 10) * 1000;

const aggregations =
aggType === Aggregators.COUNT
? {}
Expand All @@ -63,18 +67,34 @@ export const getElasticsearchMetricQuery = (
},
};

const baseAggs = {
aggregatedIntervals: {
date_range: {
field: timefield,
ranges: Array.from(Array(Math.floor((to - from) / intervalAsMS)), (_, i) => ({
from: from + intervalAsMS * i,
to: from + intervalAsMS * (i + 1),
})),
},
aggregations,
},
};
const baseAggs =
aggType === Aggregators.RATE
? {
aggregatedIntervals: {
date_histogram: {
field: timefield,
fixed_interval: interval,
offset,
extended_bounds: {
min: from,
max: to,
},
},
aggregations,
},
}
: {
aggregatedIntervals: {
date_range: {
field: timefield,
ranges: Array.from(Array(Math.floor((to - from) / intervalAsMS)), (_, i) => ({
from: from + intervalAsMS * i + offsetInMS,
to: from + intervalAsMS * (i + 1) + offsetInMS,
})),
},
aggregations,
},
};

const aggs = groupBy
? {
Expand Down

0 comments on commit 390bf91

Please sign in to comment.