From 21ae735c54ead3af08cc0e15cecf5ef28214b245 Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Thu, 30 Aug 2018 13:17:58 +0300 Subject: [PATCH] Analytic unit cache v2 (#122) --- server/src/controllers/analytics_controller.ts | 13 +++++++++---- server/src/models/analytic_unit_cache_model.ts | 14 +++++++++----- server/src/models/analytic_unit_model.ts | 9 ++++++--- server/src/services/data_service.ts | 4 ++-- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/server/src/controllers/analytics_controller.ts b/server/src/controllers/analytics_controller.ts index 2108fe6..21f4f5c 100644 --- a/server/src/controllers/analytics_controller.ts +++ b/server/src/controllers/analytics_controller.ts @@ -1,5 +1,6 @@ import { AnalyticsMessageMethod, AnalyticsMessage } from '../models/analytics_message_model'; import { AnalyticsTask, AnalyticsTaskType, AnalyticsTaskId } from '../models/analytics_task_model'; +import * as AnalyticUnitCache from '../models/analytic_unit_cache_model'; import * as Segment from '../models/segment_model'; import * as AnalyticUnit from '../models/analytic_unit_model'; import { AnalyticsService } from '../services/analytics_service'; @@ -111,14 +112,18 @@ export async function runLearning(id: AnalyticUnit.AnalyticUnitId) { } let pattern = analyticUnit.type; - // TODO: add cache + let oldCache = await AnalyticUnitCache.findById(id); + if(oldCache !== null) { + oldCache = oldCache.data; + } let task = new AnalyticsTask( - id, AnalyticsTaskType.LEARN, { pattern, segments: segmentObjs, data } + id, AnalyticsTaskType.LEARN, + { pattern, segments: segmentObjs, data, cache: oldCache } ); AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.LEARNING); let result = await runTask(task); - let { lastPredictionTime, segments: predictedSegments, cache } = await processLearningResult(result); - // TODO: save cache + let { lastPredictionTime, segments: predictedSegments, cache: newCache } = await processLearningResult(result); + AnalyticUnitCache.setData(id, newCache); previousLastPredictionTime = analyticUnit.lastPredictionTime; await Promise.all([ diff --git a/server/src/models/analytic_unit_cache_model.ts b/server/src/models/analytic_unit_cache_model.ts index c916082..5c53478 100644 --- a/server/src/models/analytic_unit_cache_model.ts +++ b/server/src/models/analytic_unit_cache_model.ts @@ -10,18 +10,19 @@ export type AnalyticUnitCacheId = string; export class AnalyticUnitCache { public constructor( public analyticUnitId: AnalyticUnitId, - public data: any, + public data?: any, public id?: AnalyticUnitCacheId, ) { - + if(analyticUnitId === undefined) { + throw new Error(`Missing field "analyticUnitId"`); + } } public toObject() { return { _id: this.id, - analyticUnitId: self.analyticUnitId, - data: self.data, - obj._id + analyticUnitId: this.analyticUnitId, + data: this.data }; } @@ -39,6 +40,9 @@ export class AnalyticUnitCache { export async function findById(id: AnalyticUnitCacheId): Promise { let obj = await db.findOne(id); + if(obj === null) { + return null; + } return AnalyticUnitCache.fromObject(obj); } diff --git a/server/src/models/analytic_unit_model.ts b/server/src/models/analytic_unit_model.ts index 091c4e9..e582ef7 100644 --- a/server/src/models/analytic_unit_model.ts +++ b/server/src/models/analytic_unit_model.ts @@ -7,11 +7,11 @@ let db = makeDBQ(Collection.ANALYTIC_UNITS); export type AnalyticUnitId = string; export enum AnalyticUnitStatus { + READY = 'READY', + PENDING = 'PENDING', LEARNING = 'LEARNING', SUCCESS = 'SUCCESS', - READY = 'READY', - FAILED = 'FAILED', - PENDING = 'PENDING' + FAILED = 'FAILED' } export class AnalyticUnit { @@ -73,6 +73,9 @@ export class AnalyticUnit { export async function findById(id: AnalyticUnitId): Promise { let obj = await db.findOne(id); + if(obj === null) { + return null; + } return AnalyticUnit.fromObject(obj); } diff --git a/server/src/services/data_service.ts b/server/src/services/data_service.ts index f9d1210..fa2cfce 100644 --- a/server/src/services/data_service.ts +++ b/server/src/services/data_service.ts @@ -13,7 +13,7 @@ export enum Collection { ANALYTIC_UNITS, SEGMENTS, ANALYTIC_UNIT_CACHES }; * @param { string | object } query: a key as a string or mongodb-style query */ export type DBQ = { - findOne: (query: string | object) => Promise, + findOne: (query: string | object) => Promise, findMany: (query: string[] | object) => Promise, insertOne: (document: object) => Promise, insertMany: (documents: object[]) => Promise, @@ -101,7 +101,7 @@ let dbUpdateOne = (collection: Collection, query: string | object, updateQuery: let dbFindOne = (collection: Collection, query: string | object) => { query = wrapIdToQuery(query); - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { db.get(collection).findOne(query, (err, doc) => { if(err) { reject(err);