Skip to content

Commit

Permalink
Reporting usage collector fetch migration (#86675)
Browse files Browse the repository at this point in the history
  • Loading branch information
TinaHeiligers authored Dec 22, 2020
1 parent 61eb83b commit a98550b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
15 changes: 9 additions & 6 deletions x-pack/plugins/reporting/server/usage/get_reporting_usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { get } from 'lodash';
import { LegacyAPICaller } from 'kibana/server';
import { ElasticsearchClient, SearchResponse } from 'kibana/server';
import { ReportingConfig } from '../';
import { ExportTypesRegistry } from '../lib/export_types_registry';
import { GetLicense } from './';
Expand All @@ -18,7 +18,7 @@ import {
KeyCountBucket,
RangeStats,
ReportingUsageType,
SearchResponse,
ReportingUsageSearchResponse,
StatusByAppBucket,
} from './types';

Expand Down Expand Up @@ -99,7 +99,9 @@ type RangeStatSets = Partial<RangeStats> & {
last7Days: Partial<RangeStats>;
};

async function handleResponse(response: SearchResponse): Promise<Partial<RangeStatSets>> {
type ESResponse = Partial<SearchResponse<ReportingUsageSearchResponse>>;

async function handleResponse(response: ESResponse): Promise<Partial<RangeStatSets>> {
const buckets = get(response, 'aggregations.ranges.buckets');
if (!buckets) {
return {};
Expand All @@ -118,7 +120,7 @@ async function handleResponse(response: SearchResponse): Promise<Partial<RangeSt
export async function getReportingUsage(
config: ReportingConfig,
getLicense: GetLicense,
callCluster: LegacyAPICaller,
esClient: ElasticsearchClient,
exportTypesRegistry: ExportTypesRegistry
): Promise<ReportingUsageType> {
const reportingIndex = config.get('index');
Expand Down Expand Up @@ -165,8 +167,9 @@ export async function getReportingUsage(
};

const featureAvailability = await getLicense();
return callCluster('search', params)
.then((response: SearchResponse) => handleResponse(response))
return esClient
.search(params)
.then(({ body: response }) => handleResponse(response))
.then(
(usage: Partial<RangeStatSets>): ReportingUsageType => {
// Allow this to explicitly throw an exception if/when this config is deprecated,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const getResponseMock = (base = {}) => base;

const getMockFetchClients = (resp: any) => {
const fetchParamsMock = createCollectorFetchContextMock();
fetchParamsMock.callCluster.mockResolvedValue(resp);
fetchParamsMock.esClient.search = jest.fn().mockResolvedValue({ body: resp });
return fetchParamsMock;
};
describe('license checks', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export function getReportingUsageCollector(
) {
return usageCollection.makeUsageCollector<ReportingUsageType>({
type: 'reporting',
fetch: ({ callCluster }: CollectorFetchContext) => {
fetch: ({ esClient }: CollectorFetchContext) => {
const config = reporting.getConfig();
return getReportingUsage(config, getLicense, callCluster, exportTypesRegistry);
return getReportingUsage(config, getLicense, esClient, exportTypesRegistry);
},
isReady,
schema: reportingSchema,
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/reporting/server/usage/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export interface AggregationResultBuckets {
doc_count: number;
}

export interface SearchResponse {
export interface ReportingUsageSearchResponse {
aggregations: {
ranges: {
buckets: {
Expand Down

0 comments on commit a98550b

Please sign in to comment.