Commit 3d0bc141 by Ryan McKinley Committed by Arve Knudsen

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

(cherry picked from commit 8b067a5f)
parent ed307897
......@@ -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,
};
});
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment