pg-rest-api/src/service/trip-updates-by-agency-day.js

31 lines
1.2 KiB
JavaScript

require('dotenv').config();
const db = require('./db');
/**
* Get trips that belong to the certain agency_id and day
*
* @return Array of trips
*/
async function get(agencyid = 0, day = '2023-12-27') {
const schema = process.env.DB_SCHEMA || 'schema';
const tomorrow = new Date(day);
tomorrow.setUTCDate(tomorrow.getUTCDate() + 1);
const query = `SELECT agency_name, agency_id, route_id, route_short_name, count(service_id) AS service_id, count(trip_id) AS trip_id, count(trip_short_name) AS trip_short_name, count(trip_headsign) AS trip_headsign, max(timestamp_pgsql) AS timestamp_pgsql, count(timestamp_pgsql) filter(WHERE timestamp_pgsql >= '${day}' AND timestamp_pgsql < '`
+ tomorrow.toISOString().substring(0, 10)
+ `') AS rt_part FROM `
+ schema
+ `.vw_join_gncy_rts_trps_srvcs_pdts WHERE dates like '%${day}%' AND agency_id = '${agencyid}' AND ((timestamp_pgsql >= '${day}' AND timestamp_pgsql < '`
+ tomorrow.toISOString().substring(0, 10)
+ `') OR timestamp_pgsql IS NULL) GROUP BY route_id, route_short_name, agency_id, agency_name ORDER BY agency_name ASC, route_short_name ASC;`
return await db.query(query);
};
module.exports = {
get
};