Browse Source

MORE RENAMING

pull/1/head
Alexey Velikiy 6 years ago
parent
commit
71db20f063
  1. 19
      server/src/index.ts
  2. 14
      server/src/models/analytic_unit.ts
  3. 13
      server/src/routes/alerts_router.ts
  4. 8
      server/src/routes/analytic_units_router.ts
  5. 2
      server/src/routes/segments_router.ts
  6. 34
      server/src/services/alerts_service.ts
  7. 46
      server/src/services/analytics_service.ts
  8. 5
      server/src/services/metrics.ts
  9. 6
      server/src/services/notification_service.ts
  10. 6
      server/src/services/segments.ts

19
server/src/index.ts

@ -1,17 +1,16 @@
import * as Koa from 'koa'; import { router as anomaliesRouter } from './routes/analytic_units_router';
import * as Router from 'koa-router'; import { router as segmentsRouter } from './routes/segments_router';
import * as bodyParser from 'koa-bodyparser'; import { router as alertsRouter } from './routes/alerts_router';
import { router as anomaliesRouter } from './routes/analytic_units';
import { router as segmentsRouter } from './routes/segments';
import { router as alertsRouter } from './routes/alerts';
import { checkDataFolders } from './services/data'; import * as Data from './services/data';
import { HASTIC_PORT } from './config'; import { HASTIC_PORT } from './config';
checkDataFolders(); import * as Koa from 'koa';
import * as Router from 'koa-router';
import * as bodyParser from 'koa-bodyparser';
Data.checkDataFolders();
var app = new Koa(); var app = new Koa();

14
server/src/models/analytic_unit.ts

