Browse Source

correct saving of segments & removing many

pull/1/head
Coin de Gamma 6 years ago
parent
commit
16f3422224
  1. 7
      server/src/models/segment_model.ts
  2. 39
      server/src/routes/segments_router.ts
  3. 26
      server/src/services/data_service.ts

7
server/src/models/segment_model.ts

@ -5,7 +5,7 @@ import { Collection, makeDBQ } from '../services/data_service';
let db = makeDBQ(Collection.SEGMENTS); let db = makeDBQ(Collection.SEGMENTS);
type SegmentId = string; export type SegmentId = string;
export class Segment { export class Segment {
constructor( constructor(
@ -64,7 +64,7 @@ export type FindManyQuery = {
} }
export async function findMany(id: AnalyticUnitId, query: FindManyQuery): Promise<Segment[]> { export async function findMany(id: AnalyticUnitId, query: FindManyQuery): Promise<Segment[]> {
var dbQuery: any = { auId: id }; var dbQuery: any = { analyticUnitId: id };
if(query.timeFromGTE !== undefined) { if(query.timeFromGTE !== undefined) {
dbQuery.from = { $gte: query.timeFromGTE }; dbQuery.from = { $gte: query.timeFromGTE };
} }
@ -72,6 +72,9 @@ export async function findMany(id: AnalyticUnitId, query: FindManyQuery): Promis
dbQuery.to = { $lte: query.timeToLTE }; dbQuery.to = { $lte: query.timeToLTE };
} }
let segs = await db.findMany(dbQuery); let segs = await db.findMany(dbQuery);
if(segs === null) {
return [];
}
return segs.map(Segment.fromObject); return segs.map(Segment.fromObject);
} }

39
server/src/routes/segments_router.ts

@ -7,30 +7,47 @@ import { runLearning } from '../controllers/analytics_controller';
async function getSegments(ctx: Router.IRouterContext) { 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 = {};
// let segments = await findMany(id, { if(!isNaN(+ctx.request.query.lastSegmentId)) {
// intexGT: ctx.request.query.lastSegmentId, query.intexGT = +ctx.request.query.lastSegmentId;
// timeFromGTE: ctx.request.query.from, }
// timeToLTE: ctx.request.query.to if(!isNaN(+ctx.request.query.from)) {
// }); query.timeFromGTE = +ctx.request.query.from;
}
if(!isNaN(+ctx.request.query.to)) {
query.timeToLTE = +ctx.request.query.to;
}
ctx.response.body = { segments: [] }; let segments = await SegmentModel.findMany(id, query);
ctx.response.body = { segments };
} }
async function updateSegments(ctx: Router.IRouterContext) { async function updateSegments(ctx: Router.IRouterContext) {
try { 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( let segmentsToInsert: SegmentModel.Segment[] = addedSegments.map(
s => SegmentModel.Segment.fromObject({ analyticUnitId: id, labeled: true, ...s }) s => SegmentModel.Segment.fromObject({ analyticUnitId: id, labeled: true, ...s })
); );
let addedIds = await SegmentModel.insertSegments(segmentsToInsert); let [addedIds, removed] = await Promise.all([
// removeSegments(id, segmentsUpdate.removedSegments); SegmentModel.insertSegments(segmentsToInsert),
ctx.response.body = { addedIds }; SegmentModel.removeSegments(removedIds)
]);
ctx.response.body = { addedIds, removed };
runLearning(id); runLearning(id);
} catch(e) { } catch(e) {
ctx.response.status = 500; ctx.response.status = 500;

26
server/src/services/data_service.ts

@ -48,6 +48,13 @@ function wrapIdsToQuery(query: string[] | object): any {
return query; return query;
} }
function isEmptyArray(obj: any): boolean {
if(!Array.isArray(obj)) {
return false;
}
return obj.length == 0;
}
const db = new Map<Collection, nedb>(); const db = new Map<Collection, nedb>();
let dbInsertOne = (collection: Collection, doc: object) => { let dbInsertOne = (collection: Collection, doc: object) => {
@ -104,9 +111,12 @@ let dbFindOne = (collection: Collection, query: string | object) => {
} }
let dbFindMany = (collection: Collection, query: string[] | object) => { let dbFindMany = (collection: Collection, query: string[] | object) => {
if(isEmptyArray(query)) {
return Promise.resolve([]);
}
query = wrapIdsToQuery(query); query = wrapIdsToQuery(query);
return new Promise<any[]>((resolve, reject) => { return new Promise<any[]>((resolve, reject) => {
db.get(collection).findOne(query, (err, docs: any[]) => { db.get(collection).find(query, (err, docs: any[]) => {
if(err) { if(err) {
reject(err); reject(err);
} else { } else {
@ -116,15 +126,15 @@ let dbFindMany = (collection: Collection, query: string[] | object) => {
}); });
} }
let dbRemoveOne = (collection: Collection, id: string) => { let dbRemoveOne = (collection: Collection, query: string | object) => {
let query = { _id: id }; query = wrapIdToQuery(query);
return new Promise<boolean>((resolve, reject) => { return new Promise<boolean>((resolve, reject) => {
db.get(collection).remove(query, (err, numRemoved) => { db.get(collection).remove(query, { /* options */ }, (err, numRemoved) => {
if(err) { if(err) {
reject(err); reject(err);
} else { } else {
if(numRemoved > 1) { 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 { } else {
resolve(numRemoved == 1); resolve(numRemoved == 1);
} }
@ -134,9 +144,12 @@ let dbRemoveOne = (collection: Collection, id: string) => {
} }
let dbRemoveMany = (collection: Collection, query: string[] | object) => { let dbRemoveMany = (collection: Collection, query: string[] | object) => {
if(isEmptyArray(query)) {
return Promise.resolve([]);
}
query = wrapIdsToQuery(query); query = wrapIdsToQuery(query);
return new Promise<number>((resolve, reject) => { return new Promise<number>((resolve, reject) => {
db.get(collection).remove(query, (err, numRemoved) => { db.get(collection).remove(query, { multi: true }, (err, numRemoved) => {
if(err) { if(err) {
reject(err); reject(err);
} else { } else {
@ -146,7 +159,6 @@ let dbRemoveMany = (collection: Collection, query: string[] | object) => {
}); });
} }
function maybeCreateDir(path: string): void { function maybeCreateDir(path: string): void {
if(fs.existsSync(path)) { if(fs.existsSync(path)) {
return; return;

Loading…
Cancel
Save