Browse Source

taskresolver type + metric._id

pull/1/head
Coin de Gamma 6 years ago
parent
commit
13df2b4349
  1. 47
      server/src/controllers/analytics_controller.ts
  2. 16
      server/src/models/metric_model.ts

47
server/src/controllers/analytics_controller.ts

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

16
server/src/models/metric_model.ts

@ -1,5 +1,11 @@
export type MetricId = string;
export class Metric { export class Metric {
constructor(public datasource: string, public targets: any[]) { constructor(
public datasource: string,
public targets: any[],
public id?: MetricId
) {
if(datasource === undefined) { if(datasource === undefined) {
throw new Error('datasource is undefined'); throw new Error('datasource is undefined');
} }
@ -14,7 +20,8 @@ export class Metric {
public toObject() { public toObject() {
return { return {
datasource: this.datasource, datasource: this.datasource,
targets: this.targets targets: this.targets,
_id: this.id
}; };
} }
@ -24,9 +31,8 @@ export class Metric {
} }
return new Metric( return new Metric(
obj.datasource, obj.datasource,
obj.targets obj.targets,
obj._id
); );
} }
} }

Loading…
Cancel
Save