@ -47,7 +47,7 @@ export function createItem(item: AnalyticUnit): AnalyticUnitId {
return newId; return newId;
} }
export function removeItem(id: AnalyticUnitId) { export function remove(id: AnalyticUnitId) {
let filename = path.join(ANALYTIC_UNITS_PATH, `${id}.json`); let filename = path.join(ANALYTIC_UNITS_PATH, `${id}.json`);
fs.unlinkSync(filename); fs.unlinkSync(filename);
} }
@ -58,7 +58,7 @@ export function save(id: AnalyticUnitId, unit: AnalyticUnit) {
} }
// TODO: make async // TODO: make async
export function loadById(id: AnalyticUnitId): AnalyticUnit { export function findById(id: AnalyticUnitId): AnalyticUnit {
let filename = path.join(ANALYTIC_UNITS_PATH, `${id}.json`); let filename = path.join(ANALYTIC_UNITS_PATH, `${id}.json`);
if(!fs.existsSync(filename)) { if(!fs.existsSync(filename)) {
throw new Error(`Can't find Analytic Unit with id ${id}`); throw new Error(`Can't find Analytic Unit with id ${id}`);
@ -66,12 +66,8 @@ export function loadById(id: AnalyticUnitId): AnalyticUnit {
return getJsonDataSync(filename); return getJsonDataSync(filename);
} }
export function getAnomalyTypeInfo(name) { export function setStatus(predictorId: AnalyticUnitId, status: string, error?: string) {
return getJsonDataSync(path.join(ANALYTIC_UNITS_PATH, `${name}.json`)); let info = findById(predictorId);
}
export function setAnomalyStatus(predictorId: AnalyticUnitId, status: string, error?: string) {
let info = loadById(predictorId);
info.status = status; info.status = status;
if(error !== undefined) { if(error !== undefined) {
info.error = error; info.error = error;
@ -82,7 +78,7 @@ export function setAnomalyStatus(predictorId: AnalyticUnitId, status: string, er
} }
export function setPredictionTime(id: AnalyticUnitId, time: number) { export function setPredictionTime(id: AnalyticUnitId, time: number) {
let info = loadById(id); let info = findById(id);
info.lastPredictionTime = time; info.lastPredictionTime = time;
save(id, info); save(id, info);
} }

13
server/src/routes/alerts.ts → server/src/routes/alerts_router.ts

@ -1,25 +1,23 @@
import * as AnalyticUnit from '../models/analytic_unit'; import * as AnalyticUnit from '../models/analytic_unit';
import { getAlertsAnomalies, saveAlertsAnomalies } from '../services/alerts'; import { getAlertsAnomalies, saveAlertsAnomalies } from '../services/alerts_service';
import * as Router from 'koa-router'; import * as Router from 'koa-router';
function getAlert(ctx: Router.IRouterContext) { function getAlert(ctx: Router.IRouterContext) {
let predictorId: AnalyticUnit.AnalyticUnitId = ctx.request.query.predictor_id.toLowerCase(); let id: AnalyticUnit.AnalyticUnitId = ctx.request.query.id;
let alertsAnomalies = getAlertsAnomalies(); let alertsAnomalies = getAlertsAnomalies();
let pos = alertsAnomalies.indexOf(predictorId); let pos = alertsAnomalies.indexOf(id);
let enable: boolean = (pos !== -1); let enabled: boolean = (pos !== -1);
ctx.response.body = { enable }; ctx.response.body = { enabled };
} }
function setAlertEnabled(ctx: Router.IRouterContext) { function setAlertEnabled(ctx: Router.IRouterContext) {
let id: AnalyticUnit.AnalyticUnitId = ctx.request.body.id; let id: AnalyticUnit.AnalyticUnitId = ctx.request.body.id;
let enabled: boolean = ctx.request.body.enabled; let enabled: boolean = ctx.request.body.enabled;
let unit = AnalyticUnit.loadById(id)
let alertsAnomalies = getAlertsAnomalies(); let alertsAnomalies = getAlertsAnomalies();
let pos: number = alertsAnomalies.indexOf(id); let pos: number = alertsAnomalies.indexOf(id);
if(enabled && pos == -1) { if(enabled && pos == -1) {
@ -30,7 +28,6 @@ function setAlertEnabled(ctx: Router.IRouterContext) {
saveAlertsAnomalies(alertsAnomalies); saveAlertsAnomalies(alertsAnomalies);
} }
ctx.response.body = { status: 'OK' }; ctx.response.body = { status: 'OK' };
} }
export const router = new Router(); export const router = new Router();

8
server/src/routes/analytic_units.ts → server/src/routes/analytic_units_router.ts

@ -2,7 +2,7 @@ import * as Router from 'koa-router';
import * as AnalyticUnit from '../models/analytic_unit'; import * as AnalyticUnit from '../models/analytic_unit';
import { runLearning } from '../services/analytics' import { runLearning } from '../services/analytics_service'
import { saveTargets } from '../services/metrics'; import { saveTargets } from '../services/metrics';
async function sendStatus(ctx: Router.IRouterContext) { async function sendStatus(ctx: Router.IRouterContext) {
@ -13,7 +13,7 @@ async function sendStatus(ctx: Router.IRouterContext) {
if(id === undefined) { if(id === undefined) {
throw new Error('Id is undefined'); throw new Error('Id is undefined');
} }
let unit = AnalyticUnit.loadById(id); let unit = AnalyticUnit.findById(id);
if(unit.status === undefined) { if(unit.status === undefined) {
throw new Error('No status for ' + name); throw new Error('No status for ' + name);
@ -36,7 +36,7 @@ async function findItem(ctx: Router.IRouterContext) {
throw new Error('No id param in query'); throw new Error('No id param in query');
} }
let unit: AnalyticUnit.AnalyticUnit = AnalyticUnit.loadById(id); let unit: AnalyticUnit.AnalyticUnit = AnalyticUnit.findById(id);
ctx.response.body = { ctx.response.body = {
name: unit.name, name: unit.name,
@ -118,7 +118,7 @@ function deleteItem(ctx: Router.IRouterContext) {
let id = ctx.request.query.id; let id = ctx.request.query.id;
if(id !== undefined) { if(id !== undefined) {
AnalyticUnit.removeItem(id); AnalyticUnit.remove(id);
} }
ctx.response.body = { ctx.response.body = {

2
server/src/routes/segments.ts → server/src/routes/segments_router.ts

@ -7,7 +7,7 @@ import {
} from '../services/segments'; } from '../services/segments';
import { AnalyticUnitId } from '../models/analytic_unit'; import { AnalyticUnitId } from '../models/analytic_unit';
import { runLearning } from '../services/analytics'; import { runLearning } from '../services/analytics_service';
async function sendSegments(ctx: Router.IRouterContext) { async function sendSegments(ctx: Router.IRouterContext) {

34
server/src/services/alerts.ts → server/src/services/alerts_service.ts

@ -1,7 +1,7 @@
import { getJsonDataSync, writeJsonDataSync } from './json'; import { getJsonDataSync, writeJsonDataSync } from './json';
import { AnalyticUnitId } from '../models/analytic_unit'; import { AnalyticUnitId } from '../models/analytic_unit';
import { runPredict } from './analytics'; import { runPredict } from './analytics_service';
import { sendNotification } from './notification'; import { sendNotification } from './notification_service';
import { getLabeledSegments } from './segments'; import { getLabeledSegments } from './segments';
import { ANALYTIC_UNITS_PATH } from '../config'; import { ANALYTIC_UNITS_PATH } from '../config';
@ -10,40 +10,41 @@ import * as path from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
const ALERT_TIMEOUT = 60000; // ms
const ALERTS_DB_PATH = path.join(ANALYTIC_UNITS_PATH, `alerts_anomalies.json`); const ALERTS_DB_PATH = path.join(ANALYTIC_UNITS_PATH, `alerts_anomalies.json`);
function getAlertsAnomalies(): AnalyticUnitId[] {
export function getAlertsAnomalies(): AnalyticUnitId[] {
if(!fs.existsSync(ALERTS_DB_PATH)) { if(!fs.existsSync(ALERTS_DB_PATH)) {
saveAlertsAnomalies([]); saveAlertsAnomalies([]);
} }
return getJsonDataSync(ALERTS_DB_PATH); return getJsonDataSync(ALERTS_DB_PATH);
} }
function saveAlertsAnomalies(anomalies: AnalyticUnitId[]) { export function saveAlertsAnomalies(units: AnalyticUnitId[]) {
return writeJsonDataSync(ALERTS_DB_PATH, anomalies); return writeJsonDataSync(ALERTS_DB_PATH, units);
} }
function processAlerts(predictorId) { function processAlerts(id: AnalyticUnitId) {
let segments = getLabeledSegments(predictorId); let segments = getLabeledSegments(id);
const currentTime = new Date().getTime(); const currentTime = new Date().getTime();
const activeAlert = activeAlerts.has(predictorId); const activeAlert = activeAlerts.has(id);
let newActiveAlert = false; let newActiveAlert = false;
if(segments.length > 0) { if(segments.length > 0) {
let lastSegment = segments[segments.length - 1]; let lastSegment = segments[segments.length - 1];
if(lastSegment.finish >= currentTime - alertTimeout) { if(lastSegment.finish >= currentTime - ALERT_TIMEOUT) {
newActiveAlert = true; newActiveAlert = true;
} }
} }
if(!activeAlert && newActiveAlert) { if(!activeAlert && newActiveAlert) {
activeAlerts.add(predictorId); activeAlerts.add(id);
sendNotification(predictorId, true); sendNotification(id, true);
} else if(activeAlert && !newActiveAlert) { } else if(activeAlert && !newActiveAlert) {
activeAlerts.delete(predictorId); activeAlerts.delete(id);
sendNotification(predictorId, false); sendNotification(id, false);
} }
} }
@ -60,9 +61,6 @@ async function alertsTick() {
setTimeout(alertsTick, 5000); setTimeout(alertsTick, 5000);
} }
const alertTimeout = 60000; // ms
const activeAlerts = new Set<string>(); const activeAlerts = new Set<string>();
setTimeout(alertsTick, 5000); setTimeout(alertsTick, 5000);
export { getAlertsAnomalies, saveAlertsAnomalies }

46
server/src/services/analytics.ts → server/src/services/analytics_service.ts

@ -1,9 +1,9 @@
import { import {
AnalyticUnit, AnalyticUnit,
AnalyticUnitId, getAnomalyTypeInfo, AnalyticUnitId,
loadById, findById,
setPredictionTime, setPredictionTime,
setAnomalyStatus setStatus
} from '../models/analytic_unit' } from '../models/analytic_unit'
import { getTarget } from './metrics'; import { getTarget } from './metrics';
import { getLabeledSegments, insertSegments, removeSegments } from './segments' import { getLabeledSegments, insertSegments, removeSegments } from './segments'
@ -28,10 +28,10 @@ function onResponse(response: any) {
} }
async function runTask(task): Promise<any> { async function runTask(task): Promise<any> {
let anomaly: AnalyticUnit = loadById(task.predictor_id); let anomaly: AnalyticUnit = findById(task.analyticUnitId);
task.metric = { task.metric = {
datasource: anomaly.metric.datasource, datasource: anomaly.metric.datasource,
targets: anomaly.metric.targets.map(t => getTarget(t)) targets: anomaly.metric.targets.map(getTarget)
}; };
task.__task_id = nextTaskId++; task.__task_id = nextTaskId++;
@ -42,14 +42,14 @@ async function runTask(task): Promise<any> {
}) })
} }
export async function runLearning(predictorId:AnalyticUnitId) { export async function runLearning(id: AnalyticUnitId) {
let segments = getLabeledSegments(predictorId); let segments = getLabeledSegments(id);
setAnomalyStatus(predictorId, 'learning'); setStatus(id, 'learning');
let anomaly:AnalyticUnit = loadById(predictorId); let anomaly: AnalyticUnit = findById(id);
let pattern = anomaly.type; let pattern = anomaly.type;
let task = { let task = {
analyticUnitId: id,
type: 'learn', type: 'learn',
predictor_id: predictorId,
pattern, pattern,
segments: segments segments: segments
}; };
@ -57,22 +57,22 @@ export async function runLearning(predictorId:AnalyticUnitId) {
let result = await runTask(task); let result = await runTask(task);
if (result.status === 'success') { if (result.status === 'success') {
setAnomalyStatus(predictorId, 'ready'); setStatus(id, 'ready');
insertSegments(predictorId, result.segments, false); insertSegments(id, result.segments, false);
setPredictionTime(predictorId, result.last_prediction_time); setPredictionTime(id, result.lastPredictionTime);
} else { } else {
setAnomalyStatus(predictorId, 'failed', result.error); setStatus(id, 'failed', result.error);
} }
} }
export async function runPredict(predictorId:AnalyticUnitId) { export async function runPredict(id: AnalyticUnitId) {
let anomaly:AnalyticUnit = loadById(predictorId); let unit: AnalyticUnit = findById(id);
let pattern = anomaly.type; let pattern = unit.type;
let task = { let task = {
type: 'predict', type: 'predict',
predictor_id: predictorId, predictor_id: id,
pattern, pattern,
last_prediction_time: anomaly.lastPredictionTime lastPredictionTime: unit.lastPredictionTime
}; };
let result = await runTask(task); let result = await runTask(task);
@ -80,18 +80,18 @@ export async function runPredict(predictorId:AnalyticUnitId) {
return []; return [];
} }
// Merging segments // Merging segments
let segments = getLabeledSegments(predictorId); let segments = getLabeledSegments(id);
if(segments.length > 0 && result.segments.length > 0) { if(segments.length > 0 && result.segments.length > 0) {
let lastOldSegment = segments[segments.length - 1]; let lastOldSegment = segments[segments.length - 1];
let firstNewSegment = result.segments[0]; let firstNewSegment = result.segments[0];
if(firstNewSegment.start <= lastOldSegment.finish) { if(firstNewSegment.start <= lastOldSegment.finish) {
result.segments[0].start = lastOldSegment.start; result.segments[0].start = lastOldSegment.start;
removeSegments(predictorId, [lastOldSegment.id]); removeSegments(id, [lastOldSegment.id]);
} }
} }
insertSegments(predictorId, result.segments, false); insertSegments(id, result.segments, false);
setPredictionTime(predictorId, result.last_prediction_time); setPredictionTime(id, result.last_prediction_time);
return result.segments; return result.segments;
} }

5
server/src/services/metrics.ts

@ -1,8 +1,11 @@
import * as path from 'path';
import { getJsonDataSync, writeJsonDataSync } from './json'; import { getJsonDataSync, writeJsonDataSync } from './json';
import { METRICS_PATH } from '../config'; import { METRICS_PATH } from '../config';
import * as crypto from 'crypto'; import * as crypto from 'crypto';
import * as path from 'path';
function saveTargets(targets) { function saveTargets(targets) {
let metrics = []; let metrics = [];
for (let target of targets) { for (let target of targets) {

6
server/src/services/notification.ts → server/src/services/notification_service.ts

@ -1,8 +1,10 @@
import { findById } from '../models/analytic_unit';
import axios from 'axios'; import axios from 'axios';
import { loadById } from '../models/analytic_unit';
export async function sendNotification(predictorId, active) { export async function sendNotification(predictorId, active) {
let anomalyName = loadById(predictorId).name; let anomalyName = findById(predictorId).name;
console.log('Notification ' + anomalyName); console.log('Notification ' + anomalyName);
let notification = { let notification = {

6
server/src/services/segments.ts

@ -1,5 +1,5 @@
import { getJsonDataSync, writeJsonDataSync } from './json'; import { getJsonDataSync, writeJsonDataSync } from './json';
import { AnalyticUnitId, loadById, save } from '../models/analytic_unit'; import { AnalyticUnitId, findById, save } from '../models/analytic_unit';
import { SEGMENTS_PATH } from '../config'; import { SEGMENTS_PATH } from '../config';
import * as _ from 'lodash'; import * as _ from 'lodash';
@ -48,9 +48,9 @@ export function saveSegments(id: AnalyticUnitId, segments) {
} }
} }
export function insertSegments(id: AnalyticUnitId, addedSegments, labeled:boolean) { export function insertSegments(id: AnalyticUnitId, addedSegments, labeled: boolean) {
// Set status // Set status
let info = loadById(id); let info = findById(id);
let segments = getLabeledSegments(id); let segments = getLabeledSegments(id);
let nextId = info.nextId; let nextId = info.nextId;

Loading…
Cancel
Save