Browse Source

pending status to controller & pretty code

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

6
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 { AnalyticsTask, AnalyticsTaskType, AnalyticsTaskId } from '../models/analytics_task_model';
import * as Segment from '../models/segment_model'; import * as Segment from '../models/segment_model';
import * as AnalyticUnit from '../models/analytic_unit_model'; import * as AnalyticUnit from '../models/analytic_unit_model';
@ -181,6 +181,8 @@ export async function updateSegments(
Segment.insertSegments(segmentsToInsert), Segment.insertSegments(segmentsToInsert),
Segment.removeSegments(removedIds) Segment.removeSegments(removedIds)
]); ]);
// TODO: move setting status somehow "inside" learning
await AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.PENDING);
runLearning(id); runLearning(id);
return { addedIds, removed } return { addedIds, removed };
} }

1
server/src/models/analytic_unit_model.ts

@ -88,6 +88,7 @@ export async function create(unit: AnalyticUnit): Promise<AnalyticUnitId> {
} }
export async function remove(id: AnalyticUnitId): Promise<void> { export async function remove(id: AnalyticUnitId): Promise<void> {
// TODO: remove it`s segments
await db.removeOne(id); await db.removeOne(id);
} }

4
server/src/routes/segments_router.ts

@ -1,6 +1,6 @@
import * as AnalyticsController from '../controllers/analytics_controller'; 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 Segment from '../models/segment_model';
import * as Router from 'koa-router'; import * as Router from 'koa-router';
@ -46,7 +46,7 @@ async function updateSegments(ctx: Router.IRouterContext) {
id, segmentsToInsert, removedIds id, segmentsToInsert, removedIds
); );
ctx.response.body = { addedIds, removed, status: AnalyticUnitStatus.PENDING }; ctx.response.body = { addedIds, removed };
} catch(e) { } catch(e) {
ctx.response.status = 500; 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) => { let dbUpdateOne = (collection: Collection, query: string | object, updateQuery: object) => {
// https://github.com/louischatriot/nedb#updating-documents
let nedbUpdateQuery = { $set: updateQuery }
query = wrapIdToQuery(query); query = wrapIdToQuery(query);
return new Promise<void>((resolve, reject) => { 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) { if(err) {
reject(err); reject(err);
} else { } else {

Loading…
Cancel
Save