rozetko
5 years ago
committed by
GitHub
8 changed files with 100 additions and 15 deletions
@ -0,0 +1,17 @@
|
||||
import * as AnalyticUnit from './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 type PanelTemplate = { |
||||
analyticUnits: AnalyticUnit.SerializedAnalyticUnit[], |
||||
caches: AnalyticUnitCache.AnalyticUnitCache[], |
||||
detectionSpans: DetectionSpan.DetectionSpan[], |
||||
segments: Segment.Segment[] |
||||
} |
||||
|
||||
export type TemplateVariables = { |
||||
grafanaUrl: string, |
||||
panelId: string, |
||||
datasourceUrl: string |
||||
}; |
@ -1,18 +1,54 @@
|
||||
import { exportPanel } from '../services/export_service'; |
||||
import { PanelTemplate, TemplateVariables } from '../models/panel_model'; |
||||
import { exportPanel, importPanel } from '../services/export_service'; |
||||
|
||||
import * as Router from 'koa-router'; |
||||
|
||||
|
||||
async function getPanelTemplate(ctx: Router.IRouterContext) { |
||||
async function exportPanelTemplate(ctx: Router.IRouterContext) { |
||||
let panelId = ctx.request.query.panelId; |
||||
if(panelId === undefined) { |
||||
throw new Error('Cannot export analytic units with undefined panelId'); |
||||
} |
||||
|
||||
const json = await exportPanel(panelId); |
||||
ctx.response.body = json; |
||||
const panelTemplate = await exportPanel(panelId); |
||||
ctx.response.body = panelTemplate; |
||||
}
|
||||
|
||||
async function importPanelTemplate(ctx: Router.IRouterContext) { |
||||
const { panelTemplate, templateVariables } = ctx.request.body as { |
||||
panelTemplate: PanelTemplate,
|
||||
templateVariables: TemplateVariables |
||||
}; |
||||
|
||||
// TODO: move to model
|
||||
if(panelTemplate.analyticUnits === undefined) { |
||||
throw new Error('Cannot import analytic units with undefined analyticUnits'); |
||||
} |
||||
if(panelTemplate.caches === undefined) { |
||||
throw new Error('Cannot import analytic units with undefined caches'); |
||||
} |
||||
if(panelTemplate.detectionSpans === undefined) { |
||||
throw new Error('Cannot import analytic units with undefined detectionSpans'); |
||||
} |
||||
if(panelTemplate.segments === undefined) { |
||||
throw new Error('Cannot import analytic units with undefined segments'); |
||||
} |
||||
|
||||
if(templateVariables.grafanaUrl === undefined) { |
||||
throw new Error('Cannot make analytic unit from template with undefined grafanaUrl'); |
||||
} |
||||
if(templateVariables.panelId === undefined) { |
||||
throw new Error('Cannot make analytic unit from template with undefined panelId'); |
||||
} |
||||
if(templateVariables.datasourceUrl === undefined) { |
||||
throw new Error('Cannot make analytic unit from template with undefined datasourceUrl'); |
||||
} |
||||
|
||||
await importPanel(panelTemplate, templateVariables); |
||||
ctx.response.status = 200; |
||||
} |
||||
|
||||
export var router = new Router(); |
||||
|
||||
router.get('/template', getPanelTemplate); |
||||
router.get('/template', exportPanelTemplate); |
||||
router.post('/template', importPanelTemplate); |
||||
|
Loading…
Reference in new issue