Browse Source

db-many querys & usage in segments

pull/1/head
Coin de Gamma 6 years ago
parent
commit
bacf05da9c
  1. 3
      server/.vscode/launch.json
  2. 7
      server/src/models/analytic_unit_model.ts
  3. 31
      server/src/models/segment_model.ts
  4. 19
      server/src/routes/analytic_units_router.ts
  5. 30
      server/src/routes/segments_router.ts
  6. 66
      server/src/services/data_service.ts

3
server/.vscode/launch.json vendored

@ -17,7 +17,8 @@
//"outFiles": [ "/home/alex/Projects/hastic-server/server.js" ], //"outFiles": [ "/home/alex/Projects/hastic-server/server.js" ],
"port": 9229, "port": 9229,
"restart": true, "restart": true,
"trace": true "trace": true,
"timeout": 100000
} }
] ]
} }

7
server/src/models/analytic_unit_model.ts

@ -61,22 +61,21 @@ export class AnalyticUnit {
); );
} }
} }
export async function findById(id: AnalyticUnitId): Promise<AnalyticUnit> { export async function findById(id: AnalyticUnitId): Promise<AnalyticUnit> {
return AnalyticUnit.fromObject(db.findOne(id)); return AnalyticUnit.fromObject(await db.findOne(id));
} }
/** /**
* Creates and updates new unit.id * Creates and updates new unit.id
* *
* @param unit to create * @param unit to create
* @returns unit.id * @returns unit.id
*/ */
export async function create(unit: AnalyticUnit): Promise<AnalyticUnitId> { export async function create(unit: AnalyticUnit): Promise<AnalyticUnitId> {
return unit.id = await db.insertOne(unit); return unit.id = await db.insertOne(unit.toObject());
} }
export async function remove(id: AnalyticUnitId): Promise<void> { export async function remove(id: AnalyticUnitId): Promise<void> {

31
server/src/models/segment_model.ts

@ -9,11 +9,15 @@ type SegmentId = string;
export class Segment { export class Segment {
constructor( constructor(
public auId: AnalyticUnitId,
public from: number, public from: number,
public to: number, public to: number,
public labeled: boolean, public labeled: boolean,
public id?: SegmentId public id?: SegmentId
) { ) {
if(auId === undefined) {
throw new Error('AnalyticUnitId is undefined');
}
if(from === undefined) { if(from === undefined) {
throw new Error('from is undefined'); throw new Error('from is undefined');
} }
@ -28,6 +32,7 @@ export class Segment {
public toObject() { public toObject() {
return { return {
_id: this.id, _id: this.id,
auId: this.auId,
from: this.from, from: this.from,
to: this.to, to: this.to,
labeled: this.labeled labeled: this.labeled
@ -39,24 +44,34 @@ export class Segment {
throw new Error('obj is undefined'); throw new Error('obj is undefined');
} }
return new Segment( return new Segment(
obj.from, obj.to, obj.auId, +obj.from, +obj.to,
obj.labeled, obj.id || obj._id obj.labeled, obj.id || obj._id
); );
} }
} }
export type FindManyQuery = {
export function getLabeledSegments(id: AnalyticUnitId) { timeFromGTE?: number,
//return db. timeToLTE?: number,
intexGT?: number
} }
export function getPredicted(id: AnalyticUnitId) { export async function findMany(id: AnalyticUnitId, query: FindManyQuery): Promise<Segment[]> {
var dbQuery: any = { auId: id };
if(query.timeFromGTE !== undefined) {
dbQuery.from = { $gte: query.timeFromGTE };
}
if(query.timeToLTE !== undefined) {
dbQuery.to = { $lte: query.timeToLTE };
}
let segs = await db.findMany(dbQuery);
return segs.map(Segment.fromObject);
} }
export async function insertSegments(id: any, segments: Segment[]) { export async function insertSegments(id: AnalyticUnitId, segments: Segment[]) {
return db.insertMany(segments.map(s => s.toObject()));
} }
export function removeSegments(idsToRemove: SegmentId[]) { export function removeSegments(idsToRemove: SegmentId[]) {
return db.removeMany(idsToRemove);
} }

19
server/src/routes/analytic_units_router.ts

@ -25,7 +25,7 @@ async function sendStatus(ctx: Router.IRouterContext) {
} }
async function findItem(ctx: Router.IRouterContext) { async function getUnit(ctx: Router.IRouterContext) {
try { try {
let id = ctx.request.query.id; let id = ctx.request.query.id;
@ -49,7 +49,7 @@ async function findItem(ctx: Router.IRouterContext) {
} }
} }
async function createItem(ctx: Router.IRouterContext) { async function createUnit(ctx: Router.IRouterContext) {
try { try {
let newId = await createAnalyticUnitFromObject(ctx.request.body); let newId = await createAnalyticUnitFromObject(ctx.request.body);
ctx.response.body = { id: newId }; ctx.response.body = { id: newId };
@ -62,14 +62,9 @@ async function createItem(ctx: Router.IRouterContext) {
} }
} }
function deleteItem(ctx: Router.IRouterContext) { async function deleteUnit(ctx: Router.IRouterContext) {
try { try {
let id = ctx.request.query.id; await AnalyticUnit.remove(ctx.request.query.id);
if(id !== undefined) {
AnalyticUnit.remove(id);
}
ctx.response.body = { ctx.response.body = {
code: 200, code: 200,
message: 'Success' message: 'Success'
@ -86,7 +81,7 @@ function deleteItem(ctx: Router.IRouterContext) {
export var router = new Router(); export var router = new Router();
router.get('/', getUnit);
router.get('/status', sendStatus); router.get('/status', sendStatus);
router.get('/', findItem); router.post('/', createUnit);
router.post('/', createItem); router.delete('/', deleteUnit);
router.delete('/', deleteItem);

30
server/src/routes/segments_router.ts

@ -3,35 +3,21 @@ import * as Router from 'koa-router';
import { AnalyticUnitId } from '../models/analytic_unit_model'; import { AnalyticUnitId } from '../models/analytic_unit_model';
import { import {
getLabeledSegments, findMany,
insertSegments, insertSegments,
removeSegments, removeSegments,
} from '../models/segment_model'; } from '../models/segment_model';
import { runLearning } from '../controllers/analytics_controller'; import { runLearning } from '../controllers/analytics_controller';
async function sendSegments(ctx: Router.IRouterContext) { async function getSegments(ctx: Router.IRouterContext) {
let id: AnalyticUnitId = ctx.request.query.id; let id: AnalyticUnitId = ctx.request.query.id;
let lastSegmentId = ctx.request.query.lastSegmentId; let segments = await findMany(id, {
let timeFrom = ctx.request.query.from; intexGT: ctx.request.query.lastSegmentId,
let timeTo = ctx.request.query.to; timeFromGTE: ctx.request.query.from,
timeToLTE: ctx.request.query.to
let segments = await getLabeledSegments(id); });
// // Id filtering
// if(lastSegmentId !== undefined) {
// segments = segments.filter(el => el.id > lastSegmentId);
// }
// // Time filtering
// if(timeFrom !== undefined) {
// segments = segments.filter(el => el.finish > timeFrom);
// }
// if(timeTo !== undefined) {
// segments = segments.filter(el => el.start < timeTo);
// }
ctx.response.body = { segments } ctx.response.body = { segments }
@ -56,5 +42,5 @@ async function updateSegments(ctx: Router.IRouterContext) {
export const router = new Router(); export const router = new Router();
router.get('/', sendSegments); router.get('/', getSegments);
router.patch('/', updateSegments); router.patch('/', updateSegments);

66
server/src/services/data_service.ts

@ -9,37 +9,48 @@ export enum Collection { ANALYTIC_UNITS, SEGMENTS };
/** /**
* Class which helps to make queries to your collection * Class which helps to make queries to your collection
* *
* @param { string | object } query: a key as a string or mongodb-style query * @param { string | object } query: a key as a string or mongodb-style query
*/ */
export type DBQ = { export type DBQ = {
findOne: (query: string | object) => any,
findMany: (query: string[] | object) => any[],
insertOne: (document: object) => string, insertOne: (document: object) => string,
insertMany: (documents: object[]) => string[], insertMany: (documents: object[]) => string[],
updateOne: (query: string | object, updateQuery: any) => void, updateOne: (query: string | object, updateQuery: any) => void,
findOne: (query: string | object) => any, removeOne: (query: string) => boolean
removeOne: (query: string | object) => number removeMany: (query: string[] | object) => number
} }
export function makeDBQ(collection: Collection): DBQ { export function makeDBQ(collection: Collection): DBQ {
return { return {
insertOne: dbInsert.bind(null, collection),
insertMany: dbInsertMany.bind(null, collection),
updateOne: dbUpdate.bind(null, collection),
findOne: dbFindOne.bind(null, collection), findOne: dbFindOne.bind(null, collection),
removeOne: dbRemove.bind(null, collection) findMany: dbFindMany.bind(null, collection),
insertOne: dbInsertOne.bind(null, collection),
insertMany: dbInsertMany.bind(null, collection),
updateOne: dbUpdateOne.bind(null, collection),
removeOne: dbRemoveOne.bind(null, collection),
removeMany: dbRemoveMany.bind(null, collection)
} }
} }
function wrapIdToQuery(query: string | object) { function wrapIdToQuery(query: string | object): any {
if(typeof query === 'string') { if(typeof query === 'string') {
return { _id: query }; return { _id: query };
} }
return query; return query;
} }
function wrapIdsToQuery(query: string[] | object): any {
if(Array.isArray(query)) {
return { _id: { $in: query } };
}
return query;
}
const db = new Map<Collection, nedb>(); const db = new Map<Collection, nedb>();
let dbInsert = (collection: Collection, doc: object) => { let dbInsertOne = (collection: Collection, doc: object) => {
return new Promise<string>((resolve, reject) => { return new Promise<string>((resolve, reject) => {
db[collection].insert(doc, (err, newDoc) => { db[collection].insert(doc, (err, newDoc) => {
if(err) { if(err) {
@ -63,7 +74,7 @@ let dbInsertMany = (collection: Collection, docs: object[]) => {
}); });
} }
let dbUpdate = (collection: Collection, query: string | object, updateQuery: object) => { let dbUpdateOne = (collection: Collection, query: string | object, updateQuery: object) => {
query = wrapIdToQuery(query); query = wrapIdToQuery(query);
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
db[collection].update(query, updateQuery, { /* options */ }, (err: Error) => { db[collection].update(query, updateQuery, { /* options */ }, (err: Error) => {
@ -89,8 +100,38 @@ let dbFindOne = (collection: Collection, query: string | object) => {
}); });
} }
let dbRemove = (collection: Collection, query: string | object) => { let dbFindMany = (collection: Collection, query: string[] | object) => {
query = wrapIdToQuery(query); query = wrapIdsToQuery(query);
return new Promise<any[]>((resolve, reject) => {
db[collection].findOne(query, (err, docs) => {
if(err) {
reject(err);
} else {
resolve(docs);
}
});
});
}
let dbRemoveOne = (collection: Collection, id: string) => {
let query = { _id: id };
return new Promise<boolean>((resolve, reject) => {
db[collection].remove(query, (err, numRemoved) => {
if(err) {
reject(err);
} else {
if(numRemoved > 1) {
throw new Error(`Removed ${numRemoved} elements with id: ${id}. Only one is Ok.`);
} else {
resolve(numRemoved == 1);
}
}
});
});
}
let dbRemoveMany = (collection: Collection, query: string[] | object) => {
query = wrapIdsToQuery(query);
return new Promise<number>((resolve, reject) => { return new Promise<number>((resolve, reject) => {
db[collection].remove(query, (err, numRemoved) => { db[collection].remove(query, (err, numRemoved) => {
if(err) { if(err) {
@ -102,6 +143,7 @@ let dbRemove = (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