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 { 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';

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 { 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<any> {
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<any> {
})
}
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;
}

4
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';

4
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';

1
server/src/services/data_service.ts

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

1
server/src/services/json_service.ts

@ -1,5 +1,6 @@
import * as fs from 'fs';
async function getJsonData(filename: string): Promise<Object> {
var data = await new Promise<string>((resolve, reject) => {
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';
}
// 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`);

Loading…
Cancel
Save