diff --git a/x-pack/platform/plugins/private/data_usage/public/app/components/data_usage_metrics.tsx b/x-pack/platform/plugins/private/data_usage/public/app/components/data_usage_metrics.tsx index 829b198e59ab3..35ff42decef0e 100644 --- a/x-pack/platform/plugins/private/data_usage/public/app/components/data_usage_metrics.tsx +++ b/x-pack/platform/plugins/private/data_usage/public/app/components/data_usage_metrics.tsx @@ -76,7 +76,7 @@ export const DataUsageMetrics = memo( if (!metricTypesFromUrl && isFirstPageLoad) { setUrlMetricTypesFilter(metricsFilters.metricTypes.join(',')); } - if (!dataStreamsFromUrl && dataStreams && isFirstPageLoad) { + if (!dataStreamsFromUrl && !!dataStreams && dataStreams.length > 0 && isFirstPageLoad) { const hasMoreThan50 = dataStreams.length > 50; const _dataStreams = hasMoreThan50 ? dataStreams.slice(0, 50) : dataStreams; setUrlDataStreamsFilter(_dataStreams.map((ds) => ds.name).join(',')); diff --git a/x-pack/platform/plugins/private/data_usage/server/routes/internal/data_streams.test.ts b/x-pack/platform/plugins/private/data_usage/server/routes/internal/data_streams.test.ts index 9316a64328c9b..e7166baea59d3 100644 --- a/x-pack/platform/plugins/private/data_usage/server/routes/internal/data_streams.test.ts +++ b/x-pack/platform/plugins/private/data_usage/server/routes/internal/data_streams.test.ts @@ -233,14 +233,18 @@ describe('registerDataStreamsRoute', () => { // @ts-expect-error if (stats && stats.datastreams && stats.datastreams.length) { - expect(mockResponse.ok).toHaveBeenCalledTimes(1); - expect(mockResponse.ok.mock.calls[0][0]).toEqual({ - body: res, + expect(mockResponse.customError).toHaveBeenCalledTimes(1); + expect(mockResponse.customError).toHaveBeenCalledWith({ + body: new CustomHttpRequestError( + 'No relevant user defined data streams found with storage size greater than zero', + 404 + ), + statusCode: 404, }); } else { expect(mockResponse.customError).toHaveBeenCalledTimes(1); expect(mockResponse.customError).toHaveBeenCalledWith({ - body: new CustomHttpRequestError('No user defined data streams found', 404), + body: new CustomHttpRequestError('No data streams found', 404), statusCode: 404, }); } diff --git a/x-pack/platform/plugins/private/data_usage/server/routes/internal/data_streams_handler.ts b/x-pack/platform/plugins/private/data_usage/server/routes/internal/data_streams_handler.ts index 0f472ca98065e..ccda00a9478dd 100644 --- a/x-pack/platform/plugins/private/data_usage/server/routes/internal/data_streams_handler.ts +++ b/x-pack/platform/plugins/private/data_usage/server/routes/internal/data_streams_handler.ts @@ -31,7 +31,15 @@ export const getDataStreamsHandler = ( core.elasticsearch.client.asSecondaryAuthUser ); - const nonSystemDataStreams = meteringStatsDataStreams?.filter((dataStream) => { + if (!meteringStatsDataStreams || !meteringStatsDataStreams.length) { + return errorHandler( + logger, + response, + new CustomHttpRequestError('No data streams found', 404) + ); + } + + const nonSystemDataStreams = meteringStatsDataStreams.filter((dataStream) => { return !dataStream.name?.startsWith('.'); }); @@ -55,6 +63,17 @@ export const getDataStreamsHandler = ( }, []) .sort((a, b) => b.storageSizeBytes - a.storageSizeBytes); + if (!body || !body.length) { + return errorHandler( + logger, + response, + new CustomHttpRequestError( + 'No relevant user defined data streams found with storage size greater than zero', + 404 + ) + ); + } + return response.ok({ body, }); diff --git a/x-pack/test_serverless/api_integration/test_suites/common/data_usage/tests/data_streams.ts b/x-pack/test_serverless/api_integration/test_suites/common/data_usage/tests/data_streams.ts index b4dd8d51c331a..e591d23b125e4 100644 --- a/x-pack/test_serverless/api_integration/test_suites/common/data_usage/tests/data_streams.ts +++ b/x-pack/test_serverless/api_integration/test_suites/common/data_usage/tests/data_streams.ts @@ -54,10 +54,7 @@ export default function ({ getService }: FtrProviderContext) { const res = await supertestAdminWithCookieCredentials .get(DATA_USAGE_DATA_STREAMS_API_ROUTE) .set('elastic-api-version', '1'); - const dataStreams: DataStreamsResponseBodySchemaBody = res.body; - const foundStream = dataStreams.find((stream) => stream.name === testDataStreamName); - expect(res.statusCode).to.be(200); - expect(foundStream).to.be(undefined); + expect(res.statusCode).to.be(404); }); }); }