grafana/public/app/features/query/state/DashboardQueryRunner/AlertStatesWorker.ts
Josh Hunt 3c6e0e8ef8
Chore: ESlint import order (#44959)
* Add and configure eslint-plugin-import

* Fix the lint:ts npm command

* Autofix + prettier all the files

* Manually fix remaining files

* Move jquery code in jest-setup to external file to safely reorder imports

* Resolve issue caused by circular dependencies within Prometheus

* Update .betterer.results

* Fix missing // @ts-ignore

* ignore iconBundle.ts

* Fix missing // @ts-ignore
2022-04-22 14:33:13 +01:00

49 lines
1.3 KiB
TypeScript

import { from, Observable } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { getBackendSrv } from '@grafana/runtime';
import { DashboardQueryRunnerOptions, DashboardQueryRunnerWorker, DashboardQueryRunnerWorkerResult } from './types';
import { emptyResult, handleDashboardQueryRunnerWorkerError } from './utils';
export class AlertStatesWorker implements DashboardQueryRunnerWorker {
canWork({ dashboard, range }: DashboardQueryRunnerOptions): boolean {
if (!dashboard.id) {
return false;
}
if (range.raw.to !== 'now') {
return false;
}
// if dashboard has no alerts, no point to query alert states
if (!dashboard.panels.find((panel) => !!panel.alert)) {
return false;
}
return true;
}
work(options: DashboardQueryRunnerOptions): Observable<DashboardQueryRunnerWorkerResult> {
if (!this.canWork(options)) {
return emptyResult();
}
const { dashboard } = options;
return from(
getBackendSrv().get(
'/api/alerts/states-for-dashboard',
{
dashboardId: dashboard.id,
},
`dashboard-query-runner-alert-states-${dashboard.id}`
)
).pipe(
map((alertStates) => {
return { alertStates, annotations: [] };
}),
catchError(handleDashboardQueryRunnerWorkerError)
);
}
}