feat: adjust trip-count-day-agency route
This commit is contained in:
parent
c47daae386
commit
a6a621c9c1
|
@ -33,7 +33,7 @@ npm i
|
|||
|
||||
* run the following instruction to start the service in development mode
|
||||
```
|
||||
DEBUG=debug,trip-count-day-route,table-calendar-dates-count,table-agency-count,table-routes-count,table-shapes-count,table-trips-count,table-calendar-count,table-frequencies-count,table-levels-count,table-names,table-count,trip-count,service-overview,gtfs,date,servicedays,service,agency-url,agency-name,route-short-name,trip-headsign,routes,frequencies,stops,index,root npm run dev
|
||||
DEBUG=debug,trip-count-day-agency,trip-count-day-route,table-calendar-dates-count,table-agency-count,table-routes-count,table-shapes-count,table-trips-count,table-calendar-count,table-frequencies-count,table-levels-count,table-names,table-count,trip-count,service-overview,gtfs,date,servicedays,service,agency-url,agency-name,route-short-name,trip-headsign,routes,frequencies,stops,index,root npm run dev
|
||||
```
|
||||
|
||||
## Production setup
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
const debug=require('debug')('debug');
|
||||
const debug=require('debug')('trip-count-day-agency');
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const tripCountDayAgency = require('../service/trip-count-day-agency');
|
||||
const utils=require('../utils');
|
||||
router.get('/', async function(req, res, next) {
|
||||
debug('trip-count-day-agency start...');
|
||||
//debug('trip-count-day-agency start...');
|
||||
const agencyId=req.query.agencyid;
|
||||
debug(`trip-count-day-agency agency id: ${agencyId}`);
|
||||
//debug(`trip-count-day-agency agency id: ${agencyId}`);
|
||||
const day=req.query.day;
|
||||
debug(`trip-count-day-agency day: ${day}`);
|
||||
//debug(`trip-count-day-agency day: ${day}`);
|
||||
try {
|
||||
res.json(await tripCountDayAgency.get(agencyId, day));
|
||||
} catch (err) {
|
||||
console.error(`Error while getting trip calendar by agency_id, msg: `, err.message);
|
||||
res.status(err.statusCode || 500).json(utils.MSGS.error);
|
||||
}
|
||||
debug('trip-count-day-agency done.');
|
||||
//debug('trip-count-day-agency done.');
|
||||
});
|
||||
module.exports = router;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
const debug=require('debug')('debug');
|
||||
const debug=require('debug')('trip-count-day-route');
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const tripCountDayRoute = require('../service/trip-count-day-route');
|
||||
|
@ -6,9 +6,9 @@ const utils=require('../utils');
|
|||
router.get('/', async function(req, res, next) {
|
||||
//debug('trip-count-day-route start...');
|
||||
const routeId=req.query.routeid;
|
||||
debug(`trip-count-day-route routeId: ${routeId}`);
|
||||
//debug(`trip-count-day-route routeId: ${routeId}`);
|
||||
const day=req.query.day;
|
||||
debug(`trip-count-day-route day: ${day}`);
|
||||
//debug(`trip-count-day-route day: ${day}`);
|
||||
try {
|
||||
res.json(await tripCountDayRoute.get(routeId, day));
|
||||
} catch (err) {
|
||||
|
|
|
@ -1,52 +1,29 @@
|
|||
const debug=require('debug')('debug');
|
||||
const db = require('./db');
|
||||
const debug=require('debug')('trip-count-day-agency');
|
||||
const routesByAgencyId=require('./routes-by-agency-id');
|
||||
const tripCalendarByRouteId=require('./trip-calendar-by-route-id');
|
||||
const mapping=require('../utils/mapping');
|
||||
const calendar=require('../utils/calendar');
|
||||
const tripCountDayRoute =require('./trip-count-day-route');
|
||||
async function get(agencyId = 0, day = 0) {
|
||||
//debug('trip-calendar-by-agency-id start...');
|
||||
debug('trip-calendar-by-agency-id agencyId: '+agencyId);
|
||||
debug('trip-calendar-by-agency-id day: '+day);
|
||||
|
||||
//get calendar map
|
||||
const mapCalendar=calendar.getCalendarMap();
|
||||
//debug('trip-calendar-by-agency-id mapCalendar.size: '+mapCalendar.size);
|
||||
//debug('trip-count-day-agency start...');
|
||||
//debug('trip-count-day-agency agencyId: '+agencyId);
|
||||
//debug('trip-count-day-agency day: '+day);
|
||||
|
||||
//get routes array
|
||||
const aryRoutes=await routesByAgencyId.get(agencyId);
|
||||
//debug('trip-calendar-by-agency-id aryRoutes.length: '+aryRoutes.length);
|
||||
//debug('trip-count-day-agency aryRoutes.length: '+aryRoutes.length);
|
||||
|
||||
//init trip calendar map
|
||||
const mapTripCalendar=new Map();
|
||||
mapCalendar.forEach((value,key)=>{
|
||||
//debug('trip-calendar-by-agency-id mapCalendar key: '+key+', value: '+value);
|
||||
mapTripCalendar.set(value,0);
|
||||
});
|
||||
//debug('trip-calendar-by-agency-id mapTripCalendar.size: '+mapTripCalendar.size);
|
||||
|
||||
//set trip calendar map
|
||||
//compute trip count
|
||||
let tripCount=0;
|
||||
//iterate over routes
|
||||
for(var i=0;i<aryRoutes.length;i++){
|
||||
const routeId=aryRoutes[i].route_id;
|
||||
//debug('trip-calendar-by-agency-id routeId: '+routeId);
|
||||
const trips=await tripCalendarByRouteId.get(routeId);
|
||||
//debug('trip-calendar-by-agency-id trips: '+Object.keys(trips).length);
|
||||
for (const key in trips) {
|
||||
//debug('trip-calendar-by-agency-id key:'+key+', value:'+trips[key]);
|
||||
let tripCalendarValue=mapTripCalendar.get(parseInt(key,10));
|
||||
//debug('trip-calendar-by-agency-id tripCalendarValue: '+tripCalendarValue);
|
||||
if(tripCalendarValue!==undefined){
|
||||
tripCalendarValue=tripCalendarValue+trips[key];
|
||||
//debug('trip-calendar-by-agency-id tripCalendarValue: '+tripCalendarValue);
|
||||
mapTripCalendar.set(parseInt(key,10),tripCalendarValue);
|
||||
}else{
|
||||
debug('trip-calendar-by-agency-id tripCalendarValue is UNDEFINED');
|
||||
}
|
||||
}
|
||||
//debug('trip-count-day-agency routeId: '+routeId);
|
||||
const trips=await tripCountDayRoute.get(routeId,day);
|
||||
//debug('trip-count-day-agency trips: '+trips);
|
||||
tripCount=tripCount+trips;
|
||||
//debug('trip-count-day-agency tripCount: '+tripCount);
|
||||
}
|
||||
//debug('trip-calendar-by-route-id done.');
|
||||
return mapping.map2Obj(mapTripCalendar);
|
||||
//debug('trip-count-day-agency tripCount: '+tripCount);
|
||||
//debug('trip-count-day-agency done.');
|
||||
return tripCount;
|
||||
};
|
||||
module.exports = {
|
||||
get
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
const debug=require('debug')('trip-count-day-route');
|
||||
const db = require('./db');
|
||||
const tripsByRouteId=require('./trips-by-route-id');
|
||||
const serviceAvailability=require('./service-availability');
|
||||
const mapping=require('../utils/mapping');
|
||||
|
||||
//remote time from a Date object
|
||||
function removeTime(date = new Date()) {
|
||||
|
@ -20,21 +18,20 @@ function removeTime(date = new Date()) {
|
|||
* @return number of trips
|
||||
*/
|
||||
async function get(routeId = 0, day = 0) {
|
||||
debug('trip-count-day-route start...');
|
||||
//debug('trip-count-day-route start...');
|
||||
//debug('trip-count-day-route routeId: '+routeId);
|
||||
//debug('trip-count-day-route day ts: '+day);
|
||||
|
||||
//convert day into date without timestamp
|
||||
const dayMs = day * 1000;
|
||||
//debug('trip-count-day-route day ts in ms: '+dayMs);
|
||||
const dayDate = new Date(dayMs);
|
||||
//debug('trip-count-day-route day as date: '+dayDate);
|
||||
const dayDateNoTime = removeTime(dayDate);
|
||||
//debug('trip-count-day-route day as dateNoTime: '+dayDateNoTime);
|
||||
const dayDateNoTimeTs = dayDateNoTime.getTime();
|
||||
debug('trip-count-day-route day as dateNoTimeTs: '+dayDateNoTimeTs);
|
||||
//debug('trip-count-day-route day as dateNoTimeTs: '+dayDateNoTimeTs);
|
||||
|
||||
//get trips array
|
||||
const aryTrips=await tripsByRouteId.get(routeId);
|
||||
debug('trip-count-day-route aryTrips.length: '+aryTrips.length);
|
||||
//debug('trip-count-day-route aryTrips.length: '+aryTrips.length);
|
||||
//debug('trip-count-day-route aryTrips: '+JSON.stringify(aryTrips));
|
||||
|
||||
//init services map
|
||||
|
@ -43,7 +40,6 @@ async function get(routeId = 0, day = 0) {
|
|||
|
||||
//compute trip count
|
||||
let tripCount=0;
|
||||
//set trip calendar map
|
||||
//iterate over trips
|
||||
for(var i=0;i<aryTrips.length;i++){
|
||||
//debug('trip-count-day-route i: '+i);
|
||||
|
@ -73,8 +69,8 @@ async function get(routeId = 0, day = 0) {
|
|||
}
|
||||
}
|
||||
}
|
||||
debug('trip-count-day-route tripCount: '+tripCount);
|
||||
debug('trip-count-day-route done.');
|
||||
//debug('trip-count-day-route tripCount: '+tripCount);
|
||||
//debug('trip-count-day-route done.');
|
||||
return tripCount;
|
||||
};
|
||||
module.exports = {
|
||||
|
|
Loading…
Reference in New Issue