|
|
@ -1,11 +1,11 @@ |
|
|
|
import { Task } from '../models/task_model'; |
|
|
|
import { AnalyticsMessageMethod, AnalyticsMessage } from '../models/analytics_message_model' |
|
|
|
|
|
|
|
import { AnalyticsTask, AnalyticsTaskType } from '../models/analytics_task_model'; |
|
|
|
import * as Segments from '../models/segment_model'; |
|
|
|
import * as Segments from '../models/segment_model'; |
|
|
|
import * as AnalyticUnit from '../models/analytic_unit_model'; |
|
|
|
import * as AnalyticUnit from '../models/analytic_unit_model'; |
|
|
|
import { AnalyticsService, AnalyticsMessage } from '../services/analytics_service'; |
|
|
|
import { AnalyticsService } from '../services/analytics_service'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const taskMap = new Map<string, any>(); |
|
|
|
const taskMap = new Map<string, any>(); |
|
|
|
let nextTaskId = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let analyticsService: AnalyticsService = undefined; |
|
|
|
let analyticsService: AnalyticsService = undefined; |
|
|
|
|
|
|
|
|
|
|
@ -26,7 +26,7 @@ async function onMessage(message: AnalyticsMessage) { |
|
|
|
let responsePayload = null; |
|
|
|
let responsePayload = null; |
|
|
|
let resolvedMethod = false; |
|
|
|
let resolvedMethod = false; |
|
|
|
|
|
|
|
|
|
|
|
if(message.method === 'TASK_RESULT') { |
|
|
|
if(message.method === AnalyticsMessageMethod.TASK_RESULT) { |
|
|
|
onTaskResult(message.payload); |
|
|
|
onTaskResult(message.payload); |
|
|
|
resolvedMethod = true; |
|
|
|
resolvedMethod = true; |
|
|
|
} |
|
|
|
} |
|
|
@ -50,7 +50,7 @@ export function terminate() { |
|
|
|
analyticsService.close(); |
|
|
|
analyticsService.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function runTask(task: Task): Promise<any> { |
|
|
|
async function runTask(task: AnalyticsTask): Promise<any> { |
|
|
|
// let anomaly: AnalyticUnit.AnalyticUnit = await AnalyticUnit.findById(task.analyticUnitId);
|
|
|
|
// let anomaly: AnalyticUnit.AnalyticUnit = await AnalyticUnit.findById(task.analyticUnitId);
|
|
|
|
// task.metric = {
|
|
|
|
// task.metric = {
|
|
|
|
// datasource: anomaly.metric.datasource,
|
|
|
|
// datasource: anomaly.metric.datasource,
|
|
|
@ -66,26 +66,59 @@ async function runTask(task: Task): Promise<any> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function runLearning(id: AnalyticUnit.AnalyticUnitId) { |
|
|
|
export async function runLearning(id: AnalyticUnit.AnalyticUnitId) { |
|
|
|
let segments = Segments.findMany(id, { labeled: true }); |
|
|
|
let segments = await Segments.findMany(id, { labeled: true }); |
|
|
|
|
|
|
|
let segmentObjs = segments.map(s => s.toObject()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let analyticUnit = await AnalyticUnit.findById(id); |
|
|
|
|
|
|
|
if(analyticUnit.status === AnalyticUnit.AnalyticUnitStatus.LEARNING) { |
|
|
|
|
|
|
|
throw new Error('Can`t starn learning when it`s already started [' + id + ']'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.LEARNING); |
|
|
|
AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.LEARNING); |
|
|
|
let unit = await AnalyticUnit.findById(id); |
|
|
|
let previousLastPredictionTime = analyticUnit.lastPredictionTime; |
|
|
|
let pattern = unit.type; |
|
|
|
|
|
|
|
let task = { |
|
|
|
try { |
|
|
|
analyticUnitId: id, |
|
|
|
let pattern = analyticUnit.type; |
|
|
|
type: 'LEARN', |
|
|
|
let task = new AnalyticsTask( |
|
|
|
pattern, |
|
|
|
id, AnalyticsTaskType.LEARN, { pattern, segments: segmentObjs } |
|
|
|
segments: segments |
|
|
|
); |
|
|
|
}; |
|
|
|
let result = await runTask(task); |
|
|
|
|
|
|
|
let { lastPredictionTime, segments } = await processLearningResult(result); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await Promise.all([ |
|
|
|
|
|
|
|
Segments.insertSegments(segments), |
|
|
|
|
|
|
|
AnalyticUnit.setPredictionTime(id, lastPredictionTime) |
|
|
|
|
|
|
|
]); |
|
|
|
|
|
|
|
await AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.READY); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (err) { |
|
|
|
|
|
|
|
await AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.FAILED, err); |
|
|
|
|
|
|
|
await AnalyticUnit.setPredictionTime(id, previousLastPredictionTime); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let result = await runTask(task); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (result.status === 'SUCCESS') { |
|
|
|
async function processLearningResult(taskResult: any): Promise<{ |
|
|
|
AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.READY); |
|
|
|
lastPredictionTime: number, |
|
|
|
insertSegments(id, result.segments, false); |
|
|
|
segments: Segments.Segment[] |
|
|
|
AnalyticUnit.setPredictionTime(id, result.lastPredictionTime); |
|
|
|
}> { |
|
|
|
} else { |
|
|
|
if(taskResult.status !== 'SUCCESS') { |
|
|
|
AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.FAILED, result.error); |
|
|
|
return Promise.reject(taskResult.error); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if(taskResult.segments === undefined || !Array.isArray(taskResult.segments)) { |
|
|
|
|
|
|
|
throw new Error('Missing segments is result or it is corrupted: ' + taskResult); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if(taskResult.lastPredictionTime === undefined || isNaN(+taskResult.lastPredictionTime)) { |
|
|
|
|
|
|
|
throw new Error( |
|
|
|
|
|
|
|
'Missing lastPredictionTime is result or it is corrupted: ' + taskResult.lastPredictionTime |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
|
|
|
lastPredictionTime: +taskResult.lastPredictionTime, |
|
|
|
|
|
|
|
segments: taskResult.segments.map(Segments.Segment.fromObject) |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function runPredict(id: AnalyticUnit.AnalyticUnitId) { |
|
|
|
export async function runPredict(id: AnalyticUnit.AnalyticUnitId) { |
|
|
|