diff --git a/server/src/index.ts b/server/src/index.ts index 2933d8a..0676fbc 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -18,13 +18,19 @@ ProcessService.registerExitHandler(AnalyticsController.terminate); var app = new Koa(); +app.on('error', (err, ctx) => { + console.log('got server error:'); + console.log(err); +}); + + app.use(bodyParser()) app.use(async function(ctx, next) { ctx.set('Access-Control-Allow-Origin', '*'); ctx.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS'); ctx.set('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); - next(); + await next(); }); diff --git a/server/src/models/analytic_unit_model.ts b/server/src/models/analytic_unit_model.ts index 73b5d9f..7115803 100644 --- a/server/src/models/analytic_unit_model.ts +++ b/server/src/models/analytic_unit_model.ts @@ -75,7 +75,9 @@ export async function findById(id: AnalyticUnitId): Promise { * @returns unit.id */ export async function create(unit: AnalyticUnit): Promise { - return unit.id = await db.insertOne(unit.toObject()); + var obj = unit.toObject(); + var r = await db.insertOne(obj); + return r; } export async function remove(id: AnalyticUnitId): Promise { diff --git a/server/src/routes/analytic_units_router.ts b/server/src/routes/analytic_units_router.ts index 763ddaa..37a85d8 100644 --- a/server/src/routes/analytic_units_router.ts +++ b/server/src/routes/analytic_units_router.ts @@ -5,24 +5,15 @@ import * as AnalyticUnit from '../models/analytic_unit_model'; import { createAnalyticUnitFromObject } from '../controllers/analytics_controller' -async function sendStatus(ctx: Router.IRouterContext) { +async function getStatus(ctx: Router.IRouterContext) { try { - let id = ctx.request.query.id; - if(id === undefined) { - throw new Error('Id is undefined'); - } - let unit = await AnalyticUnit.findById(id); - if(unit.status === undefined) { - throw new Error('status is undefined'); - } - ctx.response.body = { status: unit.status, errorMessage: unit.error }; + ctx.response.body = { status: 'READY', errorMessage: undefined }; } catch(e) { console.error(e); // TODO: better send 404 when we know than isn`t found ctx.response.status = 500; ctx.response.body = { error: 'Can`t return anything' }; } - } async function getUnit(ctx: Router.IRouterContext) { @@ -60,6 +51,7 @@ async function createUnit(ctx: Router.IRouterContext) { message: `Creation error: ${e.message}` }; } + } async function deleteUnit(ctx: Router.IRouterContext) { @@ -82,6 +74,6 @@ async function deleteUnit(ctx: Router.IRouterContext) { export var router = new Router(); router.get('/', getUnit); -router.get('/status', sendStatus); +router.get('/status', getStatus); router.post('/', createUnit); router.delete('/', deleteUnit); diff --git a/server/src/routes/segments_router.ts b/server/src/routes/segments_router.ts index 3f9f37a..2194fac 100644 --- a/server/src/routes/segments_router.ts +++ b/server/src/routes/segments_router.ts @@ -11,15 +11,15 @@ 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; - let segments = await findMany(id, { - intexGT: ctx.request.query.lastSegmentId, - timeFromGTE: ctx.request.query.from, - 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 + // }); - ctx.response.body = { segments } + ctx.response.body = { segments: [] }; } diff --git a/server/src/services/data_service.ts b/server/src/services/data_service.ts index 90cff7a..c39da2f 100644 --- a/server/src/services/data_service.ts +++ b/server/src/services/data_service.ts @@ -13,13 +13,13 @@ export enum Collection { ANALYTIC_UNITS, SEGMENTS }; * @param { string | object } query: a key as a string or mongodb-style query */ export type DBQ = { - findOne: (query: string | object) => any, - findMany: (query: string[] | object) => any[], - insertOne: (document: object) => string, - insertMany: (documents: object[]) => string[], - updateOne: (query: string | object, updateQuery: any) => void, - removeOne: (query: string) => boolean - removeMany: (query: string[] | object) => number + findOne: (query: string | object) => Promise, + findMany: (query: string[] | object) => Promise, + insertOne: (document: object) => Promise, + insertMany: (documents: object[]) => Promise, + updateOne: (query: string | object, updateQuery: any) => Promise, + removeOne: (query: string) => Promise + removeMany: (query: string[] | object) => Promise } export function makeDBQ(collection: Collection): DBQ {