mirror of
https://github.com/grafana/grafana.git
synced 2025-02-12 00:25:46 -06:00
3c6e0e8ef8
* 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
49 lines
1.3 KiB
TypeScript
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)
|
|
);
|
|
}
|
|
}
|