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