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