|
|
@ -7,85 +7,55 @@ 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'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let analyticUnit = await AnalyticUnit.findById(analyticUnitId); |
|
|
|
let analyticUnit = await AnalyticUnit.findById(analyticUnitId); |
|
|
|
if(analyticUnit === null) { |
|
|
|
if(analyticUnit === null) { |
|
|
|
throw new Error(`Cannot find analytic unit with id ${analyticUnitId}`); |
|
|
|
throw new Error(`Cannot find analytic unit with id ${analyticUnitId}`); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
ctx.response.body = { |
|
|
|
status: analyticUnit.status |
|
|
|
status: analyticUnit.status |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
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'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let analyticUnit = await AnalyticUnit.findById(analyticUnitId); |
|
|
|
let analyticUnit = await AnalyticUnit.findById(analyticUnitId); |
|
|
|
if(analyticUnit === null) { |
|
|
|
if(analyticUnit === null) { |
|
|
|
throw new Error(`Cannot find analytic unit with id ${analyticUnitId}`); |
|
|
|
throw new Error(`Cannot find analytic unit with id ${analyticUnitId}`); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
|
|
|
|
name: analyticUnit.name, |
|
|
|
|
|
|
|
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}` |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
|
|
|
|
name: analyticUnit.name, |
|
|
|
|
|
|
|
metric: analyticUnit.metric, |
|
|
|
|
|
|
|
status: analyticUnit.status |
|
|
|
|
|
|
|
};
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let analyticUnits = await AnalyticUnit.findMany({ panelUrl }); |
|
|
|
let analyticUnits = await AnalyticUnit.findMany({ panelUrl }); |
|
|
|
if(analyticUnits === null) { |
|
|
|
if(analyticUnits === null) { |
|
|
|
analyticUnits = []; |
|
|
|
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) { |
|
|
|
function getTypes(ctx: Router.IRouterContext) { |
|
|
@ -93,140 +63,88 @@ 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'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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}` |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: we can't allow to update everything
|
|
|
|
|
|
|
|
AnalyticUnit.update(unit.id, unit); |
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
|
|
|
|
code: 200, |
|
|
|
|
|
|
|
message: 'Success' |
|
|
|
|
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
}; |
|
|
|
}; |
|
|
|
if(analyticUnitId === undefined) { |
|
|
|
if(analyticUnitId === undefined) { |
|
|
|
throw new Error('Cannot update undefined id'); |
|
|
|
throw new Error('Cannot update undefined id'); |
|
|
|
} |
|
|
|
} |
|
|
|
if(metric === undefined) { |
|
|
|
if(metric === undefined) { |
|
|
|
throw new Error('Cannot set undefined metric'); |
|
|
|
throw new Error('Cannot set undefined metric'); |
|
|
|
} |
|
|
|
} |
|
|
|
if(datasource === undefined) { |
|
|
|
if(datasource === undefined) { |
|
|
|
throw new Error('Cannot set undefined datasource'); |
|
|
|
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}` |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await AnalyticsController.setMetric(analyticUnitId, metric, datasource); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
|
|
|
|
code: 200, |
|
|
|
|
|
|
|
message: 'Success' |
|
|
|
|
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
}; |
|
|
|
}; |
|
|
|
if(analyticUnitId === undefined) { |
|
|
|
if(analyticUnitId === undefined) { |
|
|
|
throw new Error('Cannot update undefined id'); |
|
|
|
throw new Error('Cannot update undefined id'); |
|
|
|
} |
|
|
|
} |
|
|
|
if(alert === undefined) { |
|
|
|
if(alert === undefined) { |
|
|
|
throw new Error('Cannot set undefined alert status'); |
|
|
|
throw new Error('Cannot set undefined alert status'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await AnalyticsController.setAlert(analyticUnitId, alert); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
|
|
|
|
code: 200, |
|
|
|
|
|
|
|
message: 'Success' |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} catch(e) { |
|
|
|
|
|
|
|
ctx.response.status = 500; |
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
|
|
|
|
code: 500, |
|
|
|
|
|
|
|
message: `PATCH /analyticUnits/alert error: ${e.message}` |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await AnalyticsController.setAlert(analyticUnitId, alert); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
|
|
|
|
code: 200, |
|
|
|
|
|
|
|
message: 'Success' |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
await AnalyticsController.remove(analyticUnitId); |
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
|
|
|
|
code: 200, |
|
|
|
|
|
|
|
message: 'Success' |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} catch(e) { |
|
|
|
|
|
|
|
console.error(e); |
|
|
|
|
|
|
|
ctx.response.status = 500; |
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
|
|
|
|
code: 500, |
|
|
|
|
|
|
|
message: `DELETE /analyticUnits error: ${e.message}` |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
await AnalyticsController.remove(analyticUnitId); |
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
|
|
|
|
code: 200, |
|
|
|
|
|
|
|
message: 'Success' |
|
|
|
|
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
AnalyticsController.runFirstLearning(analyticUnitId); |
|
|
|
code: 200, |
|
|
|
|
|
|
|
message: 'Success' |
|
|
|
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(); |
|
|
|
export var router = new Router(); |
|
|
|
|
|
|
|
|
|
|
|
router.get('/', getUnit); |
|
|
|
router.get('/', getUnit); |
|
|
|