|
|
@ -5,11 +5,12 @@ import * as AnalyticUnitCache from '../models/analytic_unit_cache_model'; |
|
|
|
import * as DetectionSpan from '../models/detection_model'; |
|
|
|
import * as DetectionSpan from '../models/detection_model'; |
|
|
|
import * as Segment from '../models/segment_model'; |
|
|
|
import * as Segment from '../models/segment_model'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import * as _ from 'lodash'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function exportPanel(panelId: string): Promise<GrafanaPanelTemplate> { |
|
|
|
export async function exportPanel(panelId: string): Promise<GrafanaPanelTemplate> { |
|
|
|
const analyticUnits = await AnalyticUnit.findMany({ panelId }); |
|
|
|
const analyticUnits = await AnalyticUnit.findMany({ panelId }); |
|
|
|
const analyticUnitIds = analyticUnits.map(analyticUnit => analyticUnit.id); |
|
|
|
const analyticUnitIds = analyticUnits.map(analyticUnit => analyticUnit.id); |
|
|
|
const analyticUnitTemplates = analyticUnits.map(analyticUnit => analyticUnit.toTemplate()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const [caches, detectionSpans, segments] = await Promise.all([ |
|
|
|
const [caches, detectionSpans, segments] = await Promise.all([ |
|
|
|
AnalyticUnitCache.findMany({ _id: { $in: analyticUnitIds } }), |
|
|
|
AnalyticUnitCache.findMany({ _id: { $in: analyticUnitIds } }), |
|
|
@ -17,25 +18,70 @@ export async function exportPanel(panelId: string): Promise<GrafanaPanelTemplate |
|
|
|
Segment.findByAnalyticUnitIds(analyticUnitIds) |
|
|
|
Segment.findByAnalyticUnitIds(analyticUnitIds) |
|
|
|
]); |
|
|
|
]); |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
// TODO: not any
|
|
|
|
analyticUnits: analyticUnitTemplates, |
|
|
|
let analyticUnitTemplates: any[] = []; |
|
|
|
caches, |
|
|
|
|
|
|
|
detectionSpans, |
|
|
|
analyticUnits.forEach(analyticUnit => { |
|
|
|
segments |
|
|
|
const analyticUnitTemplate = analyticUnit.toTemplate(); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
let analyticUnitCache = _.find(caches, cache => cache.id === analyticUnit.id) || null; |
|
|
|
|
|
|
|
if(analyticUnitCache !== null) { |
|
|
|
|
|
|
|
analyticUnitCache = analyticUnitCache.toTemplate(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const analyticUnitSegments = segments |
|
|
|
|
|
|
|
.filter(segment => segment.analyticUnitId === analyticUnit.id) |
|
|
|
|
|
|
|
.map(segment => segment.toTemplate()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const analyticUnitSpans = detectionSpans |
|
|
|
|
|
|
|
.filter(span => span.analyticUnitId === analyticUnit.id) |
|
|
|
|
|
|
|
.map(span => span.toTemplate()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
analyticUnitTemplates.push({ |
|
|
|
|
|
|
|
...analyticUnitTemplate, |
|
|
|
|
|
|
|
cache: analyticUnitCache, |
|
|
|
|
|
|
|
segments: analyticUnitSegments, |
|
|
|
|
|
|
|
detectionSpans: analyticUnitSpans |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { analyticUnitTemplates }; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function importPanel( |
|
|
|
export async function importPanel( |
|
|
|
panelTemplate: GrafanaPanelTemplate, |
|
|
|
panelTemplate: GrafanaPanelTemplate, |
|
|
|
variables: GrafanaTemplateVariables |
|
|
|
variables: GrafanaTemplateVariables |
|
|
|
): Promise<void> { |
|
|
|
): Promise<void> { |
|
|
|
panelTemplate.analyticUnits.forEach(analyticUnit => { |
|
|
|
await Promise.all(panelTemplate.analyticUnitTemplates.map( |
|
|
|
analyticUnit.grafanaUrl = variables.grafanaUrl; |
|
|
|
template => _importAnalyticUnitTemplate(template, variables) |
|
|
|
analyticUnit.panelId = variables.panelId; |
|
|
|
)); |
|
|
|
analyticUnit.metric.datasource.url = variables.datasourceUrl; |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
await AnalyticUnit.insertMany(panelTemplate.analyticUnits); |
|
|
|
export async function _importAnalyticUnitTemplate(analyticUnitTemplate: any, variables: GrafanaTemplateVariables) { |
|
|
|
await AnalyticUnitCache.insertMany(panelTemplate.caches); |
|
|
|
analyticUnitTemplate.grafanaUrl = variables.grafanaUrl; |
|
|
|
await Segment.insertMany(panelTemplate.segments); |
|
|
|
analyticUnitTemplate.panelId = variables.panelId; |
|
|
|
await DetectionSpan.insertMany(panelTemplate.detectionSpans); |
|
|
|
analyticUnitTemplate.metric.datasource.url = variables.datasourceUrl; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const cache = _.clone(analyticUnitTemplate.cache); |
|
|
|
|
|
|
|
const segments = _.clone(analyticUnitTemplate.segments); |
|
|
|
|
|
|
|
const detectionSpans = _.clone(analyticUnitTemplate.detectionSpans); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
delete analyticUnitTemplate.cache; |
|
|
|
|
|
|
|
delete analyticUnitTemplate.segments; |
|
|
|
|
|
|
|
delete analyticUnitTemplate.detectionSpans; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const [ newAnalyticUnitId ] = await AnalyticUnit.insertMany([analyticUnitTemplate]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(cache !== null) { |
|
|
|
|
|
|
|
cache._id = newAnalyticUnitId; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
segments.forEach(segment => segment.analyticUnitId = newAnalyticUnitId); |
|
|
|
|
|
|
|
detectionSpans.forEach(detectionSpan => detectionSpan.analyticUnitId = newAnalyticUnitId); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Promise.all([ |
|
|
|
|
|
|
|
AnalyticUnitCache.insertMany([cache]), |
|
|
|
|
|
|
|
Segment.insertMany(segments), |
|
|
|
|
|
|
|
DetectionSpan.insertMany(detectionSpans) |
|
|
|
|
|
|
|
]); |
|
|
|
} |
|
|
|
} |
|
|
|