Skip to content

Commit

Permalink
Normalize values
Browse files Browse the repository at this point in the history
The graphs need to show the log rate per minute instead of the log
count. This commit adds two helper functions to normalize those values.
  • Loading branch information
Alejandro Fernández Gómez committed Jul 3, 2020
1 parent 865942e commit 929afde
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions x-pack/plugins/infra/public/utils/logs_overview_fetchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,14 @@ export function getLogsOverviewDataFetcher(
data
);

const timeSpanInMinutes =
(Date.parse(params.endTime).valueOf() - Date.parse(params.startTime).valueOf()) / (1000 * 60);

return {
title: 'Log rate',
appLink: 'TBD', // TODO: what format should this be in, relative I assume?
stats,
series,
stats: normalizeStats(stats, timeSpanInMinutes),
series: normalizeSeries(series),
};
};
}
Expand Down Expand Up @@ -179,3 +182,36 @@ function processLogsOverviewAggregations(aggregations: {
series: processedSeries,
};
}

function normalizeStats(
stats: LogsFetchDataResponse['stats'],
timeSpanInMinutes: number
): LogsFetchDataResponse['stats'] {
return Object.keys(stats).reduce<LogsFetchDataResponse['stats']>((normalized, key) => {
normalized[key] = {
...stats[key],
value: stats[key].value / timeSpanInMinutes,
};
return normalized;
}, {});
}

function normalizeSeries(series: LogsFetchDataResponse['series']): LogsFetchDataResponse['series'] {
const seriesKeys = Object.keys(series);
const timestamps = seriesKeys.flatMap((key) => series[key].coordinates.map((c) => c.x));
const [first, second] = [...new Set(timestamps)].sort();
const timeSpanInMinutes = (second - first) / (1000 * 60);

return seriesKeys.reduce<LogsFetchDataResponse['series']>((normalized, key) => {
normalized[key] = {
...series[key],
coordinates: series[key].coordinates.map((c) => {
if (c.y) {
return { ...c, y: c.y / timeSpanInMinutes };
}
return c;
}),
};
return normalized;
}, {});
}

0 comments on commit 929afde

Please sign in to comment.