You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.0 KiB

import * as AnalyticsController from '../controllers/analytics_controller';
import * as AnalyticUnit from '../models/analytic_unit_model';
import { createAnalyticUnitFromObject } from '../controllers/analytics_controller';
6 years ago
import * as Router from 'koa-router';
6 years ago
6 years ago
async function getStatus(ctx: Router.IRouterContext) {
6 years ago
try {
let analyticUnitId = ctx.request.query.id;
if(analyticUnitId === undefined) {
throw new Error('Cannot get status of undefined id');
}
let analyticUnit = await AnalyticUnit.findById(analyticUnitId);
if(analyticUnit === null) {
throw new Error(`Cannot find analytic unit with id ${analyticUnitId}`);
}
ctx.response.body = {
status: analyticUnit.status
};
if(analyticUnit.status === AnalyticUnit.AnalyticUnitStatus.FAILED) {
ctx.response.body.errorMessage = analyticUnit.error;
}
6 years ago
} catch(e) {
console.error(e);
ctx.response.status = 404;
ctx.response.body = {
code: 404,
message: `GET /analyticUnits/status error: ${e.message}`
};
6 years ago
}
}
async function getUnit(ctx: Router.IRouterContext) {
6 years ago
try {
let analyticUnitId = ctx.request.query.id;
if(analyticUnitId === undefined) {
6 years ago
throw new Error('No id param in query');
6 years ago
}
6 years ago
let analyticUnit = await AnalyticUnit.findById(analyticUnitId);
if(analyticUnit === null) {
throw new Error(`Cannot find analytic unit with id ${analyticUnitId}`);
}
6 years ago
ctx.response.body = {
name: analyticUnit.name,
metric: analyticUnit.metric,
status: analyticUnit.status
};
6 years ago
6 years ago
} catch(e) {
console.error(e);
ctx.response.status = 404;
ctx.response.body = {
code: 404,
message: `GET /analyticUnits error: ${e.message}`
};
6 years ago
}
}
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');
}
let analyticUnits = await AnalyticUnit.findMany({ panelUrl });
if(analyticUnits === null) {
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}`
};
}
}
async function createUnit(ctx: Router.IRouterContext) {
6 years ago
try {
let id = await createAnalyticUnitFromObject(ctx.request.body);
ctx.response.body = { id };
6 years ago
} catch(e) {
ctx.response.status = 500;
ctx.response.body = {
6 years ago
code: 500,
message: `POST /analyticUnits error: ${e.message}`
};
6 years ago
}
}
async function setAlert(ctx: Router.IRouterContext) {
try {
const { analyticUnitId, alert } = ctx.request.body as {
analyticUnitId: AnalyticUnit.AnalyticUnitId, alert: boolean
};
if(analyticUnitId === undefined) {
throw new Error('Cannot update undefined id');
}
if(alert === undefined) {
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}`
};
}
6 years ago
}
async function deleteUnit(ctx: Router.IRouterContext) {
6 years ago
try {
const analyticUnitId = ctx.request.query.id;
if(analyticUnitId === undefined) {
throw new Error('Cannot delete undefined id');
}
await AnalyticsController.remove(analyticUnitId);
ctx.response.body = {
6 years ago
code: 200,
message: 'Success'
};
6 years ago
} catch(e) {
console.error(e);
ctx.response.status = 500;
ctx.response.body = {
6 years ago
code: 500,
message: `DELETE /analyticUnits error: ${e.message}`
};
6 years ago
}
}
export var router = new Router();
6 years ago
router.get('/', getUnit);
router.get('/units', getUnits);
6 years ago
router.get('/status', getStatus);
router.patch('/alert', setAlert);
router.post('/', createUnit);
router.delete('/', deleteUnit);