You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.4 KiB
52 lines
1.4 KiB
import * as AnalyticUnit from '../models/analytic_units'; |
|
import * as AnalyticsController from '../controllers/analytics_controller'; |
|
|
|
import * as Router from 'koa-router'; |
|
|
|
|
|
async function query(ctx: Router.IRouterContext) { |
|
|
|
let queryFrom = ctx.request.query.from as string; |
|
let queryTo = ctx.request.query.to as string; |
|
const analyticUnitId = ctx.request.query.analyticUnitId as string; |
|
|
|
if(analyticUnitId === undefined) { |
|
throw new Error(`data router error: request must contain analyticUnitId`); |
|
} |
|
|
|
if(queryFrom === undefined) { |
|
throw new Error(`data router error: request must contain 'from'`) |
|
} |
|
|
|
if(queryTo === undefined) { |
|
throw new Error(`data router error: request must contain 'to'`) |
|
} |
|
|
|
const from = +queryFrom; |
|
const to = +queryTo; |
|
|
|
if(from === NaN) { |
|
throw new Error(`from must be not NaN`); |
|
} |
|
|
|
if(to === NaN) { |
|
throw new Error(`to must be not NaN`); |
|
} |
|
|
|
if(to <= from) { |
|
throw new Error(`data router error: 'to' must be greater than 'from' (from:${from} to:${to})`); |
|
} |
|
|
|
const analyticUnit = await AnalyticUnit.findById(analyticUnitId); |
|
|
|
if(analyticUnit === null) { |
|
throw new Error(`can't find analytic unit ${analyticUnitId}`); |
|
} |
|
|
|
const results = await AnalyticsController.getHSR(analyticUnit, from, to); |
|
ctx.response.body = { results }; |
|
} |
|
|
|
export const router = new Router(); |
|
|
|
router.get('/', query);
|
|
|