Requires fields necessary for percent calculation (#29021) (#29068)

This commit is contained in:
Søren Louv-Jansen 2019-01-21 19:31:39 +01:00 committed by GitHub
parent 80fb111ef7
commit 5de4f1c58f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 3 deletions

View file

@ -43,7 +43,11 @@ describe('formatters', () => {
it('should return fallback when denominator is 0 ', () => {
expect(asPercent(3725, 0, 'n/a')).toEqual('n/a');
expect(asPercent(3725, 0)).toEqual('');
});
it('should return fallback when numerator or denominator is NaN', () => {
expect(asPercent(3725, NaN, 'n/a')).toEqual('n/a');
expect(asPercent(NaN, 10000, 'n/a')).toEqual('n/a');
});
});
});

View file

@ -139,7 +139,7 @@ export function asPercent(
denominator: number | undefined,
fallbackResult = ''
) {
if (!denominator) {
if (!denominator || isNaN(numerator)) {
return fallbackResult;
}

View file

@ -81,6 +81,18 @@ Array [
},
},
],
"must": Array [
Object {
"exists": Object {
"field": "system.memory.total",
},
},
Object {
"exists": Object {
"field": "system.memory.actual.free",
},
},
],
},
},
"size": 0,

View file

@ -36,6 +36,20 @@ const percentSystemMemoryUsedScript = {
export async function fetch(args: MetricsRequestArgs) {
return fetchMetrics<Aggs>({
...args,
bool: {
must: [
{
exists: {
field: METRIC_SYSTEM_TOTAL_MEMORY
}
},
{
exists: {
field: METRIC_SYSTEM_FREE_MEMORY
}
}
]
},
timeseriesBucketAggregations: {
averagePercentMemoryUsed: { avg: percentSystemMemoryUsedScript },
maximumPercentMemoryUsed: { max: percentSystemMemoryUsedScript }

View file

@ -12,7 +12,8 @@ export async function fetchMetrics<T = void>({
serviceName,
setup,
timeseriesBucketAggregations = {},
totalAggregations = {}
totalAggregations = {},
bool = {}
}: MetricsRequestArgs) {
const { start, end, esFilterQuery, client, config } = setup;
const { intervalString } = getBucketSize(start, end, 'auto');
@ -41,6 +42,7 @@ export async function fetchMetrics<T = void>({
size: 0,
query: {
bool: {
...bool,
filter: filters
}
},

View file

@ -11,6 +11,7 @@ export interface MetricsRequestArgs {
setup: Setup;
timeseriesBucketAggregations?: StringMap<any>;
totalAggregations?: StringMap<any>;
bool?: StringMap<any>;
}
export interface AggValue {