Skip to content

Commit a0ad1c2

Browse files
committed
Resolve some explicit anys
1 parent cc865cd commit a0ad1c2

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

src/DataSource.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import {
88
MutableDataFrame,
99
FieldType,
1010
} from '@grafana/data';
11-
import { getBackendSrv } from '@grafana/runtime';
11+
import { FetchResponse, getBackendSrv, BackendSrvRequest } from '@grafana/runtime';
1212

1313
import { buildRequestBody, combinedDesc, graphDefinitionRequest } from './graphspecs';
14-
import { MyQuery, defaultQuery, MyDataSourceOptions } from './types';
14+
import { MyQuery, defaultQuery, MyDataSourceOptions, ResponseData, ResponseDataCurves } from './types';
1515

1616
export const buildUrlWithParams = (url: string, params: Record<string, string>) =>
1717
url + '?' + new URLSearchParams(params).toString();
1818

19-
function buildMetricDataFrame(response: any, query: MyQuery) {
19+
function buildMetricDataFrame(response: FetchResponse<ResponseData<ResponseDataCurves>>, query: MyQuery) {
2020
if (response.data.result_code !== 0) {
2121
throw new Error(`${response.data.result}`);
2222
}
@@ -25,12 +25,10 @@ function buildMetricDataFrame(response: any, query: MyQuery) {
2525
const frame = new MutableDataFrame({
2626
refId: query.refId,
2727
fields: [{ name: 'Time', type: FieldType.time }].concat(
28-
curves.map((x: any) => ({ name: x.title, type: FieldType.number }))
28+
curves.map((x) => ({ name: x.title, type: FieldType.number }))
2929
),
3030
});
31-
zip(...curves.map((x: any) => x.rrddata)).forEach((d: any, i: number) =>
32-
frame.appendRow([(start_time + i * step) * 1000, ...d])
33-
);
31+
zip(...curves.map((x) => x.rrddata)).forEach((d, i) => frame.appendRow([(start_time + i * step) * 1000, ...d]));
3432
return frame;
3533
}
3634

@@ -56,7 +54,7 @@ export class DataSource extends DataSourceApi<MyQuery> {
5654
return new MutableDataFrame();
5755
}
5856
const editionMode = get(this, 'instanceSettings.jsonData.edition', 'CEE');
59-
const response = await this.doRequest({
57+
const response = await this.doRequest<ResponseDataCurves>({
6058
...query,
6159
params: { action: 'get_graph' },
6260
data: graphDefinitionRequest(editionMode, query, range),
@@ -93,27 +91,27 @@ export class DataSource extends DataSourceApi<MyQuery> {
9391
});
9492
}
9593

96-
async doRequest(options: MyQuery) {
97-
return this.cmkRequest({
94+
async doRequest<T>(options: MyQuery): Promise<FetchResponse<ResponseData<T>>> {
95+
return this.cmkRequest<T>({
9896
method: options.data == null ? 'GET' : 'POST',
9997
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
10098
url: buildUrlWithParams(`${this.instanceSettings.url}/cmk/check_mk/webapi.py`, { ...options.params }),
10199
data: options.data,
102100
});
103101
}
104102

105-
async restRequest(api_url: string, data: any) {
106-
return this.cmkRequest({
103+
async restRequest<T>(api_url: string, data: any): Promise<FetchResponse<ResponseData<T>>> {
104+
return this.cmkRequest<T>({
107105
method: 'POST',
108106
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
109107
url: `${this.instanceSettings.url}/cmk/check_mk/${api_url}`,
110108
data: buildRequestBody(data),
111109
});
112110
}
113111

114-
async cmkRequest(request: any) {
112+
async cmkRequest<T>(request: BackendSrvRequest): Promise<FetchResponse<ResponseData<T>>> {
115113
const result = await getBackendSrv()
116-
.fetch<{ result_code: number, result: any}>(request)
114+
.fetch<ResponseData<T>>(request)
117115
.toPromise()
118116
.catch((error) => {
119117
if (error.cancelled) {
@@ -126,7 +124,7 @@ export class DataSource extends DataSourceApi<MyQuery> {
126124
});
127125

128126
if (result === undefined) {
129-
return undefined;
127+
throw new Error('Got undefined result');
130128
}
131129

132130
if (result.data instanceof String) {

src/components/fields.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AutoCompleteEditorProps, AutoCompleteConfig, EditorProps } from './type
55
import { get, update as _update, cloneDeep } from 'lodash';
66
import { DataSource } from '../DataSource';
77
import { combinedDesc } from 'graphspecs';
8-
import { MyQuery } from 'types';
8+
import { MyQuery, ResponseDataAutocomplete } from 'types';
99

1010
const update = (x: MyQuery, path: string, func: () => SelectableValue<string> | string | undefined) => {
1111
const copy = cloneDeep(x);
@@ -16,7 +16,7 @@ const update = (x: MyQuery, path: string, func: () => SelectableValue<string> |
1616
export const vsAutocomplete =
1717
(datasource: DataSource, autocompleteConfig: AutoCompleteConfig) => (inputValue: string) =>
1818
datasource
19-
.restRequest('ajax_vs_autocomplete.py', {
19+
.restRequest<ResponseDataAutocomplete>('ajax_vs_autocomplete.py', {
2020
...autocompleteConfig,
2121
value: inputValue.trim(),
2222
})

src/components/filters.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { SelectableValue } from '@grafana/data';
1414
import { EditorProps } from './types';
1515
import { AsyncAutocomplete, vsAutocomplete } from './fields';
1616
import { get, update } from 'lodash';
17+
import { ResponseDataAutocompleteLabel } from 'types';
1718

1819
export const SiteFilter = (props: EditorProps) => {
1920
const sitesVS = { ident: 'sites', params: { strict: false, context: props.query.context } };
@@ -117,7 +118,7 @@ export const HostLabelsFilter = ({ datasource, onChange, query, onRunQuery }: Ed
117118
const getHostLabels = (inputValue: string) => {
118119
const search = inputValue.trim().toLowerCase();
119120
return datasource
120-
.restRequest('ajax_autocomplete_labels.py', {
121+
.restRequest<ResponseDataAutocompleteLabel>('ajax_autocomplete_labels.py', {
121122
world: 'core',
122123
search_label: search,
123124
})

src/types.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,29 @@ export interface MyDataSourceOptions extends DataSourceJsonData {
4646
export interface MySecureJsonData {
4747
secret?: string;
4848
}
49+
50+
export interface ResponseDataAutocomplete {
51+
choices: [string, string][];
52+
}
53+
54+
export type ResponseDataAutocompleteLabel = {
55+
value: string;
56+
}[];
57+
58+
export interface ResponseDataCurves {
59+
filter: unknown;
60+
start_time: number;
61+
step: number;
62+
curves: {
63+
title: string;
64+
rrddata: {
65+
i: number;
66+
d: Record<string, unknown>;
67+
}[];
68+
}[];
69+
}
70+
71+
export interface ResponseData<T> {
72+
result_code: number;
73+
result: T;
74+
}

0 commit comments

Comments
 (0)