mirror of https://github.com/grafana/grafana
DashboardScene: Prepare annotations support for public dashboards (#75146)
* WIP Dashboard to Scenes: Annotations * Bump scenes * Enable annotations and controls * Betterer * Update snapshots * Test fix * DashboardScene: Prepare annotations support for public dashboards * Use latest scenespull/75155/head
parent
501e547bea
commit
df1c80c7d8
@ -0,0 +1,71 @@ |
|||||||
|
import { map, of } from 'rxjs'; |
||||||
|
|
||||||
|
import { DataSourceApi, DataQueryRequest, PanelData } from '@grafana/data'; |
||||||
|
import { LoadingState } from '@grafana/schema'; |
||||||
|
import { PublicAnnotationsDataSource } from 'app/features/query/state/DashboardQueryRunner/PublicAnnotationsDataSource'; |
||||||
|
|
||||||
|
import { DashboardAnnotationsDataLayer } from './DashboardAnnotationsDataLayer'; |
||||||
|
|
||||||
|
const getDataSourceSrvSpy = jest.fn(); |
||||||
|
const runRequestMock = jest.fn().mockImplementation((ds: DataSourceApi, request: DataQueryRequest) => { |
||||||
|
const result: PanelData = { |
||||||
|
state: LoadingState.Loading, |
||||||
|
series: [], |
||||||
|
timeRange: request.range, |
||||||
|
}; |
||||||
|
|
||||||
|
return of([]).pipe( |
||||||
|
map(() => { |
||||||
|
result.state = LoadingState.Done; |
||||||
|
result.series = []; |
||||||
|
|
||||||
|
return result; |
||||||
|
}) |
||||||
|
); |
||||||
|
}); |
||||||
|
|
||||||
|
jest.mock('app/features/query/state/DashboardQueryRunner/PublicAnnotationsDataSource'); |
||||||
|
jest.mock('@grafana/runtime', () => ({ |
||||||
|
...jest.requireActual('@grafana/runtime'), |
||||||
|
getDataSourceSrv: () => { |
||||||
|
getDataSourceSrvSpy(); |
||||||
|
}, |
||||||
|
getRunRequest: () => (ds: DataSourceApi, request: DataQueryRequest) => { |
||||||
|
return runRequestMock(ds, request); |
||||||
|
}, |
||||||
|
config: { |
||||||
|
publicDashboardAccessToken: 'ac123', |
||||||
|
}, |
||||||
|
})); |
||||||
|
|
||||||
|
describe('DashboardAnnotationsDataLayer', () => { |
||||||
|
it('should use PublicAnnotationsDataSource when config.publicDashboardAccessToken is set', () => { |
||||||
|
const dataLayer = new DashboardAnnotationsDataLayer({ |
||||||
|
name: 'Annotations & Alerts', |
||||||
|
query: { |
||||||
|
builtIn: 1, |
||||||
|
datasource: { |
||||||
|
type: 'grafana', |
||||||
|
uid: '-- Grafana --', |
||||||
|
}, |
||||||
|
enable: true, |
||||||
|
hide: true, |
||||||
|
iconColor: 'rgba(0, 211, 255, 1)', |
||||||
|
name: 'Annotations & Alerts', |
||||||
|
target: { |
||||||
|
// @ts-expect-error
|
||||||
|
limit: 100, |
||||||
|
matchAny: false, |
||||||
|
tags: [], |
||||||
|
type: 'dashboard', |
||||||
|
}, |
||||||
|
type: 'dashboard', |
||||||
|
}, |
||||||
|
}); |
||||||
|
|
||||||
|
dataLayer.activate(); |
||||||
|
|
||||||
|
expect(PublicAnnotationsDataSource).toHaveBeenCalledTimes(1); |
||||||
|
expect(getDataSourceSrvSpy).not.toHaveBeenCalled(); |
||||||
|
}); |
||||||
|
}); |
||||||
@ -0,0 +1,45 @@ |
|||||||
|
import { AnnotationEvent, arrayToDataFrame, DataTopic, getDefaultTimeRange, PanelData } from '@grafana/data'; |
||||||
|
import { config } from '@grafana/runtime'; |
||||||
|
import { dataLayers } from '@grafana/scenes'; |
||||||
|
import { AnnotationQuery, LoadingState } from '@grafana/schema'; |
||||||
|
import { PublicAnnotationsDataSource } from 'app/features/query/state/DashboardQueryRunner/PublicAnnotationsDataSource'; |
||||||
|
|
||||||
|
/** |
||||||
|
* This class is an extension to dataLayers.AnnotationsDataLayer to provide support for public dashboards. |
||||||
|
*/ |
||||||
|
export class DashboardAnnotationsDataLayer extends dataLayers.AnnotationsDataLayer { |
||||||
|
protected async resolveDataSource(query: AnnotationQuery) { |
||||||
|
if (config.publicDashboardAccessToken) { |
||||||
|
return new PublicAnnotationsDataSource(); |
||||||
|
} |
||||||
|
return super.resolveDataSource(query); |
||||||
|
} |
||||||
|
|
||||||
|
protected processEvents( |
||||||
|
query: AnnotationQuery, |
||||||
|
events: { |
||||||
|
state: LoadingState; |
||||||
|
events: AnnotationEvent[]; |
||||||
|
} |
||||||
|
) { |
||||||
|
if (config.publicDashboardAccessToken) { |
||||||
|
const stateUpdate: PanelData = { |
||||||
|
series: [], |
||||||
|
timeRange: getDefaultTimeRange(), |
||||||
|
state: events.state, |
||||||
|
}; |
||||||
|
|
||||||
|
const df = arrayToDataFrame(events.events); |
||||||
|
df.meta = { |
||||||
|
...df.meta, |
||||||
|
dataTopic: DataTopic.Annotations, |
||||||
|
}; |
||||||
|
|
||||||
|
stateUpdate.annotations = [df]; |
||||||
|
|
||||||
|
return stateUpdate; |
||||||
|
} else { |
||||||
|
return super.processEvents(query, events); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue