feat(route-count): initial commit
This commit is contained in:
parent
ee8ae05244
commit
4581961521
|
@ -0,0 +1 @@
|
|||
.env*
|
|
@ -0,0 +1,21 @@
|
|||
## Overview
|
||||
|
||||
tbc
|
||||
|
||||
## Preparation
|
||||
Run the following command in your favorite terminal to install dependenies.
|
||||
```
|
||||
npm i
|
||||
```
|
||||
## Development setup
|
||||
Run the following command in your favorite terminal if you fancy log messages for debugging.
|
||||
```
|
||||
export DEBUG=$DEBUG,mapping,debug,agency-route-count
|
||||
```
|
||||
Run the following command in your favorite terminal to start the service in development mode.
|
||||
```
|
||||
npm run dev
|
||||
```
|
||||
## Production deployment
|
||||
|
||||
tbc
|
|
@ -0,0 +1,17 @@
|
|||
require('dotenv').config();
|
||||
const axios=require('axios');
|
||||
const debug=require('debug')('agency-route-count');
|
||||
|
||||
/*check if http get service day response includes data*/
|
||||
function hasData(res){
|
||||
if('data' in res){
|
||||
debug('data in response available');
|
||||
return true;
|
||||
}else{
|
||||
debug('data in response NOT available');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports={
|
||||
};
|
|
@ -0,0 +1,52 @@
|
|||
require('dotenv').config();
|
||||
const axios=require('axios');
|
||||
const debug=require('debug')('debug');
|
||||
const Count=require('./agency-route-count');
|
||||
const Mapping=require('./utils/mapping');
|
||||
|
||||
const URL=process.env.URL;
|
||||
debug('URL: '+URL)
|
||||
|
||||
run().catch(err => {
|
||||
debug('run: error')
|
||||
console.log(err)
|
||||
});
|
||||
|
||||
async function run() {
|
||||
debug('run started...')
|
||||
const res = await axios.get(URL);
|
||||
debug('res: '+res);
|
||||
if('data' in res){
|
||||
/*get routes*/
|
||||
const AryRoutes=res.data;
|
||||
const RouteCount=AryRoutes.length;
|
||||
debug('RouteCount: '+RouteCount);
|
||||
/*create map*/
|
||||
const MapRoutes=new Map();
|
||||
|
||||
/*interate through routes*/
|
||||
//TODO for(var i=0;i<RouteCount;i++){
|
||||
for(var i=0;i<20;i++){
|
||||
debug('i: '+i);
|
||||
let agencyId=null;
|
||||
let routeId=null;
|
||||
let routeShortName=null;
|
||||
if('agency_id' in AryRoutes[i]){
|
||||
agencyId=AryRoutes[i].agency_id;
|
||||
debug('agencyId: '+agencyId);
|
||||
}
|
||||
if('route_id' in AryRoutes[i]){
|
||||
routeId=AryRoutes[i].route_id;
|
||||
debug('routeId: '+routeId);
|
||||
}
|
||||
if('route_short_name' in AryRoutes[i]){
|
||||
routeShortName=AryRoutes[i].route_short_name;
|
||||
debug('routeShortName: '+routeShortName);
|
||||
}
|
||||
Mapping.updateMap(agencyId,routeId,routeShortName,MapRoutes);
|
||||
|
||||
}
|
||||
debug('MapRoutes size: '+MapRoutes.size);
|
||||
}
|
||||
debug('run done.')
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"private": "true",
|
||||
"name": "agency-route-count",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"dev": "nodemon index.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "Stefan Begerad",
|
||||
"license": "GPL-3.0",
|
||||
"devDependencies": {
|
||||
"nodemon": "^2.0.7"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^0.21.1",
|
||||
"dotenv": "^8.2.0"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
const debug=require('debug')('mapping');
|
||||
|
||||
function updateMap(time,tripId,tripShortName,map){
|
||||
debug('time: '+time);
|
||||
//time map empty
|
||||
if(map.size===0){
|
||||
debug('map.size: '+map.size);
|
||||
//create trip map
|
||||
let mapTrips=new Map();
|
||||
//add trip to trip map
|
||||
createEntry(mapTrips,tripId,tripShortName);
|
||||
//add time to time map
|
||||
createEntry(map,time,mapTrips);
|
||||
debug('map.size: '+map.size);
|
||||
}//time map NOT empty
|
||||
else{
|
||||
debug('map.size: '+map.size);
|
||||
//time not present yet
|
||||
if(!map.has(time)){
|
||||
debug('map has not: '+time);
|
||||
//create trips map
|
||||
let mapTrips=new Map();
|
||||
//add trip to trip map
|
||||
createEntry(mapTrips,tripId,tripShortName);
|
||||
//add time to time map
|
||||
createEntry(map,time,mapTrips);
|
||||
//debug('map.size: '+map.size);
|
||||
}
|
||||
//time already present
|
||||
else{
|
||||
debug('map has: '+time);
|
||||
let mapTrips=map.get(time);
|
||||
debug('mapTrips.size: '+mapTrips.size);
|
||||
//TODO Does it matter to override existing trips?
|
||||
//add trip to trip map
|
||||
createEntry(mapTrips,tripId,tripShortName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function createEntry(map, key, value){
|
||||
map.set(key,value);
|
||||
}
|
||||
|
||||
function times2Array(map){
|
||||
let array=[];
|
||||
map.forEach((value,key)=>{
|
||||
//debug('key: '+key+', value: '+value);
|
||||
array.push(trips2Array(value));
|
||||
//debug('array: '+array);
|
||||
});
|
||||
return array;
|
||||
}
|
||||
|
||||
function times2Obj(map){
|
||||
let object = {};
|
||||
map.forEach((value,key) => {
|
||||
trips2Obj(value);
|
||||
object[key] = trips2Obj(value);
|
||||
});
|
||||
let objSorted=Object.keys(object).sort().reduce(function (result, key) {
|
||||
result[key] = object[key];
|
||||
return result;
|
||||
}, {});
|
||||
return objSorted;
|
||||
}
|
||||
|
||||
function trips2Array(map){
|
||||
let array=Array.from(map.values());
|
||||
//debug('array: '+array);
|
||||
return array;
|
||||
}
|
||||
|
||||
function trips2Obj(map){
|
||||
let object={};
|
||||
map.forEach((value,key)=>{
|
||||
//debug('key: '+key+', value: '+value);
|
||||
object[key]=value;
|
||||
});
|
||||
//debug('JSON stringify object: '+JSON.stringify(object));
|
||||
return object;
|
||||
}
|
||||
|
||||
module.exports={
|
||||
updateMap,
|
||||
times2Obj,
|
||||
times2Array
|
||||
};
|
Loading…
Reference in New Issue