fix: UPDATE route service-availability

This commit is contained in:
dancingCycle 2023-11-20 19:23:09 +01:00
parent d59c998ad7
commit 9f374e4820
9 changed files with 112 additions and 109 deletions

View File

@ -2,24 +2,29 @@ const debug=require('debug')('debug');
require('dotenv').config();
const db = require('./db');
const date=require('../utils/date');
const gtfs=require('../utils/gtfs');
const mapping=require('../utils/mapping');
/**
* Create Array of dates in timestamp format
*
* @return Array of dates
*/
async function get(serviceId = 0) {
debug('datesServiceAdded serviceId: '+serviceId);
const schema = process.env.DB_SCHEMA || 'schema';
debug('schema: ' + schema );
const query=`SELECT calendar_dates.date, calendar_dates.exception_type FROM ` + schema + `.calendar, ` + schema + `.calendar_dates WHERE calendar.service_id = calendar_dates.service_id AND calendar.service_id = '${serviceId}' and calendar_dates.exception_type = '1';`;
const query=`SELECT calendar_dates.date FROM ` + schema + `.calendar, ` + schema + `.calendar_dates WHERE calendar.service_id = calendar_dates.service_id AND calendar.service_id = '${serviceId}' and calendar_dates.exception_type = '1';`;
debug('datesServiceAdded query: '+query);
const data = await db.query(query);
debug('datesServiceAdded data.length: '+data.length);
debug('datesServiceAdded [0]: '+JSON.stringify(data[0]));
//TODO NOT working const dates=data.map(value=>gtfs.getDatesFromCalendarDates(value).getTime());
const dates = data;
const dates = await db.query(query);
debug('datesServiceAdded dates.length: '+dates.length);
return dates;
const setDatesServiceAdded = new Set();
dates.forEach( ( date ) => setDatesServiceAdded.add( date.date.getTime() ) );
debug('datesServiceAdded setDatesServiceAdded.size: ' + setDatesServiceAdded.size );
return mapping.set2Array(setDatesServiceAdded);
};
module.exports = {

View File

@ -3,9 +3,13 @@ require('dotenv').config();
const db = require('./db');
const date=require('../utils/date');
const gtfs=require('../utils/gtfs');
const mapping=require('../utils/mapping');
/**
* Create Array of dates in timestamp format between service start_date and including end_date.
*
* @return Array of dates
*/
async function get(serviceId = 0) {
//debug('datesServiceAvailable serviceId: '+serviceId);
@ -13,56 +17,40 @@ async function get(serviceId = 0) {
debug('schema: ' + schema );
const query=`SELECT * FROM ` + schema + `.calendar WHERE service_id = '${serviceId}';`;
//debug('datesServiceAvailable query: '+query);
debug('datesServiceAvailable query: ' + query);
const dataService = await db.query(query);
//debug('datesServiceAvailable dataService.length: '+dataService.length);
//debug('datesServiceAvailable service_id: '+dataService[0].service_id);
debug('datesServiceAvailable dataService.length: ' + dataService.length);
const monday=dataService[0].monday;
//debug('monday: '+monday);
const tuesday=dataService[0].tuesday;
//debug('tuesday: '+tuesday);
const wednesday=dataService[0].wednesday;
//debug('wednesday: '+wednesday);
const thursday=dataService[0].thursday;
//debug('thursday: '+thursday);
const friday=dataService[0].friday;
//debug('datesServiceAvailable friday: '+friday);
const saturday=dataService[0].saturday;
//debug('datesServiceAvailable saturday: '+saturday);
const sunday=dataService[0].sunday;
//debug('datesServiceAvailable sunday: '+sunday);
const start_date=dataService[0].start_date;
debug('start_date: '+start_date);
/**
//TODO NOT working const dateStart=gtfs.gtfsDate2NodeDate(start_date.toString());
*/
const dateStart = start_date;
debug('dateStart: '+dateStart);
const end_date=dataService[0].end_date;
debug('end_date: '+end_date);
/**
//TODO NOT working const dateEnd=gtfs.gtfsDate2NodeDate(end_date.toString());
*/
const dateEnd = end_date;
debug('dateEnd: '+dateEnd);
let dateNext=new Date(dateStart);
debug('dateNext: '+dateNext);
const datesServiceAvailable=new Set();
while(dateNext.getTime()<=dateEnd.getTime()){
let weekday=dateNext.getDay();
if((weekday===date.weekday.monday && monday) ||
const dateStart = dataService[0].start_date;
const dateEnd = dataService[0].end_date;
let dateNext = new Date( dateStart );
const setDatesServiceAvailable = new Set();
while( dateNext.getTime() <= dateEnd.getTime() ) {
let weekday = dateNext.getDay();
if( ( weekday === date.weekday.monday && monday) ||
(weekday===date.weekday.tuesday && tuesday) ||
(weekday===date.weekday.wednesday && wednesday) ||
(weekday===date.weekday.thursday && thursday) ||
(weekday===date.weekday.friday && friday) ||
(weekday===date.weekday.saturday && saturday) ||
(weekday===date.weekday.sunday && sunday)){
datesServiceAvailable.add(dateNext.getTime());
setDatesServiceAvailable.add( dateNext.getTime() );
}
dateNext=new Date(dateNext.setDate(dateNext.getDate()+1));
//TODO Optimize!
dateNext = new Date( dateNext.setDate( dateNext.getDate() + 1 ));
}
debug('datesServiceAvailable size: '+datesServiceAvailable.size);
return mapping.set2Array(datesServiceAvailable);
debug('setDatesServiceAvailable size: ' + setDatesServiceAvailable.size );
return mapping.set2Array(setDatesServiceAvailable);
};
module.exports = {

View File

@ -2,26 +2,31 @@ const debug=require('debug')('debug');
require('dotenv').config();
const db = require('./db');
const date=require('../utils/date');
const gtfs=require('../utils/gtfs');
const mapping=require('../utils/mapping');
/**
* Create Array of dates in timestamp format
*
* @return Array of dates
*/
async function get(serviceId = 0) {
debug('datesServiceRemoved serviceId: '+serviceId);
const schema = process.env.DB_SCHEMA || 'schema';
debug('schema: ' + schema );
const query=`SELECT calendar_dates.date, calendar_dates.exception_type FROM ` + schema + `.calendar, ` + schema + `.calendar_dates WHERE calendar.service_id = calendar_dates.service_id AND calendar.service_id = '${serviceId}' and calendar_dates.exception_type = '2';`;
const query=`SELECT calendar_dates.date FROM ` + schema + `.calendar, ` + schema + `.calendar_dates WHERE calendar.service_id = calendar_dates.service_id AND calendar.service_id = '${serviceId}' and calendar_dates.exception_type = '2';`;
debug('datesServiceRemoved query: '+query);
const data = await db.query(query);
debug('datesServiceRemoved data.length: '+data.length);
debug('datesServiceRemoved [0]: '+JSON.stringify(data[0]));
//TODO NOT working const dates=data.map(value=>gtfs.getDatesFromCalendarDates(value).getTime());
const dates = data;
const dates = await db.query(query);
debug('datesServiceRemoved dates.length: '+dates.length);
return dates;
}
const setDatesServiceRemoved = new Set();
dates.forEach( ( date ) => setDatesServiceRemoved.add( date.date.getTime() ) );
debug('datesServiceAdded setDatesServiceRemoved.size: ' + setDatesServiceRemoved.size );
return mapping.set2Array(setDatesServiceRemoved);
};
module.exports = {
get
}
//debug('datesServiceRemoved done.');
};

View File

@ -10,31 +10,40 @@ const mapping=require('../utils/mapping');
async function get(serviceId = 0) {
//debug('serviceAvailability serviceId: '+serviceId);
const available=datesServiceAvailable.get(serviceId);
debug('serviceAvailability available: '+available.length);
//array[date in timestamp format]
const available = await datesServiceAvailable.get(serviceId);
debug('serviceAvailability available: ' + available.length);
//TODO Make sure this function works with not weekday set at all!
//array[date in timestamp format]
const added = await datesServiceAdded.get(serviceId);
debug('serviceAvailability added: ' + added.length);
//array[date in timestamp format]
const removed = await datesServiceRemoved.get(serviceId);
debug('serviceAvailability removed: ' + removed.length);
const datesAll = await Promise.all([available, added, removed]);
debug('serviceAvailability datesAll.length: '+datesAll.length);
//TODO ?
//const setAvail=new Set(available);
//debug('serviceAvailability setAvail: '+setAvail.size);
const datesAll=await Promise.all([available,added,removed]);
debug('serviceAvailability datesAll.length: '+datesAll.length);
const setAvail=new Set(datesAll[0]);
debug('serviceAvailability setAvail.size: '+setAvail.size);
//TODO clean up const setAvail = new Set(datesAll[0]);
const setAvail = new Set( available );
debug('serviceAvailability setAvail.size: ' + setAvail.size );
//remove dates from service
datesAll[2].forEach(rmEntry=>setAvail.delete(rmEntry));
debug('serviceAvailability setAvailWithRmed.size: '+setAvail.size);
//TODO clean up datesAll[2].forEach( rmEntry => setAvail.delete( rmEntry ) );
removed.forEach( rmEntry => setAvail.delete( rmEntry ) );
debug('serviceAvailability setAvailWithRmed.size: ' + setAvail.size );
//add dates to service
const setAvailWithAdded = new Set([...setAvail, ...new Set(datesAll[1])]);
debug('serviceAvailability size: '+setAvailWithAdded.size);
const setAvailWithAdded = new Set( [...setAvail, ...new Set( added ) ] );
debug('serviceAvailability size: ' + setAvailWithAdded.size);
return mapping.set2Array(setAvailWithAdded);
};

View File

@ -8,31 +8,34 @@ const calendar=require('../utils/calendar');
async function get(agencyId = 0) {
//debug('trip-calendar-by-agency-id start...');
//debug('trip-calendar-by-agency-id agencyId: '+agencyId);
debug('trip-calendar-by-agency-id agencyId: '+agencyId);
//get calendar map
const mapCalendar=calendar.getCalendarMap();
//debug('trip-calendar-by-agency-id mapCalendar.size: '+mapCalendar.size);
debug('trip-calendar-by-agency-id mapCalendar.size: '+mapCalendar.size);
//get routes array
const aryRoutes=await routesByAgencyId.get(agencyId);
//debug('trip-calendar-by-agency-id aryRoutes.length: '+aryRoutes.length);
debug('trip-calendar-by-agency-id 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);
//value: day without hours as timestamp
mapTripCalendar.set(value,0);
});
//debug('trip-calendar-by-agency-id mapTripCalendar.size: '+mapTripCalendar.size);
debug('trip-calendar-by-agency-id mapTripCalendar.size: '+mapTripCalendar.size);
//set trip calendar map
//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);
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));

View File

@ -2,51 +2,56 @@ const debug=require('debug')('debug');
const db = require('./db');
const tripsByRouteId=require('./trips-by-route-id');
const serviceAvailability=require('./service-availability');
const serviceAvailability = require('./service-availability');
const mapping=require('../utils/mapping');
const calendar=require('../utils/calendar');
/**
*
*/
async function get(routeId = 0) {
//debug('trip-calendar-by-route-id start...');
//debug('trip-calendar-by-route-id routeId: '+routeId);
debug('trip-calendar-by-route-id routeId: '+routeId);
//get calendar map
const mapCalendar=calendar.getCalendarMap();
//debug('trip-calendar-by-route-id mapCalendar.size: '+mapCalendar.size);
debug('trip-calendar-by-route-id mapCalendar.size: '+mapCalendar.size);
//get trips array
const aryTrips=await tripsByRouteId.get(routeId);
//debug('trip-calendar-by-route-id aryTrips.length: '+aryTrips.length);
const aryTrips = await tripsByRouteId.get(routeId);
debug('trip-calendar-by-route-id aryTrips.length: '+aryTrips.length);
//init services map
const mapServices=new Map();
//debug('trip-calendar-by-route-id mapServices.size: '+mapServices.size);
const mapServices = new Map();
debug('trip-calendar-by-route-id mapServices.size: '+mapServices.size);
//init trip calendar map
const mapTripCalendar=new Map();
const mapTripCalendar = new Map();
mapCalendar.forEach((value,key)=>{
//debug('trip-calendar-by-route-id mapCalendar key: '+key+', value: '+value);
//value: day without hours as timestamp
mapTripCalendar.set(value,0);
});
//debug('trip-calendar-by-route-id mapTripCalendar.size: '+mapTripCalendar.size);
debug('trip-calendar-by-route-id mapTripCalendar.size: '+mapTripCalendar.size);
//set trip calendar map
//iterate over trips
for(var i=0;i<aryTrips.length;i++){
const tripId=aryTrips[i].trip_id;
//debug('trip-calendar-by-route-id tripId: '+tripId);
debug('trip-calendar-by-route-id tripId: '+tripId);
const serviceId=aryTrips[i].service_id;
//debug('trip-calendar-by-route-id serviceId: '+serviceId);
debug('trip-calendar-by-route-id serviceId: '+serviceId);
//get service
let service=[];
let service = [];
if(!mapServices.has(serviceId)){
service=await serviceAvailability.get(serviceId);
mapServices.set(serviceId,service);
service = await serviceAvailability.get(serviceId);
mapServices.set( serviceId, service );
}else{
service=mapServices.get(serviceId);
}
//debug('trip-calendar-by-route-id service.length: '+service.length);
debug('trip-calendar-by-route-id service.length: '+service.length);
//iterate over service availabiltiy
for(var j=0;j<service.length;j++){

View File

@ -13,7 +13,8 @@ async function get(routeid = 0) {
const QUERY=`SELECT trips.service_id, trips.trip_id FROM ` + schema + `.trips WHERE trips.route_id = '${routeid}';`
//debug('QUERY: '+QUERY);
const trips = await db.query(QUERY);
//debug('trips-by-route-id trips.length: '+trips.length);
debug('trips-by-route-id trips.length: '+trips.length);
//debug('trips-by-route-id done.');
return trips;
}

View File

@ -1,4 +1,5 @@
const debug=require('debug')('debug');
function getDayMap(day){
debug('calendar:getDayMap() start...');
debug('calendar:getDayMap() day: '+day);
@ -13,30 +14,18 @@ function getDayMap(day){
}
function getCalendarMap(){
//debug('calendar start...');
debug('calendar start...');
//get date of today without hours
const todayNoHours=new Date(new Date().toDateString());
//debug('calendar todayNoHours: '+todayNoHours);
debug('calendar todayNoHours: '+todayNoHours);
const todayNoHoursTs=todayNoHours.getTime();
//debug('calendar todayNoHoursTs: '+todayNoHoursTs);
//TODO CLEAN UP
/*
//get date before 14 days
const before14DaysDate=new Date(new Date(todayNoHours).setDate(todayNoHours.getDate() - 14));
//debug('calendar before14DaysDate: '+before14DaysDate);
const before14DaysDateTs=before14DaysDate.getTime();
//debug('calendar before14DaysDateTs: '+before14DaysDateTs);
//get date after 28 days
const after28DaysDate=new Date(new Date(todayNoHours).setDate(todayNoHours.getDate() + 28));
//debug('calendar after28DaysDate: '+after28DaysDate);
const after28DaysDateTs=after28DaysDate.getTime();
//debug('calendar after28DaysDateTs: '+after28DaysDateTs);
*/
debug('calendar todayNoHoursTs: '+todayNoHoursTs);
const mapCalendar=new Map();
const dayString='day';
//set dates for week 14 days before
for(var i=-14;i<-10;i++){
const date=new Date(new Date(todayNoHours).setDate(todayNoHours.getDate() + i));
@ -58,7 +47,7 @@ function getCalendarMap(){
//debug('calendar week28DaysAfter: '+dateTs);
mapCalendar.set(dayString+i,dateTs);
}
//debug('calendar done.');
debug('calendar done.');
return mapCalendar;
};
module.exports = {

View File

@ -1,5 +1,3 @@
const debug=require('debug')('date');
const weekday={
sunday:0,
monday:1,