|
|
@ -10,9 +10,9 @@ import { |
|
|
|
import { runLearning } from '../services/analytics' |
|
|
|
import { runLearning } from '../services/analytics' |
|
|
|
import { saveTargets } from '../services/metrics'; |
|
|
|
import { saveTargets } from '../services/metrics'; |
|
|
|
|
|
|
|
|
|
|
|
async function sendAnomalyTypeStatus(req, res) { |
|
|
|
async function sendAnomalyTypeStatus(ctx: Router.IRouterContext) { |
|
|
|
let id = req.query.id; |
|
|
|
let id = ctx.request.query.id; |
|
|
|
let name = req.query.name; |
|
|
|
let name = ctx.request.query.name; |
|
|
|
try { |
|
|
|
try { |
|
|
|
let anomaly: Anomaly; |
|
|
|
let anomaly: Anomaly; |
|
|
|
if(id !== undefined) { |
|
|
|
if(id !== undefined) { |
|
|
@ -21,28 +21,26 @@ async function sendAnomalyTypeStatus(req, res) { |
|
|
|
anomaly = loadAnomalyByName(name); |
|
|
|
anomaly = loadAnomalyByName(name); |
|
|
|
} |
|
|
|
} |
|
|
|
if(anomaly === null) { |
|
|
|
if(anomaly === null) { |
|
|
|
res.status(404).send({ |
|
|
|
ctx.response.status = 404; |
|
|
|
code: 404, |
|
|
|
|
|
|
|
message: 'Not found' |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
if(anomaly.status === undefined) { |
|
|
|
if(anomaly.status === undefined) { |
|
|
|
throw new Error('No status for ' + name); |
|
|
|
throw new Error('No status for ' + name); |
|
|
|
} |
|
|
|
} |
|
|
|
res.status(200).send({ status: anomaly.status, errorMessage: anomaly.error }); |
|
|
|
ctx.response.body = { status: anomaly.status, errorMessage: anomaly.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
|
|
|
|
res.status(500).send({ error: 'Can`t return anything' }); |
|
|
|
ctx.response.status = 500; |
|
|
|
|
|
|
|
ctx.response.body = { error: 'Can`t return anything' }; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function getAnomaly(req, res) { |
|
|
|
async function getAnomaly(ctx: Router.IRouterContext) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
let id = req.query.id; |
|
|
|
let id = ctx.request.query.id; |
|
|
|
let name = req.query.name; |
|
|
|
let name = ctx.request.query.name; |
|
|
|
|
|
|
|
|
|
|
|
let anomaly:Anomaly; |
|
|
|
let anomaly:Anomaly; |
|
|
|
if(id !== undefined) { |
|
|
|
if(id !== undefined) { |
|
|
@ -51,66 +49,65 @@ async function getAnomaly(req, res) { |
|
|
|
anomaly = loadAnomalyByName(name.toLowerCase()); |
|
|
|
anomaly = loadAnomalyByName(name.toLowerCase()); |
|
|
|
} |
|
|
|
} |
|
|
|
if(anomaly === null) { |
|
|
|
if(anomaly === null) { |
|
|
|
res.status(404).send({ |
|
|
|
ctx.response.status = 404; |
|
|
|
code: 404, |
|
|
|
|
|
|
|
message: 'Not found' |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let payload = JSON.stringify({ |
|
|
|
ctx.response.body = { |
|
|
|
name: anomaly.name, |
|
|
|
name: anomaly.name, |
|
|
|
metric: anomaly.metric, |
|
|
|
metric: anomaly.metric, |
|
|
|
status: anomaly.status |
|
|
|
status: anomaly.status |
|
|
|
}); |
|
|
|
}; |
|
|
|
res.status(200).send(payload) |
|
|
|
|
|
|
|
} 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
|
|
|
|
res.status(500).send('Can`t get anything'); |
|
|
|
ctx.response.status = 500; |
|
|
|
|
|
|
|
ctx.response.body = 'Can`t get anything'; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function createAnomaly(req, res) { |
|
|
|
async function createAnomaly(ctx: Router.IRouterContext) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
|
|
|
|
let body = ctx.request.body; |
|
|
|
const metric:Metric = { |
|
|
|
const metric:Metric = { |
|
|
|
datasource: req.body.metric.datasource, |
|
|
|
datasource: body.metric.datasource, |
|
|
|
targets: saveTargets(req.body.metric.targets) |
|
|
|
targets: saveTargets(body.metric.targets) |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const anomaly:Anomaly = { |
|
|
|
const anomaly:Anomaly = { |
|
|
|
name: req.body.name, |
|
|
|
name: body.name, |
|
|
|
panelUrl: req.body.panelUrl, |
|
|
|
panelUrl: body.panelUrl, |
|
|
|
metric: metric, |
|
|
|
metric: metric, |
|
|
|
datasource: req.body.datasource, |
|
|
|
datasource: body.datasource, |
|
|
|
status: 'learning', |
|
|
|
status: 'learning', |
|
|
|
last_prediction_time: 0, |
|
|
|
last_prediction_time: 0, |
|
|
|
next_id: 0 |
|
|
|
next_id: 0 |
|
|
|
}; |
|
|
|
}; |
|
|
|
let anomalyId = insertAnomaly(anomaly); |
|
|
|
let anomalyId = insertAnomaly(anomaly); |
|
|
|
if(anomalyId === null) { |
|
|
|
if(anomalyId === null) { |
|
|
|
res.status(403).send({ |
|
|
|
ctx.response.status = 403; |
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
code: 403, |
|
|
|
code: 403, |
|
|
|
message: 'Already exists' |
|
|
|
message: 'Already exists' |
|
|
|
}); |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let payload = JSON.stringify({ anomaly_id: anomalyId }) |
|
|
|
ctx.response.body = { anomaly_id: anomalyId }; |
|
|
|
res.status(200).send(payload); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
runLearning(anomalyId); |
|
|
|
runLearning(anomalyId); |
|
|
|
} catch(e) { |
|
|
|
} catch(e) { |
|
|
|
res.status(500).send({ |
|
|
|
ctx.response.status = 500; |
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
code: 500, |
|
|
|
code: 500, |
|
|
|
message: 'Internal error' |
|
|
|
message: 'Internal error' |
|
|
|
}); |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function deleteAnomaly(req, res) { |
|
|
|
function deleteAnomaly(ctx: Router.IRouterContext) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
let id = req.query.id; |
|
|
|
let id = ctx.request.query.id; |
|
|
|
let name = req.query.name; |
|
|
|
let name = ctx.request.query.name; |
|
|
|
|
|
|
|
|
|
|
|
if(id !== undefined) { |
|
|
|
if(id !== undefined) { |
|
|
|
removeAnomaly(id); |
|
|
|
removeAnomaly(id); |
|
|
@ -118,15 +115,16 @@ function deleteAnomaly(req, res) { |
|
|
|
removeAnomaly(name.toLowerCase()); |
|
|
|
removeAnomaly(name.toLowerCase()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
res.status(200).send({ |
|
|
|
ctx.response.body = { |
|
|
|
code: 200, |
|
|
|
code: 200, |
|
|
|
message: 'Success' |
|
|
|
message: 'Success' |
|
|
|
}); |
|
|
|
}; |
|
|
|
} catch(e) { |
|
|
|
} catch(e) { |
|
|
|
res.status(500).send({ |
|
|
|
ctx.response.status = 500; |
|
|
|
|
|
|
|
ctx.response.body = { |
|
|
|
code: 500, |
|
|
|
code: 500, |
|
|
|
message: 'Internal error' |
|
|
|
message: 'Internal error' |
|
|
|
}); |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|