|
|
|
@ -1,33 +1,28 @@
|
|
|
|
|
import * as Router from 'koa-router'; |
|
|
|
|
|
|
|
|
|
import { |
|
|
|
|
Datasource, |
|
|
|
|
Metric, |
|
|
|
|
AnalyticUnit, |
|
|
|
|
import * as AnalyticUnit from '../models/analytic_unit'; |
|
|
|
|
|
|
|
|
|
insertAnomaly, removeItem, loadPredictorById |
|
|
|
|
} from '../models/analytic_unit'; |
|
|
|
|
import { runLearning } from '../services/analytics' |
|
|
|
|
import { saveTargets } from '../services/metrics'; |
|
|
|
|
|
|
|
|
|
async function sendAnomalyTypeStatus(ctx: Router.IRouterContext) { |
|
|
|
|
async function sendStatus(ctx: Router.IRouterContext) { |
|
|
|
|
let id = ctx.request.query.id; |
|
|
|
|
let name = ctx.request.query.name.toLowerCase(); |
|
|
|
|
let name = ctx.request.query.name; |
|
|
|
|
try { |
|
|
|
|
let anomaly: AnalyticUnit; |
|
|
|
|
let unit: AnalyticUnit.AnalyticUnit; |
|
|
|
|
if(id === undefined) { |
|
|
|
|
throw new Error('Id is undefined'); |
|
|
|
|
} |
|
|
|
|
anomaly = loadPredictorById(id); |
|
|
|
|
unit = AnalyticUnit.loadById(id); |
|
|
|
|
|
|
|
|
|
if(anomaly === null) { |
|
|
|
|
if(unit === null) { |
|
|
|
|
ctx.response.status = 404; |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if(anomaly.status === undefined) { |
|
|
|
|
if(unit.status === undefined) { |
|
|
|
|
throw new Error('No status for ' + name); |
|
|
|
|
} |
|
|
|
|
ctx.response.body = { status: anomaly.status, errorMessage: anomaly.error }; |
|
|
|
|
ctx.response.body = { status: unit.status, errorMessage: unit.error }; |
|
|
|
|
} catch(e) { |
|
|
|
|
console.error(e); |
|
|
|
|
// TODO: better send 404 when we know than isn`t found
|
|
|
|
@ -37,20 +32,15 @@ async function sendAnomalyTypeStatus(ctx: Router.IRouterContext) {
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async function getAnalyticUnit(ctx: Router.IRouterContext) { |
|
|
|
|
async function findItem(ctx: Router.IRouterContext) { |
|
|
|
|
try { |
|
|
|
|
let id = ctx.request.query.id; |
|
|
|
|
let name = ctx.request.query.name.toLowerCase(); |
|
|
|
|
|
|
|
|
|
if(id === undefined) { |
|
|
|
|
throw new Error('No id param in query'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(name === undefined) { |
|
|
|
|
throw new Error('No name param in query'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let unit: AnalyticUnit = loadPredictorById(id); |
|
|
|
|
let unit: AnalyticUnit.AnalyticUnit = AnalyticUnit.loadById(id); |
|
|
|
|
|
|
|
|
|
if(unit === null) { |
|
|
|
|
ctx.response.status = 404; |
|
|
|
@ -67,19 +57,19 @@ async function getAnalyticUnit(ctx: Router.IRouterContext) {
|
|
|
|
|
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'; |
|
|
|
|
ctx.response.body = 'Can`t find anything'; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async function createAnalyticUnit(ctx: Router.IRouterContext) { |
|
|
|
|
async function createItem(ctx: Router.IRouterContext) { |
|
|
|
|
try { |
|
|
|
|
let body = ctx.request.body; |
|
|
|
|
const metric:Metric = { |
|
|
|
|
const metric: AnalyticUnit.Metric = { |
|
|
|
|
datasource: body.metric.datasource, |
|
|
|
|
targets: saveTargets(body.metric.targets) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const anomaly:AnalyticUnit = { |
|
|
|
|
const unit: AnalyticUnit.AnalyticUnit = { |
|
|
|
|
name: body.name.toLowerCase(), |
|
|
|
|
panelUrl: body.panelUrl, |
|
|
|
|
pattern: body.pattern.toLowerCase(), |
|
|
|
@ -89,36 +79,34 @@ async function createAnalyticUnit(ctx: Router.IRouterContext) {
|
|
|
|
|
lastPredictionTime: 0, |
|
|
|
|
nextId: 0 |
|
|
|
|
}; |
|
|
|
|
let predictorId = insertAnomaly(anomaly); |
|
|
|
|
if(predictorId === null) { |
|
|
|
|
|
|
|
|
|
let newId = AnalyticUnit.createItem(unit); |
|
|
|
|
if(newId === null) { |
|
|
|
|
ctx.response.status = 403; |
|
|
|
|
ctx.response.body = { |
|
|
|
|
code: 403, |
|
|
|
|
message: 'Already exists' |
|
|
|
|
message: 'Item exists' |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ctx.response.body = { predictor_id: predictorId }; |
|
|
|
|
ctx.response.body = { id: newId }; |
|
|
|
|
|
|
|
|
|
runLearning(predictorId); |
|
|
|
|
runLearning(newId); |
|
|
|
|
} catch(e) { |
|
|
|
|
ctx.response.status = 500; |
|
|
|
|
ctx.response.body = { |
|
|
|
|
code: 500, |
|
|
|
|
message: `Anomaly creation error: ${e.message}` |
|
|
|
|
message: `Creation error: ${e.message}` |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function deleteAnomaly(ctx: Router.IRouterContext) { |
|
|
|
|
function deleteItem(ctx: Router.IRouterContext) { |
|
|
|
|
try { |
|
|
|
|
let id = ctx.request.query.id; |
|
|
|
|
let name = ctx.request.query.name.toLowerCase(); |
|
|
|
|
|
|
|
|
|
if(id !== undefined) { |
|
|
|
|
removeItem(id); |
|
|
|
|
} else { |
|
|
|
|
removeItem(name); |
|
|
|
|
AnalyticUnit.removeItem(id); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
@ -129,7 +117,7 @@ function deleteAnomaly(ctx: Router.IRouterContext) {
|
|
|
|
|
ctx.response.status = 500; |
|
|
|
|
ctx.response.body = { |
|
|
|
|
code: 500, |
|
|
|
|
message: `Anomaly deletion error: ${e.message}` |
|
|
|
|
message: `Deletion error: ${e.message}` |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -137,7 +125,7 @@ function deleteAnomaly(ctx: Router.IRouterContext) {
|
|
|
|
|
|
|
|
|
|
export var router = new Router(); |
|
|
|
|
|
|
|
|
|
router.get('/status', sendAnomalyTypeStatus); |
|
|
|
|
router.get('/', getAnalyticUnit); |
|
|
|
|
router.post('/', createAnalyticUnit); |
|
|
|
|
router.delete('/', deleteAnomaly); |
|
|
|
|
router.get('/status', sendStatus); |
|
|
|
|
router.get('/', findItem); |
|
|
|
|
router.post('/', createItem); |
|
|
|
|
router.delete('/', deleteItem); |
|
|
|
|