From bc9041daa5316a1612fbf4d8f8b2be1165039f31 Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Sun, 19 Aug 2018 15:46:40 +0300 Subject: [PATCH] pending status to controller & pretty code --- server/src/controllers/analytics_controller.ts | 8 +++++--- server/src/models/analytic_unit_model.ts | 3 ++- server/src/routes/segments_router.ts | 4 ++-- server/src/services/data_service.ts | 4 +++- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/server/src/controllers/analytics_controller.ts b/server/src/controllers/analytics_controller.ts index 72081ad..53ab9b1 100644 --- a/server/src/controllers/analytics_controller.ts +++ b/server/src/controllers/analytics_controller.ts @@ -1,4 +1,4 @@ -import { AnalyticsMessageMethod, AnalyticsMessage } from '../models/analytics_message_model' +import { AnalyticsMessageMethod, AnalyticsMessage } from '../models/analytics_message_model'; import { AnalyticsTask, AnalyticsTaskType, AnalyticsTaskId } from '../models/analytics_task_model'; import * as Segment from '../models/segment_model'; import * as AnalyticUnit from '../models/analytic_unit_model'; @@ -181,6 +181,8 @@ export async function updateSegments( Segment.insertSegments(segmentsToInsert), Segment.removeSegments(removedIds) ]); + // TODO: move setting status somehow "inside" learning + await AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.PENDING); runLearning(id); - return { addedIds, removed } -} \ No newline at end of file + return { addedIds, removed }; +} diff --git a/server/src/models/analytic_unit_model.ts b/server/src/models/analytic_unit_model.ts index 631ecf6..4f22474 100644 --- a/server/src/models/analytic_unit_model.ts +++ b/server/src/models/analytic_unit_model.ts @@ -6,7 +6,7 @@ let db = makeDBQ(Collection.ANALYTIC_UNITS); export type AnalyticUnitId = string; -export enum AnalyticUnitStatus { +export enum AnalyticUnitStatus { LEARNING = 'LEARNING', SUCCESS = 'SUCCESS', READY = 'READY', @@ -88,6 +88,7 @@ export async function create(unit: AnalyticUnit): Promise { } export async function remove(id: AnalyticUnitId): Promise { + // TODO: remove it`s segments await db.removeOne(id); } diff --git a/server/src/routes/segments_router.ts b/server/src/routes/segments_router.ts index 1b34e78..da7e7ea 100644 --- a/server/src/routes/segments_router.ts +++ b/server/src/routes/segments_router.ts @@ -1,6 +1,6 @@ import * as AnalyticsController from '../controllers/analytics_controller'; -import { AnalyticUnitId, AnalyticUnit, AnalyticUnitStatus } from '../models/analytic_unit_model'; +import { AnalyticUnitId, AnalyticUnitStatus } from '../models/analytic_unit_model'; import * as Segment from '../models/segment_model'; import * as Router from 'koa-router'; @@ -46,7 +46,7 @@ async function updateSegments(ctx: Router.IRouterContext) { id, segmentsToInsert, removedIds ); - ctx.response.body = { addedIds, removed, status: AnalyticUnitStatus.PENDING }; + ctx.response.body = { addedIds, removed }; } catch(e) { ctx.response.status = 500; diff --git a/server/src/services/data_service.ts b/server/src/services/data_service.ts index ec9908c..799b3e6 100644 --- a/server/src/services/data_service.ts +++ b/server/src/services/data_service.ts @@ -85,9 +85,11 @@ let dbInsertMany = (collection: Collection, docs: object[]) => { } let dbUpdateOne = (collection: Collection, query: string | object, updateQuery: object) => { + // https://github.com/louischatriot/nedb#updating-documents + let nedbUpdateQuery = { $set: updateQuery } query = wrapIdToQuery(query); return new Promise((resolve, reject) => { - db.get(collection).update(query, updateQuery, { /* options */ }, (err: Error) => { + db.get(collection).update(query, nedbUpdateQuery, { /* options */ }, (err: Error) => { if(err) { reject(err); } else {