Skip to content

Commit

Permalink
[Data Usage] use auto ops service (#196312)
Browse files Browse the repository at this point in the history
## Summary

- use AutoOpsAPIService service and removes mock data
- removes checking for serviceConfig.enabled to register routes as they
won't register if the plugin isn't enabled
- adds UI toasts with error message when requests fail
- adds centralized DataUsageService for managing other services

(cherry picked from commit 7af4c6d)
  • Loading branch information
neptunian committed Oct 21, 2024
1 parent 5248361 commit a947134
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 237 deletions.
12 changes: 12 additions & 0 deletions x-pack/plugins/data_usage/public/hooks/use_get_data_streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import type { UseQueryOptions, UseQueryResult } from '@tanstack/react-query';
import { i18n } from '@kbn/i18n';
import { useQuery } from '@tanstack/react-query';
import type { IHttpFetchError } from '@kbn/core-http-browser';
import { DATA_USAGE_DATA_STREAMS_API_ROUTE } from '../../common';
Expand All @@ -32,6 +33,9 @@ export const useGetDataUsageDataStreams = ({
options?: UseQueryOptions<GetDataUsageDataStreamsResponse, IHttpFetchError>;
}): UseQueryResult<GetDataUsageDataStreamsResponse, IHttpFetchError> => {
const http = useKibanaContextForPlugin().services.http;
const {
services: { notifications },
} = useKibanaContextForPlugin();

return useQuery<GetDataUsageDataStreamsResponse, IHttpFetchError>({
queryKey: ['get-data-usage-data-streams'],
Expand Down Expand Up @@ -83,5 +87,13 @@ export const useGetDataUsageDataStreams = ({
: PAGING_PARAMS.default
);
},
onError: (error: IHttpFetchError) => {
notifications.toasts.addDanger({
title: i18n.translate('xpack.dataUsage.getDataStreams.addFailure.toast.title', {
defaultMessage: 'Error getting data streams',
}),
text: error.message,
});
},
});
};
12 changes: 12 additions & 0 deletions x-pack/plugins/data_usage/public/hooks/use_get_usage_metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import type { UseQueryOptions, UseQueryResult } from '@tanstack/react-query';
import { i18n } from '@kbn/i18n';
import { useQuery } from '@tanstack/react-query';
import type { IHttpFetchError } from '@kbn/core-http-browser';
import { UsageMetricsRequestBody, UsageMetricsResponseSchemaBody } from '../../common/rest_types';
Expand All @@ -22,6 +23,9 @@ export const useGetDataUsageMetrics = (
options: UseQueryOptions<UsageMetricsResponseSchemaBody, IHttpFetchError<ErrorType>> = {}
): UseQueryResult<UsageMetricsResponseSchemaBody, IHttpFetchError<ErrorType>> => {
const http = useKibanaContextForPlugin().services.http;
const {
services: { notifications },
} = useKibanaContextForPlugin();

return useQuery<UsageMetricsResponseSchemaBody, IHttpFetchError<ErrorType>>({
queryKey: ['get-data-usage-metrics', body],
Expand All @@ -39,5 +43,13 @@ export const useGetDataUsageMetrics = (
}),
});
},
onError: (error: IHttpFetchError<ErrorType>) => {
notifications.toasts.addDanger({
title: i18n.translate('xpack.dataUsage.getMetrics.addFailure.toast.title', {
defaultMessage: 'Error getting usage metrics',
}),
text: error.message,
});
},
});
};
16 changes: 4 additions & 12 deletions x-pack/plugins/data_usage/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type {
} from './types';
import { registerDataUsageRoutes } from './routes';
import { PLUGIN_ID } from '../common';
import { appContextService } from './app_context';
import { DataUsageService } from './services';

export class DataUsagePlugin
implements
Expand Down Expand Up @@ -53,6 +53,8 @@ export class DataUsagePlugin
}
setup(coreSetup: CoreSetup, pluginsSetup: DataUsageSetupDependencies): DataUsageServerSetup {
this.logger.debug('data usage plugin setup');
const dataUsageService = new DataUsageService(this.dataUsageContext);

pluginsSetup.features.registerElasticsearchFeature({
id: PLUGIN_ID,
management: {
Expand All @@ -66,22 +68,12 @@ export class DataUsagePlugin
],
});
const router = coreSetup.http.createRouter<DataUsageRequestHandlerContext>();
registerDataUsageRoutes(router, this.dataUsageContext);
registerDataUsageRoutes(router, dataUsageService);

return {};
}

