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.

144 lines
3.3 KiB

import * as Router from 'koa-router';
7 years ago
import {
Datasource,
7 years ago
Metric,
7 years ago
AnalyticUnit,
insertAnomaly, removeItem, loadPredictorById
} from '../models/analytic_unit';
7 years ago
import { runLearning } from '../services/analytics'
import { saveTargets } from '../services/metrics';
async function sendAnomalyTypeStatus(ctx: Router.IRouterContext) {
let id = ctx.request.query.id;
let name = ctx.request.query.name.toLowerCase();
7 years ago
try {
7 years ago
let anomaly: AnalyticUnit;
if(id === undefined) {
throw new Error('Id is undefined');
7 years ago
}
7 years ago
anomaly = loadPredictorById(id);
7 years ago
if(anomaly === null) {
ctx.response.status = 404;
7 years ago
return;
}
if(anomaly.status === undefined) {
throw new Error('No status for ' + name);
}
ctx.response.body = { status: anomaly.status, errorMessage: anomaly.error };
7 years ago
} catch(e) {
console.error(e);
// TODO: better send 404 when we know than isn`t found
ctx.response.status = 500;
ctx.response.body = { error: 'Can`t return anything' };
7 years ago
}
}
7 years ago
async function getAnalyticUnit(ctx: Router.IRouterContext) {
7 years ago
try {
let id = ctx.request.query.id;
let name = ctx.request.query.name.toLowerCase();
7 years ago
7 years ago
if(id === undefined) {
throw new Error('No id param in query');
7 years ago
}
7 years ago
if(name === undefined) {
throw new Error('No name param in query');
}
let unit: AnalyticUnit = loadPredictorById(id);
if(unit === null) {
ctx.response.status = 404;
7 years ago
return;
}
ctx.response.body = {
7 years ago
name: unit.name,
metric: unit.metric,
status: unit.status
};
7 years ago
7 years ago
} catch(e) {
console.error(e);
// TODO: better send 404 when we know than isn`t found
ctx.response.status = 500;
ctx.response.body = 'Can`t get anything';
7 years ago
}
}
7 years ago
async function createAnalyticUnit(ctx: Router.IRouterContext) {
7 years ago
try {
let body = ctx.request.body;
7 years ago
const metric:Metric = {
datasource: body.metric.datasource,
targets: saveTargets(body.metric.targets)
7 years ago
};
7 years ago
const anomaly:AnalyticUnit = {
name: body.name.toLowerCase(),
panelUrl: body.panelUrl,
pattern: body.pattern.toLowerCase(),
7 years ago
metric: metric,
datasource: body.datasource,
7 years ago
status: 'learning',
7 years ago
lastPredictionTime: 0,
nextId: 0
7 years ago
};
let predictorId = insertAnomaly(anomaly);
if(predictorId === null) {
ctx.response.status = 403;
ctx.response.body = {
7 years ago
code: 403,
message: 'Already exists'
};
7 years ago
}
ctx.response.body = { predictor_id: predictorId };
7 years ago
runLearning(predictorId);
7 years ago
} catch(e) {
ctx.response.status = 500;
ctx.response.body = {
7 years ago
code: 500,
message: `Anomaly creation error: ${e.message}`
};
7 years ago
}
}
function deleteAnomaly(ctx: Router.IRouterContext) {
7 years ago
try {
let id = ctx.request.query.id;
let name = ctx.request.query.name.toLowerCase();
7 years ago
if(id !== undefined) {
7 years ago
removeItem(id);
7 years ago
} else {
7 years ago
removeItem(name);
7 years ago
}
7 years ago
ctx.response.body = {
7 years ago
code: 200,
message: 'Success'
};
7 years ago
} catch(e) {
ctx.response.status = 500;
ctx.response.body = {
7 years ago
code: 500,
message: `Anomaly deletion error: ${e.message}`
};
7 years ago
}
}
export var router = new Router();
7 years ago
router.get('/status', sendAnomalyTypeStatus);
7 years ago
router.get('/', getAnalyticUnit);
router.post('/', createAnalyticUnit);
7 years ago
router.delete('/', deleteAnomaly);