Browse Source

async bugfxs

pull/1/head
Coin de Gamma 6 years ago
parent
commit
1d047087b4
  1. 8
      server/src/index.ts
  2. 4
      server/src/models/analytic_unit_model.ts
  3. 16
      server/src/routes/analytic_units_router.ts
  4. 14
      server/src/routes/segments_router.ts
  5. 14
      server/src/services/data_service.ts

8
server/src/index.ts

@ -18,13 +18,19 @@ ProcessService.registerExitHandler(AnalyticsController.terminate);
var app = new Koa(); var app = new Koa();
app.on('error', (err, ctx) => {
console.log('got server error:');
console.log(err);
});
app.use(bodyParser()) app.use(bodyParser())
app.use(async function(ctx, next) { app.use(async function(ctx, next) {
ctx.set('Access-Control-Allow-Origin', '*'); ctx.set('Access-Control-Allow-Origin', '*');
ctx.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS'); 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'); ctx.set('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next(); await next();
}); });

4
server/src/models/analytic_unit_model.ts

@ -75,7 +75,9 @@ export async function findById(id: AnalyticUnitId): Promise<AnalyticUnit> {
* @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.toObject()); var obj = unit.toObject();
var r = await db.insertOne(obj);
return r;
} }
export async function remove(id: AnalyticUnitId): Promise<void> { export async function remove(id: AnalyticUnitId): Promise<void> {

16
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' import { createAnalyticUnitFromObject } from '../controllers/analytics_controller'
async function sendStatus(ctx: Router.IRouterContext) { async function getStatus(ctx: Router.IRouterContext) {
try { try {
let id = ctx.request.query.id; ctx.response.body = { status: 'READY', errorMessage: undefined };
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 };
} catch(e) { } catch(e) {
console.error(e); console.error(e);
// TODO: better send 404 when we know than isn`t found // TODO: better send 404 when we know than isn`t found
ctx.response.status = 500; ctx.response.status = 500;
ctx.response.body = { error: 'Can`t return anything' }; ctx.response.body = { error: 'Can`t return anything' };
} }
} }
async function getUnit(ctx: Router.IRouterContext) { async function getUnit(ctx: Router.IRouterContext) {
@ -60,6 +51,7 @@ async function createUnit(ctx: Router.IRouterContext) {
message: `Creation error: ${e.message}` message: `Creation error: ${e.message}`
}; };
} }
} }
async function deleteUnit(ctx: Router.IRouterContext) { async function deleteUnit(ctx: Router.IRouterContext) {
@ -82,6 +74,6 @@ async function deleteUnit(ctx: Router.IRouterContext) {
export var router = new Router(); export var router = new Router();
router.get('/', getUnit); router.get('/', getUnit);
router.get('/status', sendStatus); router.get('/status', getStatus);
router.post('/', createUnit); router.post('/', createUnit);
router.delete('/', deleteUnit); router.delete('/', deleteUnit);

14
server/src/routes/segments_router.ts

@ -11,15 +11,15 @@ 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;
let segments = await findMany(id, { // let segments = await findMany(id, {
intexGT: ctx.request.query.lastSegmentId, // intexGT: ctx.request.query.lastSegmentId,
timeFromGTE: ctx.request.query.from, // timeFromGTE: ctx.request.query.from,
timeToLTE: ctx.request.query.to // timeToLTE: ctx.request.query.to
}); // });
ctx.response.body = { segments } ctx.response.body = { segments: [] };
} }

14
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 * @param { string | object } query: a key as a string or mongodb-style query
*/ */
export type DBQ = { export type DBQ = {
findOne: (query: string | object) => any, findOne: (query: string | object) => Promise<any>,
findMany: (query: string[] | object) => any[], findMany: (query: string[] | object) => Promise<any[]>,
insertOne: (document: object) => string, insertOne: (document: object) => Promise<string>,
insertMany: (documents: object[]) => string[], insertMany: (documents: object[]) => Promise<string[]>,
updateOne: (query: string | object, updateQuery: any) => void, updateOne: (query: string | object, updateQuery: any) => Promise<void>,
removeOne: (query: string) => boolean removeOne: (query: string) => Promise<boolean>
removeMany: (query: string[] | object) => number removeMany: (query: string[] | object) => Promise<number>
} }
export function makeDBQ(collection: Collection): DBQ { export function makeDBQ(collection: Collection): DBQ {

Loading…
Cancel
Save