Skip to content

Commit

Permalink
[8.7] [Infrastructure UI] Filter out null bucket items from average c…
Browse files Browse the repository at this point in the history
…alculation (#152333) (#152461)

# Backport

This will backport the following commits from `main` to `8.7`:
- [[Infrastructure UI] Filter out null bucket items from average
calculation (#152333)](#152333)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Carlos
Crespo","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-03-01T14:31:35Z","message":"[Infrastructure
UI] Filter out null bucket items from average calculation
(#152333)\n\n## Summary\r\n\r\nCloses
[#152328](https://github.com/elastic/kibana/issues/152328)\r\n\r\nThis
PR fixes the average calculation in the Snapshot API, filtering
out\r\nbuckets with null values from it, which are more likely to appear
with\r\nqueries that use small data ranges.\r\n\r\nThe results after
this change are equal to what Elasticsearch would\r\ncalculate in the
avg aggregation\r\n\r\n\r\n### How to test \r\n\r\n- Make sure you have
metrics data (either through enabling the system\r\nmodule in metricbeat
or connecting your local kibana to an oblt-cli\r\ncluster)\r\n- Navigate
to `Infrastructure` > `Hosts`\r\n- Filter the results to see a single
host\r\n- Change the data range filter and compare the KPIs against the
table.\r\n- Validate other pages that use the Snapshot API (Inventory UI
and\r\nMetrics UI to see if the results there are still
correct\r\n\r\n---------\r\n\r\nCo-authored-by: Kibana Machine
<[email protected]>","sha":"f6a0b886b143dea6b3b6f8101e21be5c1816558a","branchLabelMapping":{"^v8.8.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Metrics
UI","Team:Infra Monitoring
UI","release_note:skip","backport:prev-minor","v8.7.0","Feature:ObsHosts","v8.8.0"],"number":152333,"url":"https://github.com/elastic/kibana/pull/152333","mergeCommit":{"message":"[Infrastructure
UI] Filter out null bucket items from average calculation
(#152333)\n\n## Summary\r\n\r\nCloses
[#152328](https://github.com/elastic/kibana/issues/152328)\r\n\r\nThis
PR fixes the average calculation in the Snapshot API, filtering
out\r\nbuckets with null values from it, which are more likely to appear
with\r\nqueries that use small data ranges.\r\n\r\nThe results after
this change are equal to what Elasticsearch would\r\ncalculate in the
avg aggregation\r\n\r\n\r\n### How to test \r\n\r\n- Make sure you have
metrics data (either through enabling the system\r\nmodule in metricbeat
or connecting your local kibana to an oblt-cli\r\ncluster)\r\n- Navigate
to `Infrastructure` > `Hosts`\r\n- Filter the results to see a single
host\r\n- Change the data range filter and compare the KPIs against the
table.\r\n- Validate other pages that use the Snapshot API (Inventory UI
and\r\nMetrics UI to see if the results there are still
correct\r\n\r\n---------\r\n\r\nCo-authored-by: Kibana Machine
<[email protected]>","sha":"f6a0b886b143dea6b3b6f8101e21be5c1816558a"}},"sourceBranch":"main","suggestedTargetBranches":["8.7"],"targetPullRequestStates":[{"branch":"8.7","label":"v8.7.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.8.0","labelRegex":"^v8.8.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/152333","number":152333,"mergeCommit":{"message":"[Infrastructure
UI] Filter out null bucket items from average calculation
(#152333)\n\n## Summary\r\n\r\nCloses
[#152328](https://github.com/elastic/kibana/issues/152328)\r\n\r\nThis
PR fixes the average calculation in the Snapshot API, filtering
out\r\nbuckets with null values from it, which are more likely to appear
with\r\nqueries that use small data ranges.\r\n\r\nThe results after
this change are equal to what Elasticsearch would\r\ncalculate in the
avg aggregation\r\n\r\n\r\n### How to test \r\n\r\n- Make sure you have
metrics data (either through enabling the system\r\nmodule in metricbeat
or connecting your local kibana to an oblt-cli\r\ncluster)\r\n- Navigate
to `Infrastructure` > `Hosts`\r\n- Filter the results to see a single
host\r\n- Change the data range filter and compare the KPIs against the
table.\r\n- Validate other pages that use the Snapshot API (Inventory UI
and\r\nMetrics UI to see if the results there are still
correct\r\n\r\n---------\r\n\r\nCo-authored-by: Kibana Machine
<[email protected]>","sha":"f6a0b886b143dea6b3b6f8101e21be5c1816558a"}}]}]
BACKPORT-->

Co-authored-by: Carlos Crespo <[email protected]>
  • Loading branch information
kibanamachine and crespocarlos authored Mar 1, 2023
1 parent 932f1b8 commit 1d25c4f
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/infra/common/http_api/snapshot_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export const SnapshotRequestRT = rt.intersection([
region: rt.string,
filterQuery: rt.union([rt.string, rt.null]),
overrideCompositeSize: rt.number,
dropPartialBuckets: rt.boolean,
}),
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const Tile = ({ type, ...props }: Props) => {
metrics: [{ type }],
groupBy: null,
includeTimeseries: true,
dropPartialBuckets: false,
});

return <KPIChart id={`$metric-${type}`} type={type} nodes={nodes} loading={loading} {...props} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function useSnapshot({
groupBy = null,
sendRequestImmediately = true,
includeTimeseries = true,
dropPartialBuckets = true,
requestTs,
...args
}: UseSnapshotRequest) {
Expand All @@ -56,6 +57,7 @@ export function useSnapshot({
lookbackSize: 5,
},
includeTimeseries,
dropPartialBuckets,
};

const { error, loading, response, makeRequest } = useHTTPRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ import { applyMetadataToLastPath } from './apply_metadata_to_last_path';
const getMetricValue = (row: MetricsAPIRow) => {
if (!isNumber(row.metric_0)) return null;
const value = row.metric_0;
return isFinite(value) ? value : null;
return Number.isFinite(value) ? value : null;
};

const calculateMax = (rows: MetricsAPIRow[]) => {
return max(rows.map(getMetricValue)) || 0;
};

const calculateAvg = (rows: MetricsAPIRow[]): number => {
return sum(rows.map(getMetricValue)) / rows.length || 0;
const values = rows.map(getMetricValue).filter(Number.isFinite);
return sum(values) / Math.max(values.length, 1);
};

const getLastValue = (rows: MetricsAPIRow[]) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const transformRequestToMetricsAPIRequest = async ({
? snapshotRequest.overrideCompositeSize
: compositeSize,
alignDataToEnd: true,
dropPartialBuckets: true,
dropPartialBuckets: snapshotRequest.dropPartialBuckets ?? true,
includeTimeseries: snapshotRequest.includeTimeseries,
};

Expand Down
2 changes: 1 addition & 1 deletion x-pack/test/api_integration/apis/metrics_ui/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export default function ({ getService }: FtrProviderContext) {
name: 'cpu',
value: null,
max: 0.47105555555555556,
avg: 0.0672936507936508,
avg: 0.47105555555555556,
};

expect(snapshot).to.have.property('nodes');
Expand Down

0 comments on commit 1d25c4f

Please sign in to comment.