From b2c48242c9602901eaae28afd4f280b5f300a4a0 Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Sat, 18 Aug 2018 00:34:40 +0300 Subject: [PATCH] Task definition + some refactorings --- .../src/controllers/analytics_controller.ts | 42 +++++++++---------- server/src/models/analytic_unit_model.ts | 11 ++++- server/src/models/segment_model.ts | 6 ++- server/src/models/task_model.ts | 6 +++ server/src/services/analytics_service.ts | 4 -- 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/server/src/controllers/analytics_controller.ts b/server/src/controllers/analytics_controller.ts index ae65a91..fb400a2 100644 --- a/server/src/controllers/analytics_controller.ts +++ b/server/src/controllers/analytics_controller.ts @@ -1,5 +1,5 @@ import { Task } from '../models/task_model'; -import * as SegmentsController from '../models/segment_model'; +import * as Segments from '../models/segment_model'; import * as AnalyticUnit from '../models/analytic_unit_model'; import { AnalyticsService, AnalyticsMessage } from '../services/analytics_service'; @@ -66,26 +66,26 @@ async function runTask(task: Task): Promise { } export async function runLearning(id: AnalyticUnit.AnalyticUnitId) { - // let segments = getLabeledSegments(id); - // AnalyticUnit.setStatus(id, 'LEARNING'); - // let unit = await AnalyticUnit.findById(id); - // let pattern = unit.type; - // let task = { - // analyticUnitId: id, - // type: 'LEARN', - // pattern, - // segments: segments - // }; - - // let result = await runTask(task); - - // if (result.status === 'SUCCESS') { - // AnalyticUnit.setStatus(id, 'READY'); - // insertSegments(id, result.segments, false); - // AnalyticUnit.setPredictionTime(id, result.lastPredictionTime); - // } else { - // AnalyticUnit.setStatus(id, 'FAILED', result.error); - // } + let segments = Segments.findMany(id, { labeled: true }); + AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.LEARNING); + let unit = await AnalyticUnit.findById(id); + let pattern = unit.type; + let task = { + analyticUnitId: id, + type: 'LEARN', + pattern, + segments: segments + }; + + let result = await runTask(task); + + if (result.status === 'SUCCESS') { + AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.READY); + insertSegments(id, result.segments, false); + AnalyticUnit.setPredictionTime(id, result.lastPredictionTime); + } else { + AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.FAILED, result.error); + } } export async function runPredict(id: AnalyticUnit.AnalyticUnitId) { diff --git a/server/src/models/analytic_unit_model.ts b/server/src/models/analytic_unit_model.ts index 26b0278..e018de3 100644 --- a/server/src/models/analytic_unit_model.ts +++ b/server/src/models/analytic_unit_model.ts @@ -5,7 +5,14 @@ import { Collection, makeDBQ } from '../services/data_service'; let db = makeDBQ(Collection.ANALYTIC_UNITS); + export type AnalyticUnitId = string; +export enum AnalyticUnitStatus { + LEARNING = 'LEARNING', + SUCCESS = 'SUCCESS', + READY = 'READY', + FAILED = 'FAILED' +} export class AnalyticUnit { constructor( @@ -15,7 +22,7 @@ export class AnalyticUnit { public metric: Metric, public id?: AnalyticUnitId, public lastPredictionTime?: number, - public status?: string, + public status?: AnalyticUnitStatus, public error?: string, ) { if(name === undefined) { @@ -54,7 +61,7 @@ export class AnalyticUnit { obj.panelUrl, obj.type, Metric.fromObject(obj.metric), - obj.status, + obj.status as AnalyticUnitStatus, obj.lastPredictionTime, obj._id, obj.error, diff --git a/server/src/models/segment_model.ts b/server/src/models/segment_model.ts index 507622a..d78435a 100644 --- a/server/src/models/segment_model.ts +++ b/server/src/models/segment_model.ts @@ -60,7 +60,8 @@ export class Segment { export type FindManyQuery = { timeFromGTE?: number, timeToLTE?: number, - intexGT?: number + intexGT?: number, + labeled?: boolean } export async function findMany(id: AnalyticUnitId, query: FindManyQuery): Promise { @@ -71,6 +72,9 @@ export async function findMany(id: AnalyticUnitId, query: FindManyQuery): Promis if(query.timeToLTE !== undefined) { dbQuery.to = { $lte: query.timeToLTE }; } + if(query.labeled !== undefined) { + dbQuery.labeled = query.labeled; + } let segs = await db.findMany(dbQuery); if(segs === null) { return []; diff --git a/server/src/models/task_model.ts b/server/src/models/task_model.ts index c19e3d4..bd01f5a 100644 --- a/server/src/models/task_model.ts +++ b/server/src/models/task_model.ts @@ -2,15 +2,20 @@ import { AnalyticUnitId } from "./analytic_unit_model"; export type TaskId = string; +export enum TaskType { LEARN = 'LEARN' }; export class Task { constructor( public analyticUnitId: AnalyticUnitId, + public type: TaskType, public id?: TaskId ) { if(analyticUnitId === undefined) { throw new Error('analyticUnitId is undefined'); } + if(type === undefined || type === null) { + throw new Error('type is undefined or null'); + } } public toObject() { @@ -26,6 +31,7 @@ export class Task { } return new Task( obj.analyticUnitId, + obj.type as TaskType, obj._id, ); } diff --git a/server/src/services/analytics_service.ts b/server/src/services/analytics_service.ts index 940207f..c38efea 100644 --- a/server/src/services/analytics_service.ts +++ b/server/src/services/analytics_service.ts @@ -20,9 +20,6 @@ export class AnalyticsMessage { } } -function analyticsMessageFromJson(obj: any): AnalyticsMessage { - return new AnalyticsMessage(obj); -} export class AnalyticsService { @@ -194,7 +191,6 @@ export class AnalyticsService { return; } - let text = data.toString(); let response; try {