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.
42 lines
1.6 KiB
42 lines
1.6 KiB
4 years ago
|
import { GrafanaPanelTemplate, GrafanaTemplateVariables } from '../models/grafana_panel_model';
|
||
4 years ago
|
|
||
4 years ago
|
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';
|
||
|
|
||
|
|
||
4 years ago
|
export async function exportPanel(panelId: string): Promise<GrafanaPanelTemplate> {
|
||
4 years ago
|
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 {
|
||
4 years ago
|
analyticUnits: analyticUnitTemplates,
|
||
4 years ago
|
caches,
|
||
|
detectionSpans,
|
||
|
segments
|
||
|
};
|
||
|
}
|
||
4 years ago
|
|
||
|
export async function importPanel(
|
||
4 years ago
|
panelTemplate: GrafanaPanelTemplate,
|
||
|
variables: GrafanaTemplateVariables
|
||
4 years ago
|
): 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);
|
||
|
}
|