Browse Source

more refactoring

pull/1/head
Alexey Velikiy 6 years ago
parent
commit
1943785db9
  1. 7
      server/src/controllers/alerts_controller.ts
  2. 30
      server/src/controllers/analytics_controller.ts
  3. 4
      server/src/routes/segments_router.ts
  4. 4
      server/src/services/analytics_service.ts
  5. 1
      server/src/services/data_service.ts
  6. 1
      server/src/services/json_service.ts
  7. 1
      server/src/services/notification_service.ts

7
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 { runPredict } from './analytics_controller';
import { sendNotification } from '../services/notification_service';
import { getLabeledSegments } from './segments_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 { ANALYTIC_UNITS_PATH } from '../config';
import * as path from 'path'; import * as path from 'path';

30
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 { getTarget } from './metrics_controler';
import { getLabeledSegments, insertSegments, removeSegments } from './segments_controller' import { getLabeledSegments, insertSegments, removeSegments } from './segments_controller'
import * as AnalyticUnit from '../models/analytic_unit'
import { AnalyticsConnection } from '../services/analytics_service' import { AnalyticsConnection } from '../services/analytics_service'
@ -28,7 +22,7 @@ function onResponse(response: any) {
} }
async function runTask(task): Promise<any> { async function runTask(task): Promise<any> {
let anomaly: AnalyticUnit = findById(task.analyticUnitId); let anomaly: AnalyticUnit.AnalyticUnit = AnalyticUnit.findById(task.analyticUnitId);
task.metric = { task.metric = {
datasource: anomaly.metric.datasource, datasource: anomaly.metric.datasource,
targets: anomaly.metric.targets.map(getTarget) targets: anomaly.metric.targets.map(getTarget)
@ -42,11 +36,11 @@ async function runTask(task): Promise<any> {
}) })
} }
export async function runLearning(id: AnalyticUnitId) { export async function runLearning(id: AnalyticUnit.AnalyticUnitId) {
let segments = getLabeledSegments(id); let segments = getLabeledSegments(id);
setStatus(id, 'learning'); AnalyticUnit.setStatus(id, 'learning');
let anomaly: AnalyticUnit = findById(id); let unit = AnalyticUnit.findById(id);
let pattern = anomaly.type; let pattern = unit.type;
let task = { let task = {
analyticUnitId: id, analyticUnitId: id,
type: 'learn', type: 'learn',
@ -57,16 +51,16 @@ export async function runLearning(id: AnalyticUnitId) {
let result = await runTask(task); let result = await runTask(task);
if (result.status === 'success') { if (result.status === 'success') {
setStatus(id, 'ready'); AnalyticUnit.setStatus(id, 'ready');
insertSegments(id, result.segments, false); insertSegments(id, result.segments, false);
setPredictionTime(id, result.lastPredictionTime); AnalyticUnit.setPredictionTime(id, result.lastPredictionTime);
} else { } else {
setStatus(id, 'failed', result.error); AnalyticUnit.setStatus(id, 'failed', result.error);
} }
} }
export async function runPredict(id: AnalyticUnitId) { export async function runPredict(id: AnalyticUnit.AnalyticUnitId) {
let unit: AnalyticUnit = findById(id); let unit = AnalyticUnit.findById(id);
let pattern = unit.type; let pattern = unit.type;
let task = { let task = {
type: 'predict', type: 'predict',
@ -92,6 +86,6 @@ export async function runPredict(id: AnalyticUnitId) {
} }
insertSegments(id, result.segments, false); insertSegments(id, result.segments, false);
setPredictionTime(id, result.last_prediction_time); AnalyticUnit.setPredictionTime(id, result.lastPredictionTime);
return result.segments; return result.segments;
} }

4
server/src/routes/segments_router.ts

@ -1,12 +1,12 @@
import * as Router from 'koa-router'; import * as Router from 'koa-router';
import { AnalyticUnitId } from '../models/analytic_unit';
import { import {
getLabeledSegments, getLabeledSegments,
insertSegments, insertSegments,
removeSegments, removeSegments,
} from '../controllers/segments_controller'; } from '../controllers/segments_controller';
import { AnalyticUnitId } from '../models/analytic_unit';
import { runLearning } from '../controllers/analytics_controller'; import { runLearning } from '../controllers/analytics_controller';

4
server/src/services/analytics_service.ts

@ -1,8 +1,8 @@
import { ANALYTICS_PATH, ZEROMQ_CONNECTION_STRING } from '../config'
const zmq = require('zeromq'); const zmq = require('zeromq');
import { spawn } from 'child_process' import { spawn } from 'child_process'
import { ANALYTICS_PATH, ZEROMQ_CONNECTION_STRING } from '../config'
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';

1
server/src/services/data_service.ts

@ -1,4 +1,5 @@
import * as config from '../config' import * as config from '../config'
import * as fs from 'fs'; import * as fs from 'fs';

1
server/src/services/json_service.ts

@ -1,5 +1,6 @@
import * as fs from 'fs'; import * as fs from 'fs';
async function getJsonData(filename: string): Promise<Object> { async function getJsonData(filename: string): Promise<Object> {
var data = await new Promise<string>((resolve, reject) => { var data = await new Promise<string>((resolve, reject) => {
fs.readFile(filename, 'utf8', (err, data) => { fs.readFile(filename, 'utf8', (err, data) => {

1
server/src/services/notification_service.ts

@ -17,6 +17,7 @@ export async function sendNotification(predictorId, active) {
notification.status = 'OK'; notification.status = 'OK';
} }
// TODO: more to config
let endpoint = process.env.HASTIC_ALERT_ENDPOINT; let endpoint = process.env.HASTIC_ALERT_ENDPOINT;
if(endpoint === undefined) { if(endpoint === undefined) {
console.error(`Can't send alert, env HASTIC_ALERT_ENDPOINT is undefined`); console.error(`Can't send alert, env HASTIC_ALERT_ENDPOINT is undefined`);

Loading…
Cancel
Save