DataSourceWithBackend: use /health endpoint for test (#22789)

pull/22144/head
Ryan McKinley 5 years ago committed by GitHub
parent dfb3272bdb
commit 8b067a5fe0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 48
      packages/grafana-runtime/src/utils/DataSourceWithBackend.ts

@ -13,6 +13,18 @@ import { getBackendSrv } from '../services';
// Ideally internal (exported for consistency)
const ExpressionDatasourceID = '__expr__';
export enum HealthStatus {
Unknown = 'UNKNOWN',
OK = 'OK',
Error = 'ERROR',
}
export interface HealthCheckResult {
status: HealthStatus;
message: string;
details?: Record<string, any>;
}
export class DataSourceWithBackend<
TQuery extends DataQuery = DataQuery,
TOptions extends DataSourceJsonData = DataSourceJsonData
@ -22,7 +34,7 @@ export class DataSourceWithBackend<
}
/**
* Ideally final -- any other implementation would be wrong!
* Ideally final -- any other implementation may not work as expected
*/
query(request: DataQueryRequest): Observable<DataQueryResponse> {
const { targets, intervalMs, maxDataPoints, range } = request;
@ -101,8 +113,36 @@ export class DataSourceWithBackend<
return getBackendSrv().post(`/api/datasources/${this.id}/resources/${path}`, { ...body });
}
testDatasource() {
// TODO, this will call the backend healthcheck endpoint
return Promise.resolve({});
/**
* Run the datasource healthcheck
*/
async callHealthCheck(): Promise<HealthCheckResult> {
return getBackendSrv()
.get(`/api/datasources/${this.id}/health`)
.then(v => {
return v as HealthCheckResult;
})
.catch(err => {
err.isHandled = true; // Avoid extra popup warning
return err.data as HealthCheckResult;
});
}
/**
* Checks the plugin health
*/
async testDatasource(): Promise<any> {
return this.callHealthCheck().then(res => {
if (res.status === HealthStatus.OK) {
return {
status: 'success',
message: res.message,
};
}
return {
status: 'fail',
message: res.message,
};
});
}
}

Loading…
Cancel
Save