|
|
|
@ -5,19 +5,27 @@ import * as AnalyticUnit from '../models/analytic_unit_model';
|
|
|
|
|
import { AnalyticsService } from '../services/analytics_service'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const taskResolvers = new Map<AnalyticsTaskId, any>(); |
|
|
|
|
type TaskResult = any; |
|
|
|
|
export type TaskResolver = (taskResult: TaskResult) => void; |
|
|
|
|
|
|
|
|
|
const taskResolvers = new Map<AnalyticsTaskId, TaskResolver>(); |
|
|
|
|
|
|
|
|
|
let analyticsService: AnalyticsService = undefined; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function onTaskResult(taskResult: any) { |
|
|
|
|
let taskId = taskResult._id; |
|
|
|
|
function onTaskResult(taskResult: TaskResult) { |
|
|
|
|
let id = taskResult._id; |
|
|
|
|
if(id === undefined) { |
|
|
|
|
throw new Error('id of task is undefined'); |
|
|
|
|
} |
|
|
|
|
let status = taskResult.status; |
|
|
|
|
if(status === 'SUCCESS' || status === 'FAILED') { |
|
|
|
|
if(taskId in taskResolvers) { |
|
|
|
|
let resolver: any = taskResolvers.get(taskId); |
|
|
|
|
if(id in taskResolvers) { |
|
|
|
|
let resolver: any = taskResolvers.get(id); |
|
|
|
|
resolver(taskResult); |
|
|
|
|
taskResolvers.delete(taskId); |
|
|
|
|
taskResolvers.delete(id); |
|
|
|
|
} else { |
|
|
|
|
throw new Error(`TaskResut [${id}] has no resolver`); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -50,23 +58,23 @@ export function terminate() {
|
|
|
|
|
analyticsService.close(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async function runTask(task: AnalyticsTask): Promise<any> { |
|
|
|
|
// let anomaly: AnalyticUnit.AnalyticUnit = await AnalyticUnit.findById(task.analyticUnitId);
|
|
|
|
|
async function runTask(task: AnalyticsTask): Promise<TaskResult> { |
|
|
|
|
// task.metric = {
|
|
|
|
|
// datasource: anomaly.metric.datasource,
|
|
|
|
|
// targets: anomaly.metric.targets.map(getTarget)
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// task._taskId = nextTaskId++;
|
|
|
|
|
// await ;
|
|
|
|
|
|
|
|
|
|
return new Promise<void>(resolve => { |
|
|
|
|
taskResolvers[task.id] = resolve; |
|
|
|
|
}) |
|
|
|
|
.then(() => analyticsService.sendTask(task)); |
|
|
|
|
return new Promise<TaskResult>((resolver: TaskResolver) => { |
|
|
|
|
taskResolvers.set(task.id, resolver); // it will be resolved in onTaskResult()
|
|
|
|
|
analyticsService.sendTask(task); // we dont wait for result here
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export async function runLearning(id: AnalyticUnit.AnalyticUnitId) { |
|
|
|
|
|
|
|
|
|
let previousLastPredictionTime: number = undefined; |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
let segments = await Segments.findMany(id, { labeled: true }); |
|
|
|
|
let segmentObjs = segments.map(s => s.toObject()); |
|
|
|
|
|
|
|
|
@ -76,26 +84,27 @@ export async function runLearning(id: AnalyticUnit.AnalyticUnitId) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.LEARNING); |
|
|
|
|
let previousLastPredictionTime = analyticUnit.lastPredictionTime; |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
let pattern = analyticUnit.type; |
|
|
|
|
let task = new AnalyticsTask( |
|
|
|
|
id, AnalyticsTaskType.LEARN, { pattern, segments: segmentObjs } |
|
|
|
|
); |
|
|
|
|
let result = await runTask(task); |
|
|
|
|
let { lastPredictionTime, segments } = await processLearningResult(result); |
|
|
|
|
let { lastPredictionTime, segments: predictedSegments } = await processLearningResult(result); |
|
|
|
|
previousLastPredictionTime = analyticUnit.lastPredictionTime; |
|
|
|
|
|
|
|
|
|
await Promise.all([ |
|
|
|
|
Segments.insertSegments(segments), |
|
|
|
|
Segments.insertSegments(predictedSegments), |
|
|
|
|
AnalyticUnit.setPredictionTime(id, lastPredictionTime) |
|
|
|
|
]); |
|
|
|
|
await AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.READY); |
|
|
|
|
|
|
|
|
|
} catch (err) { |
|
|
|
|
await AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.FAILED, err); |
|
|
|
|
if(previousLastPredictionTime !== undefined) { |
|
|
|
|
await AnalyticUnit.setPredictionTime(id, previousLastPredictionTime); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|