You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.6 KiB
41 lines
1.6 KiB
import { GrafanaPanelTemplate, GrafanaTemplateVariables } from '../models/grafana_panel_model'; |
|
|
|
import * as AnalyticUnit from '../models/analytic_units'; |
|
import * as AnalyticUnitCache from '../models/analytic_unit_cache_model'; |
|
import * as DetectionSpan from '../models/detection_model'; |
|
import * as Segment from '../models/segment_model'; |
|
|
|
|
|
export async function exportPanel(panelId: string): Promise<GrafanaPanelTemplate> { |
|
const analyticUnits = await AnalyticUnit.findMany({ panelId }); |
|
const analyticUnitIds = analyticUnits.map(analyticUnit => analyticUnit.id); |
|
const analyticUnitTemplates = analyticUnits.map(analyticUnit => analyticUnit.toTemplate()); |
|
|
|
const [caches, detectionSpans, segments] = await Promise.all([ |
|
AnalyticUnitCache.findMany({ _id: { $in: analyticUnitIds } }), |
|
DetectionSpan.findByAnalyticUnitIds(analyticUnitIds), |
|
Segment.findByAnalyticUnitIds(analyticUnitIds) |
|
]); |
|
|
|
return { |
|
analyticUnits: analyticUnitTemplates, |
|
caches, |
|
detectionSpans, |
|
segments |
|
}; |
|
} |
|
|
|
export async function importPanel( |
|
panelTemplate: GrafanaPanelTemplate, |
|
variables: GrafanaTemplateVariables |
|
): Promise<void> { |
|
panelTemplate.analyticUnits.forEach(analyticUnit => { |
|
analyticUnit.grafanaUrl = variables.grafanaUrl; |
|
analyticUnit.panelId = variables.panelId; |
|
analyticUnit.metric.datasource.url = variables.datasourceUrl; |
|
}); |
|
await AnalyticUnit.insertMany(panelTemplate.analyticUnits); |
|
await AnalyticUnitCache.insertMany(panelTemplate.caches); |
|
await Segment.insertMany(panelTemplate.segments); |
|
await DetectionSpan.insertMany(panelTemplate.detectionSpans); |
|
}
|
|
|