|
|
@ -7,7 +7,6 @@ import * as Router from 'koa-router'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function getStatus(ctx: Router.IRouterContext) { |
|
|
|
async function getStatus(ctx: Router.IRouterContext) { |
|
|
|
try { |
|
|
|
|
|
|
|
let analyticUnitId = ctx.request.query.id; |
|
|
|
let analyticUnitId = ctx.request.query.id; |
|
|
|
if(analyticUnitId === undefined) { |
|
|
|
if(analyticUnitId === undefined) { |
|
|
|
throw new Error('Cannot get status of undefined id'); |
|
|
|
throw new Error('Cannot get status of undefined id'); |
|
|
@ -25,18 +24,9 @@ async function getStatus(ctx: Router.IRouterContext) { |
|
|
|
if(analyticUnit.status === AnalyticUnit.AnalyticUnitStatus.FAILED) { |
|
|
|
if(analyticUnit.status === AnalyticUnit.AnalyticUnitStatus.FAILED) { |
|
|
|
ctx.response.body.errorMessage = analyticUnit.error; |
|
|
|
ctx.response.body.errorMessage = analyticUnit.error; |
|
|
|
} |
|
|
|
} |
|
|
|
} catch(e) { |
|
|
|
|
|
|
|
console.error(e); |
|
|
|
|
|
|
|
ctx.response.status = 404; |
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
|
|
|
|
code: 404, |
|
|
|
|
|
|
|
message: `GET /analyticUnits/status error: ${e.message}` |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function getUnit(ctx: Router.IRouterContext) { |
|
|
|
async function getUnit(ctx: Router.IRouterContext) { |
|
|
|
try { |
|
|
|
|
|
|
|
let analyticUnitId = ctx.request.query.id; |
|
|
|
let analyticUnitId = ctx.request.query.id; |
|
|
|
if(analyticUnitId === undefined) { |
|
|
|
if(analyticUnitId === undefined) { |
|
|
|
throw new Error('No id param in query'); |
|
|
|
throw new Error('No id param in query'); |
|
|
@ -52,19 +42,9 @@ async function getUnit(ctx: Router.IRouterContext) { |
|
|
|
metric: analyticUnit.metric, |
|
|
|
metric: analyticUnit.metric, |
|
|
|
status: analyticUnit.status |
|
|
|
status: analyticUnit.status |
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} catch(e) { |
|
|
|
|
|
|
|
console.error(e); |
|
|
|
|
|
|
|
ctx.response.status = 404; |
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
|
|
|
|
code: 404, |
|
|
|
|
|
|
|
message: `GET /analyticUnits error: ${e.message}` |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function getUnits(ctx: Router.IRouterContext) { |
|
|
|
async function getUnits(ctx: Router.IRouterContext) { |
|
|
|
try { |
|
|
|
|
|
|
|
const panelUrl = ctx.request.query.panelUrl; |
|
|
|
const panelUrl = ctx.request.query.panelUrl; |
|
|
|
if(panelUrl === undefined) { |
|
|
|
if(panelUrl === undefined) { |
|
|
|
throw new Error('Cannot get alerts of undefined panelUrl'); |
|
|
|
throw new Error('Cannot get alerts of undefined panelUrl'); |
|
|
@ -75,17 +55,7 @@ async function getUnits(ctx: Router.IRouterContext) { |
|
|
|
analyticUnits = []; |
|
|
|
analyticUnits = []; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
ctx.response.body = { analyticUnits }; |
|
|
|
analyticUnits |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} catch(e) { |
|
|
|
|
|
|
|
console.error(e); |
|
|
|
|
|
|
|
ctx.response.status = 404; |
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
|
|
|
|
code: 404, |
|
|
|
|
|
|
|
message: `GET /analyticUnits/units error: ${e.message}` |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function getTypes(ctx: Router.IRouterContext) { |
|
|
|
function getTypes(ctx: Router.IRouterContext) { |
|
|
@ -93,20 +63,11 @@ function getTypes(ctx: Router.IRouterContext) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function createUnit(ctx: Router.IRouterContext) { |
|
|
|
async function createUnit(ctx: Router.IRouterContext) { |
|
|
|
try { |
|
|
|
|
|
|
|
let id = await createAnalyticUnitFromObject(ctx.request.body); |
|
|
|
let id = await createAnalyticUnitFromObject(ctx.request.body); |
|
|
|
ctx.response.body = { id }; |
|
|
|
ctx.response.body = { id }; |
|
|
|
} catch(e) { |
|
|
|
|
|
|
|
ctx.response.status = 500; |
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
|
|
|
|
code: 500, |
|
|
|
|
|
|
|
message: `POST /analyticUnits error: ${e.message}` |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function updateUnit(ctx: Router.IRouterContext) { |
|
|
|
async function updateUnit(ctx: Router.IRouterContext) { |
|
|
|
try { |
|
|
|
|
|
|
|
const unit = ctx.request.body as AnalyticUnit.AnalyticUnit; |
|
|
|
const unit = ctx.request.body as AnalyticUnit.AnalyticUnit; |
|
|
|
if(unit.id === undefined) { |
|
|
|
if(unit.id === undefined) { |
|
|
|
throw new Error('Cannot update undefined id'); |
|
|
|
throw new Error('Cannot update undefined id'); |
|
|
@ -114,22 +75,13 @@ async function updateUnit(ctx: Router.IRouterContext) { |
|
|
|
|
|
|
|
|
|
|
|
// TODO: we can't allow to update everything
|
|
|
|
// TODO: we can't allow to update everything
|
|
|
|
AnalyticUnit.update(unit.id, unit); |
|
|
|
AnalyticUnit.update(unit.id, unit); |
|
|
|
|
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
ctx.response.body = { |
|
|
|
code: 200, |
|
|
|
code: 200, |
|
|
|
message: 'Success' |
|
|
|
message: 'Success' |
|
|
|
}; |
|
|
|
}; |
|
|
|
} catch (e) { |
|
|
|
|
|
|
|
ctx.response.status = 500; |
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
|
|
|
|
code: 500, |
|
|
|
|
|
|
|
message: `PATCH /analyticUnits error: ${e.message}` |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function updateMetric(ctx: Router.IRouterContext) { |
|
|
|
async function updateMetric(ctx: Router.IRouterContext) { |
|
|
|
try { |
|
|
|
|
|
|
|
const { analyticUnitId, metric, datasource } = ctx.request.body as { |
|
|
|
const { analyticUnitId, metric, datasource } = ctx.request.body as { |
|
|
|
analyticUnitId: AnalyticUnit.AnalyticUnitId, metric: any, datasource: any |
|
|
|
analyticUnitId: AnalyticUnit.AnalyticUnitId, metric: any, datasource: any |
|
|
|
}; |
|
|
|
}; |
|
|
@ -149,17 +101,9 @@ async function updateMetric(ctx: Router.IRouterContext) { |
|
|
|
code: 200, |
|
|
|
code: 200, |
|
|
|
message: 'Success' |
|
|
|
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) { |
|
|
|
async function updateAlert(ctx: Router.IRouterContext) { |
|
|
|
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 |
|
|
|
}; |
|
|
|
}; |
|
|
@ -176,17 +120,10 @@ async function updateAlert(ctx: Router.IRouterContext) { |
|
|
|
code: 200, |
|
|
|
code: 200, |
|
|
|
message: 'Success' |
|
|
|
message: 'Success' |
|
|
|
}; |
|
|
|
}; |
|
|
|
} catch(e) { |
|
|
|
|
|
|
|
ctx.response.status = 500; |
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
|
|
|
|
code: 500, |
|
|
|
|
|
|
|
message: `PATCH /analyticUnits/alert error: ${e.message}` |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function deleteUnit(ctx: Router.IRouterContext) { |
|
|
|
async function deleteUnit(ctx: Router.IRouterContext) { |
|
|
|
try { |
|
|
|
|
|
|
|
const analyticUnitId = ctx.request.query.id; |
|
|
|
const analyticUnitId = ctx.request.query.id; |
|
|
|
if(analyticUnitId === undefined) { |
|
|
|
if(analyticUnitId === undefined) { |
|
|
|
throw new Error('Cannot delete undefined id'); |
|
|
|
throw new Error('Cannot delete undefined id'); |
|
|
@ -196,35 +133,16 @@ async function deleteUnit(ctx: Router.IRouterContext) { |
|
|
|
code: 200, |
|
|
|
code: 200, |
|
|
|
message: 'Success' |
|
|
|
message: 'Success' |
|
|
|
}; |
|
|
|
}; |
|
|
|
} catch(e) { |
|
|
|
|
|
|
|
console.error(e); |
|
|
|
|
|
|
|
ctx.response.status = 500; |
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
|
|
|
|
code: 500, |
|
|
|
|
|
|
|
message: `DELETE /analyticUnits error: ${e.message}` |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function runDetect(ctx: Router.IRouterContext) { |
|
|
|
async function runDetect(ctx: Router.IRouterContext) { |
|
|
|
try { |
|
|
|
|
|
|
|
const { id: analyticUnitId } = ctx.request.body as { id: AnalyticUnit.AnalyticUnitId }; |
|
|
|
const { id: analyticUnitId } = ctx.request.body as { id: AnalyticUnit.AnalyticUnitId }; |
|
|
|
|
|
|
|
|
|
|
|
AnalyticsController.runFirstLearning(analyticUnitId); |
|
|
|
AnalyticsController.runFirstLearning(analyticUnitId); |
|
|
|
|
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
ctx.response.body = { |
|
|
|
code: 200, |
|
|
|
code: 200, |
|
|
|
message: 'Success' |
|
|
|
message: 'Success' |
|
|
|
}; |
|
|
|
}; |
|
|
|
} catch (e) { |
|
|
|
|
|
|
|
ctx.response.status = 500; |
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
|
|
|
|
code: 500, |
|
|
|
|
|
|
|
message: `POST /analyticUnits/detect error: ${e.message}` |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export var router = new Router(); |
|
|
|
export var router = new Router(); |
|
|
|