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
144 lines
3.3 KiB
![]()
7 years ago
|
import * as Router from 'koa-router';
|
||
![]()
7 years ago
|
|
||
|
import {
|
||
![]()
7 years ago
|
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';
|
||
|
|
||
7 years ago
|
async function sendAnomalyTypeStatus(ctx: Router.IRouterContext) {
|
||
|
let id = ctx.request.query.id;
|
||
7 years ago
|
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) {
|
||
7 years ago
|
ctx.response.status = 404;
|
||
![]()
7 years ago
|
return;
|
||
|
}
|
||
|
if(anomaly.status === undefined) {
|
||
|
throw new Error('No status for ' + name);
|
||
|
}
|
||
7 years ago
|
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
|
||
7 years ago
|
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 {
|
||
7 years ago
|
let id = ctx.request.query.id;
|
||
7 years ago
|
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) {
|
||
7 years ago
|
ctx.response.status = 404;
|
||
![]()
7 years ago
|
return;
|
||
|
}
|
||
|
|
||
7 years ago
|
ctx.response.body = {
|
||
![]()
7 years ago
|
name: unit.name,
|
||
|
metric: unit.metric,
|
||
|
status: unit.status
|
||
7 years ago
|
};
|
||
![]()
7 years ago
|
|
||
![]()
7 years ago
|
} catch(e) {
|
||
|
console.error(e);
|
||
|
// TODO: better send 404 when we know than isn`t found
|
||
7 years ago
|
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 {
|
||
7 years ago
|
let body = ctx.request.body;
|
||
![]()
7 years ago
|
const metric:Metric = {
|
||
7 years ago
|
datasource: body.metric.datasource,
|
||
|
targets: saveTargets(body.metric.targets)
|
||
![]()
7 years ago
|
};
|
||
|
|
||
![]()
7 years ago
|
const anomaly:AnalyticUnit = {
|
||
7 years ago
|
name: body.name.toLowerCase(),
|
||
7 years ago
|
panelUrl: body.panelUrl,
|
||
7 years ago
|
pattern: body.pattern.toLowerCase(),
|
||
![]()
7 years ago
|
metric: metric,
|
||
7 years ago
|
datasource: body.datasource,
|
||
![]()
7 years ago
|
status: 'learning',
|
||
![]()
7 years ago
|
lastPredictionTime: 0,
|
||
|
nextId: 0
|
||
![]()
7 years ago
|
};
|
||
7 years ago
|
let predictorId = insertAnomaly(anomaly);
|
||
|
if(predictorId === null) {
|
||
7 years ago
|
ctx.response.status = 403;
|
||
|
ctx.response.body = {
|
||
![]()
7 years ago
|
code: 403,
|
||
|
message: 'Already exists'
|
||
7 years ago
|
};
|
||
![]()
7 years ago
|
}
|
||
|
|
||
7 years ago
|
ctx.response.body = { predictor_id: predictorId };
|
||
![]()
7 years ago
|
|
||
7 years ago
|
runLearning(predictorId);
|
||
![]()
7 years ago
|
} catch(e) {
|
||
7 years ago
|
ctx.response.status = 500;
|
||
|
ctx.response.body = {
|
||
![]()
7 years ago
|
code: 500,
|
||
7 years ago
|
message: `Anomaly creation error: ${e.message}`
|
||
7 years ago
|
};
|
||
![]()
7 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
function deleteAnomaly(ctx: Router.IRouterContext) {
|
||
![]()
7 years ago
|
try {
|
||
7 years ago
|
let id = ctx.request.query.id;
|
||
7 years ago
|
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
|
|
||
7 years ago
|
ctx.response.body = {
|
||
![]()
7 years ago
|
code: 200,
|
||
|
message: 'Success'
|
||
7 years ago
|
};
|
||
![]()
7 years ago
|
} catch(e) {
|
||
7 years ago
|
ctx.response.status = 500;
|
||
|
ctx.response.body = {
|
||
![]()
7 years ago
|
code: 500,
|
||
7 years ago
|
message: `Anomaly deletion error: ${e.message}`
|
||
7 years ago
|
};
|
||
![]()
7 years ago
|
}
|
||
|
}
|
||
|
|
||
![]()
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);
|