feat(axios-get-trip): extend index and add utils

This commit is contained in:
dancingCycle 2022-05-18 12:42:16 +02:00
parent 0b4d85da7b
commit 871e091145
2 changed files with 131 additions and 84 deletions

View File

@ -6,92 +6,120 @@ const debug=require('debug')('trips');
const URL=process.env.URL; const URL=process.env.URL;
debug('URL: '+URL) debug('URL: '+URL)
run().catch(err => { let dataGet='';
let dataServices='';
run(dataGet,dataServices).catch(err => {
debug('run: error') debug('run: error')
console.log(err) console.log(err)
}); });
async function run() { async function handleTrip(element){
const mapTime=new Map();
mapTime.clear();
const tripId=element.trip_id;
//debug('tripId: %s',tripId);
const tripShortName=element.trip_short_name;
//debug('tripShortName: %s',tripShortName);
const serviceId=element.service_id;
//debug('serviceId: %s',serviceId);
const queryService=`http://localhost:65534/service?serviceid=${serviceId}`;
//debug('queryService: '+queryService);
dataServices = await axios.get(queryService);
const monday=dataServices.data[0].monday;
const tuesday=dataServices.data[0].tuesday;
//debug('tuesday: '+tuesday);
const wednesday=dataServices.data[0].wednesday;
//debug('wednesday: '+wednesday);
const thursday=dataServices.data[0].thursday;
const friday=dataServices.data[0].friday;
const saturday=dataServices.data[0].saturday;
const sunday=dataServices.data[0].sunday;
const start_date=dataServices.data[0].start_date;
const dateStart=utils.gtfsDate2NodeDate(start_date);
//debug('dateStart: '+dateStart);
const end_date=dataServices.data[0].end_date;
const dateEnd=utils.gtfsDate2NodeDate(end_date);
//debug('dateEnd: '+dateEnd);
let dateNext=new Date(dateStart);
//debug('dateNext: '+dateNext);
while(dateNext.getTime()<=dateEnd.getTime()){
let weekday=dateNext.getDay();
//debug('weekday: '+weekday);
if((weekday===utils.dateWeekday.monday && monday) ||
(weekday===utils.dateWeekday.tuesday && tuesday) ||
(weekday===utils.dateWeekday.wednesday && wednesday) ||
(weekday===utils.dateWeekday.thursday && thursday) ||
(weekday===utils.dateWeekday.friday && friday) ||
(weekday===utils.dateWeekday.saturday && saturday) ||
(weekday===utils.dateWeekday.sunday && sunday)){
//update map
updateMap(dateNext.getTime(),tripId,tripShortName,mapTime);
}
dateNext=new Date(dateNext.setDate(dateNext.getDate()+1));
//debug('dateNext: '+dateNext.getDate());
//debug('dateNext.getTime(): '+dateNext.getTime());
}
//debug('dateEnd: '+dateEnd);
//debug('dateNext: '+dateNext);
return mapTime;
}
async function run(dataTrips, dataServices) {
debug('run:...') debug('run:...')
//HTTP GET //HTTP GET
let dataGet = await axios.get( dataTrips = await axios.get(URL);
URL
//async example to process responce
// ).then(res => {
// debug('res.data: %s',res.data)
// });
);
debug('data received via GET'); //TODO refactor callback function
debug('dataGet: %s',dataGet); const map=new Map(dataTrips.data.forEach(handleTrip));
debug('dataGet.data.length: %s',dataGet.data.length); debug('map.size: '+map.size);
debug('dataGet.data[0].ageny_name: %s',dataGet.data[0].agency_name); }
debug('dataGet.data[0].route_short_name: %s',dataGet.data[0].route_short_name);
debug('dataGet.data[0].agency_id: %s',dataGet.data[0].agency_id);
debug('dataGet.data[0].route_id: %s',dataGet.data[0].route_id);
/*
const service_id=dataGet.data[0].service_id;
debug('service_id: %s',service_id);
debug('dataGet.data[0].trip_id: %s',dataGet.data[0].trip_id); function updateMap(time,tripId,tripShortName,map){
//debug('time: '+time);
const trip_id=dataGet.data[0].trip_id; //time map empty
debug('trip_id: %s',trip_id); if(map.size===0){
*/ //debug('map.size: '+map.size);
for(var i = 0; i < 2; i++) //create trip map
{ let mapTrips=new Map();
const tripId=dataGet.data[i].trip_id; //add trip to trip map
debug('tripId: %s',tripId); addTrip(tripId,tripShortName,mapTrips);
const serviceId=dataGet.data[0].service_id; //add time to time map
debug('serviceId: %s',serviceId); map.set(time,mapTrips);
//debug('map.size: '+map.size);
const queryService=`http://localhost:65534/service?serviceid=${serviceId}`; }//time map NOT empty
debug('queryService: '+queryService); else{
let dataService = await axios.get(queryService); //debug('map.size: '+map.size);
//time not present yet
const monday=dataService.data[0].monday; if(!map.has(time)){
const tuesday=dataService.data[0].tuesday; //debug('map has not: '+time);
const wednesday=dataService.data[0].wednesday; //create trips map
const thursday=dataService.data[0].thursday; let mapTrips=new Map();
const friday=dataService.data[0].friday; //add trip to trip map
const saturday=dataService.data[0].saturday; addTrip(tripId,tripShortName,mapTrips);
const sunday=dataService.data[0].sunday; //add time to time map
const start_date=dataService.data[0].start_date; map.set(time,mapTrips);
const end_date=dataService.data[0].end_date; //debug('map.size: '+map.size);
const mapTime=new Map(); }
const timeStart=utils.gtfsDate2NodeDate(start_date).getTime(); //time already present
debug('timeStart: '+timeStart); else{
debug('mapTime size: '+mapTime.size); //debug('map has: '+time);
debug('mapTime has timeStart: '+mapTime.has(timeStart)); let mapTrips=map.get(time);
mapTime.set(timeStart,tripId); //debug('mapTrips.size: '+mapTrips.size);
debug('mapTime size: '+mapTime.size); //TODO Does it matter to override existing trips?
debug('mapTime has timeStart: '+mapTime.has(timeStart)); //add trip to trip map
addTrip(tripId,tripShortName,mapTrips);
}
} }
/* }
const map=new Map();
debug('map size: '+map.size); function addTrip(tripId, tripShortName, map){
debug('map has start_date: '+map.has(start_date)); map.set(tripId,tripShortName);
map.set(start_date,trip_id); //debug('map.size: '+map.size);
debug('map size: '+map.size); }
debug('map has start_date: '+map.has(start_date));
debug('map has end_date: '+map.has(end_date));
map.set(end_date,trip_id);
debug('map size: '+map.size);
debug('map has end_date: '+map.has(end_date));
const mapDate=new Map();
const nodeDateStart=utils.gtfsDate2NodeDate(start_date);
debug('nodeDateStart: '+nodeDateStart);
debug('mapDate size: '+mapDate.size);
debug('mapDate has nodeDateStart: '+mapDate.has(nodeDateStart));
mapDate.set(nodeDateStart,trip_id);
debug('mapDate size: '+mapDate.size);
debug('mapDate has nodeDateStart: '+mapDate.has(nodeDateStart));
const nodeDateStartCopy=new Date(nodeDateStart);
debug('mapDate has nodeDateStartCopy: '+mapDate.has(nodeDateStartCopy));
*/
}

