From e412642ac2c2466322a27e4425566ad930cfe260 Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Thu, 2 Aug 2018 22:52:41 +0300 Subject: [PATCH] rm alarting --- server/src/config.ts | 2 +- server/src/controllers/alerts_controller.ts | 137 ++++++++++---------- server/src/index.ts | 4 +- server/src/routes/alerts_router.ts | 61 +++++---- 4 files changed, 109 insertions(+), 95 deletions(-) diff --git a/server/src/config.ts b/server/src/config.ts index 66930e4..b6b3c46 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -16,7 +16,7 @@ export const SEGMENTS_DATABASE_PATH = path.join(DATA_PATH, 'segments.db'); export const FILES_DATABASE_PATH = path.join(DATA_PATH, 'files.db'); export const DATASETS_PATH = path.join(DATA_PATH, 'datasets'); -export const ANALYTIC_UNITS_PATH = path.join(DATA_PATH, 'analytic_units'); +//export const ANALYTIC_UNITS_PATH = path.join(DATA_PATH, 'analytic_units'); export const MODELS_PATH = path.join(DATA_PATH, 'models'); export const METRICS_PATH = path.join(DATA_PATH, 'metrics'); export const SEGMENTS_PATH = path.join(DATA_PATH, 'segments'); diff --git a/server/src/controllers/alerts_controller.ts b/server/src/controllers/alerts_controller.ts index baf6ed5..0989ae2 100644 --- a/server/src/controllers/alerts_controller.ts +++ b/server/src/controllers/alerts_controller.ts @@ -1,65 +1,72 @@ -import { runPredict } from './analytics_controller'; -import { getLabeledSegments } from './segments_controller'; -import { AnalyticUnitId } from '../models/analytic_unit'; -import { sendNotification } from '../services/notification_service'; -import { getJsonDataSync, writeJsonDataSync } from '../services/json_service'; -import { ANALYTIC_UNITS_PATH } from '../config'; - -import * as path from 'path'; -import * as fs from 'fs'; - - -const ALERT_TIMEOUT = 60000; // ms -const ALERTS_DB_PATH = path.join(ANALYTIC_UNITS_PATH, `alerts_anomalies.json`); - - -export function getAlertsAnomalies(): AnalyticUnitId[] { - if(!fs.existsSync(ALERTS_DB_PATH)) { - saveAlertsAnomalies([]); - } - return getJsonDataSync(ALERTS_DB_PATH); -} - -export function saveAlertsAnomalies(units: AnalyticUnitId[]) { - return writeJsonDataSync(ALERTS_DB_PATH, units); -} - -function processAlerts(id: AnalyticUnitId) { - let segments = getLabeledSegments(id); - - const currentTime = new Date().getTime(); - const activeAlert = activeAlerts.has(id); - let newActiveAlert = false; - - if(segments.length > 0) { - let lastSegment = segments[segments.length - 1]; - if(lastSegment.finish >= currentTime - ALERT_TIMEOUT) { - newActiveAlert = true; - } - } - - if(!activeAlert && newActiveAlert) { - activeAlerts.add(id); - sendNotification(id, true); - } else if(activeAlert && !newActiveAlert) { - activeAlerts.delete(id); - sendNotification(id, false); - } -} - -async function alertsTick() { - let alertsAnomalies = getAlertsAnomalies(); - for (let predictorId of alertsAnomalies) { - try { - await runPredict(predictorId); - processAlerts(predictorId); - } catch (e) { - console.error(e); - } - } - setTimeout(alertsTick, 5000); -} - - -const activeAlerts = new Set(); -setTimeout(alertsTick, 5000); +/** + * Alarting is not supported yet + */ + +throw new console.error("Not supported"); + + +// import { runPredict } from './analytics_controller'; +// import { getLabeledSegments } from './segments_controller'; +// import { AnalyticUnitId } from '../models/analytic_unit'; +// import { sendNotification } from '../services/notification_service'; +// import { getJsonDataSync, writeJsonDataSync } from '../services/json_service'; +// import { ANALYTIC_UNITS_PATH } from '../config'; + +// import * as path from 'path'; +// import * as fs from 'fs'; + + +// const ALERT_TIMEOUT = 60000; // ms +// const ALERTS_DB_PATH = path.join(ANALYTIC_UNITS_PATH, `alerts_anomalies.json`); + + +// export function getAlertsAnomalies(): AnalyticUnitId[] { +// if(!fs.existsSync(ALERTS_DB_PATH)) { +// saveAlertsAnomalies([]); +// } +// return getJsonDataSync(ALERTS_DB_PATH); +// } + +// export function saveAlertsAnomalies(units: AnalyticUnitId[]) { +// return writeJsonDataSync(ALERTS_DB_PATH, units); +// } + +// function processAlerts(id: AnalyticUnitId) { +// let segments = getLabeledSegments(id); + +// const currentTime = new Date().getTime(); +// const activeAlert = activeAlerts.has(id); +// let newActiveAlert = false; + +// if(segments.length > 0) { +// let lastSegment = segments[segments.length - 1]; +// if(lastSegment.finish >= currentTime - ALERT_TIMEOUT) { +// newActiveAlert = true; +// } +// } + +// if(!activeAlert && newActiveAlert) { +// activeAlerts.add(id); +// sendNotification(id, true); +// } else if(activeAlert && !newActiveAlert) { +// activeAlerts.delete(id); +// sendNotification(id, false); +// } +// } + +// async function alertsTick() { +// let alertsAnomalies = getAlertsAnomalies(); +// for (let predictorId of alertsAnomalies) { +// try { +// await runPredict(predictorId); +// processAlerts(predictorId); +// } catch (e) { +// console.error(e); +// } +// } +// setTimeout(alertsTick, 5000); +// } + + +// const activeAlerts = new Set(); +// setTimeout(alertsTick, 5000); diff --git a/server/src/index.ts b/server/src/index.ts index c77f47d..31ca62d 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -1,6 +1,6 @@ import { router as anomaliesRouter } from './routes/analytic_units_router'; import { router as segmentsRouter } from './routes/segments_router'; -import { router as alertsRouter } from './routes/alerts_router'; +//import { router as alertsRouter } from './routes/alerts_router'; import * as AnalyticsController from './controllers/analytics_controller'; @@ -33,7 +33,7 @@ app.use(async function(ctx, next) { var rootRouter = new Router(); rootRouter.use('/analyticUnits', anomaliesRouter.routes(), anomaliesRouter.allowedMethods()); rootRouter.use('/segments', segmentsRouter.routes(), segmentsRouter.allowedMethods()); -rootRouter.use('/alerts', alertsRouter.routes(), alertsRouter.allowedMethods()); +//rootRouter.use('/alerts', alertsRouter.routes(), alertsRouter.allowedMethods()); rootRouter.get('/', async (ctx) => { ctx.response.body = { server: 'OK', diff --git a/server/src/routes/alerts_router.ts b/server/src/routes/alerts_router.ts index ca4d27a..68211e6 100644 --- a/server/src/routes/alerts_router.ts +++ b/server/src/routes/alerts_router.ts @@ -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) { - let id: AnalyticUnit.AnalyticUnitId = ctx.request.query.id; +// import * as AnalyticUnit from '../models/analytic_unit'; +// import { getAlertsAnomalies, saveAlertsAnomalies } from '../controllers/alerts_controller'; - let alertsAnomalies = getAlertsAnomalies(); - let pos = alertsAnomalies.indexOf(id); +// import * as Router from 'koa-router'; - let enabled: boolean = (pos !== -1); - ctx.response.body = { enabled }; -} -function setAlertEnabled(ctx: Router.IRouterContext) { - let id: AnalyticUnit.AnalyticUnitId = ctx.request.body.id; - let enabled: boolean = ctx.request.body.enabled; +// function getAlert(ctx: Router.IRouterContext) { +// let id: AnalyticUnit.AnalyticUnitId = ctx.request.query.id; - 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' }; -} +// let alertsAnomalies = getAlertsAnomalies(); +// let pos = alertsAnomalies.indexOf(id); -export const router = new Router(); +// let enabled: boolean = (pos !== -1); +// ctx.response.body = { enabled }; +// } -router.get('/', getAlert); -router.post('/', setAlertEnabled); +// function setAlertEnabled(ctx: Router.IRouterContext) { +// 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);