Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Data Usage] Remove metrics from autoops response type #200770

Merged
merged 14 commits into from
Nov 21, 2024
Merged
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/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"@elastic/security-solution"
],
"group": "platform",
"visibility": "private",
"visibility": "shared",
neptunian marked this conversation as resolved.
Show resolved Hide resolved
"plugin": {
"id": "dataUsage",
"server": true,
Expand Down
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