|
|
@ -1,51 +1,48 @@ |
|
|
|
import * as express from 'express'; |
|
|
|
import * as Router from 'koa-router'; |
|
|
|
|
|
|
|
|
|
|
|
import { |
|
|
|
import { |
|
|
|
getLabeledSegments, |
|
|
|
getLabeledSegments, |
|
|
|
insertSegments, |
|
|
|
insertSegments, |
|
|
|
removeSegments, |
|
|
|
removeSegments, |
|
|
|
} from '../services/segments'; |
|
|
|
} from '../services/segments'; |
|
|
|
import {runLearning} from '../services/analytics'; |
|
|
|
|
|
|
|
import {Anomaly, AnomalyId, getAnomalyIdByName, loadAnomalyById} from '../services/anomalyType'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { |
|
|
|
|
|
|
|
Anomaly, AnomalyId, getAnomalyIdByName, loadAnomalyById |
|
|
|
|
|
|
|
} from '../services/anomalyType'; |
|
|
|
|
|
|
|
|
|
|
|
async function sendSegments(req, res) { |
|
|
|
import { runLearning } from '../services/analytics'; |
|
|
|
try { |
|
|
|
|
|
|
|
let anomalyId: AnomalyId = req.query.anomaly_id; |
|
|
|
|
|
|
|
let anomaly:Anomaly = loadAnomalyById(anomalyId); |
|
|
|
|
|
|
|
if(anomaly === null) { |
|
|
|
|
|
|
|
anomalyId = getAnomalyIdByName(anomalyId); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let lastSegmentId = req.query.last_segment; |
|
|
|
|
|
|
|
let timeFrom = req.query.from; |
|
|
|
|
|
|
|
let timeTo = req.query.to; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let segments = getLabeledSegments(anomalyId); |
|
|
|
async function sendSegments(ctx: Router.IRouterContext) { |
|
|
|
|
|
|
|
|
|
|
|
// Id filtering
|
|
|
|
let anomalyId: AnomalyId = ctx.query.anomaly_id; |
|
|
|
if(lastSegmentId !== undefined) { |
|
|
|
let anomaly:Anomaly = loadAnomalyById(anomalyId); |
|
|
|
segments = segments.filter(el => el.id > lastSegmentId); |
|
|
|
if(anomaly === null) { |
|
|
|
} |
|
|
|
anomalyId = getAnomalyIdByName(anomalyId); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Time filtering
|
|
|
|
let lastSegmentId = ctx.query.last_segment; |
|
|
|
if(timeFrom !== undefined) { |
|
|
|
let timeFrom = ctx.query.from; |
|
|
|
segments = segments.filter(el => el.finish > timeFrom); |
|
|
|
let timeTo = ctx.query.to; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(timeTo !== undefined) { |
|
|
|
let segments = getLabeledSegments(anomalyId); |
|
|
|
segments = segments.filter(el => el.start < timeTo); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let payload = JSON.stringify({ |
|
|
|
// Id filtering
|
|
|
|
segments |
|
|
|
if(lastSegmentId !== undefined) { |
|
|
|
}); |
|
|
|
segments = segments.filter(el => el.id > lastSegmentId); |
|
|
|
res.status(200).send(payload); |
|
|
|
} |
|
|
|
} catch(e) { |
|
|
|
|
|
|
|
res.status(500).send({ |
|
|
|
// Time filtering
|
|
|
|
code: 500, |
|
|
|
if(timeFrom !== undefined) { |
|
|
|
message: 'Internal error' |
|
|
|
segments = segments.filter(el => el.finish > timeFrom); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(timeTo !== undefined) { |
|
|
|
|
|
|
|
segments = segments.filter(el => el.start < timeTo); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ctx.response.body = { segments } |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function updateSegments(req, res) { |
|
|
|
async function updateSegments(req, res) { |
|
|
@ -74,7 +71,7 @@ async function updateSegments(req, res) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export const router = express.Router(); |
|
|
|
export const router = new Router(); |
|
|
|
|
|
|
|
|
|
|
|
router.get('/', sendSegments); |
|
|
|
router.get('/', sendSegments); |
|
|
|
router.patch('/', updateSegments); |
|
|
|
router.patch('/', updateSegments); |
|
|
|