Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new es client in Uptime usage collector #86718

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,21 @@
*/

import moment from 'moment';
import {
ISavedObjectsRepository,
ILegacyScopedClusterClient,
SavedObjectsClientContract,
ElasticsearchClient,
} from 'kibana/server';
import { ISavedObjectsRepository, SavedObjectsClientContract } from 'kibana/server';
import { CollectorFetchContext, UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { PageViewParams, UptimeTelemetry, Usage } from './types';
import { savedObjectsAdapter } from '../../saved_objects';
import { UptimeESClient } from '../../lib';
import { UptimeESClient, createUptimeESClient } from '../../lib';

interface UptimeTelemetryCollector {
[key: number]: UptimeTelemetry;
}

// seconds in an hour
const BUCKET_SIZE = 3600;
// take buckets in the last day
const BUCKET_NUMBER = 24;

export class KibanaTelemetryAdapter {
public static callCluster: ILegacyScopedClusterClient['callAsCurrentUser'] | ElasticsearchClient;

public static registerUsageCollector = (
usageCollector: UsageCollectionSetup,
getSavedObjectsClient: () => ISavedObjectsRepository | undefined
Expand Down Expand Up @@ -76,10 +68,11 @@ export class KibanaTelemetryAdapter {
},
},
},
fetch: async ({ callCluster }: CollectorFetchContext) => {
fetch: async ({ esClient }: CollectorFetchContext) => {
const savedObjectsClient = getSavedObjectsClient()!;
if (savedObjectsClient) {
await this.countNoOfUniqueMonitorAndLocations(callCluster, savedObjectsClient);
const uptimeEsClient = createUptimeESClient({ esClient, savedObjectsClient });
await this.countNoOfUniqueMonitorAndLocations(uptimeEsClient, savedObjectsClient);
}
const report = this.getReport();
return { last_24_hours: { hits: { ...report } } };
Expand Down Expand Up @@ -132,7 +125,7 @@ export class KibanaTelemetryAdapter {
}

public static async countNoOfUniqueMonitorAndLocations(
callCluster: ILegacyScopedClusterClient['callAsCurrentUser'] | UptimeESClient,
callCluster: UptimeESClient,
savedObjectsClient: ISavedObjectsRepository | SavedObjectsClientContract
) {
const dynamicSettings = await savedObjectsAdapter.getUptimeDynamicSettings(savedObjectsClient);
Expand Down Expand Up @@ -194,15 +187,12 @@ export class KibanaTelemetryAdapter {
},
};

const { body: result } =
typeof callCluster === 'function'
? await callCluster('search', params)
: await callCluster.search(params);
const { body: result } = await callCluster.search(params);

const numberOfUniqueMonitors: number = result?.aggregations?.unique_monitors?.value ?? 0;
const numberOfUniqueLocations: number = result?.aggregations?.unique_locations?.value ?? 0;
const monitorNameStats: any = result?.aggregations?.monitor_name;
const locationNameStats: any = result?.aggregations?.observer_loc_name;
const monitorNameStats = result?.aggregations?.monitor_name;
const locationNameStats = result?.aggregations?.observer_loc_name;
const uniqueMonitors: any = result?.aggregations?.monitors.buckets;

const bucketId = this.getBucketToIncrement();
Expand Down
9 changes: 7 additions & 2 deletions x-pack/plugins/uptime/server/lib/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { ElasticsearchClient, SavedObjectsClientContract, KibanaRequest } from 'kibana/server';
import {
ElasticsearchClient,
SavedObjectsClientContract,
KibanaRequest,
ISavedObjectsRepository,
} from 'kibana/server';
import chalk from 'chalk';
import { UMBackendFrameworkAdapter } from './adapters';
import { UMLicenseCheck } from './domains';
Expand Down Expand Up @@ -41,7 +46,7 @@ export function createUptimeESClient({
}: {
esClient: ElasticsearchClient;
request?: KibanaRequest;
savedObjectsClient: SavedObjectsClientContract;
savedObjectsClient: SavedObjectsClientContract | ISavedObjectsRepository;
}) {
const { _debug = false } = (request?.query as { _debug: boolean }) ?? {};

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