Browse Source

pending status to controller & pretty code

pull/1/head
Coin de Gamma 6 years ago
parent
commit
bc9041daa5
  1. 8
      server/src/controllers/analytics_controller.ts
  2. 3
      server/src/models/analytic_unit_model.ts
  3. 4
      server/src/routes/segments_router.ts
  4. 4
      server/src/services/data_service.ts

8
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 }
}
return { addedIds, removed };
}

3
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<AnalyticUnitId> {
}
export async function remove(id: AnalyticUnitId): Promise<void> {
// TODO: remove it`s segments
await db.removeOne(id);
}

4
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;

4
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<void>((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 {

Loading…
Cancel
Save