Skip to content

Commit

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

This will backport the following commits from `main` to `8.16`:
- [[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","v8.16.0","backport:version","v8.17.0"],"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":["8.16"],"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"}},{"branch":"8.16","label":"v8.16.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.x","label":"v8.17.0","branchLabelMappingKey":"^v8.17.0$","isSourceBranch":false,"url":"https://github.com/elastic/kibana/pull/196769","number":196769,"state":"MERGED","mergeCommit":{"sha":"d2bcd89599918d7094b2c16dbfb34a227e762441","message":"[8.x]
[Data Usage] use auto ops service (#196312) (#196769)\n\n#
Backport\n\nThis will backport the following commits from `main` to
`8.x`:\n- [[Data Usage] use auto ops
service\n(#196312)](https://github.com/elastic/kibana/pull/196312)\n\n<!---
Backport version: 9.4.3 -->\n\n### Questions ?\nPlease refer to the
[Backport
tool\ndocumentation](https://github.com/sqren/backport)\n\n<!--BACKPORT
[{\"author\":{\"name\":\"Sandra\nG\",\"email\":\"[email protected]\"},\"sourceCommit\":{\"committedDate\":\"2024-10-17T19:30:05Z\",\"message\":\"[Data\nUsage]
use auto ops service (#196312)\\n\\n## Summary\\r\\n\\r\\n-
use\nAutoOpsAPIService service and removes mock data\\r\\n- removes
checking\nfor serviceConfig.enabled to register routes as
they\\r\\nwon't register\nif the plugin isn't enabled\\r\\n- adds UI
toasts with error message when\nrequests fail\\r\\n- adds centralized
DataUsageService for managing
other\nservices\",\"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\nUsage]
use auto
ops\nservice\",\"number\":196312,\"url\":\"https://github.com/elastic/kibana/pull/196312\",\"mergeCommit\":{\"message\":\"[Data\nUsage]
use auto ops service (#196312)\\n\\n## Summary\\r\\n\\r\\n-
use\nAutoOpsAPIService service and removes mock data\\r\\n- removes
checking\nfor serviceConfig.enabled to register routes as
they\\r\\nwon't register\nif the plugin isn't enabled\\r\\n- adds UI
toasts with error message when\nrequests fail\\r\\n- adds centralized
DataUsageService for managing
other\nservices\",\"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\nUsage]
use auto ops service (#196312)\\n\\n## Summary\\r\\n\\r\\n-
use\nAutoOpsAPIService service and removes mock data\\r\\n- removes
checking\nfor serviceConfig.enabled to register routes as
they\\r\\nwon't register\nif the plugin isn't enabled\\r\\n- adds UI
toasts with error message when\nrequests fail\\r\\n- adds centralized
DataUsageService for managing
other\nservices\",\"sha\":\"7af4c6da4a08b6452c0c6e1264d1ceffbf628c42\"}}]}]\nBACKPORT-->\n\nCo-authored-by:
Sandra G <[email protected]>"}}]}] BACKPORT-->

Co-authored-by: Sandra G <[email protected]>
  • Loading branch information
kibanamachine and neptunian authored Oct 21, 2024
1 parent a8012b7 commit 4b9256f
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 4b9256f

Please sign in to comment.