From 2a34293689a703f586a25abba30f94bcc8431fc9 Mon Sep 17 00:00:00 2001 From: Ashley Harrison Date: Thu, 9 Feb 2023 09:03:13 +0000 Subject: [PATCH] Chore: more type fixes (#62952) * fix some more any/type assertions * more any/type assertion fixes * implement review comments --- .betterer.results | 199 +----------- .../datasource/loki/getDerivedFields.ts | 4 +- .../loki/liveStreamsResultTransformer.ts | 10 +- .../components/LokiQueryBuilder.tsx | 2 +- .../components/LokiQueryBuilderContainer.tsx | 4 +- .../datasource/loki/querybuilder/parsing.ts | 6 +- .../datasource/loki/querybuilder/state.ts | 9 +- .../datasource/mixed/MixedDataSource.test.ts | 24 +- .../datasource/mixed/MixedDataSource.ts | 6 +- .../components/PromExploreExtraField.test.tsx | 9 +- .../prometheus/components/PromLink.test.tsx | 6 +- .../components/PromQueryEditorByApp.test.tsx | 5 +- .../components/PromQueryField.test.tsx | 7 +- .../components/PrometheusMetricsBrowser.tsx | 2 +- .../configuration/AzureCredentials.ts | 4 +- .../datasource/prometheus/datasource.test.ts | 302 +++++++++--------- .../datasource/testdata/QueryEditor.test.tsx | 3 +- .../components/PredictablePulseEditor.tsx | 9 +- .../testdata/components/RawFrameEditor.tsx | 2 +- .../components/SimulationQueryEditor.tsx | 2 +- .../plugins/datasource/testdata/constants.ts | 4 +- .../datasource/testdata/nodeGraphUtils.ts | 2 +- .../plugins/datasource/testdata/runStreams.ts | 6 +- public/app/plugins/panel/barchart/bars.ts | 6 +- public/app/plugins/panel/barchart/module.tsx | 2 +- public/app/plugins/panel/barchart/utils.ts | 6 +- .../plugins/panel/debug/RenderInfoViewer.tsx | 6 +- .../plugins/panel/gauge/GaugeMigrations.ts | 4 +- .../panel/gettingstarted/GettingStarted.tsx | 4 +- .../panel/gettingstarted/components/Step.tsx | 4 +- .../app/plugins/panel/gettingstarted/types.ts | 7 +- 31 files changed, 259 insertions(+), 407 deletions(-) diff --git a/.betterer.results b/.betterer.results index 5ceb2e76e47..21e2d29019f 100644 --- a/.betterer.results +++ b/.betterer.results @@ -6030,27 +6030,12 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "7"] ], "public/app/plugins/datasource/loki/getDerivedFields.ts:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"], - [0, 0, 0, "Do not use any type assertions.", "1"] - ], - "public/app/plugins/datasource/loki/liveStreamsResultTransformer.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], "public/app/plugins/datasource/loki/querybuilder/binaryScalarOperations.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], "public/app/plugins/datasource/loki/querybuilder/components/LokiQueryBuilder.tsx:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"], - [0, 0, 0, "Do not use any type assertions.", "1"] - ], - "public/app/plugins/datasource/loki/querybuilder/components/LokiQueryBuilderContainer.tsx:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"] - ], - "public/app/plugins/datasource/loki/querybuilder/parsing.ts:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"], - [0, 0, 0, "Do not use any type assertions.", "1"] - ], - "public/app/plugins/datasource/loki/querybuilder/state.ts:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"] ], "public/app/plugins/datasource/loki/streaming.ts:5381": [ @@ -6058,22 +6043,6 @@ exports[`better eslint`] = { [0, 0, 0, "Do not use any type assertions.", "1"], [0, 0, 0, "Unexpected any. Specify a different type.", "2"] ], - "public/app/plugins/datasource/mixed/MixedDataSource.test.ts:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"], - [0, 0, 0, "Unexpected any. Specify a different type.", "2"], - [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"] - ], - "public/app/plugins/datasource/mixed/MixedDataSource.ts:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"], - [0, 0, 0, "Do not use any type assertions.", "1"], - [0, 0, 0, "Do not use any type assertions.", "2"] - ], "public/app/plugins/datasource/opentsdb/datasource.d.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], @@ -6144,22 +6113,11 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "1"] ], - "public/app/plugins/datasource/prometheus/components/PromExploreExtraField.test.tsx:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"] - ], - "public/app/plugins/datasource/prometheus/components/PromLink.test.tsx:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"] - ], "public/app/plugins/datasource/prometheus/components/PromLink.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"] ], - "public/app/plugins/datasource/prometheus/components/PromQueryEditorByApp.test.tsx:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"] - ], "public/app/plugins/datasource/prometheus/components/PromQueryField.test.tsx:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"], - [0, 0, 0, "Unexpected any. Specify a different type.", "2"] + [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], "public/app/plugins/datasource/prometheus/components/PromQueryField.tsx:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], @@ -6170,15 +6128,11 @@ exports[`better eslint`] = { ], "public/app/plugins/datasource/prometheus/components/PrometheusMetricsBrowser.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], - [0, 0, 0, "Do not use any type assertions.", "1"], - [0, 0, 0, "Do not use any type assertions.", "2"] + [0, 0, 0, "Do not use any type assertions.", "1"] ], "public/app/plugins/datasource/prometheus/components/monaco-query-field/MonacoQueryField.tsx:5381": [ [0, 0, 0, "Use data-testid for E2E selectors instead of aria-label", "0"] ], - "public/app/plugins/datasource/prometheus/configuration/AzureCredentials.ts:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"] - ], "public/app/plugins/datasource/prometheus/configuration/AzureCredentialsConfig.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "1"], @@ -6227,102 +6181,7 @@ exports[`better eslint`] = { [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.", "23"], - [0, 0, 0, "Unexpected any. Specify a different type.", "24"], - [0, 0, 0, "Unexpected any. Specify a different type.", "25"], - [0, 0, 0, "Unexpected any. Specify a different type.", "26"], - [0, 0, 0, "Unexpected any. Specify a different type.", "27"], - [0, 0, 0, "Unexpected any. Specify a different type.", "28"], - [0, 0, 0, "Unexpected any. Specify a different type.", "29"], - [0, 0, 0, "Unexpected any. Specify a different type.", "30"], - [0, 0, 0, "Unexpected any. Specify a different type.", "31"], - [0, 0, 0, "Unexpected any. Specify a different type.", "32"], - [0, 0, 0, "Unexpected any. Specify a different type.", "33"], - [0, 0, 0, "Unexpected any. Specify a different type.", "34"], - [0, 0, 0, "Unexpected any. Specify a different type.", "35"], - [0, 0, 0, "Unexpected any. Specify a different type.", "36"], - [0, 0, 0, "Unexpected any. Specify a different type.", "37"], - [0, 0, 0, "Unexpected any. Specify a different type.", "38"], - [0, 0, 0, "Unexpected any. Specify a different type.", "39"], - [0, 0, 0, "Unexpected any. Specify a different type.", "40"], - [0, 0, 0, "Unexpected any. Specify a different type.", "41"], - [0, 0, 0, "Unexpected any. Specify a different type.", "42"], - [0, 0, 0, "Unexpected any. Specify a different type.", "43"], - [0, 0, 0, "Unexpected any. Specify a different type.", "44"], - [0, 0, 0, "Unexpected any. Specify a different type.", "45"], - [0, 0, 0, "Unexpected any. Specify a different type.", "46"], - [0, 0, 0, "Unexpected any. Specify a different type.", "47"], - [0, 0, 0, "Unexpected any. Specify a different type.", "48"], - [0, 0, 0, "Unexpected any. Specify a different type.", "49"], - [0, 0, 0, "Unexpected any. Specify a different type.", "50"], - [0, 0, 0, "Unexpected any. Specify a different type.", "51"], - [0, 0, 0, "Unexpected any. Specify a different type.", "52"], - [0, 0, 0, "Unexpected any. Specify a different type.", "53"], - [0, 0, 0, "Unexpected any. Specify a different type.", "54"], - [0, 0, 0, "Unexpected any. Specify a different type.", "55"], - [0, 0, 0, "Unexpected any. Specify a different type.", "56"], - [0, 0, 0, "Unexpected any. Specify a different type.", "57"], - [0, 0, 0, "Unexpected any. Specify a different type.", "58"], - [0, 0, 0, "Unexpected any. Specify a different type.", "59"], - [0, 0, 0, "Unexpected any. Specify a different type.", "60"], - [0, 0, 0, "Unexpected any. Specify a different type.", "61"], - [0, 0, 0, "Unexpected any. Specify a different type.", "62"], - [0, 0, 0, "Unexpected any. Specify a different type.", "63"], - [0, 0, 0, "Unexpected any. Specify a different type.", "64"], - [0, 0, 0, "Unexpected any. Specify a different type.", "65"], - [0, 0, 0, "Unexpected any. Specify a different type.", "66"], - [0, 0, 0, "Unexpected any. Specify a different type.", "67"], - [0, 0, 0, "Unexpected any. Specify a different type.", "68"], - [0, 0, 0, "Unexpected any. Specify a different type.", "69"], - [0, 0, 0, "Unexpected any. Specify a different type.", "70"], - [0, 0, 0, "Unexpected any. Specify a different type.", "71"], - [0, 0, 0, "Unexpected any. Specify a different type.", "72"], - [0, 0, 0, "Unexpected any. Specify a different type.", "73"], - [0, 0, 0, "Unexpected any. Specify a different type.", "74"], - [0, 0, 0, "Unexpected any. Specify a different type.", "75"], - [0, 0, 0, "Unexpected any. Specify a different type.", "76"], - [0, 0, 0, "Unexpected any. Specify a different type.", "77"], - [0, 0, 0, "Unexpected any. Specify a different type.", "78"], - [0, 0, 0, "Unexpected any. Specify a different type.", "79"], - [0, 0, 0, "Unexpected any. Specify a different type.", "80"], - [0, 0, 0, "Unexpected any. Specify a different type.", "81"], - [0, 0, 0, "Unexpected any. Specify a different type.", "82"], - [0, 0, 0, "Unexpected any. Specify a different type.", "83"], - [0, 0, 0, "Unexpected any. Specify a different type.", "84"], - [0, 0, 0, "Unexpected any. Specify a different type.", "85"], - [0, 0, 0, "Unexpected any. Specify a different type.", "86"], - [0, 0, 0, "Unexpected any. Specify a different type.", "87"], - [0, 0, 0, "Unexpected any. Specify a different type.", "88"], - [0, 0, 0, "Unexpected any. Specify a different type.", "89"], - [0, 0, 0, "Unexpected any. Specify a different type.", "90"], - [0, 0, 0, "Unexpected any. Specify a different type.", "91"], - [0, 0, 0, "Unexpected any. Specify a different type.", "92"], - [0, 0, 0, "Unexpected any. Specify a different type.", "93"], - [0, 0, 0, "Unexpected any. Specify a different type.", "94"], - [0, 0, 0, "Unexpected any. Specify a different type.", "95"], - [0, 0, 0, "Unexpected any. Specify a different type.", "96"], - [0, 0, 0, "Unexpected any. Specify a different type.", "97"], - [0, 0, 0, "Unexpected any. Specify a different type.", "98"], - [0, 0, 0, "Unexpected any. Specify a different type.", "99"], - [0, 0, 0, "Unexpected any. Specify a different type.", "100"], - [0, 0, 0, "Unexpected any. Specify a different type.", "101"], - [0, 0, 0, "Unexpected any. Specify a different type.", "102"], - [0, 0, 0, "Unexpected any. Specify a different type.", "103"], - [0, 0, 0, "Unexpected any. Specify a different type.", "104"], - [0, 0, 0, "Unexpected any. Specify a different type.", "105"], - [0, 0, 0, "Unexpected any. Specify a different type.", "106"] + [0, 0, 0, "Unexpected any. Specify a different type.", "11"] ], "public/app/plugins/datasource/prometheus/datasource.tsx:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], @@ -6613,9 +6472,6 @@ exports[`better eslint`] = { "public/app/plugins/datasource/testdata/ConfigEditor.tsx:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], - "public/app/plugins/datasource/testdata/QueryEditor.test.tsx:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"] - ], "public/app/plugins/datasource/testdata/QueryEditor.tsx:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Do not use any type assertions.", "1"], @@ -6624,9 +6480,6 @@ exports[`better eslint`] = { [0, 0, 0, "Do not use any type assertions.", "4"], [0, 0, 0, "Unexpected any. Specify a different type.", "5"] ], - "public/app/plugins/datasource/testdata/components/PredictablePulseEditor.tsx:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"] - ], "public/app/plugins/datasource/testdata/components/RandomWalkEditor.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Do not use any type assertions.", "1"], @@ -6634,15 +6487,13 @@ exports[`better eslint`] = { [0, 0, 0, "Do not use any type assertions.", "3"] ], "public/app/plugins/datasource/testdata/components/RawFrameEditor.tsx:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"] + [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], "public/app/plugins/datasource/testdata/components/SimulationQueryEditor.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Do not use any type assertions.", "1"], [0, 0, 0, "Unexpected any. Specify a different type.", "2"], - [0, 0, 0, "Do not use any type assertions.", "3"], - [0, 0, 0, "Unexpected any. Specify a different type.", "4"] + [0, 0, 0, "Unexpected any. Specify a different type.", "3"] ], "public/app/plugins/datasource/testdata/components/SimulationSchemaForm.tsx:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], @@ -6651,9 +6502,6 @@ exports[`better eslint`] = { "public/app/plugins/datasource/testdata/components/StreamingClientEditor.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"] ], - "public/app/plugins/datasource/testdata/constants.ts:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"] - ], "public/app/plugins/datasource/testdata/datasource.ts:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "1"], @@ -6664,14 +6512,10 @@ exports[`better eslint`] = { ], "public/app/plugins/datasource/testdata/nodeGraphUtils.ts:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"], - [0, 0, 0, "Unexpected any. Specify a different type.", "2"] + [0, 0, 0, "Unexpected any. Specify a different type.", "1"] ], "public/app/plugins/datasource/testdata/runStreams.ts:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"], - [0, 0, 0, "Unexpected any. Specify a different type.", "2"], - [0, 0, 0, "Unexpected any. Specify a different type.", "3"] + [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], "public/app/plugins/datasource/testdata/testData/serviceMapResponse.ts:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], @@ -6756,27 +6600,18 @@ exports[`better eslint`] = { [0, 0, 0, "Do not use any type assertions.", "1"] ], "public/app/plugins/panel/barchart/bars.ts:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"], + [0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "1"], - [0, 0, 0, "Unexpected any. Specify a different type.", "2"], - [0, 0, 0, "Do not use any type assertions.", "3"], - [0, 0, 0, "Unexpected any. Specify a different type.", "4"], - [0, 0, 0, "Do not use any type assertions.", "5"], - [0, 0, 0, "Unexpected any. Specify a different type.", "6"], - [0, 0, 0, "Do not use any type assertions.", "7"] + [0, 0, 0, "Do not use any type assertions.", "2"], + [0, 0, 0, "Unexpected any. Specify a different type.", "3"], + [0, 0, 0, "Do not use any type assertions.", "4"] ], "public/app/plugins/panel/barchart/module.tsx:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"] + [0, 0, 0, "Do not use any type assertions.", "0"] ], "public/app/plugins/panel/barchart/quadtree.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], - "public/app/plugins/panel/barchart/utils.ts:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"], - [0, 0, 0, "Unexpected any. Specify a different type.", "2"] - ], "public/app/plugins/panel/candlestick/CandlestickPanel.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Do not use any type assertions.", "1"], @@ -6860,13 +6695,8 @@ exports[`better eslint`] = { [0, 0, 0, "Do not use any type assertions.", "1"], [0, 0, 0, "Unexpected any. Specify a different type.", "2"] ], - "public/app/plugins/panel/debug/RenderInfoViewer.tsx:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"] - ], "public/app/plugins/panel/gauge/GaugeMigrations.ts:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"], - [0, 0, 0, "Do not use any type assertions.", "2"] + [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], "public/app/plugins/panel/geomap/components/MarkersLegend.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], @@ -6933,9 +6763,6 @@ exports[`better eslint`] = { [0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Do not use any type assertions.", "1"] ], - "public/app/plugins/panel/gettingstarted/components/Step.tsx:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"] - ], "public/app/plugins/panel/graph/GraphContextMenuCtrl.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "1"] diff --git a/public/app/plugins/datasource/loki/getDerivedFields.ts b/public/app/plugins/datasource/loki/getDerivedFields.ts index 5c0a465c051..6fae391a7ef 100644 --- a/public/app/plugins/datasource/loki/getDerivedFields.ts +++ b/public/app/plugins/datasource/loki/getDerivedFields.ts @@ -38,7 +38,7 @@ export function getDerivedFields(dataFrame: DataFrame, derivedFieldConfigs: Deri function fieldFromDerivedFieldConfig(derivedFieldConfigs: DerivedFieldConfig[]): Field { const dataSourceSrv = getDataSourceSrv(); - const dataLinks = derivedFieldConfigs.reduce((acc, derivedFieldConfig) => { + const dataLinks = derivedFieldConfigs.reduce((acc, derivedFieldConfig) => { // Having field.datasourceUid means it is an internal link. if (derivedFieldConfig.datasourceUid) { const dsSettings = dataSourceSrv.getInstanceSettings(derivedFieldConfig.datasourceUid); @@ -63,7 +63,7 @@ function fieldFromDerivedFieldConfig(derivedFieldConfigs: DerivedFieldConfig[]): }); } return acc; - }, [] as DataLink[]); + }, []); return { name: derivedFieldConfigs[0].name, diff --git a/public/app/plugins/datasource/loki/liveStreamsResultTransformer.ts b/public/app/plugins/datasource/loki/liveStreamsResultTransformer.ts index 894bcdcf096..79c0c79575a 100644 --- a/public/app/plugins/datasource/loki/liveStreamsResultTransformer.ts +++ b/public/app/plugins/datasource/loki/liveStreamsResultTransformer.ts @@ -27,7 +27,7 @@ export function appendResponseToBufferedData(response: LokiTailResponse, data: M // We are comparing used ids only within the received stream. This could be a problem if the same line + labels + nanosecond timestamp came in 2 separate batches. // As this is very unlikely, and the result would only affect live-tailing css animation we have decided to not compare all received uids from data param as this would slow down processing. - const usedUids: { string?: number } = {}; + const usedUids: Record = {}; for (const stream of streams) { // Find unique labels @@ -45,7 +45,13 @@ export function appendResponseToBufferedData(response: LokiTailResponse, data: M } } -function createUid(ts: string, labelsString: string, line: string, usedUids: any, refId?: string): string { +function createUid( + ts: string, + labelsString: string, + line: string, + usedUids: Record, + refId?: string +): string { // Generate id as hashed nanosecond timestamp, labels and line (this does not have to be unique) let id = uuidv5(`${ts}_${labelsString}_${line}`, UUID_NAMESPACE); diff --git a/public/app/plugins/datasource/loki/querybuilder/components/LokiQueryBuilder.tsx b/public/app/plugins/datasource/loki/querybuilder/components/LokiQueryBuilder.tsx index 7fa74deefaa..9ff76b04b3f 100644 --- a/public/app/plugins/datasource/loki/querybuilder/components/LokiQueryBuilder.tsx +++ b/public/app/plugins/datasource/loki/querybuilder/components/LokiQueryBuilder.tsx @@ -45,7 +45,7 @@ export const LokiQueryBuilder = React.memo(({ datasource, query, onChange return [...datasource.getVariables(), ...options].map((value) => ({ label: value, value })); }; - const onGetLabelNames = async (forLabel: Partial): Promise => { + const onGetLabelNames = async (forLabel: Partial): Promise => { const labelsToConsider = query.labels.filter((x) => x !== forLabel); if (labelsToConsider.length === 0) { diff --git a/public/app/plugins/datasource/loki/querybuilder/components/LokiQueryBuilderContainer.tsx b/public/app/plugins/datasource/loki/querybuilder/components/LokiQueryBuilderContainer.tsx index 6ad2b7bed13..e903b47b3df 100644 --- a/public/app/plugins/datasource/loki/querybuilder/components/LokiQueryBuilderContainer.tsx +++ b/public/app/plugins/datasource/loki/querybuilder/components/LokiQueryBuilderContainer.tsx @@ -71,9 +71,11 @@ export function LokiQueryBuilderContainer(props: Props) { ); } +const initialState: State = { expr: '' }; + const stateSlice = createSlice({ name: 'loki-builder-container', - initialState: { expr: '' } as State, + initialState, reducers: { visualQueryChange: (state, action: PayloadAction<{ visQuery: LokiVisualQuery; expr: string }>) => { state.expr = action.payload.expr; diff --git a/public/app/plugins/datasource/loki/querybuilder/parsing.ts b/public/app/plugins/datasource/loki/querybuilder/parsing.ts index a248f491536..9fbaa784c00 100644 --- a/public/app/plugins/datasource/loki/querybuilder/parsing.ts +++ b/public/app/plugins/datasource/loki/querybuilder/parsing.ts @@ -241,7 +241,7 @@ function getLineFilter(expr: string, node: SyntaxNode): { operation?: QueryBuild }, }; } - const mapFilter: any = { + const mapFilter: Record = { '|=': LokiOperationId.LineContains, '!=': LokiOperationId.LineContainsNot, '|~': LokiOperationId.LineMatchesRegex, @@ -469,13 +469,13 @@ function handleVectorAggregation(expr: string, node: SyntaxNode, context: Contex return op; } -const operatorToOpName = binaryScalarDefs.reduce((acc, def) => { +const operatorToOpName = binaryScalarDefs.reduce>((acc, def) => { acc[def.sign] = { id: def.id, comparison: def.comparison, }; return acc; -}, {} as Record); +}, {}); /** * Right now binary expressions can be represented in 2 way in visual query. As additional operation in case it is diff --git a/public/app/plugins/datasource/loki/querybuilder/state.ts b/public/app/plugins/datasource/loki/querybuilder/state.ts index 821051d87f9..759a6626d9f 100644 --- a/public/app/plugins/datasource/loki/querybuilder/state.ts +++ b/public/app/plugins/datasource/loki/querybuilder/state.ts @@ -20,11 +20,12 @@ export function getDefaultEditorMode(expr: string) { return QueryEditorMode.Code; } - const value = store.get(queryEditorModeDefaultLocalStorageKey) as QueryEditorMode; + const value: string | undefined = store.get(queryEditorModeDefaultLocalStorageKey); switch (value) { - case QueryEditorMode.Builder: - case QueryEditorMode.Code: - return value; + case 'code': + return QueryEditorMode.Code; + + case 'builder': default: return QueryEditorMode.Builder; } diff --git a/public/app/plugins/datasource/mixed/MixedDataSource.test.ts b/public/app/plugins/datasource/mixed/MixedDataSource.test.ts index 7fa133926c5..b66d083be51 100644 --- a/public/app/plugins/datasource/mixed/MixedDataSource.test.ts +++ b/public/app/plugins/datasource/mixed/MixedDataSource.test.ts @@ -2,7 +2,7 @@ import { lastValueFrom } from 'rxjs'; import { getQueryOptions } from 'test/helpers/getQueryOptions'; import { DatasourceSrvMock, MockObservableDataSourceApi } from 'test/mocks/datasource_srv'; -import { LoadingState } from '@grafana/data'; +import { DataQueryRequest, DataSourceInstanceSettings, LoadingState } from '@grafana/data'; import { MIXED_DATASOURCE_NAME } from './MixedDataSource'; import { MixedDatasource } from './module'; @@ -30,7 +30,7 @@ jest.mock('@grafana/runtime', () => ({ describe('MixedDatasource', () => { describe('with no errors', () => { it('direct query should return results', async () => { - const ds = new MixedDatasource({} as any); + const ds = new MixedDatasource({} as DataSourceInstanceSettings); const requestMixed = getQueryOptions({ targets: [ { refId: 'QA', datasource: { uid: 'A' } }, // 1 @@ -52,7 +52,7 @@ describe('MixedDatasource', () => { describe('with errors', () => { it('direct query should return results', async () => { - const ds = new MixedDatasource({} as any); + const ds = new MixedDatasource({} as DataSourceInstanceSettings); const requestMixed = getQueryOptions({ targets: [ { refId: 'QA', datasource: { uid: 'A' } }, // 1 @@ -84,14 +84,14 @@ describe('MixedDatasource', () => { }); it('should return both query results from the same data source', async () => { - const ds = new MixedDatasource({} as any); - const request: any = { + const ds = new MixedDatasource({} as DataSourceInstanceSettings); + const request = { targets: [ { refId: 'A', datasource: { uid: 'Loki' } }, { refId: 'B', datasource: { uid: 'Loki' } }, { refId: 'C', datasource: { uid: 'A' } }, ], - }; + } as DataQueryRequest; await expect(ds.query(request)).toEmitValuesWith((results) => { expect(results).toHaveLength(3); @@ -104,14 +104,14 @@ describe('MixedDatasource', () => { }); it('should not return the error for the second time', async () => { - const ds = new MixedDatasource({} as any); - const request: any = { + const ds = new MixedDatasource({} as DataSourceInstanceSettings); + const request = { targets: [ { refId: 'A', datasource: 'Loki' }, { refId: 'DD', datasource: 'D' }, { refId: 'C', datasource: 'A' }, ], - }; + } as unknown as DataQueryRequest; await lastValueFrom(ds.query(request)); @@ -121,7 +121,7 @@ describe('MixedDatasource', () => { { refId: 'QA', datasource: { uid: 'A' } }, { refId: 'QB', datasource: { uid: 'B' } }, ], - } as any) + } as DataQueryRequest) ).toEmitValuesWith((results) => { expect(results).toHaveLength(2); expect(results[0].key).toBe('mixed-0-'); @@ -131,12 +131,12 @@ describe('MixedDatasource', () => { }); it('should filter out MixedDataSource queries', async () => { - const ds = new MixedDatasource({} as any); + const ds = new MixedDatasource({} as DataSourceInstanceSettings); await expect( ds.query({ targets: [{ refId: 'A', datasource: { uid: MIXED_DATASOURCE_NAME, id: 'datasource' } }], - } as any) + } as unknown as DataQueryRequest) ).toEmitValuesWith((results) => { expect(results).toHaveLength(1); expect(results[0].data).toHaveLength(0); diff --git a/public/app/plugins/datasource/mixed/MixedDataSource.ts b/public/app/plugins/datasource/mixed/MixedDataSource.ts index a266215149c..b6ca7a20591 100644 --- a/public/app/plugins/datasource/mixed/MixedDataSource.ts +++ b/public/app/plugins/datasource/mixed/MixedDataSource.ts @@ -31,7 +31,7 @@ export class MixedDatasource extends DataSourceApi { }); if (!queries.length) { - return of({ data: [] } as DataQueryResponse); // nothing + return of({ data: [] }); // nothing } // Build groups of queries to run in parallel @@ -49,7 +49,7 @@ export class MixedDatasource extends DataSourceApi { // Missing UIDs? if (!mixed.length) { - return of({ data: [] } as DataQueryResponse); // nothing + return of({ data: [] }); // nothing } return this.batchQueries(mixed, request); @@ -70,7 +70,7 @@ export class MixedDatasource extends DataSourceApi { data: response.data || [], state: LoadingState.Loading, key: `mixed-${i}-${response.key || ''}`, - } as DataQueryResponse; + }; }), toArray(), catchError((err) => { diff --git a/public/app/plugins/datasource/prometheus/components/PromExploreExtraField.test.tsx b/public/app/plugins/datasource/prometheus/components/PromExploreExtraField.test.tsx index b654037d06a..44fcd65b7a8 100644 --- a/public/app/plugins/datasource/prometheus/components/PromExploreExtraField.test.tsx +++ b/public/app/plugins/datasource/prometheus/components/PromExploreExtraField.test.tsx @@ -1,15 +1,18 @@ import { render, screen } from '@testing-library/react'; import React from 'react'; +import { PrometheusDatasource } from '../datasource'; +import { PromQuery } from '../types'; + import { PromExploreExtraFieldProps, PromExploreExtraField, testIds } from './PromExploreExtraField'; const setup = (propOverrides?: PromExploreExtraFieldProps) => { - const query = { exemplar: false }; - const datasource = {}; + const query = { exemplar: false } as PromQuery; + const datasource = {} as PrometheusDatasource; const onChange = jest.fn(); const onRunQuery = jest.fn(); - const props: any = { + const props: PromExploreExtraFieldProps = { onChange, onRunQuery, query, diff --git a/public/app/plugins/datasource/prometheus/components/PromLink.test.tsx b/public/app/plugins/datasource/prometheus/components/PromLink.test.tsx index 735133d40dc..5edb830120e 100644 --- a/public/app/plugins/datasource/prometheus/components/PromLink.test.tsx +++ b/public/app/plugins/datasource/prometheus/components/PromLink.test.tsx @@ -75,7 +75,11 @@ describe('PromLink', () => { render(
- +
); const promLinkButtons = screen.getAllByText('Prometheus'); diff --git a/public/app/plugins/datasource/prometheus/components/PromQueryEditorByApp.test.tsx b/public/app/plugins/datasource/prometheus/components/PromQueryEditorByApp.test.tsx index 036513e42b0..cd8a373b563 100644 --- a/public/app/plugins/datasource/prometheus/components/PromQueryEditorByApp.test.tsx +++ b/public/app/plugins/datasource/prometheus/components/PromQueryEditorByApp.test.tsx @@ -8,13 +8,14 @@ import { PrometheusDatasource } from '../datasource'; import { PromQueryEditorByApp } from './PromQueryEditorByApp'; import { testIds as alertingTestIds } from './PromQueryEditorForAlerting'; +import { Props } from './monaco-query-field/MonacoQueryFieldProps'; // the monaco-based editor uses lazy-loading and that does not work // well with this test, and we do not need the monaco-related // functionality in this test anyway, so we mock it out. jest.mock('./monaco-query-field/MonacoQueryFieldLazy', () => { - const fakeQueryField = (props: any) => { - return ; + const fakeQueryField = (props: Props) => { + return props.onBlur(e.currentTarget.value)} data-testid={'dummy-code-input'} type={'text'} />; }; return { MonacoQueryFieldLazy: fakeQueryField, diff --git a/public/app/plugins/datasource/prometheus/components/PromQueryField.test.tsx b/public/app/plugins/datasource/prometheus/components/PromQueryField.test.tsx index 0538d8c9455..6dc1d23111e 100644 --- a/public/app/plugins/datasource/prometheus/components/PromQueryField.test.tsx +++ b/public/app/plugins/datasource/prometheus/components/PromQueryField.test.tsx @@ -9,13 +9,14 @@ import { PrometheusDatasource } from '../datasource'; import PromQlLanguageProvider from '../language_provider'; import PromQueryField from './PromQueryField'; +import { Props } from './monaco-query-field/MonacoQueryFieldProps'; // the monaco-based editor uses lazy-loading and that does not work // well with this test, and we do not need the monaco-related // functionality in this test anyway, so we mock it out. jest.mock('./monaco-query-field/MonacoQueryFieldLazy', () => { - const fakeQueryField = (props: any) => { - return ; + const fakeQueryField = (props: Props) => { + return props.onBlur(e.currentTarget.value)} data-testid={'dummy-code-input'} type={'text'} />; }; return { MonacoQueryFieldLazy: fakeQueryField, @@ -154,7 +155,7 @@ describe('PromQueryField', () => { function makeLanguageProvider(options: { metrics: string[][] }) { const metricsStack = [...options.metrics]; return { - histogramMetrics: [] as any, + histogramMetrics: [], metrics: [], metricsMetadata: {}, lookupsDisabled: false, diff --git a/public/app/plugins/datasource/prometheus/components/PrometheusMetricsBrowser.tsx b/public/app/plugins/datasource/prometheus/components/PrometheusMetricsBrowser.tsx index 0b2f27a9bf7..81421ffc49d 100644 --- a/public/app/plugins/datasource/prometheus/components/PrometheusMetricsBrowser.tsx +++ b/public/app/plugins/datasource/prometheus/components/PrometheusMetricsBrowser.tsx @@ -191,7 +191,7 @@ const getStyles = stylesFactory((theme: GrafanaTheme2) => ({ export class UnthemedPrometheusMetricsBrowser extends React.Component { valueListsRef = React.createRef(); state: BrowserState = { - labels: [] as SelectableLabel[], + labels: [], labelSearchTerm: '', metricSearchTerm: '', status: 'Ready', diff --git a/public/app/plugins/datasource/prometheus/configuration/AzureCredentials.ts b/public/app/plugins/datasource/prometheus/configuration/AzureCredentials.ts index 7ca0a69f064..c561b355153 100644 --- a/public/app/plugins/datasource/prometheus/configuration/AzureCredentials.ts +++ b/public/app/plugins/datasource/prometheus/configuration/AzureCredentials.ts @@ -7,11 +7,11 @@ export enum AzureCloud { None = '', } -export const KnownAzureClouds = [ +export const KnownAzureClouds: Array> = [ { value: AzureCloud.Public, label: 'Azure' }, { value: AzureCloud.China, label: 'Azure China' }, { value: AzureCloud.USGovernment, label: 'Azure US Government' }, -] as SelectableValue[]; +]; export type AzureAuthType = 'msi' | 'clientsecret'; diff --git a/public/app/plugins/datasource/prometheus/datasource.test.ts b/public/app/plugins/datasource/prometheus/datasource.test.ts index 2c2eb4c0573..8e8cce4aed7 100644 --- a/public/app/plugins/datasource/prometheus/datasource.test.ts +++ b/public/app/plugins/datasource/prometheus/datasource.test.ts @@ -2,8 +2,11 @@ import { cloneDeep } from 'lodash'; import { lastValueFrom, of, throwError } from 'rxjs'; import { + AnnotationEvent, + AnnotationQueryRequest, CoreApp, DataQueryRequest, + DataQueryResponse, DataQueryResponseData, DataSourceInstanceSettings, dateTime, @@ -25,32 +28,35 @@ import { prometheusRegularEscape, prometheusSpecialRegexEscape, } from './datasource'; -import { PromOptions, PromQuery } from './types'; +import PromQlLanguageProvider from './language_provider'; +import { PromOptions, PromQuery, PromQueryRequest } from './types'; const fetchMock = jest.fn().mockReturnValue(of(createDefaultPromResponse())); jest.mock('./metric_find_query'); jest.mock('@grafana/runtime', () => ({ - // @ts-ignore ...jest.requireActual('@grafana/runtime'), getBackendSrv: () => ({ fetch: fetchMock, }), })); +const getAdhocFiltersMock = jest.fn().mockImplementation(() => []); +const replaceMock = jest.fn().mockImplementation((a: string, ...rest: unknown[]) => a); + const templateSrvStub = { - getAdhocFilters: jest.fn(() => [] as any[]), - replace: jest.fn((a: string, ...rest: any) => a), -}; + getAdhocFilters: getAdhocFiltersMock, + replace: replaceMock, +} as unknown as TemplateSrv; const timeSrvStub = { - timeRange(): any { + timeRange() { return { from: dateTime(1531468681), to: dateTime(1531489712), }; }, -}; +} as unknown as TimeSrv; beforeEach(() => { jest.clearAllMocks(); @@ -67,11 +73,11 @@ describe('PrometheusDatasource', () => { password: 'mupp', jsonData: { customQueryParameters: '', - } as any, + }, } as unknown as DataSourceInstanceSettings; beforeEach(() => { - ds = new PrometheusDatasource(instanceSettings, templateSrvStub as any, timeSrvStub as any); + ds = new PrometheusDatasource(instanceSettings, templateSrvStub, timeSrvStub); }); describe('Query', () => { @@ -113,10 +119,10 @@ describe('PrometheusDatasource', () => { access: 'direct', jsonData: { customQueryParameters: '', - } as any, + }, } as unknown as DataSourceInstanceSettings; const range = { from: time({ seconds: 63 }), to: time({ seconds: 183 }) }; - const directDs = new PrometheusDatasource(instanceSettings, templateSrvStub as any, timeSrvStub as any); + const directDs = new PrometheusDatasource(instanceSettings, templateSrvStub, timeSrvStub); await expect( lastValueFrom(directDs.query(createDataRequest([{}, {}], { app: CoreApp.Dashboard }))) @@ -166,7 +172,7 @@ describe('PrometheusDatasource', () => { it('should still perform a GET request with the DS HTTP method set to POST and not POST-friendly endpoint', () => { const postSettings = cloneDeep(instanceSettings); postSettings.jsonData.httpMethod = 'POST'; - const promDs = new PrometheusDatasource(postSettings, templateSrvStub as any, timeSrvStub as any); + const promDs = new PrometheusDatasource(postSettings, templateSrvStub, timeSrvStub); promDs.metadataRequest('/foo'); expect(fetchMock.mock.calls.length).toBe(1); expect(fetchMock.mock.calls[0][0].method).toBe('GET'); @@ -174,7 +180,7 @@ describe('PrometheusDatasource', () => { it('should try to perform a POST request with the DS HTTP method set to POST and POST-friendly endpoint', () => { const postSettings = cloneDeep(instanceSettings); postSettings.jsonData.httpMethod = 'POST'; - const promDs = new PrometheusDatasource(postSettings, templateSrvStub as any, timeSrvStub as any); + const promDs = new PrometheusDatasource(postSettings, templateSrvStub, timeSrvStub); promDs.metadataRequest('api/v1/series', { bar: 'baz baz', foo: 'foo' }); expect(fetchMock.mock.calls.length).toBe(1); expect(fetchMock.mock.calls[0][0].method).toBe('POST'); @@ -190,14 +196,14 @@ describe('PrometheusDatasource', () => { range: { from: time({ seconds: 63 }), to: time({ seconds: 183 }) }, targets: [target], interval: '60s', - } as any; + } as DataQueryRequest; } describe('with GET http method', () => { const promDs = new PrometheusDatasource( - { ...instanceSettings, jsonData: { customQueryParameters: 'customQuery=123', httpMethod: 'GET' } as any }, - templateSrvStub as any, - timeSrvStub as any + { ...instanceSettings, jsonData: { customQueryParameters: 'customQuery=123', httpMethod: 'GET' } }, + templateSrvStub, + timeSrvStub ); it('added to metadata request', () => { @@ -230,9 +236,9 @@ describe('PrometheusDatasource', () => { describe('with POST http method', () => { const promDs = new PrometheusDatasource( - { ...instanceSettings, jsonData: { customQueryParameters: 'customQuery=123', httpMethod: 'POST' } as any }, - templateSrvStub as any, - timeSrvStub as any + { ...instanceSettings, jsonData: { customQueryParameters: 'customQuery=123', httpMethod: 'POST' } }, + templateSrvStub, + timeSrvStub ); it('added to metadata request with non-POST endpoint', () => { @@ -278,20 +284,19 @@ describe('PrometheusDatasource', () => { describe('When using adhoc filters', () => { const DEFAULT_QUERY_EXPRESSION = 'metric{job="foo"} - metric'; - const target = { expr: DEFAULT_QUERY_EXPRESSION }; - const originalAdhocFiltersMock = templateSrvStub.getAdhocFilters(); + const target: PromQuery = { expr: DEFAULT_QUERY_EXPRESSION, refId: 'A' }; afterAll(() => { - templateSrvStub.getAdhocFilters.mockReturnValue(originalAdhocFiltersMock); + getAdhocFiltersMock.mockImplementation(() => []); }); it('should not modify expression with no filters', () => { - const result = ds.createQuery(target as any, { interval: '15s' } as any, 0, 0); + const result = ds.createQuery(target, { interval: '15s' } as DataQueryRequest, 0, 0); expect(result).toMatchObject({ expr: DEFAULT_QUERY_EXPRESSION }); }); it('should add filters to expression', () => { - templateSrvStub.getAdhocFilters.mockReturnValue([ + getAdhocFiltersMock.mockReturnValue([ { key: 'k1', operator: '=', @@ -303,12 +308,12 @@ describe('PrometheusDatasource', () => { value: 'v2', }, ]); - const result = ds.createQuery(target as any, { interval: '15s' } as any, 0, 0); + const result = ds.createQuery(target, { interval: '15s' } as DataQueryRequest, 0, 0); expect(result).toMatchObject({ expr: 'metric{job="foo", k1="v1", k2!="v2"} - metric{k1="v1", k2!="v2"}' }); }); it('should add escaping if needed to regex filter expressions', () => { - templateSrvStub.getAdhocFilters.mockReturnValue([ + getAdhocFiltersMock.mockReturnValue([ { key: 'k1', operator: '=~', @@ -320,7 +325,7 @@ describe('PrometheusDatasource', () => { value: `v'.*`, }, ]); - const result = ds.createQuery(target as any, { interval: '15s' } as any, 0, 0); + const result = ds.createQuery(target, { interval: '15s' } as DataQueryRequest, 0, 0); expect(result).toMatchObject({ expr: `metric{job="foo", k1=~"v.*", k2=~"v\\\\'.*"} - metric{k1=~"v.*", k2=~"v\\\\'.*"}`, }); @@ -328,13 +333,13 @@ describe('PrometheusDatasource', () => { }); describe('When converting prometheus histogram to heatmap format', () => { - let query: any; + let query: DataQueryRequest; beforeEach(() => { query = { range: { from: dateTime(1443454528000), to: dateTime(1443454528000) }, targets: [{ expr: 'test{job="testjob"}', format: 'heatmap', legendFormat: '{{le}}' }], interval: '1s', - }; + } as DataQueryRequest; }); it('should convert cumulative histogram to ordinary', async () => { @@ -619,7 +624,7 @@ describe('PrometheusDatasource', () => { refId: 'A', }; const interval = '10m'; - templateSrvStub.replace.mockReturnValue(interval); + replaceMock.mockReturnValue(interval); const queries = ds.interpolateVariablesInQueries([query], { Interval: { text: interval, value: interval } }); expect(templateSrvStub.replace).toBeCalledTimes(2); @@ -640,11 +645,9 @@ describe('PrometheusDatasource', () => { }); describe('applyTemplateVariables', () => { - const originalAdhocFiltersMock = templateSrvStub.getAdhocFilters(); - const originalReplaceMock = jest.fn((a: string, ...rest: any) => a); afterAll(() => { - templateSrvStub.getAdhocFilters.mockReturnValue(originalAdhocFiltersMock); - templateSrvStub.replace = originalReplaceMock; + getAdhocFiltersMock.mockImplementation(() => []); + replaceMock.mockImplementation((a: string, ...rest: unknown[]) => a); }); it('should call replace function for legendFormat', () => { @@ -654,7 +657,7 @@ describe('PrometheusDatasource', () => { refId: 'A', }; const legend = 'baz'; - templateSrvStub.replace.mockReturnValue(legend); + replaceMock.mockReturnValue(legend); const interpolatedQuery = ds.applyTemplateVariables(query, { legend: { text: legend, value: legend } }); expect(interpolatedQuery.legendFormat).toBe(legend); @@ -667,7 +670,7 @@ describe('PrometheusDatasource', () => { refId: 'A', }; const step = '5s'; - templateSrvStub.replace.mockReturnValue(step); + replaceMock.mockReturnValue(step); const interpolatedQuery = ds.applyTemplateVariables(query, { step: { text: step, value: step } }); expect(interpolatedQuery.interval).toBe(step); @@ -679,15 +682,15 @@ describe('PrometheusDatasource', () => { refId: 'A', }; const job = 'bar'; - templateSrvStub.replace.mockReturnValue(job); + replaceMock.mockReturnValue(job); const interpolatedQuery = ds.applyTemplateVariables(query, { job: { text: job, value: job } }); expect(interpolatedQuery.expr).toBe(job); }); it('should add ad-hoc filters to expr', () => { - templateSrvStub.replace = jest.fn((a: string) => a); - templateSrvStub.getAdhocFilters.mockReturnValue([ + replaceMock.mockImplementation((a: string) => a); + getAdhocFiltersMock.mockReturnValue([ { key: 'k1', operator: '=', @@ -713,30 +716,25 @@ describe('PrometheusDatasource', () => { describe('metricFindQuery', () => { beforeEach(() => { const query = 'query_result(topk(5,rate(http_request_duration_microseconds_count[$__interval])))'; - templateSrvStub.replace = jest.fn(); ds.metricFindQuery(query); }); - afterAll(() => { - templateSrvStub.replace = jest.fn((a: string) => a); - }); - it('should call templateSrv.replace with scopedVars', () => { - expect(templateSrvStub.replace.mock.calls[0][1]).toBeDefined(); + expect(replaceMock.mock.calls[0][1]).toBeDefined(); }); it('should have the correct range and range_ms', () => { - const range = templateSrvStub.replace.mock.calls[0][1].__range; - const rangeMs = templateSrvStub.replace.mock.calls[0][1].__range_ms; - const rangeS = templateSrvStub.replace.mock.calls[0][1].__range_s; + const range = replaceMock.mock.calls[0][1].__range; + const rangeMs = replaceMock.mock.calls[0][1].__range_ms; + const rangeS = replaceMock.mock.calls[0][1].__range_s; expect(range).toEqual({ text: '21s', value: '21s' }); expect(rangeMs).toEqual({ text: 21031, value: 21031 }); expect(rangeS).toEqual({ text: 21, value: 21 }); }); it('should pass the default interval value', () => { - const interval = templateSrvStub.replace.mock.calls[0][1].__interval; - const intervalMs = templateSrvStub.replace.mock.calls[0][1].__interval_ms; + const interval = replaceMock.mock.calls[0][1].__interval; + const intervalMs = replaceMock.mock.calls[0][1].__interval_ms; expect(interval).toEqual({ text: '15s', value: '15s' }); expect(intervalMs).toEqual({ text: 15000, value: 15000 }); }); @@ -762,17 +760,17 @@ describe('PrometheusDatasource2', () => { let ds: PrometheusDatasource; beforeEach(() => { - ds = new PrometheusDatasource(instanceSettings, templateSrvStub as any, timeSrvStub as any); + ds = new PrometheusDatasource(instanceSettings, templateSrvStub, timeSrvStub); }); describe('When querying prometheus with one target using query editor target spec', () => { describe('and query syntax is valid', () => { - let results: any; + let results: DataQueryResponse; const query = { range: { from: time({ seconds: 63 }), to: time({ seconds: 183 }) }, targets: [{ expr: 'test{job="testjob"}', format: 'time_series' }], interval: '60s', - }; + } as DataQueryRequest; // Interval alignment with step const urlExpected = `proxied/api/v1/query_range?query=${encodeURIComponent( @@ -795,7 +793,7 @@ describe('PrometheusDatasource2', () => { }, }; fetchMock.mockImplementation(() => of(response)); - ds.query(query as any).subscribe((data: any) => { + ds.query(query).subscribe((data) => { results = data; }); }); @@ -819,7 +817,7 @@ describe('PrometheusDatasource2', () => { range: { from: time({ seconds: 63 }), to: time({ seconds: 183 }) }, targets: [{ expr: 'tes;;t{job="testjob"}', format: 'time_series' }], interval: '60s', - }; + } as DataQueryRequest; const errMessage = 'parse error at char 25: could not parse remaining input'; const response = { @@ -832,7 +830,7 @@ describe('PrometheusDatasource2', () => { it('should generate an error', () => { fetchMock.mockImplementation(() => throwError(response)); - ds.query(query as any).subscribe((e: any) => { + ds.query(query).subscribe((e: any) => { results = e.message; expect(results).toBe(`"${errMessage}"`); }); @@ -841,7 +839,7 @@ describe('PrometheusDatasource2', () => { }); describe('When querying prometheus with one target which returns multiple series', () => { - let results: any; + let results: DataQueryResponse; const start = 60; const end = 360; const step = 60; @@ -850,7 +848,7 @@ describe('PrometheusDatasource2', () => { range: { from: time({ seconds: start }), to: time({ seconds: end }) }, targets: [{ expr: 'test{job="testjob"}', format: 'time_series' }], interval: '60s', - }; + } as DataQueryRequest; beforeEach(async () => { const response = { @@ -878,7 +876,7 @@ describe('PrometheusDatasource2', () => { fetchMock.mockImplementation(() => of(response)); - ds.query(query as any).subscribe((data: any) => { + ds.query(query).subscribe((data) => { results = data; }); }); @@ -915,7 +913,7 @@ describe('PrometheusDatasource2', () => { }); describe('When querying prometheus with one target and instant = true', () => { - let results: any; + let results: DataQueryResponse; const urlExpected = `/api/datasources/uid/ABCDEF/resources/api/v1/query?query=${encodeURIComponent( 'test{job="testjob"}' )}&time=123`; @@ -923,7 +921,7 @@ describe('PrometheusDatasource2', () => { range: { from: time({ seconds: 63 }), to: time({ seconds: 123 }) }, targets: [{ expr: 'test{job="testjob"}', format: 'time_series', instant: true }], interval: '60s', - }; + } as DataQueryRequest; beforeEach(async () => { const response = { @@ -942,7 +940,7 @@ describe('PrometheusDatasource2', () => { }; fetchMock.mockImplementation(() => of(response)); - ds.query(query as any).subscribe((data: any) => { + ds.query(query).subscribe((data) => { results = data; }); }); @@ -962,8 +960,8 @@ describe('PrometheusDatasource2', () => { }); describe('annotationQuery', () => { - let results: any; - const options: any = { + let results: AnnotationEvent[]; + const options = { annotation: { expr: 'ALERTS{alertstate="firing"}', tagKeys: 'job', @@ -974,7 +972,7 @@ describe('PrometheusDatasource2', () => { from: time({ seconds: 63 }), to: time({ seconds: 123 }), }, - }; + } as unknown as AnnotationQueryRequest; const response = createAnnotationResponse(); @@ -982,7 +980,7 @@ describe('PrometheusDatasource2', () => { it('should return empty results', async () => { fetchMock.mockImplementation(() => of({ cancelled: true })); - await ds.annotationQuery(options).then((data: any) => { + await ds.annotationQuery(options).then((data) => { results = data; }); @@ -995,7 +993,7 @@ describe('PrometheusDatasource2', () => { options.annotation.useValueForTime = false; fetchMock.mockImplementation(() => of(response)); - await ds.annotationQuery(options).then((data: any) => { + await ds.annotationQuery(options).then((data) => { results = data; }); }); @@ -1014,7 +1012,7 @@ describe('PrometheusDatasource2', () => { options.annotation.useValueForTime = true; fetchMock.mockImplementation(() => of(response)); - await ds.annotationQuery(options).then((data: any) => { + await ds.annotationQuery(options).then((data) => { results = data; }); }); @@ -1036,7 +1034,7 @@ describe('PrometheusDatasource2', () => { from: time({ seconds: 63 }), to: time({ seconds: 123 }), }, - }; + } as AnnotationQueryRequest; ds.annotationQuery(query); const req = fetchMock.mock.calls[0][0]; expect(req.data.queries[0].interval).toBe('60s'); @@ -1053,7 +1051,7 @@ describe('PrometheusDatasource2', () => { from: time({ seconds: 63 }), to: time({ seconds: 123 }), }, - }; + } as unknown as AnnotationQueryRequest; ds.annotationQuery(query); const req = fetchMock.mock.calls[0][0]; expect(req.data.queries[0].interval).toBe('60s'); @@ -1071,7 +1069,7 @@ describe('PrometheusDatasource2', () => { from: time({ seconds: 63 }), to: time({ seconds: 123 }), }, - }; + } as unknown as AnnotationQueryRequest; ds.annotationQuery(query); const req = fetchMock.mock.calls[0][0]; expect(req.data.queries[0].interval).toBe('10s'); @@ -1135,10 +1133,10 @@ describe('PrometheusDatasource2', () => { expect(results.map((result) => [result.time, result.timeEnd])).toEqual([[120000, 120000]]); }); }); + describe('with template variables', () => { - const originalReplaceMock = jest.fn((a: string, ...rest: any) => a); afterAll(() => { - templateSrvStub.replace = originalReplaceMock; + replaceMock.mockImplementation((a: string, ...rest: unknown[]) => a); }); it('should interpolate variables in query expr', () => { @@ -1152,9 +1150,9 @@ describe('PrometheusDatasource2', () => { from: time({ seconds: 1 }), to: time({ seconds: 2 }), }, - }; + } as unknown as AnnotationQueryRequest; const interpolated = 'interpolated_expr'; - templateSrvStub.replace.mockReturnValue(interpolated); + replaceMock.mockReturnValue(interpolated); ds.annotationQuery(query); const req = fetchMock.mock.calls[0][0]; expect(req.data.queries[0].expr).toBe(interpolated); @@ -1163,12 +1161,12 @@ describe('PrometheusDatasource2', () => { }); describe('When resultFormat is table and instant = true', () => { - let results: any; + let results: DataQueryResponse; const query = { range: { from: time({ seconds: 63 }), to: time({ seconds: 123 }) }, targets: [{ expr: 'test{job="testjob"}', format: 'time_series', instant: true }], interval: '60s', - }; + } as DataQueryRequest; beforeEach(async () => { const response = { @@ -1187,7 +1185,7 @@ describe('PrometheusDatasource2', () => { }; fetchMock.mockImplementation(() => of(response)); - ds.query(query as any).subscribe((data: any) => { + ds.query(query).subscribe((data: any) => { results = data; }); }); @@ -1219,11 +1217,11 @@ describe('PrometheusDatasource2', () => { }, ], interval: '5s', - }; + } as DataQueryRequest; const urlExpected = 'proxied/api/v1/query_range?query=test&start=60&end=420&step=10'; fetchMock.mockImplementation(() => of(response)); - ds.query(query as any); + ds.query(query); const res = fetchMock.mock.calls[0][0]; expect(res.method).toBe('GET'); expect(res.url).toBe(urlExpected); @@ -1235,10 +1233,10 @@ describe('PrometheusDatasource2', () => { range: { from: time({ minutes: 1 }), to: time({ minutes: 7 }) }, targets: [{ expr: 'test' }], interval: '100ms', - }; + } as DataQueryRequest; const urlExpected = 'proxied/api/v1/query_range?query=test&start=60&end=420&step=0.1'; fetchMock.mockImplementation(() => of(response)); - ds.query(query as any); + ds.query(query); const res = fetchMock.mock.calls[0][0]; expect(res.method).toBe('GET'); expect(res.url).toBe(urlExpected); @@ -1255,10 +1253,10 @@ describe('PrometheusDatasource2', () => { }, ], interval: '10s', - }; + } as DataQueryRequest; const urlExpected = 'proxied/api/v1/query_range?query=test&start=60&end=420&step=10'; fetchMock.mockImplementation(() => of(response)); - ds.query(query as any); + ds.query(query); const res = fetchMock.mock.calls[0][0]; expect(res.method).toBe('GET'); expect(res.url).toBe(urlExpected); @@ -1270,12 +1268,12 @@ describe('PrometheusDatasource2', () => { range: { from: time({ hours: 1 }), to: time({ hours: 7 }) }, targets: [{ expr: 'test' }], interval: '1s', - }; + } as DataQueryRequest; const end = 7 * 60 * 60; const start = 60 * 60; const urlExpected = 'proxied/api/v1/query_range?query=test&start=' + start + '&end=' + end + '&step=2'; fetchMock.mockImplementation(() => of(response)); - ds.query(query as any); + ds.query(query); const res = fetchMock.mock.calls[0][0]; expect(res.method).toBe('GET'); expect(res.url).toBe(urlExpected); @@ -1293,11 +1291,11 @@ describe('PrometheusDatasource2', () => { }, ], interval: '5s', - }; + } as DataQueryRequest; // times get rounded up to interval const urlExpected = 'proxied/api/v1/query_range?query=test&start=50&end=400&step=50'; fetchMock.mockImplementation(() => of(response)); - ds.query(query as any); + ds.query(query); const res = fetchMock.mock.calls[0][0]; expect(res.method).toBe('GET'); expect(res.url).toBe(urlExpected); @@ -1315,10 +1313,10 @@ describe('PrometheusDatasource2', () => { }, ], interval: '5s', - }; + } as DataQueryRequest; const urlExpected = 'proxied/api/v1/query_range?query=test' + '&start=60&end=420&step=15'; fetchMock.mockImplementation(() => of(response)); - ds.query(query as any); + ds.query(query); const res = fetchMock.mock.calls[0][0]; expect(res.method).toBe('GET'); expect(res.url).toBe(urlExpected); @@ -1336,11 +1334,11 @@ describe('PrometheusDatasource2', () => { }, ], interval: '10s', - }; + } as DataQueryRequest; // times get aligned to interval const urlExpected = 'proxied/api/v1/query_range?query=test' + '&start=0&end=400&step=100'; fetchMock.mockImplementation(() => of(response)); - ds.query(query as any); + ds.query(query); const res = fetchMock.mock.calls[0][0]; expect(res.method).toBe('GET'); expect(res.url).toBe(urlExpected); @@ -1357,12 +1355,12 @@ describe('PrometheusDatasource2', () => { }, ], interval: '10s', - }; + } as DataQueryRequest; const end = 7 * 24 * 60 * 60; const start = 0; const urlExpected = 'proxied/api/v1/query_range?query=test' + '&start=' + start + '&end=' + end + '&step=100'; fetchMock.mockImplementation(() => of(response)); - ds.query(query as any); + ds.query(query); const res = fetchMock.mock.calls[0][0]; expect(res.method).toBe('GET'); expect(res.url).toBe(urlExpected); @@ -1379,7 +1377,7 @@ describe('PrometheusDatasource2', () => { }, ], interval: '5s', - }; + } as DataQueryRequest; let end = 7 * 24 * 60 * 60; end -= end % 55; const start = 0; @@ -1388,7 +1386,7 @@ describe('PrometheusDatasource2', () => { const urlExpected = 'proxied/api/v1/query_range?query=test' + '&start=' + adjusted.start + '&end=' + adjusted.end + '&step=' + step; fetchMock.mockImplementation(() => of(response)); - ds.query(query as any); + ds.query(query); const res = fetchMock.mock.calls[0][0]; expect(res.method).toBe('GET'); expect(res.url).toBe(urlExpected); @@ -1428,14 +1426,14 @@ describe('PrometheusDatasource2', () => { encodeURIComponent('rate(test[$__interval])') + '&start=60&end=420&step=10'; - templateSrvStub.replace = jest.fn((str) => str) as any; + replaceMock.mockImplementation((str) => str); fetchMock.mockImplementation(() => of(response)); ds.query(query as any); const res = fetchMock.mock.calls[0][0]; expect(res.method).toBe('GET'); expect(res.url).toBe(urlExpected); - expect(templateSrvStub.replace.mock.calls[0][1]).toEqual({ + expect(replaceMock.mock.calls[0][1]).toEqual({ __interval: { text: '10s', value: '10s', @@ -1468,13 +1466,13 @@ describe('PrometheusDatasource2', () => { encodeURIComponent('rate(test[$__interval])') + '&start=60&end=420&step=10'; fetchMock.mockImplementation(() => of(response)); - templateSrvStub.replace = jest.fn((str) => str) as any; + replaceMock.mockImplementation((str) => str); ds.query(query as any); const res = fetchMock.mock.calls[0][0]; expect(res.method).toBe('GET'); expect(res.url).toBe(urlExpected); - expect(templateSrvStub.replace.mock.calls[0][1]).toEqual({ + expect(replaceMock.mock.calls[0][1]).toEqual({ __interval: { text: '5s', value: '5s', @@ -1508,13 +1506,13 @@ describe('PrometheusDatasource2', () => { encodeURIComponent('rate(test[$__interval])') + '&start=0&end=400&step=100'; fetchMock.mockImplementation(() => of(response)); - templateSrvStub.replace = jest.fn((str) => str) as any; + replaceMock.mockImplementation((str) => str); ds.query(query as any); const res = fetchMock.mock.calls[0][0]; expect(res.method).toBe('GET'); expect(res.url).toBe(urlExpected); - expect(templateSrvStub.replace.mock.calls[0][1]).toEqual({ + expect(replaceMock.mock.calls[0][1]).toEqual({ __interval: { text: '10s', value: '10s', @@ -1553,14 +1551,14 @@ describe('PrometheusDatasource2', () => { encodeURIComponent('rate(test[$__interval])') + '&start=50&end=400&step=50'; - templateSrvStub.replace = jest.fn((str) => str) as any; + replaceMock.mockImplementation((str) => str); fetchMock.mockImplementation(() => of(response)); ds.query(query as any); const res = fetchMock.mock.calls[0][0]; expect(res.method).toBe('GET'); expect(res.url).toBe(urlExpected); - expect(templateSrvStub.replace.mock.calls[0][1]).toEqual({ + expect(replaceMock.mock.calls[0][1]).toEqual({ __interval: { text: '5s', value: '5s', @@ -1600,7 +1598,7 @@ describe('PrometheusDatasource2', () => { expect(res.method).toBe('GET'); expect(res.url).toBe(urlExpected); - expect(templateSrvStub.replace.mock.calls[0][1]).toEqual({ + expect(replaceMock.mock.calls[0][1]).toEqual({ __interval: { text: '5s', value: '5s', @@ -1643,13 +1641,13 @@ describe('PrometheusDatasource2', () => { '&step=' + step; fetchMock.mockImplementation(() => of(response)); - templateSrvStub.replace = jest.fn((str) => str) as any; + replaceMock.mockImplementation((str) => str); ds.query(query as any); const res = fetchMock.mock.calls[0][0]; expect(res.method).toBe('GET'); expect(res.url).toBe(urlExpected); - expect(templateSrvStub.replace.mock.calls[0][1]).toEqual({ + expect(replaceMock.mock.calls[0][1]).toEqual({ __interval: { text: '5s', value: '5s', @@ -1687,18 +1685,18 @@ describe('PrometheusDatasource2', () => { }, ], interval: '60s', - }; + } as DataQueryRequest; const urlExpected = `proxied/api/v1/query_range?query=${encodeURIComponent( query.targets[0].expr )}&start=0&end=3600&step=60`; - templateSrvStub.replace = jest.fn((str) => str) as any; + replaceMock.mockImplementation((str) => str); fetchMock.mockImplementation(() => of(response)); - ds.query(query as any); + ds.query(query); const res = fetchMock.mock.calls[0][0]; expect(res.url).toBe(urlExpected); - expect(templateSrvStub.replace.mock.calls[1][1]).toEqual({ + expect(replaceMock.mock.calls[1][1]).toEqual({ __range_s: { text: expectedRangeSecond, value: expectedRangeSecond, @@ -1723,46 +1721,46 @@ describe('PrometheusDatasource2', () => { const target = { expr: 'rate(process_cpu_seconds_total[$__rate_interval])', refId: 'A' }; beforeEach(() => { - templateSrvStub.replace.mockClear(); + replaceMock.mockClear(); }); it('should be 4 times the scrape interval if interval + scrape interval is lower', () => { - ds.createQuery(target, { interval: '15s' } as any, 0, 300); - expect(templateSrvStub.replace.mock.calls[1][1]['__rate_interval'].value).toBe('60s'); + ds.createQuery(target, { interval: '15s' } as DataQueryRequest, 0, 300); + expect(replaceMock.mock.calls[1][1]['__rate_interval'].value).toBe('60s'); }); it('should be interval + scrape interval if 4 times the scrape interval is lower', () => { - ds.createQuery(target, { interval: '5m' } as any, 0, 10080); - expect(templateSrvStub.replace.mock.calls[1][1]['__rate_interval'].value).toBe('315s'); + ds.createQuery(target, { interval: '5m' } as DataQueryRequest, 0, 10080); + expect(replaceMock.mock.calls[1][1]['__rate_interval'].value).toBe('315s'); }); it('should fall back to a scrape interval of 15s if min step is set to 0, resulting in 4*15s = 60s', () => { - ds.createQuery({ ...target, interval: '' }, { interval: '15s' } as any, 0, 300); - expect(templateSrvStub.replace.mock.calls[1][1]['__rate_interval'].value).toBe('60s'); + ds.createQuery({ ...target, interval: '' }, { interval: '15s' } as DataQueryRequest, 0, 300); + expect(replaceMock.mock.calls[1][1]['__rate_interval'].value).toBe('60s'); }); it('should be 4 times the scrape interval if min step set to 1m and interval is 15s', () => { // For a 5m graph, $__interval is 15s - ds.createQuery({ ...target, interval: '1m' }, { interval: '15s' } as any, 0, 300); - expect(templateSrvStub.replace.mock.calls[2][1]['__rate_interval'].value).toBe('240s'); + ds.createQuery({ ...target, interval: '1m' }, { interval: '15s' } as DataQueryRequest, 0, 300); + expect(replaceMock.mock.calls[2][1]['__rate_interval'].value).toBe('240s'); }); it('should be interval + scrape interval if min step set to 1m and interval is 5m', () => { // For a 7d graph, $__interval is 5m - ds.createQuery({ ...target, interval: '1m' }, { interval: '5m' } as any, 0, 10080); - expect(templateSrvStub.replace.mock.calls[2][1]['__rate_interval'].value).toBe('360s'); + ds.createQuery({ ...target, interval: '1m' }, { interval: '5m' } as DataQueryRequest, 0, 10080); + expect(replaceMock.mock.calls[2][1]['__rate_interval'].value).toBe('360s'); }); it('should be interval + scrape interval if resolution is set to 1/2 and interval is 10m', () => { // For a 7d graph, $__interval is 10m - ds.createQuery({ ...target, intervalFactor: 2 }, { interval: '10m' } as any, 0, 10080); - expect(templateSrvStub.replace.mock.calls[1][1]['__rate_interval'].value).toBe('1215s'); + ds.createQuery({ ...target, intervalFactor: 2 }, { interval: '10m' } as DataQueryRequest, 0, 10080); + expect(replaceMock.mock.calls[1][1]['__rate_interval'].value).toBe('1215s'); }); it('should be 4 times the scrape interval if resolution is set to 1/2 and interval is 15s', () => { // For a 5m graph, $__interval is 15s - ds.createQuery({ ...target, intervalFactor: 2 }, { interval: '15s' } as any, 0, 300); - expect(templateSrvStub.replace.mock.calls[1][1]['__rate_interval'].value).toBe('60s'); + ds.createQuery({ ...target, intervalFactor: 2 }, { interval: '15s' } as DataQueryRequest, 0, 300); + expect(replaceMock.mock.calls[1][1]['__rate_interval'].value).toBe('60s'); }); it('should interpolate min step if set', () => { - templateSrvStub.replace = jest.fn((_: string) => '15s'); - ds.createQuery({ ...target, interval: '$int' }, { interval: '15s' } as any, 0, 300); - expect(templateSrvStub.replace.mock.calls).toHaveLength(3); - templateSrvStub.replace = jest.fn((a: string) => a); + replaceMock.mockImplementation((_: string) => '15s'); + ds.createQuery({ ...target, interval: '$int' }, { interval: '15s' } as DataQueryRequest, 0, 300); + expect(replaceMock.mock.calls).toHaveLength(3); + replaceMock.mockImplementation((str) => str); }); }); @@ -1780,7 +1778,7 @@ describe('PrometheusDatasource2', () => { ds.languageProvider = { histogramMetrics: ['tns_request_duration_seconds_bucket'], - } as any; + } as PromQlLanguageProvider; const request = { targets: [targetA, targetB], @@ -1807,11 +1805,11 @@ describe('PrometheusDatasource for POST', () => { let ds: PrometheusDatasource; beforeEach(() => { - ds = new PrometheusDatasource(instanceSettings, templateSrvStub as any, timeSrvStub as any); + ds = new PrometheusDatasource(instanceSettings, templateSrvStub, timeSrvStub); }); describe('When querying prometheus with one target using query editor target spec', () => { - let results: any; + let results: DataQueryResponse; const urlExpected = 'proxied/api/v1/query_range'; const dataExpected = { query: 'test{job="testjob"}', @@ -1823,7 +1821,7 @@ describe('PrometheusDatasource for POST', () => { range: { from: time({ minutes: 1, seconds: 3 }), to: time({ minutes: 2, seconds: 3 }) }, targets: [{ expr: 'test{job="testjob"}', format: 'time_series' }], interval: '60s', - }; + } as DataQueryRequest; beforeEach(async () => { const response = { @@ -1841,7 +1839,7 @@ describe('PrometheusDatasource for POST', () => { }, }; fetchMock.mockImplementation(() => of(response)); - ds.query(query as any).subscribe((data: any) => { + ds.query(query).subscribe((data) => { results = data; }); }); @@ -1861,10 +1859,10 @@ describe('PrometheusDatasource for POST', () => { }); describe('When querying prometheus via check headers X-Dashboard-Id X-Panel-Id and X-Dashboard-UID', () => { - const options = { dashboardId: 1, panelId: 2, dashboardUID: 'WFlOM-jM1' }; + const options = { dashboardId: 1, panelId: 2, dashboardUID: 'WFlOM-jM1' } as DataQueryRequest; const httpOptions = { headers: {} as { [key: string]: number | undefined }, - }; + } as PromQueryRequest; const instanceSettings = { url: 'proxied', directUrl: 'direct', @@ -1884,7 +1882,7 @@ describe('PrometheusDatasource for POST', () => { }); it('with proxy access tracing headers should be added', () => { - ds._addTracingHeaders(httpOptions as any, options as any); + ds._addTracingHeaders(httpOptions, options); expect(httpOptions.headers['X-Dashboard-Id']).toBe(options.dashboardId); expect(httpOptions.headers['X-Panel-Id']).toBe(options.panelId); expect(httpOptions.headers['X-Dashboard-UID']).toBe(options.dashboardUID); @@ -1901,10 +1899,10 @@ describe('PrometheusDatasource for POST', () => { const mockDs = new PrometheusDatasource( { ...instanceSettings, url: 'http://127.0.0.1:8000' }, - templateSrvStub as any, - timeSrvStub as any + templateSrvStub, + timeSrvStub ); - mockDs._addTracingHeaders(httpOptions as any, options as any); + mockDs._addTracingHeaders(httpOptions, options); expect(httpOptions.headers['X-Dashboard-Id']).toBe(undefined); expect(httpOptions.headers['X-Panel-Id']).toBe(undefined); expect(httpOptions.headers['X-Dashboard-UID']).toBe(undefined); @@ -1921,7 +1919,7 @@ function getPrepareTargetsContext({ targets: PromQuery[]; app?: CoreApp; queryOptions?: Partial; - languageProvider?: any; + languageProvider?: PromQlLanguageProvider; }) { const instanceSettings = { url: 'proxied', @@ -1942,7 +1940,7 @@ function getPrepareTargetsContext({ ...queryOptions, } as unknown as DataQueryRequest; - const ds = new PrometheusDatasource(instanceSettings, templateSrvStub as any, timeSrvStub as any); + const ds = new PrometheusDatasource(instanceSettings, templateSrvStub, timeSrvStub); if (languageProvider) { ds.languageProvider = languageProvider; } @@ -2004,7 +2002,7 @@ describe('prepareTargets', () => { targets: [targetA, targetB], languageProvider: { histogramMetrics: ['tns_request_duration_seconds_bucket'], - }, + } as PromQlLanguageProvider, }); expect(queries).toHaveLength(3); expect(activeTargets).toHaveLength(3); @@ -2026,7 +2024,7 @@ describe('prepareTargets', () => { targets: [targetA, targetB], languageProvider: { histogramMetrics: ['tns_request_duration_seconds_bucket'], - }, + } as PromQlLanguageProvider, }); expect(queries).toHaveLength(4); expect(activeTargets).toHaveLength(4); @@ -2083,7 +2081,7 @@ describe('prepareTargets', () => { app: CoreApp.Explore, languageProvider: { histogramMetrics: ['tns_request_duration_seconds_bucket'], - }, + } as PromQlLanguageProvider, }); expect(queries).toHaveLength(6); expect(activeTargets).toHaveLength(6); @@ -2110,7 +2108,7 @@ describe('prepareTargets', () => { app: CoreApp.Explore, languageProvider: { histogramMetrics: ['tns_request_duration_seconds_bucket'], - }, + } as PromQlLanguageProvider, }); expect(queries).toHaveLength(5); expect(activeTargets).toHaveLength(5); @@ -2259,7 +2257,7 @@ describe('modifyQuery', () => { const query: PromQuery = { refId: 'A', expr: 'go_goroutines' }; const action = { options: { key: 'cluster', value: 'us-cluster' }, type: 'ADD_FILTER' }; const instanceSettings = { jsonData: {} } as unknown as DataSourceInstanceSettings; - const ds = new PrometheusDatasource(instanceSettings, templateSrvStub as any, timeSrvStub as any); + const ds = new PrometheusDatasource(instanceSettings, templateSrvStub, timeSrvStub); const result = ds.modifyQuery(query, action); @@ -2273,7 +2271,7 @@ describe('modifyQuery', () => { const query: PromQuery = { refId: 'A', expr: 'go_goroutines{cluster="us-cluster"}' }; const action = { options: { key: 'pod', value: 'pod-123' }, type: 'ADD_FILTER' }; const instanceSettings = { jsonData: {} } as unknown as DataSourceInstanceSettings; - const ds = new PrometheusDatasource(instanceSettings, templateSrvStub as any, timeSrvStub as any); + const ds = new PrometheusDatasource(instanceSettings, templateSrvStub, timeSrvStub); const result = ds.modifyQuery(query, action); @@ -2289,7 +2287,7 @@ describe('modifyQuery', () => { const query: PromQuery = { refId: 'A', expr: 'go_goroutines' }; const action = { options: { key: 'cluster', value: 'us-cluster' }, type: 'ADD_FILTER_OUT' }; const instanceSettings = { jsonData: {} } as unknown as DataSourceInstanceSettings; - const ds = new PrometheusDatasource(instanceSettings, templateSrvStub as any, timeSrvStub as any); + const ds = new PrometheusDatasource(instanceSettings, templateSrvStub, timeSrvStub); const result = ds.modifyQuery(query, action); @@ -2303,7 +2301,7 @@ describe('modifyQuery', () => { const query: PromQuery = { refId: 'A', expr: 'go_goroutines{cluster="us-cluster"}' }; const action = { options: { key: 'pod', value: 'pod-123' }, type: 'ADD_FILTER_OUT' }; const instanceSettings = { jsonData: {} } as unknown as DataSourceInstanceSettings; - const ds = new PrometheusDatasource(instanceSettings, templateSrvStub as any, timeSrvStub as any); + const ds = new PrometheusDatasource(instanceSettings, templateSrvStub, timeSrvStub); const result = ds.modifyQuery(query, action); diff --git a/public/app/plugins/datasource/testdata/QueryEditor.test.tsx b/public/app/plugins/datasource/testdata/QueryEditor.test.tsx index c4eeee9a3ad..1d031955b30 100644 --- a/public/app/plugins/datasource/testdata/QueryEditor.test.tsx +++ b/public/app/plugins/datasource/testdata/QueryEditor.test.tsx @@ -6,6 +6,7 @@ import { QueryEditor, Props } from './QueryEditor'; import { scenarios } from './__mocks__/scenarios'; import { defaultQuery } from './constants'; import { TestDataQueryType } from './dataquery.gen'; +import { TestDataDataSource } from './datasource'; import { defaultStreamQuery } from './runStreams'; beforeEach(() => { @@ -19,7 +20,7 @@ const props = { onChange: mockOnChange, datasource: { getScenarios: () => Promise.resolve(scenarios), - } as any, + } as TestDataDataSource, }; const setup = (testProps?: Partial) => { diff --git a/public/app/plugins/datasource/testdata/components/PredictablePulseEditor.tsx b/public/app/plugins/datasource/testdata/components/PredictablePulseEditor.tsx index 428d4053dfa..cdb2cb4455d 100644 --- a/public/app/plugins/datasource/testdata/components/PredictablePulseEditor.tsx +++ b/public/app/plugins/datasource/testdata/components/PredictablePulseEditor.tsx @@ -5,7 +5,12 @@ import { InlineField, InlineFieldRow, Input } from '@grafana/ui'; import { EditorProps } from '../QueryEditor'; import { PulseWaveQuery } from '../dataquery.gen'; -const fields = [ +const fields: Array<{ + label: string; + id: keyof PulseWaveQuery; + placeholder: string; + tooltip: string; +}> = [ { label: 'Step', id: 'timeStep', placeholder: '60', tooltip: 'The number of seconds between datapoints.' }, { label: 'On Count', @@ -46,7 +51,7 @@ export const PredictablePulseEditor = ({ onChange, query }: EditorProps) => { type="number" name={id} id={`pulseWave.${id}-${query.refId}`} - value={query.pulseWave?.[id as keyof PulseWaveQuery]} + value={query.pulseWave?.[id]} placeholder={placeholder} onChange={onInputChange} /> diff --git a/public/app/plugins/datasource/testdata/components/RawFrameEditor.tsx b/public/app/plugins/datasource/testdata/components/RawFrameEditor.tsx index 71678a51d55..db6b1ca5fad 100644 --- a/public/app/plugins/datasource/testdata/components/RawFrameEditor.tsx +++ b/public/app/plugins/datasource/testdata/components/RawFrameEditor.tsx @@ -21,7 +21,7 @@ export const RawFrameEditor = ({ onChange, query }: EditorProps) => { return; } - let data: any = undefined; + let data = undefined; // Copy paste from panel json if (isArray(json.series) && json.state) { diff --git a/public/app/plugins/datasource/testdata/components/SimulationQueryEditor.tsx b/public/app/plugins/datasource/testdata/components/SimulationQueryEditor.tsx index 742e7bed551..ac131ceb02b 100644 --- a/public/app/plugins/datasource/testdata/components/SimulationQueryEditor.tsx +++ b/public/app/plugins/datasource/testdata/components/SimulationQueryEditor.tsx @@ -31,7 +31,7 @@ export const SimulationQueryEditor = ({ onChange, query, ds }: EditorProps) => { // This only changes once const info = useAsync(async () => { - const v = (await ds.getResource('sims')) as SimInfo[]; + const v = await ds.getResource('sims'); return { sims: v, options: v.map((s) => ({ label: s.name, value: s.type, description: s.description })), diff --git a/public/app/plugins/datasource/testdata/constants.ts b/public/app/plugins/datasource/testdata/constants.ts index 7b20d743b0d..3acb06f3c53 100644 --- a/public/app/plugins/datasource/testdata/constants.ts +++ b/public/app/plugins/datasource/testdata/constants.ts @@ -1,6 +1,6 @@ -import { CSVWave, TestData, TestDataQueryType } from './dataquery.gen'; +import { CSVWave, PulseWaveQuery, TestData, TestDataQueryType } from './dataquery.gen'; -export const defaultPulseQuery: any = { +export const defaultPulseQuery: PulseWaveQuery = { timeStep: 60, onCount: 3, onValue: 2, diff --git a/public/app/plugins/datasource/testdata/nodeGraphUtils.ts b/public/app/plugins/datasource/testdata/nodeGraphUtils.ts index c4b5b86448f..66ee06d53c4 100644 --- a/public/app/plugins/datasource/testdata/nodeGraphUtils.ts +++ b/public/app/plugins/datasource/testdata/nodeGraphUtils.ts @@ -160,7 +160,7 @@ function makeRandomNode(index: number) { }; } -export function savedNodesResponse(): any { +export function savedNodesResponse() { return [new MutableDataFrame(nodes), new MutableDataFrame(edges)]; } diff --git a/public/app/plugins/datasource/testdata/runStreams.ts b/public/app/plugins/datasource/testdata/runStreams.ts index cb1da0e1e32..149c330c964 100644 --- a/public/app/plugins/datasource/testdata/runStreams.ts +++ b/public/app/plugins/datasource/testdata/runStreams.ts @@ -67,7 +67,7 @@ export function runSignalStream( const frame = StreamingDataFrame.fromDataFrameJSON({ schema }, { maxLength: maxDataPoints }); let value = Math.random() * 100; - let timeoutId: any = null; + let timeoutId: ReturnType; let lastSent = -1; const addNextRow = (time: number) => { @@ -148,7 +148,7 @@ export function runLogsStream( const { speed } = query; - let timeoutId: any = null; + let timeoutId: ReturnType; const pushNextEvent = () => { data.fields[0].values.add(Date.now()); @@ -205,7 +205,7 @@ export function runFetchStream( data.addField(field); } }, - onRow: (row: any[]) => { + onRow: (row) => { data.add(row); }, }, diff --git a/public/app/plugins/panel/barchart/bars.ts b/public/app/plugins/panel/barchart/bars.ts index b1ec44cc195..eb08d702b9c 100644 --- a/public/app/plugins/panel/barchart/bars.ts +++ b/public/app/plugins/panel/barchart/bars.ts @@ -48,10 +48,10 @@ export interface BarsOptions { showValue: VisibilityMode; stacking: StackingMode; rawValue: (seriesIdx: number, valueIdx: number) => number | null; - getColor?: (seriesIdx: number, valueIdx: number, value: any) => string | null; + getColor?: (seriesIdx: number, valueIdx: number, value: unknown) => string | null; fillOpacity?: number; - formatValue: (seriesIdx: number, value: any) => string; - formatShortValue: (seriesIdx: number, value: any) => string; + formatValue: (seriesIdx: number, value: unknown) => string; + formatShortValue: (seriesIdx: number, value: unknown) => string; timeZone?: TimeZone; text?: VizTextDisplayOptions; onHover?: (seriesIdx: number, valueIdx: number) => void; diff --git a/public/app/plugins/panel/barchart/module.tsx b/public/app/plugins/panel/barchart/module.tsx index 3493b6cc001..237bf532f5e 100644 --- a/public/app/plugins/panel/barchart/module.tsx +++ b/public/app/plugins/panel/barchart/module.tsx @@ -108,7 +108,7 @@ export const plugin = new PanelPlugin(BarChartPa }, }) .setPanelOptions((builder, context) => { - const disp = prepareBarChartDisplayValues(context.data, config.theme2, context.options ?? ({} as any)); + const disp = prepareBarChartDisplayValues(context.data, config.theme2, context.options ?? ({} as PanelOptions)); let xaxisPlaceholder = 'First string or time field'; const viz = 'viz' in disp ? disp.viz[0] : undefined; if (viz?.fields?.length) { diff --git a/public/app/plugins/panel/barchart/utils.ts b/public/app/plugins/panel/barchart/utils.ts index 3c4d471d1bd..5ea2ae98c6c 100644 --- a/public/app/plugins/panel/barchart/utils.ts +++ b/public/app/plugins/panel/barchart/utils.ts @@ -55,7 +55,7 @@ function getBarCharScaleOrientation(orientation: VizOrientation) { export interface BarChartOptionsEX extends PanelOptions { rawValue: (seriesIdx: number, valueIdx: number) => number | null; - getColor?: (seriesIdx: number, valueIdx: number, value: any) => string | null; + getColor?: (seriesIdx: number, valueIdx: number, value: unknown) => string | null; timeZone?: TimeZone; fillOpacity?: number; } @@ -83,11 +83,11 @@ export const preparePlotConfigBuilder: UPlotConfigPrepFn = ({ }) => { const builder = new UPlotConfigBuilder(); - const formatValue = (seriesIdx: number, value: any) => { + const formatValue = (seriesIdx: number, value: unknown) => { return formattedValueToString(frame.fields[seriesIdx].display!(value)); }; - const formatShortValue = (seriesIdx: number, value: any) => { + const formatShortValue = (seriesIdx: number, value: unknown) => { return shortenValue(formatValue(seriesIdx, value), xTickLabelMaxLength); }; diff --git a/public/app/plugins/panel/debug/RenderInfoViewer.tsx b/public/app/plugins/panel/debug/RenderInfoViewer.tsx index 22267f477eb..503aae803fd 100644 --- a/public/app/plugins/panel/debug/RenderInfoViewer.tsx +++ b/public/app/plugins/panel/debug/RenderInfoViewer.tsx @@ -59,7 +59,11 @@ export class RenderInfoViewer extends Component { render() { const { data, options } = this.props; - const showCounters = options.counters ?? ({} as UpdateConfig); + const showCounters = options.counters ?? { + render: false, + dataChanged: false, + schemaChanged: false, + }; this.counters.render++; const now = Date.now(); const elapsed = now - this.lastRender; diff --git a/public/app/plugins/panel/gauge/GaugeMigrations.ts b/public/app/plugins/panel/gauge/GaugeMigrations.ts index fbe6a33da67..de5ca2ba668 100644 --- a/public/app/plugins/panel/gauge/GaugeMigrations.ts +++ b/public/app/plugins/panel/gauge/GaugeMigrations.ts @@ -10,12 +10,12 @@ export const gaugePanelMigrationHandler = (panel: PanelModel): Par // This is called when the panel changes from another panel export const gaugePanelChangedHandler = ( - panel: PanelModel> | any, + panel: PanelModel>, prevPluginId: string, prevOptions: any ) => { // This handles most config changes - const opts = sharedSingleStatPanelChangedHandler(panel, prevPluginId, prevOptions) as PanelOptions; + const opts: PanelOptions = sharedSingleStatPanelChangedHandler(panel, prevPluginId, prevOptions); // Changing from angular singlestat if (prevPluginId === 'singlestat' && prevOptions.angular) { diff --git a/public/app/plugins/panel/gettingstarted/GettingStarted.tsx b/public/app/plugins/panel/gettingstarted/GettingStarted.tsx index da99ceca45a..2a1351dc0fe 100644 --- a/public/app/plugins/panel/gettingstarted/GettingStarted.tsx +++ b/public/app/plugins/panel/gettingstarted/GettingStarted.tsx @@ -11,7 +11,7 @@ import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv'; import { Step } from './components/Step'; import { getSteps } from './steps'; -import { Card, SetupStep } from './types'; +import { SetupStep } from './types'; interface State { checksDone: boolean; @@ -30,7 +30,7 @@ export class GettingStarted extends PureComponent { const { steps } = this.state; const checkedStepsPromises: Array> = steps.map(async (step: SetupStep) => { - const checkedCardsPromises: Array> = step.cards.map((card: Card) => { + const checkedCardsPromises = step.cards.map(async (card) => { return card.check().then((passed) => { return { ...card, done: passed }; }); diff --git a/public/app/plugins/panel/gettingstarted/components/Step.tsx b/public/app/plugins/panel/gettingstarted/components/Step.tsx index 88acc80c9c8..43d40642643 100644 --- a/public/app/plugins/panel/gettingstarted/components/Step.tsx +++ b/public/app/plugins/panel/gettingstarted/components/Step.tsx @@ -4,7 +4,7 @@ import React from 'react'; import { GrafanaTheme2 } from '@grafana/data'; import { useStyles2 } from '@grafana/ui'; -import { SetupStep, TutorialCardType } from '../types'; +import { SetupStep } from '../types'; import { DocsCard } from './DocsCard'; import { TutorialCard } from './TutorialCard'; @@ -26,7 +26,7 @@ export const Step = ({ step }: Props) => { {step.cards.map((card, index) => { const key = `${card.title}-${index}`; if (card.type === 'tutorial') { - return ; + return ; } return ; })} diff --git a/public/app/plugins/panel/gettingstarted/types.ts b/public/app/plugins/panel/gettingstarted/types.ts index 24a1814d324..330fb7fde0a 100644 --- a/public/app/plugins/panel/gettingstarted/types.ts +++ b/public/app/plugins/panel/gettingstarted/types.ts @@ -1,10 +1,8 @@ import { IconName } from '@grafana/ui'; -export type CardType = 'tutorial' | 'docs' | 'other'; - export interface Card { title: string; - type: CardType; + type: 'docs' | 'other'; icon: IconName; href: string; check: () => Promise; @@ -13,8 +11,9 @@ export interface Card { learnHref?: string; } -export interface TutorialCardType extends Card { +export interface TutorialCardType extends Omit { info?: string; + type: 'tutorial'; // For local storage key: string; }