Alexey Velikiy
6 years ago
4 changed files with 109 additions and 95 deletions
@ -1,65 +1,72 @@ |
|||||||
import { runPredict } from './analytics_controller'; |
/** |
||||||
import { getLabeledSegments } from './segments_controller'; |
* Alarting is not supported yet |
||||||
import { AnalyticUnitId } from '../models/analytic_unit'; |
*/ |
||||||
import { sendNotification } from '../services/notification_service'; |
|
||||||
import { getJsonDataSync, writeJsonDataSync } from '../services/json_service'; |
throw new console.error("Not supported"); |
||||||
import { ANALYTIC_UNITS_PATH } from '../config'; |
|
||||||
|
|
||||||
import * as path from 'path'; |
// import { runPredict } from './analytics_controller';
|
||||||
import * as fs from 'fs'; |
// import { getLabeledSegments } from './segments_controller';
|
||||||
|
// import { AnalyticUnitId } from '../models/analytic_unit';
|
||||||
|
// import { sendNotification } from '../services/notification_service';
|
||||||
const ALERT_TIMEOUT = 60000; // ms
|
// import { getJsonDataSync, writeJsonDataSync } from '../services/json_service';
|
||||||
const ALERTS_DB_PATH = path.join(ANALYTIC_UNITS_PATH, `alerts_anomalies.json`); |
// import { ANALYTIC_UNITS_PATH } from '../config';
|
||||||
|
|
||||||
|
// import * as path from 'path';
|
||||||
export function getAlertsAnomalies(): AnalyticUnitId[] { |
// import * as fs from 'fs';
|
||||||
if(!fs.existsSync(ALERTS_DB_PATH)) { |
|
||||||
saveAlertsAnomalies([]); |
|
||||||
} |
// const ALERT_TIMEOUT = 60000; // ms
|
||||||
return getJsonDataSync(ALERTS_DB_PATH); |
// const ALERTS_DB_PATH = path.join(ANALYTIC_UNITS_PATH, `alerts_anomalies.json`);
|
||||||
} |
|
||||||
|
|
||||||
export function saveAlertsAnomalies(units: AnalyticUnitId[]) { |
// export function getAlertsAnomalies(): AnalyticUnitId[] {
|
||||||
return writeJsonDataSync(ALERTS_DB_PATH, units); |
// if(!fs.existsSync(ALERTS_DB_PATH)) {
|
||||||
} |
// saveAlertsAnomalies([]);
|
||||||
|
// }
|
||||||
function processAlerts(id: AnalyticUnitId) { |
// return getJsonDataSync(ALERTS_DB_PATH);
|
||||||
let segments = getLabeledSegments(id); |
// }
|
||||||
|
|
||||||
const currentTime = new Date().getTime(); |
// export function saveAlertsAnomalies(units: AnalyticUnitId[]) {
|
||||||
const activeAlert = activeAlerts.has(id); |
// return writeJsonDataSync(ALERTS_DB_PATH, units);
|
||||||
let newActiveAlert = false; |
// }
|
||||||
|
|
||||||
if(segments.length > 0) { |
// function processAlerts(id: AnalyticUnitId) {
|
||||||
let lastSegment = segments[segments.length - 1]; |
// let segments = getLabeledSegments(id);
|
||||||
if(lastSegment.finish >= currentTime - ALERT_TIMEOUT) { |
|
||||||
newActiveAlert = true; |
// const currentTime = new Date().getTime();
|
||||||
} |
// const activeAlert = activeAlerts.has(id);
|
||||||
} |
// let newActiveAlert = false;
|
||||||
|
|
||||||
if(!activeAlert && newActiveAlert) { |
// if(segments.length > 0) {
|
||||||
activeAlerts.add(id); |
// let lastSegment = segments[segments.length - 1];
|
||||||
sendNotification(id, true); |
// if(lastSegment.finish >= currentTime - ALERT_TIMEOUT) {
|
||||||
} else if(activeAlert && !newActiveAlert) { |
// newActiveAlert = true;
|
||||||
activeAlerts.delete(id); |
// }
|
||||||
sendNotification(id, false); |
// }
|
||||||
} |
|
||||||
} |
// if(!activeAlert && newActiveAlert) {
|
||||||
|
// activeAlerts.add(id);
|
||||||
async function alertsTick() { |
// sendNotification(id, true);
|
||||||
let alertsAnomalies = getAlertsAnomalies(); |
// } else if(activeAlert && !newActiveAlert) {
|
||||||
for (let predictorId of alertsAnomalies) { |
// activeAlerts.delete(id);
|
||||||
try { |
// sendNotification(id, false);
|
||||||
await runPredict(predictorId); |
// }
|
||||||
processAlerts(predictorId); |
// }
|
||||||
} catch (e) { |
|
||||||
console.error(e); |
// async function alertsTick() {
|
||||||
} |
// let alertsAnomalies = getAlertsAnomalies();
|
||||||
} |
// for (let predictorId of alertsAnomalies) {
|
||||||
setTimeout(alertsTick, 5000); |
// try {
|
||||||
} |
// await runPredict(predictorId);
|
||||||
|
// processAlerts(predictorId);
|
||||||
|
// } catch (e) {
|
||||||
const activeAlerts = new Set<string>(); |
// console.error(e);
|
||||||
setTimeout(alertsTick, 5000); |
// }
|
||||||
|
// }
|
||||||
|
// setTimeout(alertsTick, 5000);
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// const activeAlerts = new Set<string>();
|
||||||
|
// setTimeout(alertsTick, 5000);
|
||||||
|
@ -1,36 +1,43 @@ |
|||||||
import * as AnalyticUnit from '../models/analytic_unit'; |
// /**
|
||||||
import { getAlertsAnomalies, saveAlertsAnomalies } from '../controllers/alerts_controller'; |
// * Alarting is not supported yet
|
||||||
|
// */
|
||||||
|
|
||||||
import * as Router from 'koa-router'; |
// throw new console.error("Not supported");
|
||||||
|
|
||||||
|
|
||||||
function getAlert(ctx: Router.IRouterContext) { |
// import * as AnalyticUnit from '../models/analytic_unit';
|
||||||
let id: AnalyticUnit.AnalyticUnitId = ctx.request.query.id; |
// import { getAlertsAnomalies, saveAlertsAnomalies } from '../controllers/alerts_controller';
|
||||||
|
|
||||||
let alertsAnomalies = getAlertsAnomalies(); |
// import * as Router from 'koa-router';
|
||||||
let pos = alertsAnomalies.indexOf(id); |
|
||||||
|
|
||||||
let enabled: boolean = (pos !== -1); |
|
||||||
ctx.response.body = { enabled }; |
|
||||||
} |
|
||||||
|
|
||||||
function setAlertEnabled(ctx: Router.IRouterContext) { |
// function getAlert(ctx: Router.IRouterContext) {
|
||||||
let id: AnalyticUnit.AnalyticUnitId = ctx.request.body.id; |
// let id: AnalyticUnit.AnalyticUnitId = ctx.request.query.id;
|
||||||
let enabled: boolean = ctx.request.body.enabled; |
|
||||||
|
|
||||||
let alertsAnomalies = getAlertsAnomalies(); |
// let alertsAnomalies = getAlertsAnomalies();
|
||||||
let pos: number = alertsAnomalies.indexOf(id); |
// let pos = alertsAnomalies.indexOf(id);
|
||||||
if(enabled && pos == -1) { |
|
||||||
alertsAnomalies.push(id); |
|
||||||
saveAlertsAnomalies(alertsAnomalies); |
|
||||||
} else if(!enabled && pos > -1) { |
|
||||||
alertsAnomalies.splice(pos, 1); |
|
||||||
saveAlertsAnomalies(alertsAnomalies); |
|
||||||
} |
|
||||||
ctx.response.body = { status: 'OK' }; |
|
||||||
} |
|
||||||
|
|
||||||
export const router = new Router(); |
// let enabled: boolean = (pos !== -1);
|
||||||
|
// ctx.response.body = { enabled };
|
||||||
|
// }
|
||||||
|
|
||||||
router.get('/', getAlert); |
// function setAlertEnabled(ctx: Router.IRouterContext) {
|
||||||
router.post('/', setAlertEnabled); |
// let id: AnalyticUnit.AnalyticUnitId = ctx.request.body.id;
|
||||||
|
// let enabled: boolean = ctx.request.body.enabled;
|
||||||
|
|
||||||
|
// let alertsAnomalies = getAlertsAnomalies();
|
||||||
|
// let pos: number = alertsAnomalies.indexOf(id);
|
||||||
|
// if(enabled && pos == -1) {
|
||||||
|
// alertsAnomalies.push(id);
|
||||||
|
// saveAlertsAnomalies(alertsAnomalies);
|
||||||
|
// } else if(!enabled && pos > -1) {
|
||||||
|
// alertsAnomalies.splice(pos, 1);
|
||||||
|
// saveAlertsAnomalies(alertsAnomalies);
|
||||||
|
// }
|
||||||
|
// ctx.response.body = { status: 'OK' };
|
||||||
|
// }
|
||||||
|
|
||||||
|
// export const router = new Router();
|
||||||
|
|
||||||
|
// router.get('/', getAlert);
|
||||||
|
// router.post('/', setAlertEnabled);
|
||||||
|
Loading…
Reference in new issue