Browse Source

Analytic unit cache v2 (#122)

pull/1/head
Alexey Velikiy 6 years ago committed by rozetko
parent
commit
21ae735c54
  1. 13
      server/src/controllers/analytics_controller.ts
  2. 14
      server/src/models/analytic_unit_cache_model.ts
  3. 9
      server/src/models/analytic_unit_model.ts
  4. 4
      server/src/services/data_service.ts

13
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([

14
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<AnalyticUnitCache> {
let obj = await db.findOne(id);
if(obj === null) {
return null;
}
return AnalyticUnitCache.fromObject(obj);
}

9
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<AnalyticUnit> {
let obj = await db.findOne(id);
if(obj === null) {
return null;
}
return AnalyticUnit.fromObject(obj);
}

4
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<any>,
findOne: (query: string | object) => Promise<any | null>,
findMany: (query: string[] | object) => Promise<any[]>,
insertOne: (document: object) => Promise<string>,
insertMany: (documents: object[]) => Promise<string[]>,
@ -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<any>((resolve, reject) => {
return new Promise<any | null>((resolve, reject) => {
db.get(collection).findOne(query, (err, doc) => {
if(err) {
reject(err);

Loading…
Cancel
Save