mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
DataSourceWithBackend: use /health endpoint for test (#22789)
This commit is contained in:
parent
dfb3272bdb
commit
8b067a5fe0
@ -13,6 +13,18 @@ import { getBackendSrv } from '../services';
|
|||||||
// Ideally internal (exported for consistency)
|
// Ideally internal (exported for consistency)
|
||||||
const ExpressionDatasourceID = '__expr__';
|
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<
|
export class DataSourceWithBackend<
|
||||||
TQuery extends DataQuery = DataQuery,
|
TQuery extends DataQuery = DataQuery,
|
||||||
TOptions extends DataSourceJsonData = DataSourceJsonData
|
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> {
|
query(request: DataQueryRequest): Observable<DataQueryResponse> {
|
||||||
const { targets, intervalMs, maxDataPoints, range } = request;
|
const { targets, intervalMs, maxDataPoints, range } = request;
|
||||||
@ -101,8 +113,36 @@ export class DataSourceWithBackend<
|
|||||||
return getBackendSrv().post(`/api/datasources/${this.id}/resources/${path}`, { ...body });
|
return getBackendSrv().post(`/api/datasources/${this.id}/resources/${path}`, { ...body });
|
||||||
}
|
}
|
||||||
|
|
||||||
testDatasource() {
|
/**
|
||||||
// TODO, this will call the backend healthcheck endpoint
|
* Run the datasource healthcheck
|
||||||
return Promise.resolve({});
|
*/
|
||||||
|
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…
Reference in New Issue
Block a user