Skip to content

Commit

Permalink
Uses the new es client in canvas usage collector's fetch methods (#86668
Browse files Browse the repository at this point in the history
)
  • Loading branch information
TinaHeiligers authored Dec 21, 2020
1 parent 58b4297 commit 68ec9d2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions x-pack/plugins/canvas/server/collectors/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ export function registerCanvasUsageCollector(
const canvasCollector = usageCollection.makeUsageCollector<CanvasUsage>({
type: 'canvas',
isReady: () => true,
fetch: async ({ callCluster }: CollectorFetchContext) => {
fetch: async ({ esClient }: CollectorFetchContext) => {
const collectorResults = await Promise.all(
collectors.map((collector) => collector(kibanaIndex, callCluster))
collectors.map((collector) => collector(kibanaIndex, esClient))
);

return collectorResults.reduce((reduction, usage) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SearchParams } from 'elasticsearch';
import { SearchResponse } from 'elasticsearch';
import { get } from 'lodash';
import { MakeSchemaFrom } from 'src/plugins/usage_collection/server';
import { collectFns } from './collector_helpers';
Expand Down Expand Up @@ -114,17 +114,19 @@ export function summarizeCustomElements(

const customElementCollector: TelemetryCollector = async function customElementCollector(
kibanaIndex,
callCluster
esClient
) {
const customElementParams: SearchParams = {
const customElementParams = {
size: 10000,
index: kibanaIndex,
ignoreUnavailable: true,
filterPath: [`hits.hits._source.${CUSTOM_ELEMENT_TYPE}.content`],
body: { query: { bool: { filter: { term: { type: CUSTOM_ELEMENT_TYPE } } } } },
};

const esResponse = await callCluster<CustomElementSearch>('search', customElementParams);
const { body: esResponse } = await esClient.search<SearchResponse<CustomElementSearch>>(
customElementParams
);

if (get(esResponse, 'hits.hits.length') > 0) {
const customElements = esResponse.hits.hits.map((hit) => hit._source[CUSTOM_ELEMENT_TYPE]);
Expand Down
9 changes: 5 additions & 4 deletions x-pack/plugins/canvas/server/collectors/workpad_collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SearchParams } from 'elasticsearch';
import { SearchResponse } from 'elasticsearch';
import { sum as arraySum, min as arrayMin, max as arrayMax, get } from 'lodash';
import { MakeSchemaFrom } from 'src/plugins/usage_collection/server';
import { CANVAS_TYPE } from '../../common/lib/constants';
Expand Down Expand Up @@ -229,17 +229,18 @@ export function summarizeWorkpads(workpadDocs: CanvasWorkpad[]): WorkpadTelemetr
variables: variableInfo,
};
}
type ESResponse = SearchResponse<WorkpadSearch>;

const workpadCollector: TelemetryCollector = async function (kibanaIndex, callCluster) {
const searchParams: SearchParams = {
const workpadCollector: TelemetryCollector = async function (kibanaIndex, esClient) {
const searchParams = {
size: 10000, // elasticsearch index.max_result_window default value
index: kibanaIndex,
ignoreUnavailable: true,
filterPath: ['hits.hits._source.canvas-workpad', '-hits.hits._source.canvas-workpad.assets'],
body: { query: { bool: { filter: { term: { type: CANVAS_TYPE } } } } },
};

const esResponse = await callCluster<WorkpadSearch>('search', searchParams);
const { body: esResponse } = await esClient.search<ESResponse>(searchParams);

if (get(esResponse, 'hits.hits.length') > 0) {
const workpads = esResponse.hits.hits.map((hit) => hit._source[CANVAS_TYPE]);
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/canvas/types/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { LegacyAPICaller } from 'kibana/server';
import { ElasticsearchClient } from 'kibana/server';

/**
Function for collecting information about canvas usage
Expand All @@ -13,7 +13,7 @@ export type TelemetryCollector = (
/** The server instance */
kibanaIndex: string,
/** Function for calling elasticsearch */
callCluster: LegacyAPICaller
esClient: ElasticsearchClient
) => Record<string, any>;

export interface TelemetryCustomElementDocument {
Expand Down

0 comments on commit 68ec9d2

Please sign in to comment.