View File

@ -2,7 +2,7 @@ const debug=require('debug')('utils');
function findSubStr(str, start, end) { function findSubStr(str, start, end) {
var index = str.slice(start, end); var index = str.slice(start, end);
debug('index: '+index); //debug('index: '+index);
return index; return index;
} }
@ -10,20 +10,39 @@ function gtfsDate2NodeDate(date){
let start = 0; let start = 0;
let end = 4; let end = 4;
let year=findSubStr(date, start, end); let year=findSubStr(date, start, end);
debug('year: '+year); //debug('year: '+year);
start=4; start=4;
end=6; end=6;
let month=findSubStr(date, start, end); let month=findSubStr(date, start, end);
debug('month: '+month); //debug('month: '+month);
start=6; start=6;
end=8; end=8;
let day=findSubStr(date, start, end); let day=findSubStr(date, start, end);
debug('day: '+day); //debug('day: '+day);
let nodeDate = new Date(year, month - 1, day); let nodeDate = new Date(year, month - 1, day);
debug('nodeDate: '+nodeDate.toDateString()); //debug('nodeDate: '+nodeDate);
return nodeDate; return nodeDate;
} }
const dateWeekday={
sunday:0,
monday:1,
tuesday:2,
wednesday:3,
thursday:4,
friday:5,
saturday:6
}
function nextDay(epoch){
debug('epoch: '+epoch);
let epochNext=epoch + (24 * 60 * 60 * 1000) // 1 day in millisecond
debug('epochNext: '+epochNext);
return epochNext;
}
module.exports={ module.exports={
gtfsDate2NodeDate gtfsDate2NodeDate,
nextDay,
dateWeekday
}; };