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] use auto ops service #196312

Merged
merged 10 commits into from
Oct 17, 2024
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 @@ -31,6 +32,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 @@ -80,5 +84,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 @@ -38,5 +42,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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of adding only the auto ops service to the appContextService on start, but this is much neater. 🚀


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) {
neptunian marked this conversation as resolved.
Show resolved Hide resolved
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