Browse Source

Panel export: change IDs #841 (#861)

pull/1/head
rozetko 4 years ago committed by GitHub
parent
commit
abb73944ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      server/src/models/analytic_unit_cache_model.ts
  2. 1
      server/src/models/analytic_units/analytic_unit_model.ts
  3. 8
      server/src/models/detection_model.ts
  4. 11
      server/src/models/grafana_panel_model.ts
  5. 8
      server/src/models/segment_model.ts
  6. 29
      server/src/routes/panel_router.ts
  7. 78
      server/src/services/grafana_service.ts

7
server/src/models/analytic_unit_cache_model.ts

@ -29,6 +29,13 @@ export class AnalyticUnitCache {
}; };
} }
public toTemplate(): any {
return {
...this.toObject(),
_id: undefined
};
}
static fromObject(obj: any): AnalyticUnitCache { static fromObject(obj: any): AnalyticUnitCache {
return new AnalyticUnitCache( return new AnalyticUnitCache(
obj._id, obj._id,

1
server/src/models/analytic_units/analytic_unit_model.ts

@ -84,6 +84,7 @@ export abstract class AnalyticUnit {
public toTemplate(): any { public toTemplate(): any {
const obj = _.cloneDeep(this.toObject()); const obj = _.cloneDeep(this.toObject());
delete obj._id;
obj.grafanaUrl = '${GRAFANA_URL}'; obj.grafanaUrl = '${GRAFANA_URL}';
obj.panelId = '${PANEL_ID}'; obj.panelId = '${PANEL_ID}';
obj.metric.datasource.url = '${DATASOURCE_URL}'; obj.metric.datasource.url = '${DATASOURCE_URL}';

8
server/src/models/detection_model.ts

@ -57,6 +57,14 @@ export class DetectionSpan {
}; };
} }
public toTemplate(): any {
return {
...this.toObject(),
_id: undefined,
analyticUnitId: undefined
};
}
static fromObject(obj: any): DetectionSpan { static fromObject(obj: any): DetectionSpan {
if(obj === undefined) { if(obj === undefined) {
throw new Error('obj is undefined'); throw new Error('obj is undefined');

11
server/src/models/grafana_panel_model.ts

@ -1,13 +1,6 @@
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 GrafanaPanelTemplate = { export type GrafanaPanelTemplate = {
analyticUnits: AnalyticUnit.AnalyticUnit[], // TODO: not any
caches: AnalyticUnitCache.AnalyticUnitCache[], analyticUnitTemplates: any[]
detectionSpans: DetectionSpan.DetectionSpan[],
segments: Segment.Segment[]
} }
export type GrafanaTemplateVariables = { export type GrafanaTemplateVariables = {

8
server/src/models/segment_model.ts

@ -49,6 +49,14 @@ export class Segment {
}; };
} }
public toTemplate(): any {
return {
...this.toObject(),
_id: undefined,
analyticUnitId: undefined
};
}
static fromObject(obj: any): Segment { static fromObject(obj: any): Segment {
if(obj === undefined) { if(obj === undefined) {
throw new Error('obj is undefined'); throw new Error('obj is undefined');

29
server/src/routes/panel_router.ts

@ -12,38 +12,17 @@ async function exportGrafanaPanelTemplate(ctx: Router.IRouterContext) {
const panelTemplate = await exportPanel(panelId); const panelTemplate = await exportPanel(panelId);
ctx.response.body = panelTemplate; ctx.response.body = panelTemplate;
} }
async function importGrafanaPanelTemplate(ctx: Router.IRouterContext) { async function importGrafanaPanelTemplate(ctx: Router.IRouterContext) {
const { panelTemplate, templateVariables } = ctx.request.body as { const { panelTemplate, templateVariables } = ctx.request.body as {
panelTemplate: GrafanaPanelTemplate, panelTemplate: GrafanaPanelTemplate,
templateVariables: GrafanaTemplateVariables templateVariables: GrafanaTemplateVariables
}; };
// TODO: move to model if(panelTemplate.analyticUnitTemplates === undefined) {
if(panelTemplate.analyticUnits === undefined) { throw new Error('Cannot import analytic units with undefined analyticUnitTemplates');
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); await importPanel(panelTemplate, templateVariables);
ctx.response.status = 200; ctx.response.status = 200;
} }

78
server/src/services/grafana_service.ts

@ -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)
]);
} }

Loading…
Cancel
Save