mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Annotations: Fix so annotations work in panel edit (#35195)
This commit is contained in:
parent
1f16fac8d7
commit
a7606d130e
@ -1,5 +1,5 @@
|
|||||||
import { merge, Observable, Subject, Unsubscribable } from 'rxjs';
|
import { merge, Observable, ReplaySubject, Subject, Unsubscribable } from 'rxjs';
|
||||||
import { map, mergeAll, reduce, share, takeUntil } from 'rxjs/operators';
|
import { mergeAll, reduce, share, takeUntil } from 'rxjs/operators';
|
||||||
import { AnnotationQuery } from '@grafana/data';
|
import { AnnotationQuery } from '@grafana/data';
|
||||||
|
|
||||||
import { dedupAnnotations } from 'app/features/annotations/events_processing';
|
import { dedupAnnotations } from 'app/features/annotations/events_processing';
|
||||||
@ -13,13 +13,13 @@ import {
|
|||||||
import { AlertStatesWorker } from './AlertStatesWorker';
|
import { AlertStatesWorker } from './AlertStatesWorker';
|
||||||
import { SnapshotWorker } from './SnapshotWorker';
|
import { SnapshotWorker } from './SnapshotWorker';
|
||||||
import { AnnotationsWorker } from './AnnotationsWorker';
|
import { AnnotationsWorker } from './AnnotationsWorker';
|
||||||
import { getAnnotationsByPanelId } from './utils';
|
import { emptyResult, getAnnotationsByPanelId } from './utils';
|
||||||
import { DashboardModel } from '../../../dashboard/state';
|
import { DashboardModel } from '../../../dashboard/state';
|
||||||
import { getTimeSrv, TimeSrv } from '../../../dashboard/services/TimeSrv';
|
import { getTimeSrv, TimeSrv } from '../../../dashboard/services/TimeSrv';
|
||||||
import { RefreshEvent } from '../../../../types/events';
|
import { RefreshEvent } from '../../../../types/events';
|
||||||
|
|
||||||
class DashboardQueryRunnerImpl implements DashboardQueryRunner {
|
class DashboardQueryRunnerImpl implements DashboardQueryRunner {
|
||||||
private readonly results: Subject<DashboardQueryRunnerWorkerResult>;
|
private readonly results: ReplaySubject<DashboardQueryRunnerWorkerResult>;
|
||||||
private readonly runs: Subject<DashboardQueryRunnerOptions>;
|
private readonly runs: Subject<DashboardQueryRunnerOptions>;
|
||||||
private readonly cancellationStream: Subject<AnnotationQuery>;
|
private readonly cancellationStream: Subject<AnnotationQuery>;
|
||||||
private readonly runsSubscription: Unsubscribable;
|
private readonly runsSubscription: Unsubscribable;
|
||||||
@ -39,7 +39,7 @@ class DashboardQueryRunnerImpl implements DashboardQueryRunner {
|
|||||||
this.cancel = this.cancel.bind(this);
|
this.cancel = this.cancel.bind(this);
|
||||||
this.destroy = this.destroy.bind(this);
|
this.destroy = this.destroy.bind(this);
|
||||||
this.executeRun = this.executeRun.bind(this);
|
this.executeRun = this.executeRun.bind(this);
|
||||||
this.results = new Subject<DashboardQueryRunnerWorkerResult>();
|
this.results = new ReplaySubject<DashboardQueryRunnerWorkerResult>();
|
||||||
this.runs = new Subject<DashboardQueryRunnerOptions>();
|
this.runs = new Subject<DashboardQueryRunnerOptions>();
|
||||||
this.cancellationStream = new Subject<any>();
|
this.cancellationStream = new Subject<any>();
|
||||||
this.runsSubscription = this.runs.subscribe((options) => this.executeRun(options));
|
this.runsSubscription = this.runs.subscribe((options) => this.executeRun(options));
|
||||||
@ -53,21 +53,26 @@ class DashboardQueryRunnerImpl implements DashboardQueryRunner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getResult(panelId?: number): Observable<DashboardQueryRunnerResult> {
|
getResult(panelId?: number): Observable<DashboardQueryRunnerResult> {
|
||||||
return this.results.asObservable().pipe(
|
return new Observable<DashboardQueryRunnerResult>((subscriber) => {
|
||||||
map((result) => {
|
const subscription = this.results.subscribe({
|
||||||
const annotations = getAnnotationsByPanelId(result.annotations, panelId);
|
next: (result) => {
|
||||||
|
const annotations = getAnnotationsByPanelId(result.annotations, panelId);
|
||||||
const alertState = result.alertStates.find((res) => Boolean(panelId) && res.panelId === panelId);
|
const alertState = result.alertStates.find((res) => Boolean(panelId) && res.panelId === panelId);
|
||||||
|
subscriber.next({ annotations: dedupAnnotations(annotations), alertState });
|
||||||
return { annotations: dedupAnnotations(annotations), alertState };
|
},
|
||||||
}),
|
error: subscriber.error,
|
||||||
share() // sharing this so we can merge this with it self in mergePanelAndDashData
|
complete: subscriber.complete,
|
||||||
);
|
});
|
||||||
|
return () => {
|
||||||
|
subscription.unsubscribe();
|
||||||
|
};
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private executeRun(options: DashboardQueryRunnerOptions) {
|
private executeRun(options: DashboardQueryRunnerOptions) {
|
||||||
const workers = this.workers.filter((w) => w.canWork(options));
|
const workers = this.workers.filter((w) => w.canWork(options));
|
||||||
const observables = workers.map((w) => w.work(options));
|
const workerObservables = workers.map((w) => w.work(options));
|
||||||
|
const observables = [emptyResult()].concat(workerObservables);
|
||||||
|
|
||||||
merge(observables)
|
merge(observables)
|
||||||
.pipe(
|
.pipe(
|
||||||
|
@ -32,6 +32,7 @@ import {
|
|||||||
} from '@grafana/data';
|
} from '@grafana/data';
|
||||||
import { getDashboardQueryRunner } from './DashboardQueryRunner/DashboardQueryRunner';
|
import { getDashboardQueryRunner } from './DashboardQueryRunner/DashboardQueryRunner';
|
||||||
import { mergePanelAndDashData } from './mergePanelAndDashData';
|
import { mergePanelAndDashData } from './mergePanelAndDashData';
|
||||||
|
import { PanelModel } from '../../dashboard/state';
|
||||||
|
|
||||||
export interface QueryRunnerOptions<
|
export interface QueryRunnerOptions<
|
||||||
TQuery extends DataQuery = DataQuery,
|
TQuery extends DataQuery = DataQuery,
|
||||||
@ -255,7 +256,9 @@ export class PanelQueryRunner {
|
|||||||
const dataSupport = this.dataConfigSource.getDataSupport();
|
const dataSupport = this.dataConfigSource.getDataSupport();
|
||||||
|
|
||||||
if (dataSupport.alertStates || dataSupport.annotations) {
|
if (dataSupport.alertStates || dataSupport.annotations) {
|
||||||
panelData = mergePanelAndDashData(observable, getDashboardQueryRunner().getResult(panelId));
|
const panel = (this.dataConfigSource as unknown) as PanelModel;
|
||||||
|
const id = panel.editSourceId ?? panel.id;
|
||||||
|
panelData = mergePanelAndDashData(observable, getDashboardQueryRunner().getResult(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
this.subscription = panelData.subscribe({
|
this.subscription = panelData.subscribe({
|
||||||
|
Loading…
Reference in New Issue
Block a user