Browse Source

Endpoint for setting analytic unit metric #338 (#339)

pull/1/head
rozetko 6 years ago committed by GitHub
parent
commit
4bafca64c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      server/src/controllers/analytics_controller.ts
  2. 22
      server/src/models/analytic_unit_model.ts
  3. 35
      server/src/routes/analytic_units_router.ts

5
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( export async function updateSegments(
id: AnalyticUnit.AnalyticUnitId, id: AnalyticUnit.AnalyticUnitId,
segmentsToInsert: Segment.Segment[], segmentsToInsert: Segment.Segment[],

22
server/src/models/analytic_unit_model.ts

@ -69,7 +69,7 @@ export class AnalyticUnit {
public name: string, public name: string,
public panelUrl: string, public panelUrl: string,
public type: string, public type: string,
public metric: Metric, public metric?: Metric,
public alert?: boolean, public alert?: boolean,
public id?: AnalyticUnitId, public id?: AnalyticUnitId,
public lastDetectionTime?: number, public lastDetectionTime?: number,
@ -85,18 +85,20 @@ export class AnalyticUnit {
if(type === undefined) { if(type === undefined) {
throw new Error(`Missing field "type"`); throw new Error(`Missing field "type"`);
} }
if(metric === undefined) {
throw new Error(`Missing field "metric"`);
}
} }
public toObject() { public toObject() {
let metric;
if(this.metric !== undefined) {
metric = this.metric.toObject();
}
return { return {
_id: this.id, _id: this.id,
name: this.name, name: this.name,
panelUrl: this.panelUrl, panelUrl: this.panelUrl,
type: this.type, type: this.type,
metric: this.metric.toObject(), metric,
alert: this.alert, alert: this.alert,
lastDetectionTime: this.lastDetectionTime, lastDetectionTime: this.lastDetectionTime,
status: this.status, status: this.status,
@ -108,11 +110,15 @@ export class AnalyticUnit {
if(obj === undefined) { if(obj === undefined) {
throw new Error('obj is undefined'); throw new Error('obj is undefined');
} }
let metric: Metric;
if(obj.metric !== undefined) {
metric = Metric.fromObject(obj.metric);
}
return new AnalyticUnit( return new AnalyticUnit(
obj.name, obj.name,
obj.panelUrl, obj.panelUrl,
obj.type, obj.type,
Metric.fromObject(obj.metric), metric,
obj.alert, obj.alert,
obj._id, obj._id,
obj.lastDetectionTime, obj.lastDetectionTime,
@ -174,6 +180,10 @@ export async function setAlert(id: AnalyticUnitId, alert: boolean) {
return db.updateOne(id, { alert }); return db.updateOne(id, { alert });
} }
export async function setMetric(id: AnalyticUnitId, metric: Metric) {
return db.updateOne(id, { metric });
}
export function getDetectorByType(analyticUnitType: string): DetectorType { export function getDetectorByType(analyticUnitType: string): DetectorType {
let detector; let detector;
_.forOwn(ANALYTIC_UNIT_TYPES, (types, detectorType) => { _.forOwn(ANALYTIC_UNIT_TYPES, (types, detectorType) => {

35
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 { try {
const { analyticUnitId, alert } = ctx.request.body as { const { analyticUnitId, alert } = ctx.request.body as {
analyticUnitId: AnalyticUnit.AnalyticUnitId, alert: boolean analyticUnitId: AnalyticUnit.AnalyticUnitId, alert: boolean
@ -160,6 +190,7 @@ router.get('/', getUnit);
router.get('/units', getUnits); router.get('/units', getUnits);
router.get('/status', getStatus); router.get('/status', getStatus);
router.get('/types', getTypes); router.get('/types', getTypes);
router.patch('/alert', setAlert); router.patch('/metric', updateMetric);
router.patch('/alert', updateAlert);
router.post('/', createUnit); router.post('/', createUnit);
router.delete('/', deleteUnit); router.delete('/', deleteUnit);

Loading…
Cancel
Save