diff --git a/server/src/models/segment_model.ts b/server/src/models/segment_model.ts index bc46b9c..507622a 100644 --- a/server/src/models/segment_model.ts +++ b/server/src/models/segment_model.ts @@ -5,7 +5,7 @@ import { Collection, makeDBQ } from '../services/data_service'; let db = makeDBQ(Collection.SEGMENTS); -type SegmentId = string; +export type SegmentId = string; export class Segment { constructor( @@ -64,7 +64,7 @@ export type FindManyQuery = { } export async function findMany(id: AnalyticUnitId, query: FindManyQuery): Promise { - var dbQuery: any = { auId: id }; + var dbQuery: any = { analyticUnitId: id }; if(query.timeFromGTE !== undefined) { dbQuery.from = { $gte: query.timeFromGTE }; } @@ -72,6 +72,9 @@ export async function findMany(id: AnalyticUnitId, query: FindManyQuery): Promis dbQuery.to = { $lte: query.timeToLTE }; } let segs = await db.findMany(dbQuery); + if(segs === null) { + return []; + } return segs.map(Segment.fromObject); } diff --git a/server/src/routes/segments_router.ts b/server/src/routes/segments_router.ts index a03d8c3..07bc1fc 100644 --- a/server/src/routes/segments_router.ts +++ b/server/src/routes/segments_router.ts @@ -7,30 +7,47 @@ import { runLearning } from '../controllers/analytics_controller'; async function getSegments(ctx: Router.IRouterContext) { - // let id: AnalyticUnitId = ctx.request.query.id; + let id: AnalyticUnitId = ctx.request.query.id; + if(id === undefined || id === '') { + throw new Error('analyticUnitId (id) is missing'); + } + let query: SegmentModel.FindManyQuery = {}; + + if(!isNaN(+ctx.request.query.lastSegmentId)) { + query.intexGT = +ctx.request.query.lastSegmentId; + } + if(!isNaN(+ctx.request.query.from)) { + query.timeFromGTE = +ctx.request.query.from; + } + if(!isNaN(+ctx.request.query.to)) { + query.timeToLTE = +ctx.request.query.to; + } - // let segments = await findMany(id, { - // intexGT: ctx.request.query.lastSegmentId, - // timeFromGTE: ctx.request.query.from, - // timeToLTE: ctx.request.query.to - // }); + let segments = await SegmentModel.findMany(id, query); - ctx.response.body = { segments: [] }; + ctx.response.body = { segments }; } async function updateSegments(ctx: Router.IRouterContext) { try { - - let { addedSegments, id } = ctx.request.body as { addedSegments: any[], id: AnalyticUnitId }; + + let { + addedSegments, id, removedSegments: removedIds + } = ctx.request.body as { + addedSegments: any[], id: AnalyticUnitId, removedSegments: SegmentModel.SegmentId[] + }; let segmentsToInsert: SegmentModel.Segment[] = addedSegments.map( s => SegmentModel.Segment.fromObject({ analyticUnitId: id, labeled: true, ...s }) ); - - let addedIds = await SegmentModel.insertSegments(segmentsToInsert); - // removeSegments(id, segmentsUpdate.removedSegments); - ctx.response.body = { addedIds }; + + let [addedIds, removed] = await Promise.all([ + SegmentModel.insertSegments(segmentsToInsert), + SegmentModel.removeSegments(removedIds) + ]); + + ctx.response.body = { addedIds, removed }; runLearning(id); } catch(e) { ctx.response.status = 500; diff --git a/server/src/services/data_service.ts b/server/src/services/data_service.ts index e892c99..ec9908c 100644 --- a/server/src/services/data_service.ts +++ b/server/src/services/data_service.ts @@ -48,6 +48,13 @@ function wrapIdsToQuery(query: string[] | object): any { return query; } +function isEmptyArray(obj: any): boolean { + if(!Array.isArray(obj)) { + return false; + } + return obj.length == 0; +} + const db = new Map(); let dbInsertOne = (collection: Collection, doc: object) => { @@ -104,9 +111,12 @@ let dbFindOne = (collection: Collection, query: string | object) => { } let dbFindMany = (collection: Collection, query: string[] | object) => { + if(isEmptyArray(query)) { + return Promise.resolve([]); + } query = wrapIdsToQuery(query); return new Promise((resolve, reject) => { - db.get(collection).findOne(query, (err, docs: any[]) => { + db.get(collection).find(query, (err, docs: any[]) => { if(err) { reject(err); } else { @@ -116,15 +126,15 @@ let dbFindMany = (collection: Collection, query: string[] | object) => { }); } -let dbRemoveOne = (collection: Collection, id: string) => { - let query = { _id: id }; +let dbRemoveOne = (collection: Collection, query: string | object) => { + query = wrapIdToQuery(query); return new Promise((resolve, reject) => { - db.get(collection).remove(query, (err, numRemoved) => { + db.get(collection).remove(query, { /* options */ }, (err, numRemoved) => { if(err) { reject(err); } else { if(numRemoved > 1) { - throw new Error(`Removed ${numRemoved} elements with id: ${id}. Only one is Ok.`); + throw new Error(`Removed ${numRemoved} elements with query: ${JSON.stringify(query)}. Only one is Ok.`); } else { resolve(numRemoved == 1); } @@ -134,9 +144,12 @@ let dbRemoveOne = (collection: Collection, id: string) => { } let dbRemoveMany = (collection: Collection, query: string[] | object) => { + if(isEmptyArray(query)) { + return Promise.resolve([]); + } query = wrapIdsToQuery(query); return new Promise((resolve, reject) => { - db.get(collection).remove(query, (err, numRemoved) => { + db.get(collection).remove(query, { multi: true }, (err, numRemoved) => { if(err) { reject(err); } else { @@ -146,7 +159,6 @@ let dbRemoveMany = (collection: Collection, query: string[] | object) => { }); } - function maybeCreateDir(path: string): void { if(fs.existsSync(path)) { return;