Skip to content

Commit

Permalink
[8.x] [Data Usage] Remove metrics from autoops response type (elastic…
Browse files Browse the repository at this point in the history
…#200770) (elastic#201292)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Data Usage] Remove metrics from autoops response type
(elastic#200770)](elastic#200770)

<!--- Backport version: 9.4.3 -->

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

<!--BACKPORT [{"author":{"name":"Sandra
G","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-11-21T21:15:46Z","message":"[Data
Usage] Remove metrics from autoops response type (elastic#200770)\n\n##
Summary\r\n\r\n- Removes `metrics` from autoops response type\r\n-
changes the plugin from \"private\" to \"shared\". This was changed
here:\r\nhttps://github.com/elastic/pull/199302, and cause
warnings in our\r\nfunctional tests due to importing types from the
data_usage plugin as\r\nits not allowed for private plugins. Not sure
why it was changed to be\r\nprivate vs shared.\r\n- changes dates in
tests to iso strings\r\n- removes flaky test for checking system indices
(we'll no longer be\r\ngetting system indices soon anyway)\r\n\r\n###
Checklist\r\n\r\nCheck the PR satisfies following conditions.
\r\n\r\nReviewers should verify this PR satisfies this list as
well.\r\n\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] If a plugin
configuration key changed, check if it needs to be\r\nallowlisted in the
cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This was checked for breaking HTTP API changes, and any
breaking\r\nchanges have been approved by the breaking-change committee.
The\r\n`release_note:breaking` label should be applied in these
situations.\r\n- [ ] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests changed\r\n- [ ] The PR description includes
the appropriate Release Notes section,\r\nand the correct
`release_node:*` label is applied per
the\r\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n---------\r\n\r\nCo-authored-by:
Ash
<[email protected]>","sha":"8e1b0bd94a665cbeb2610a2d8dd01d8464973875","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:prev-minor","ci:build-serverless-image"],"title":"[Data
Usage] Remove metrics from autoops response
type","number":200770,"url":"https://github.com/elastic/kibana/pull/200770","mergeCommit":{"message":"[Data
Usage] Remove metrics from autoops response type (elastic#200770)\n\n##
Summary\r\n\r\n- Removes `metrics` from autoops response type\r\n-
changes the plugin from \"private\" to \"shared\". This was changed
here:\r\nhttps://github.com/elastic/pull/199302, and cause
warnings in our\r\nfunctional tests due to importing types from the
data_usage plugin as\r\nits not allowed for private plugins. Not sure
why it was changed to be\r\nprivate vs shared.\r\n- changes dates in
tests to iso strings\r\n- removes flaky test for checking system indices
(we'll no longer be\r\ngetting system indices soon anyway)\r\n\r\n###
Checklist\r\n\r\nCheck the PR satisfies following conditions.
\r\n\r\nReviewers should verify this PR satisfies this list as
well.\r\n\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] If a plugin
configuration key changed, check if it needs to be\r\nallowlisted in the
cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This was checked for breaking HTTP API changes, and any
breaking\r\nchanges have been approved by the breaking-change committee.
The\r\n`release_note:breaking` label should be applied in these
situations.\r\n- [ ] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests changed\r\n- [ ] The PR description includes
the appropriate Release Notes section,\r\nand the correct
`release_node:*` label is applied per
the\r\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n---------\r\n\r\nCo-authored-by:
Ash
<[email protected]>","sha":"8e1b0bd94a665cbeb2610a2d8dd01d8464973875"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/200770","number":200770,"mergeCommit":{"message":"[Data
Usage] Remove metrics from autoops response type (elastic#200770)\n\n##
Summary\r\n\r\n- Removes `metrics` from autoops response type\r\n-
changes the plugin from \"private\" to \"shared\". This was changed
here:\r\nhttps://github.com/elastic/pull/199302, and cause
warnings in our\r\nfunctional tests due to importing types from the
data_usage plugin as\r\nits not allowed for private plugins. Not sure
why it was changed to be\r\nprivate vs shared.\r\n- changes dates in
tests to iso strings\r\n- removes flaky test for checking system indices
(we'll no longer be\r\ngetting system indices soon anyway)\r\n\r\n###
Checklist\r\n\r\nCheck the PR satisfies following conditions.
\r\n\r\nReviewers should verify this PR satisfies this list as
well.\r\n\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] If a plugin
configuration key changed, check if it needs to be\r\nallowlisted in the
cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This was checked for breaking HTTP API changes, and any
breaking\r\nchanges have been approved by the breaking-change committee.
The\r\n`release_note:breaking` label should be applied in these
situations.\r\n- [ ] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests changed\r\n- [ ] The PR description includes
the appropriate Release Notes section,\r\nand the correct
`release_node:*` label is applied per
the\r\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n---------\r\n\r\nCo-authored-by:
Ash
<[email protected]>","sha":"8e1b0bd94a665cbeb2610a2d8dd01d8464973875"}}]}]
BACKPORT-->

Co-authored-by: Sandra G <[email protected]>
  • Loading branch information
kibanamachine and neptunian authored Nov 21, 2024
1 parent 0e4476f commit 8f29268
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 145 deletions.
79 changes: 34 additions & 45 deletions x-pack/plugins/data_usage/common/rest_types/usage_metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,56 +82,45 @@ export type UsageMetricsRequestBody = TypeOf<typeof UsageMetricsRequestSchema>;

export const UsageMetricsResponseSchema = {
body: () =>
schema.object({
metrics: schema.recordOf(
metricTypesSchema,
schema.arrayOf(
schema.object({
name: schema.string(),
error: schema.nullable(schema.string()),
data: schema.arrayOf(
schema.object({
x: schema.number(),
y: schema.number(),
})
),
})
)
),
}),
schema.recordOf(
metricTypesSchema,
schema.arrayOf(
schema.object({
name: schema.string(),
error: schema.nullable(schema.string()),
data: schema.arrayOf(
schema.object({
x: schema.number(),
y: schema.number(),
})
),
})
)
),
};
export type UsageMetricsResponseSchemaBody = Omit<
TypeOf<typeof UsageMetricsResponseSchema.body>,
'metrics'
> & {
metrics: Partial<Record<MetricTypes, MetricSeries[]>>;
};
export type MetricSeries = TypeOf<
typeof UsageMetricsResponseSchema.body
>['metrics'][MetricTypes][number];

export type UsageMetricsResponseSchemaBody = Partial<Record<MetricTypes, MetricSeries[]>>;

export type MetricSeries = TypeOf<typeof UsageMetricsResponseSchema.body>[MetricTypes][number];

export const UsageMetricsAutoOpsResponseSchema = {
body: () =>
schema.object({
metrics: schema.recordOf(
metricTypesSchema,
schema.arrayOf(
schema.object({
name: schema.string(),
error: schema.nullable(schema.string()),
data: schema.arrayOf(schema.arrayOf(schema.number(), { minSize: 2, maxSize: 2 })),
})
)
),
}),
schema.recordOf(
metricTypesSchema,
schema.arrayOf(
schema.object({
name: schema.string(),
error: schema.nullable(schema.string()),
data: schema.arrayOf(schema.arrayOf(schema.number(), { minSize: 2, maxSize: 2 })),
})
)
),
};

export type UsageMetricsAutoOpsResponseMetricSeries = TypeOf<
typeof UsageMetricsAutoOpsResponseSchema.body
>['metrics'][MetricTypes][number];
>[MetricTypes][number];

export type UsageMetricsAutoOpsResponseSchemaBody = Omit<
TypeOf<typeof UsageMetricsAutoOpsResponseSchema.body>,
'metrics'
> & {
metrics: Partial<Record<MetricTypes, UsageMetricsAutoOpsResponseMetricSeries[]>>;
};
export type UsageMetricsAutoOpsResponseSchemaBody = Partial<
Record<MetricTypes, UsageMetricsAutoOpsResponseMetricSeries[]>
>;
2 changes: 1 addition & 1 deletion x-pack/plugins/data_usage/public/app/components/charts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const Charts: React.FC<ChartsProps> = ({ data, 'data-test-subj': dataTest

return (
<EuiFlexGroup direction="column" data-test-subj={getTestId('charts')}>
{Object.entries(data.metrics).map(([metricType, series], idx) => (
{Object.entries(data).map(([metricType, series], idx) => (
<ChartPanel
key={metricType}
metricType={metricType as MetricTypes}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,28 +268,26 @@ describe('DataUsageMetrics', () => {
...getBaseMockedDataUsageMetrics,
isFetched: true,
data: {
metrics: {
ingest_rate: [
{
name: '.ds-1',
data: [{ x: new Date(), y: 1000 }],
},
{
name: '.ds-10',
data: [{ x: new Date(), y: 1100 }],
},
],
storage_retained: [
{
name: '.ds-2',
data: [{ x: new Date(), y: 2000 }],
},
{
name: '.ds-20',
data: [{ x: new Date(), y: 2100 }],
},
],
},
ingest_rate: [
{
name: '.ds-1',
data: [{ x: new Date(), y: 1000 }],
},
{
name: '.ds-10',
data: [{ x: new Date(), y: 1100 }],
},
],
storage_retained: [
{
name: '.ds-2',
data: [{ x: new Date(), y: 2000 }],
},
{
name: '.ds-20',
data: [{ x: new Date(), y: 2100 }],
},
],
},
});
const { getByTestId } = render(<DataUsageMetrics data-test-subj={testId} />);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export const DataUsageMetrics = memo(
</FlexItemWithCss>

<FlexItemWithCss>
{isFetched && data?.metrics ? (
{isFetched && data ? (
<Charts data={data} data-test-subj={dataTestSubj} />
) : isFetching ? (
<EuiLoadingElastic data-test-subj={getTestId('charts-loading')} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,16 @@ export const getUsageMetricsHandler = (
export function transformMetricsData(
data: UsageMetricsAutoOpsResponseSchemaBody
): UsageMetricsResponseSchemaBody {
return {
metrics: Object.fromEntries(
Object.entries(data.metrics).map(([metricType, series]) => [
metricType,
series.map((metricSeries) => ({
name: metricSeries.name,
data: (metricSeries.data as Array<[number, number]>).map(([timestamp, value]) => ({
x: timestamp,
y: value,
})),
return Object.fromEntries(
Object.entries(data).map(([metricType, series]) => [
metricType,
series.map((metricSeries) => ({
name: metricSeries.name,
data: (metricSeries.data as Array<[number, number]>).map(([timestamp, value]) => ({
x: timestamp,
y: value,
})),
])
),
};
})),
])
) as UsageMetricsResponseSchemaBody;
}
6 changes: 1 addition & 5 deletions x-pack/plugins/data_usage/server/services/autoops_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,7 @@ export class AutoOpsAPIService {
}
);

const validatedResponse = response.data.metrics
? UsageMetricsAutoOpsResponseSchema.body().validate(response.data)
: UsageMetricsAutoOpsResponseSchema.body().validate({
metrics: response.data,
});
const validatedResponse = UsageMetricsAutoOpsResponseSchema.body().validate(response.data);

this.logger.debug(`[AutoOps API] Successfully created an autoops agent ${response}`);
return validatedResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,40 @@
*/

export const mockAutoOpsResponse = {
metrics: {
ingest_rate: [
{
name: 'metrics-system.cpu-default',
error: null,
data: [
[1726858530000, 13756849],
[1726862130000, 14657904],
],
},
{
name: 'logs-nginx.access-default',
error: null,
data: [
[1726858530000, 12894623],
[1726862130000, 14436905],
],
},
],
storage_retained: [
{
name: 'metrics-system.cpu-default',
error: null,
data: [
[1726858530000, 12576413],
[1726862130000, 13956423],
],
},
{
name: 'logs-nginx.access-default',
error: null,
data: [
[1726858530000, 12894623],
[1726862130000, 14436905],
],
},
],
},
ingest_rate: [
{
name: 'metrics-system.cpu-default',
error: null,
data: [
[1726858530000, 13756849],
[1726862130000, 14657904],
],
},
{
name: 'logs-nginx.access-default',
error: null,
data: [
[1726858530000, 12894623],
[1726862130000, 14436905],
],
},
],
storage_retained: [
{
name: 'metrics-system.cpu-default',
error: null,
data: [
[1726858530000, 12576413],
[1726862130000, 13956423],
],
},
{
name: 'logs-nginx.access-default',
error: null,
data: [
[1726858530000, 12894623],
[1726862130000, 14436905],
],
},
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/

import expect from '@kbn/expect';
import { SupertestWithRoleScope } from '@kbn/test-suites-xpack/api_integration/deployment_agnostic/services/role_scoped_supertest';
import { DataStreamsResponseBodySchemaBody } from '@kbn/data-usage-plugin/common/rest_types';
import { DATA_USAGE_DATA_STREAMS_API_ROUTE } from '@kbn/data-usage-plugin/common';
import { SupertestWithRoleScope } from '@kbn/test-suites-xpack/api_integration/deployment_agnostic/services/role_scoped_supertest';
import { FtrProviderContext } from '../../../../ftr_provider_context';

export default function ({ getService }: FtrProviderContext) {
Expand Down Expand Up @@ -43,14 +43,5 @@ export default function ({ getService }: FtrProviderContext) {
expect(foundStream?.storageSizeBytes).to.be(0);
expect(res.statusCode).to.be(200);
});
it('returns system indices', async () => {
const res = await supertestAdminWithCookieCredentials
.get(DATA_USAGE_DATA_STREAMS_API_ROUTE)
.set('elastic-api-version', '1');
const dataStreams: DataStreamsResponseBodySchemaBody = res.body;
const systemDataStreams = dataStreams.filter((stream) => stream.name.startsWith('.'));
expect(systemDataStreams.length).to.be.greaterThan(0);
expect(res.statusCode).to.be(200);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import { FtrProviderContext } from '../../../../ftr_provider_context';
import { setupMockServer } from '../mock_api';
import { mockAutoOpsResponse } from '../mock_data';

const now = new Date();
const to = now.toISOString(); // Current time in ISO format

const nowMinus24Hours = new Date(now.getTime() - 24 * 60 * 60 * 1000);
const from = nowMinus24Hours.toISOString();

export default function ({ getService }: FtrProviderContext) {
const svlDatastreamsHelpers = getService('svlDatastreamsHelpers');
const roleScopedSupertest = getService('roleScopedSupertest');
Expand Down Expand Up @@ -46,8 +52,8 @@ export default function ({ getService }: FtrProviderContext) {
after(async () => await svlDatastreamsHelpers.deleteDataStream(testDataStreamName));
it('returns 400 with non-existent data streams', async () => {
const requestBody: UsageMetricsRequestBody = {
from: 'now-24h/h',
to: 'now',
from,
to,
metricTypes: ['ingest_rate', 'storage_retained'],
dataStreams: ['invalid-data-stream'],
};
Expand All @@ -61,8 +67,8 @@ export default function ({ getService }: FtrProviderContext) {

it('returns 400 when requesting no data streams', async () => {
const requestBody = {
from: 'now-24h/h',
to: 'now',
from,
to,
metricTypes: ['ingest_rate'],
dataStreams: [],
};
Expand All @@ -76,8 +82,8 @@ export default function ({ getService }: FtrProviderContext) {

it('returns 400 when requesting an invalid metric type', async () => {
const requestBody = {
from: 'now-24h/h',
to: 'now',
from,
to,
metricTypes: [testDataStreamName],
dataStreams: ['datastream'],
};
Expand All @@ -93,8 +99,8 @@ export default function ({ getService }: FtrProviderContext) {

it('returns 200 with valid request', async () => {
const requestBody: UsageMetricsRequestBody = {
from: 'now-24h/h',
to: 'now',
from,
to,
metricTypes: ['ingest_rate', 'storage_retained'],
dataStreams: [testDataStreamName],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
await pageObjects.svlManagementPage.clickDataUsageManagementCard();
});

after(async () => {});

it('renders data usage page', async () => {
await retry.waitFor('page to be visible', async () => {
return await testSubjects.exists('DataUsagePage');
Expand Down

0 comments on commit 8f29268

Please sign in to comment.