Files
grafana/public/app/plugins/datasource/cloud-monitoring/api.ts
idafurjes e822c8a24d CloudMonitoring: Migrate to use backend plugin SDK contracts (#38650)
* Use SDK contracts for cloudmonitoring

* Get build running, tests passing and do some refactoring (#38754)

* fix build+tests and refactor

* remove alerting stuff

* remove unused field

* fix plugin fetch

* end to end

* resp rename

* tidy annotations

* reformatting

* update refID

* reformat imports

* fix styling

* clean up unmarshalling

* uncomment + fix tests

* appease linter

* remove spaces

* remove old cruft

* add check for empty queries

* update tests

* remove pm as dep

* adjust proxy route contract

* fix service loading

* use UNIX val

* fix endpoint + resp

* h@ckz for frontend

* fix resp

* fix interval

* always set custom meta

* remove unused param

* fix labels fetch

* fix linter

* fix test + remove unused field

* apply pr feedback

* fix grafana-auto intervals

* fix tests

* resolve conflicts

* fix bad merge

* fix conflicts

* remove bad logger import

Co-authored-by: Will Browne <wbrowne@users.noreply.github.com>
Co-authored-by: Will Browne <will.browne@grafana.com>
2021-10-08 14:46:35 +02:00

88 lines
2.4 KiB
TypeScript

import { lastValueFrom, Observable, of } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { SelectableValue } from '@grafana/data';
import { FetchResponse, getBackendSrv } from '@grafana/runtime';
import appEvents from 'app/core/app_events';
import { CoreEvents } from 'app/types';
import { formatCloudMonitoringError } from './functions';
import { MetricDescriptor } from './types';
export interface PostResponse {
results: Record<string, any>;
}
interface Options {
responseMap?: (res: any) => SelectableValue<string> | MetricDescriptor;
baseUrl?: string;
useCache?: boolean;
}
export default class Api {
cache: { [key: string]: Array<SelectableValue<string>> };
defaultOptions: Options;
constructor(private baseUrl: string) {
this.cache = {};
this.defaultOptions = {
useCache: true,
responseMap: (res: any) => res,
baseUrl: this.baseUrl,
};
}
get(path: string, options?: Options): Promise<Array<SelectableValue<string>> | MetricDescriptor[]> {
const { useCache, responseMap, baseUrl } = { ...this.defaultOptions, ...options };
if (useCache && this.cache[path]) {
return Promise.resolve(this.cache[path]);
}
return lastValueFrom(
getBackendSrv()
.fetch<Record<string, any>>({
url: baseUrl + path,
method: 'GET',
})
.pipe(
map((response) => {
const responsePropName = path.match(/([^\/]*)\/*$/)![1].split('?')[0];
let res = [];
if (response && response.data && response.data[responsePropName]) {
res = response.data[responsePropName].map(responseMap);
}
if (useCache) {
this.cache[path] = res;
}
return res;
}),
catchError((error) => {
appEvents.emit(CoreEvents.dsRequestError, {
error: { data: { error: formatCloudMonitoringError(error) } },
});
return of([]);
})
)
);
}
post(data: Record<string, any>): Observable<FetchResponse<PostResponse>> {
return getBackendSrv().fetch<PostResponse>({
url: '/api/ds/query',
method: 'POST',
data,
});
}
test(projectName: string) {
return lastValueFrom(
getBackendSrv().fetch<any>({
url: `${this.baseUrl}${projectName}/metricDescriptors`,
method: 'GET',
})
);
}
}