Skip to content

Commit

Permalink
Refactors to fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
TinaHeiligers committed Dec 22, 2020
1 parent eb87c64 commit e2014a3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import {
ElasticsearchClient,
} from 'kibana/server';
import { CollectorFetchContext, UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { ESSearchResponse } from '../../../../../../typings/elasticsearch';
import { PageViewParams, UptimeTelemetry, Usage } from './types';
import { savedObjectsAdapter } from '../../saved_objects';
import { UptimeESClient } from '../../lib';
import { UptimeESClient, isUptimeESClient } from '../../lib';

interface UptimeTelemetryCollector {
[key: number]: UptimeTelemetry;
Expand Down Expand Up @@ -189,8 +190,10 @@ export class KibanaTelemetryAdapter {
},
},
};
// @ts-ignore: Union types do not have compatible signatures
const { body: result } = await callCluster.search(params);

const { body: result } = isUptimeESClient(callCluster)
? await callCluster.search(params)
: await callCluster.search<ESSearchResponse<unknown, typeof params>>(params);

const numberOfUniqueMonitors: number = result?.aggregations?.unique_monitors?.value ?? 0;
const numberOfUniqueLocations: number = result?.aggregations?.unique_locations?.value ?? 0;
Expand Down
8 changes: 8 additions & 0 deletions x-pack/plugins/uptime/server/lib/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ export function createUptimeESClient({
};
}

export function isUptimeESClient(client: any): client is UptimeESClient {
return (
'baseESClient' in client &&
'getSavedObjectClient' in client &&
'search' in client &&
'count' in client
);
}
/* eslint-disable no-console */

function formatObj(obj: Record<string, any>) {
Expand Down
8 changes: 8 additions & 0 deletions x-pack/typings/elasticsearch/aggregations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export interface AggregationOptionsByType {
extended_stats: {
field: string;
};
string_stats: { field: string };
top_hits: {
from?: number;
size?: number;
Expand Down Expand Up @@ -274,6 +275,13 @@ interface AggregationResponsePart<TAggregationOptionsMap extends AggregationOpti
lower: number | null;
};
};
string_stats: {
count: number;
min_length: number;
max_length: number;
avg_length: number;
entropy: number;
};
top_hits: {
hits: {
total: {
Expand Down

0 comments on commit e2014a3

Please sign in to comment.