From 77df7eddf8d6e8e958609bce6aea972db9894d56 Mon Sep 17 00:00:00 2001 From: Matias Chomicki Date: Mon, 5 Jun 2023 18:32:43 +0200 Subject: [PATCH] Elasticsearch: remove usages of any --- .betterer.results | 18 +--- .../datasource/elasticsearch/datasource.ts | 98 ++++++++++--------- .../datasource/elasticsearch/tracking.ts | 13 +-- .../plugins/datasource/elasticsearch/types.ts | 21 +++- 4 files changed, 73 insertions(+), 77 deletions(-) diff --git a/.betterer.results b/.betterer.results index 60ca53d69cf..7aa6c9f7c73 100644 --- a/.betterer.results +++ b/.betterer.results @@ -3882,23 +3882,7 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "3"], [0, 0, 0, "Unexpected any. Specify a different type.", "4"], [0, 0, 0, "Unexpected any. Specify a different type.", "5"], - [0, 0, 0, "Unexpected any. Specify a different type.", "6"], - [0, 0, 0, "Unexpected any. Specify a different type.", "7"], - [0, 0, 0, "Unexpected any. Specify a different type.", "8"], - [0, 0, 0, "Unexpected any. Specify a different type.", "9"], - [0, 0, 0, "Unexpected any. Specify a different type.", "10"], - [0, 0, 0, "Unexpected any. Specify a different type.", "11"], - [0, 0, 0, "Unexpected any. Specify a different type.", "12"], - [0, 0, 0, "Unexpected any. Specify a different type.", "13"], - [0, 0, 0, "Unexpected any. Specify a different type.", "14"], - [0, 0, 0, "Unexpected any. Specify a different type.", "15"], - [0, 0, 0, "Unexpected any. Specify a different type.", "16"], - [0, 0, 0, "Unexpected any. Specify a different type.", "17"], - [0, 0, 0, "Unexpected any. Specify a different type.", "18"], - [0, 0, 0, "Unexpected any. Specify a different type.", "19"], - [0, 0, 0, "Unexpected any. Specify a different type.", "20"], - [0, 0, 0, "Unexpected any. Specify a different type.", "21"], - [0, 0, 0, "Unexpected any. Specify a different type.", "22"] + [0, 0, 0, "Unexpected any. Specify a different type.", "6"] ], "public/app/plugins/datasource/elasticsearch/hooks/useStatelessReducer.ts:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"] diff --git a/public/app/plugins/datasource/elasticsearch/datasource.ts b/public/app/plugins/datasource/elasticsearch/datasource.ts index a0f9efa3414..aae00c6cde4 100644 --- a/public/app/plugins/datasource/elasticsearch/datasource.ts +++ b/public/app/plugins/datasource/elasticsearch/datasource.ts @@ -60,6 +60,8 @@ import { ElasticsearchQuery, TermsQuery, Interval, + ElasticsearchAnnotationQuery, + RangeMap, } from './types'; import { getScriptValue, isSupportedVersion, unsupportedVersionMessage } from './utils'; @@ -233,17 +235,7 @@ export class ElasticDatasource ); } - private prepareAnnotationRequest(options: { - annotation: { - target: ElasticsearchQuery; - timeField?: string; - timeEndField?: string; - titleField?: string; - query?: string; - index?: string; - }; - range: TimeRange; - }) { + private prepareAnnotationRequest(options: { annotation: ElasticsearchAnnotationQuery; range: TimeRange }) { const annotation = options.annotation; const timeField = annotation.timeField || '@timestamp'; const timeEndField = annotation.timeEndField || null; @@ -260,7 +252,7 @@ export class ElasticDatasource const queryString = annotation.query ?? annotation.target?.query ?? ''; const dateRanges = []; - const rangeStart: any = {}; + const rangeStart: RangeMap = {}; rangeStart[timeField] = { from: options.range.from.valueOf(), to: options.range.to.valueOf(), @@ -269,7 +261,7 @@ export class ElasticDatasource dateRanges.push({ range: rangeStart }); if (timeEndField) { - const rangeEnd: any = {}; + const rangeEnd: RangeMap = {}; rangeEnd[timeEndField] = { from: options.range.from.valueOf(), to: options.range.to.valueOf(), @@ -279,7 +271,9 @@ export class ElasticDatasource } const queryInterpolated = this.interpolateLuceneQuery(queryString); - const query: any = { + const query: { + bool: { filter: Array>>> }; + } = { bool: { filter: [ { @@ -299,12 +293,12 @@ export class ElasticDatasource }, }); } - const data: any = { + const data = { query, size: 10000, }; - const header: any = { + const header: Record = { search_type: 'query_then_fetch', ignore_unavailable: true, }; @@ -322,17 +316,8 @@ export class ElasticDatasource } private processHitsToAnnotationEvents( - annotation: { - target: ElasticsearchQuery; - timeField?: string; - titleField?: string; - timeEndField?: string; - query?: string; - tagsField?: string; - textField?: string; - index?: string; - }, - hits: Array<{ [key: string]: any }> + annotation: ElasticsearchAnnotationQuery, + hits: Array>> ) { const timeField = annotation.timeField || '@timestamp'; const timeEndField = annotation.timeEndField || null; @@ -340,7 +325,7 @@ export class ElasticDatasource const tagsField = annotation.tagsField || null; const list: AnnotationEvent[] = []; - const getFieldFromSource = (source: any, fieldName: any) => { + const getFieldFromSource = (source: any, fieldName: string | null) => { if (!fieldName) { return; } @@ -363,7 +348,7 @@ export class ElasticDatasource let time = getFieldFromSource(source, timeField); if (typeof hits[i].fields !== 'undefined') { const fields = hits[i].fields; - if (isString(fields[timeField]) || isNumber(fields[timeField])) { + if (typeof fields === 'object' && (isString(fields[timeField]) || isNumber(fields[timeField]))) { time = fields[timeField]; } } @@ -419,7 +404,7 @@ export class ElasticDatasource return lastValueFrom( this.getFields(['date']).pipe( mergeMap((dateFields) => { - const timeField: any = find(dateFields, { text: this.timeField }); + const timeField = find(dateFields, { text: this.timeField }); if (!timeField) { return of({ status: 'error', @@ -440,8 +425,8 @@ export class ElasticDatasource ); } - getQueryHeader(searchType: any, timeFrom?: DateTime, timeTo?: DateTime): string { - const queryHeader: any = { + getQueryHeader(searchType: string, timeFrom?: DateTime, timeTo?: DateTime): string { + const queryHeader = { search_type: searchType, ignore_unavailable: true, index: this.indexPattern.getIndexList(timeFrom, timeTo), @@ -683,8 +668,8 @@ export class ElasticDatasource }; // Store subfield names: [system, process, cpu, total] -> system.process.cpu.total - const fieldNameParts: any = []; - const fields: any = {}; + const fieldNameParts: string[] = []; + const fields: Record = {}; function getFieldsRecursively(obj: any) { for (const key in obj) { @@ -781,7 +766,7 @@ export class ElasticDatasource return ('_msearch?' + searchParams.toString()).replace(/\?$/, ''); } - metricFindQuery(query: string, options?: any): Promise { + metricFindQuery(query: string, options?: { range: TimeRange }): Promise { const range = options?.range; const parsedQuery = JSON.parse(query); if (query) { @@ -804,36 +789,53 @@ export class ElasticDatasource return lastValueFrom(this.getFields()); } - getTagValues(options: any) { + getTagValues(options: { key: string }) { const range = this.timeSrv.timeRange(); return lastValueFrom(this.getTerms({ field: options.key }, range)); } - targetContainsTemplate(target: any) { + targetContainsTemplate(target: ElasticsearchQuery) { if (this.templateSrv.containsTemplate(target.query) || this.templateSrv.containsTemplate(target.alias)) { return true; } - for (const bucketAgg of target.bucketAggs) { - if (this.templateSrv.containsTemplate(bucketAgg.field) || this.objectContainsTemplate(bucketAgg.settings)) { - return true; + if (target.bucketAggs) { + for (const bucketAgg of target.bucketAggs) { + if (this.objectContainsTemplate(bucketAgg.settings)) { + return true; + } } } - for (const metric of target.metrics) { - if ( - this.templateSrv.containsTemplate(metric.field) || - this.objectContainsTemplate(metric.settings) || - this.objectContainsTemplate(metric.meta) - ) { - return true; + if (target.metrics) { + for (const metric of target.metrics) { + if ( + metric.type === 'count' || + metric.type === 'bucket_script' || + metric.type === 'raw_data' || + metric.type === 'raw_document' || + metric.type === 'logs' || + metric.type === 'top_metrics' || + metric.type === 'moving_avg' + ) { + continue; + } + if (metric.field && this.templateSrv.containsTemplate(metric.field)) { + return true; + } + if (metric.settings && this.objectContainsTemplate(metric.settings)) { + return true; + } + if (metric.type === 'extended_stats' && this.objectContainsTemplate(metric.meta)) { + return true; + } } } return false; } - private isPrimitive(obj: any) { + private isPrimitive(obj: undefined | null | string | number | boolean | unknown) { if (obj === null || obj === undefined) { return true; } diff --git a/public/app/plugins/datasource/elasticsearch/tracking.ts b/public/app/plugins/datasource/elasticsearch/tracking.ts index 720a7eef24c..bc0456756e6 100644 --- a/public/app/plugins/datasource/elasticsearch/tracking.ts +++ b/public/app/plugins/datasource/elasticsearch/tracking.ts @@ -4,7 +4,7 @@ import { variableRegex } from 'app/features/variables/utils'; import { REF_ID_STARTER_LOG_VOLUME } from './datasource'; import pluginJson from './plugin.json'; -import { ElasticsearchQuery } from './types'; +import { ElasticsearchAnnotationQuery, ElasticsearchQuery } from './types'; type ElasticSearchOnDashboardLoadedTrackingEvent = { grafana_version?: string; @@ -141,16 +141,7 @@ export function trackQuery( } } -export function trackAnnotationQuery(annotation: { - target: ElasticsearchQuery; - timeField?: string; - timeEndField?: string; - query?: string; - tagsField?: string; - textField?: string; - index?: string; - [key: string]: unknown; -}): void { +export function trackAnnotationQuery(annotation: ElasticsearchAnnotationQuery): void { reportInteraction('grafana_elasticsearch_annotation_query_executed', { grafana_version: config.buildInfo.version, has_target_query: !!annotation.target?.query, diff --git a/public/app/plugins/datasource/elasticsearch/types.ts b/public/app/plugins/datasource/elasticsearch/types.ts index c7f7e68df62..63c0e715d83 100644 --- a/public/app/plugins/datasource/elasticsearch/types.ts +++ b/public/app/plugins/datasource/elasticsearch/types.ts @@ -1,4 +1,4 @@ -import { DataSourceJsonData } from '@grafana/data'; +import { DataSourceJsonData, TimeRange } from '@grafana/data'; import { BucketAggregationType, @@ -14,6 +14,7 @@ import { MovingAverage as SchemaMovingAverage, BucketAggregation, Logs as SchemaLogs, + Elasticsearch, } from './dataquery.gen'; export * from './dataquery.gen'; @@ -121,3 +122,21 @@ export type DataLinkConfig = { urlDisplayLabel?: string; datasourceUid?: string; }; + +export interface ElasticsearchAnnotationQuery { + target: Elasticsearch; + timeField?: string; + titleField?: string; + timeEndField?: string; + query?: string; + tagsField?: string; + textField?: string; + index?: string; +} + +export interface ElasticsearchAnnotationQueryOptions { + annotation: ElasticsearchAnnotationQuery; + range: TimeRange; +} + +export type RangeMap = Record;