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();
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();
});

4
server/src/models/analytic_unit_model.ts

@ -75,7 +75,9 @@ export async function findById(id: AnalyticUnitId): Promise<AnalyticUnit> {
* @returns unit.id
*/
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> {

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'
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);

14
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: [] };
}

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
*/
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<any>,
findMany: (query: string[] | object) => Promise<any[]>,
insertOne: (document: object) => Promise<string>,
insertMany: (documents: object[]) => Promise<string[]>,
updateOne: (query: string | object, updateQuery: any) => Promise<void>,
removeOne: (query: string) => Promise<boolean>
removeMany: (query: string[] | object) => Promise<number>
}
export function makeDBQ(collection: Collection): DBQ {

Loading…
Cancel
Save