start(_coreStart: CoreStart, _pluginsStart: DataUsageStartDependencies): DataUsageServerStart {
appContextService.start({
logFactory: this.dataUsageContext.logFactory,
configInitialValue: this.dataUsageContext.configInitialValue,
serverConfig: this.dataUsageContext.serverConfig,
config$: this.dataUsageContext.config$,
kibanaVersion: this.dataUsageContext.kibanaVersion,
kibanaBranch: this.dataUsageContext.kibanaBranch,
kibanaInstanceId: this.dataUsageContext.kibanaInstanceId,
cloud: this.dataUsageContext.cloud,
});
return {};
}

Expand Down
9 changes: 5 additions & 4 deletions x-pack/plugins/data_usage/server/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
* 2.0.
*/

import { DataUsageContext, DataUsageRouter } from '../types';
import { DataUsageRouter } from '../types';
import { registerDataStreamsRoute, registerUsageMetricsRoute } from './internal';
import { DataUsageService } from '../services';

export const registerDataUsageRoutes = (
router: DataUsageRouter,
dataUsageContext: DataUsageContext
dataUsageService: DataUsageService
) => {
registerUsageMetricsRoute(router, dataUsageContext);
registerDataStreamsRoute(router, dataUsageContext);
registerUsageMetricsRoute(router, dataUsageService);
registerDataStreamsRoute(router, dataUsageService);
};
38 changes: 18 additions & 20 deletions x-pack/plugins/data_usage/server/routes/internal/data_streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,29 @@

import { DataStreamsResponseSchema } from '../../../common/rest_types';
import { DATA_USAGE_DATA_STREAMS_API_ROUTE } from '../../../common';
import { DataUsageContext, DataUsageRouter } from '../../types';

import { DataUsageRouter } from '../../types';
import { DataUsageService } from '../../services';
import { getDataStreamsHandler } from './data_streams_handler';

export const registerDataStreamsRoute = (
router: DataUsageRouter,
dataUsageContext: DataUsageContext
dataUsageService: DataUsageService
) => {
if (dataUsageContext.serverConfig.enabled) {
router.versioned
.get({
access: 'internal',
path: DATA_USAGE_DATA_STREAMS_API_ROUTE,
})
.addVersion(
{
version: '1',
validate: {
request: {},
response: {
200: DataStreamsResponseSchema,
},
router.versioned
.get({
access: 'internal',
path: DATA_USAGE_DATA_STREAMS_API_ROUTE,
})
.addVersion(
{
version: '1',
validate: {
request: {},
response: {
200: DataStreamsResponseSchema,
},
},
getDataStreamsHandler(dataUsageContext)
);
}
},
getDataStreamsHandler(dataUsageService)
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
*/

import { type ElasticsearchClient, RequestHandler } from '@kbn/core/server';
import { DataUsageContext, DataUsageRequestHandlerContext } from '../../types';
import { DataUsageRequestHandlerContext } from '../../types';
import { errorHandler } from '../error_handler';
import { DataUsageService } from '../../services';

export interface MeteringStats {
name: string;
Expand All @@ -27,9 +28,9 @@ const getMeteringStats = (client: ElasticsearchClient) => {
};

export const getDataStreamsHandler = (
dataUsageContext: DataUsageContext
dataUsageService: DataUsageService
): RequestHandler<never, unknown, DataUsageRequestHandlerContext> => {
const logger = dataUsageContext.logFactory.get('dataStreamsRoute');
const logger = dataUsageService.getLogger('dataStreamsRoute');

return async (context, _, response) => {
logger.debug('Retrieving user data streams');
Expand Down
41 changes: 20 additions & 21 deletions x-pack/plugins/data_usage/server/routes/internal/usage_metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,32 @@

import { UsageMetricsRequestSchema, UsageMetricsResponseSchema } from '../../../common/rest_types';
import { DATA_USAGE_METRICS_API_ROUTE } from '../../../common';
import { DataUsageContext, DataUsageRouter } from '../../types';
import { DataUsageRouter } from '../../types';
import { DataUsageService } from '../../services';

import { getUsageMetricsHandler } from './usage_metrics_handler';

export const registerUsageMetricsRoute = (
router: DataUsageRouter,
dataUsageContext: DataUsageContext
dataUsageService: DataUsageService
) => {
if (dataUsageContext.serverConfig.enabled) {
router.versioned
.post({
access: 'internal',
path: DATA_USAGE_METRICS_API_ROUTE,
})
.addVersion(
{
version: '1',
validate: {
request: {
body: UsageMetricsRequestSchema,
},
response: {
200: UsageMetricsResponseSchema,
},
router.versioned
.post({
access: 'internal',
path: DATA_USAGE_METRICS_API_ROUTE,
})
.addVersion(
{
version: '1',
validate: {
request: {
body: UsageMetricsRequestSchema,
},
response: {
200: UsageMetricsResponseSchema,
},
},
getUsageMetricsHandler(dataUsageContext)
);
}
},
getUsageMetricsHandler(dataUsageService)
);
};
Loading

0 comments on commit a947134

Please sign in to comment.