Skip to content

Commit

Permalink
Requires fields necessary for percent calculation (#29021) (#29068)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv authored Jan 21, 2019
1 parent 80fb111 commit 5de4f1c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 deletions.
6 changes: 5 additions & 1 deletion x-pack/plugins/apm/public/utils/__test__/formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
2 changes: 1 addition & 1 deletion x-pack/plugins/apm/public/utils/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export function asPercent(
denominator: number | undefined,
fallbackResult = ''
) {
if (!denominator) {
if (!denominator || isNaN(numerator)) {
return fallbackResult;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ Array [
},
},
],
"must": Array [
Object {
"exists": Object {
"field": "system.memory.total",
},
},
Object {
"exists": Object {
"field": "system.memory.actual.free",
},
},
],
},
},
"size": 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 3 additions & 1 deletion x-pack/plugins/apm/server/lib/metrics/metricsFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -41,6 +42,7 @@ export async function fetchMetrics<T = void>({
size: 0,
query: {
bool: {
...bool,
filter: filters
}
},
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/apm/server/lib/metrics/query_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface MetricsRequestArgs {
setup: Setup;
timeseriesBucketAggregations?: StringMap<any>;
totalAggregations?: StringMap<any>;
bool?: StringMap<any>;
}

export interface AggValue {
Expand Down

0 comments on commit 5de4f1c

Please sign in to comment.