Skip to content

Commit

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

- Removes `metrics` from autoops response type
- changes the plugin from "private" to "shared". This was changed here:
elastic#199302, and cause warnings in our
functional tests due to importing types from the data_usage plugin as
its not allowed for private plugins. Not sure why it was changed to be
private vs shared.
- changes dates in tests to iso strings
- removes flaky test for checking system indices (we'll no longer be
getting system indices soon anyway)

### Checklist

Check the PR satisfies following conditions. 

Reviewers should verify this PR satisfies this list as well.

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This was checked for breaking HTTP API changes, and any breaking
changes have been approved by the breaking-change committee. The
`release_note:breaking` label should be applied in these situations.
- [ ] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
- [ ] The PR description includes the appropriate Release Notes section,
and the correct `release_node:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

---------

Co-authored-by: Ash <[email protected]>
  • Loading branch information
neptunian and ashokaditya authored Nov 21, 2024
1 parent 0f6e7fc commit 8e1b0bd
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 8e1b0bd

Please sign in to comment.