From 1943785db99ba0ae145c2bef8c859d9696e16af2 Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Sat, 7 Jul 2018 19:40:34 +0300 Subject: [PATCH] more refactoring --- server/src/controllers/alerts_controller.ts | 7 ++--- .../src/controllers/analytics_controller.ts | 30 ++++++++----------- server/src/routes/segments_router.ts | 4 +-- server/src/services/analytics_service.ts | 4 +-- server/src/services/data_service.ts | 1 + server/src/services/json_service.ts | 1 + server/src/services/notification_service.ts | 1 + 7 files changed, 22 insertions(+), 26 deletions(-) diff --git a/server/src/controllers/alerts_controller.ts b/server/src/controllers/alerts_controller.ts index e3ce0cd..baf6ed5 100644 --- a/server/src/controllers/alerts_controller.ts +++ b/server/src/controllers/alerts_controller.ts @@ -1,9 +1,8 @@ -import { getJsonDataSync, writeJsonDataSync } from '../services/json_service'; -import { AnalyticUnitId } from '../models/analytic_unit'; import { runPredict } from './analytics_controller'; -import { sendNotification } from '../services/notification_service'; 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'; diff --git a/server/src/controllers/analytics_controller.ts b/server/src/controllers/analytics_controller.ts index 56a72ed..4e6bcaf 100644 --- a/server/src/controllers/analytics_controller.ts +++ b/server/src/controllers/analytics_controller.ts @@ -1,12 +1,6 @@ -import { - AnalyticUnit, - AnalyticUnitId, - findById, - setPredictionTime, - setStatus -} from '../models/analytic_unit' import { getTarget } from './metrics_controler'; import { getLabeledSegments, insertSegments, removeSegments } from './segments_controller' +import * as AnalyticUnit from '../models/analytic_unit' import { AnalyticsConnection } from '../services/analytics_service' @@ -28,7 +22,7 @@ function onResponse(response: any) { } async function runTask(task): Promise { - let anomaly: AnalyticUnit = findById(task.analyticUnitId); + let anomaly: AnalyticUnit.AnalyticUnit = AnalyticUnit.findById(task.analyticUnitId); task.metric = { datasource: anomaly.metric.datasource, targets: anomaly.metric.targets.map(getTarget) @@ -42,11 +36,11 @@ async function runTask(task): Promise { }) } -export async function runLearning(id: AnalyticUnitId) { +export async function runLearning(id: AnalyticUnit.AnalyticUnitId) { let segments = getLabeledSegments(id); - setStatus(id, 'learning'); - let anomaly: AnalyticUnit = findById(id); - let pattern = anomaly.type; + AnalyticUnit.setStatus(id, 'learning'); + let unit = AnalyticUnit.findById(id); + let pattern = unit.type; let task = { analyticUnitId: id, type: 'learn', @@ -57,16 +51,16 @@ export async function runLearning(id: AnalyticUnitId) { let result = await runTask(task); if (result.status === 'success') { - setStatus(id, 'ready'); + AnalyticUnit.setStatus(id, 'ready'); insertSegments(id, result.segments, false); - setPredictionTime(id, result.lastPredictionTime); + AnalyticUnit.setPredictionTime(id, result.lastPredictionTime); } else { - setStatus(id, 'failed', result.error); + AnalyticUnit.setStatus(id, 'failed', result.error); } } -export async function runPredict(id: AnalyticUnitId) { - let unit: AnalyticUnit = findById(id); +export async function runPredict(id: AnalyticUnit.AnalyticUnitId) { + let unit = AnalyticUnit.findById(id); let pattern = unit.type; let task = { type: 'predict', @@ -92,6 +86,6 @@ export async function runPredict(id: AnalyticUnitId) { } insertSegments(id, result.segments, false); - setPredictionTime(id, result.last_prediction_time); + AnalyticUnit.setPredictionTime(id, result.lastPredictionTime); return result.segments; } diff --git a/server/src/routes/segments_router.ts b/server/src/routes/segments_router.ts index 16308db..3c88864 100644 --- a/server/src/routes/segments_router.ts +++ b/server/src/routes/segments_router.ts @@ -1,12 +1,12 @@ import * as Router from 'koa-router'; +import { AnalyticUnitId } from '../models/analytic_unit'; + import { getLabeledSegments, insertSegments, removeSegments, } from '../controllers/segments_controller'; - -import { AnalyticUnitId } from '../models/analytic_unit'; import { runLearning } from '../controllers/analytics_controller'; diff --git a/server/src/services/analytics_service.ts b/server/src/services/analytics_service.ts index da28053..15fc120 100644 --- a/server/src/services/analytics_service.ts +++ b/server/src/services/analytics_service.ts @@ -1,8 +1,8 @@ +import { ANALYTICS_PATH, ZEROMQ_CONNECTION_STRING } from '../config' + const zmq = require('zeromq'); import { spawn } from 'child_process' -import { ANALYTICS_PATH, ZEROMQ_CONNECTION_STRING } from '../config' - import * as fs from 'fs'; import * as path from 'path'; diff --git a/server/src/services/data_service.ts b/server/src/services/data_service.ts index 91f1d97..af23925 100644 --- a/server/src/services/data_service.ts +++ b/server/src/services/data_service.ts @@ -1,4 +1,5 @@ import * as config from '../config' + import * as fs from 'fs'; diff --git a/server/src/services/json_service.ts b/server/src/services/json_service.ts index 0390661..f51d1a2 100644 --- a/server/src/services/json_service.ts +++ b/server/src/services/json_service.ts @@ -1,5 +1,6 @@ import * as fs from 'fs'; + async function getJsonData(filename: string): Promise { var data = await new Promise((resolve, reject) => { fs.readFile(filename, 'utf8', (err, data) => { diff --git a/server/src/services/notification_service.ts b/server/src/services/notification_service.ts index 0deaf8b..97bba99 100644 --- a/server/src/services/notification_service.ts +++ b/server/src/services/notification_service.ts @@ -17,6 +17,7 @@ export async function sendNotification(predictorId, active) { notification.status = 'OK'; } + // TODO: more to config let endpoint = process.env.HASTIC_ALERT_ENDPOINT; if(endpoint === undefined) { console.error(`Can't send alert, env HASTIC_ALERT_ENDPOINT is undefined`);