diff --git a/server/src/controllers/analytics_controller.ts b/server/src/controllers/analytics_controller.ts index 84fb038..b3ad076 100644 --- a/server/src/controllers/analytics_controller.ts +++ b/server/src/controllers/analytics_controller.ts @@ -340,6 +340,11 @@ export async function setAlert(analyticUnitId: AnalyticUnit.AnalyticUnitId, aler } } +export async function setMetric(analyticUnitId: AnalyticUnit.AnalyticUnitId, metric: any, datasource: any) { + metric.datasource = datasource; + AnalyticUnit.setMetric(analyticUnitId, metric); +} + export async function updateSegments( id: AnalyticUnit.AnalyticUnitId, segmentsToInsert: Segment.Segment[], diff --git a/server/src/models/analytic_unit_model.ts b/server/src/models/analytic_unit_model.ts index 9cdaae0..600a956 100644 --- a/server/src/models/analytic_unit_model.ts +++ b/server/src/models/analytic_unit_model.ts @@ -69,7 +69,7 @@ export class AnalyticUnit { public name: string, public panelUrl: string, public type: string, - public metric: Metric, + public metric?: Metric, public alert?: boolean, public id?: AnalyticUnitId, public lastDetectionTime?: number, @@ -85,18 +85,20 @@ export class AnalyticUnit { if(type === undefined) { throw new Error(`Missing field "type"`); } - if(metric === undefined) { - throw new Error(`Missing field "metric"`); - } } public toObject() { + let metric; + if(this.metric !== undefined) { + metric = this.metric.toObject(); + } + return { _id: this.id, name: this.name, panelUrl: this.panelUrl, type: this.type, - metric: this.metric.toObject(), + metric, alert: this.alert, lastDetectionTime: this.lastDetectionTime, status: this.status, @@ -108,11 +110,15 @@ export class AnalyticUnit { if(obj === undefined) { throw new Error('obj is undefined'); } + let metric: Metric; + if(obj.metric !== undefined) { + metric = Metric.fromObject(obj.metric); + } return new AnalyticUnit( obj.name, obj.panelUrl, obj.type, - Metric.fromObject(obj.metric), + metric, obj.alert, obj._id, obj.lastDetectionTime, @@ -174,6 +180,10 @@ export async function setAlert(id: AnalyticUnitId, alert: boolean) { return db.updateOne(id, { alert }); } +export async function setMetric(id: AnalyticUnitId, metric: Metric) { + return db.updateOne(id, { metric }); +} + export function getDetectorByType(analyticUnitType: string): DetectorType { let detector; _.forOwn(ANALYTIC_UNIT_TYPES, (types, detectorType) => { diff --git a/server/src/routes/analytic_units_router.ts b/server/src/routes/analytic_units_router.ts index 6de6335..b2f28fb 100644 --- a/server/src/routes/analytic_units_router.ts +++ b/server/src/routes/analytic_units_router.ts @@ -105,7 +105,37 @@ async function createUnit(ctx: Router.IRouterContext) { } } -async function setAlert(ctx: Router.IRouterContext) { +async function updateMetric(ctx: Router.IRouterContext) { + try { + const { analyticUnitId, metric, datasource } = ctx.request.body as { + analyticUnitId: AnalyticUnit.AnalyticUnitId, metric: any, datasource: any + }; + if(analyticUnitId === undefined) { + throw new Error('Cannot update undefined id'); + } + if(metric === undefined) { + throw new Error('Cannot set undefined metric'); + } + if(datasource === undefined) { + throw new Error('Cannot set undefined datasource'); + } + + await AnalyticsController.setMetric(analyticUnitId, metric, datasource); + + ctx.response.body = { + code: 200, + message: 'Success' + }; + } catch (e) { + ctx.response.status = 500; + ctx.response.body = { + code: 500, + message: `PATCH /analyticUnits/metric error: ${e.message}` + }; + } +} + +async function updateAlert(ctx: Router.IRouterContext) { try { const { analyticUnitId, alert } = ctx.request.body as { analyticUnitId: AnalyticUnit.AnalyticUnitId, alert: boolean @@ -160,6 +190,7 @@ router.get('/', getUnit); router.get('/units', getUnits); router.get('/status', getStatus); router.get('/types', getTypes); -router.patch('/alert', setAlert); +router.patch('/metric', updateMetric); +router.patch('/alert', updateAlert); router.post('/', createUnit); router.delete('/', deleteUnit);