Skip to content

Commit

Permalink
[8.x] [Data Usage] use auto ops service (#196312) (#196769)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.x`:
- [[Data Usage] use auto ops service
(#196312)](#196312)

<!--- 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-10-17T19:30:05Z","message":"[Data
Usage] use auto ops service (#196312)\n\n## Summary\r\n\r\n- use
AutoOpsAPIService service and removes mock data\r\n- removes checking
for serviceConfig.enabled to register routes as they\r\nwon't register
if the plugin isn't enabled\r\n- adds UI toasts with error message when
requests fail\r\n- adds centralized DataUsageService for managing other
services","sha":"7af4c6da4a08b6452c0c6e1264d1ceffbf628c42","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:prev-minor"],"title":"[Data
Usage] use auto ops
service","number":196312,"url":"https://github.com/elastic/kibana/pull/196312","mergeCommit":{"message":"[Data
Usage] use auto ops service (#196312)\n\n## Summary\r\n\r\n- use
AutoOpsAPIService service and removes mock data\r\n- removes checking
for serviceConfig.enabled to register routes as they\r\nwon't register
if the plugin isn't enabled\r\n- adds UI toasts with error message when
requests fail\r\n- adds centralized DataUsageService for managing other
services","sha":"7af4c6da4a08b6452c0c6e1264d1ceffbf628c42"}},"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/196312","number":196312,"mergeCommit":{"message":"[Data
Usage] use auto ops service (#196312)\n\n## Summary\r\n\r\n- use
AutoOpsAPIService service and removes mock data\r\n- removes checking
for serviceConfig.enabled to register routes as they\r\nwon't register
if the plugin isn't enabled\r\n- adds UI toasts with error message when
requests fail\r\n- adds centralized DataUsageService for managing other
services","sha":"7af4c6da4a08b6452c0c6e1264d1ceffbf628c42"}}]}]
BACKPORT-->

Co-authored-by: Sandra G <[email protected]>
  • Loading branch information
kibanamachine and neptunian authored Oct 17, 2024
1 parent df22573 commit d2bcd89
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 @@ -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);

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 d2bcd89

Please sign in to comment.