Cleanup and rename

This commit is contained in:
2026-03-13 00:01:14 +01:00
parent c90be4a981
commit 3882e3b0a6
5 changed files with 75 additions and 57 deletions

View File

@@ -1,21 +1,15 @@
#!/usr/bin/env bun
/**
* Background tracker for popular bus routes in Skopje
* Continuously monitors GTFS-RT feeds and stores data as parquet segments
*/
import GtfsRealtimeBindings from 'gtfs-realtime-bindings';
import { config } from './config';
import { GtfsRoute, GtfsStop, loadGtfsRoutes, loadGtfsStops } from './lib/gtfs';
import {
initDatabase,
initStorage,
logVehiclePositions,
logVehicleFeedSnapshot,
logArrival,
closeDatabase,
closeStorage,
VehicleFeedSnapshot,
VehiclePosition
} from './lib/database';
} from './lib/storage';
// Popular routes to track
const TRACKED_ROUTES = [
@@ -53,6 +47,7 @@ const REFRESH_INTERVAL = 30000; // 30 seconds
const ARRIVAL_STOP_CAP = 150; // Max stops to query per cycle
const SAVE_ALL_VEHICLE_SNAPSHOTS = (process.env.SAVE_ALL_VEHICLE_SNAPSHOTS ?? 'true').toLowerCase() === 'true';
const SAVE_ALL_VEHICLE_POSITIONS = (process.env.SAVE_ALL_VEHICLE_POSITIONS ?? 'true').toLowerCase() === 'true';
const VERBOSE_TRACKER_LOGS = (process.env.VERBOSE_TRACKER_LOGS ?? 'false').toLowerCase() === 'true';
let stats = {
cycles: 0,
@@ -109,7 +104,9 @@ async function trackVehicles() {
}
const allVehicles = await vehiclesResponse.json() as any[];
console.log(` Found ${allVehicles.length} total vehicles`);
if (VERBOSE_TRACKER_LOGS) {
console.log(` Found ${allVehicles.length} total vehicles`);
}
if (SAVE_ALL_VEHICLE_SNAPSHOTS && allVehicles.length > 0) {
const captureTime = Date.now();
@@ -187,19 +184,8 @@ async function trackVehicles() {
}
}
console.log(` Matched ${vehicleRouteMap.size} vehicles to GTFS routes`);
// Debug: Show sample vehicle IDs from both sources
if (vehicleRouteMap.size > 0) {
const sampleGtfsIds = Array.from(vehicleRouteMap.keys()).slice(0, 5);
console.log(` Sample GTFS-RT vehicle IDs: ${sampleGtfsIds.join(', ')}`);
}
if (allVehicles.length > 0) {
const sampleJsonIds = allVehicles.slice(0, 5).map(v =>
`${v.identificationNumber || v.inventoryNumber || 'unknown'}`
);
console.log(` Sample JSON API vehicle IDs: ${sampleJsonIds.join(', ')}`);
if (VERBOSE_TRACKER_LOGS) {
console.log(` Matched ${vehicleRouteMap.size} vehicles to GTFS routes`);
}
// Prepare vehicle positions.
@@ -231,7 +217,7 @@ async function trackVehicles() {
});
}
// Log to database
// Persist current cycle positions
if (positions.length > 0) {
await logVehiclePositions(positions);
console.log(` [OK] Logged ${positions.length} vehicle positions${SAVE_ALL_VEHICLE_POSITIONS ? ' (all vehicles mode)' : ''}`);
@@ -350,10 +336,10 @@ async function trackArrivals() {
console.log(` [OK] Logged ${arrivalsLogged} new arrivals (${duplicates} duplicates skipped, ${arrivalsFound} total found)`);
stats.arrivalsLogged += arrivalsLogged;
} else {
console.log(` [INFO] Found ${arrivalsFound} arrivals but all were duplicates (already in database)`);
console.log(` [INFO] Found ${arrivalsFound} arrivals but all were duplicates (already recorded)`);
}
if (matchedRouteCounts.size > 0) {
if (VERBOSE_TRACKER_LOGS && matchedRouteCounts.size > 0) {
const matchedSummary = Array.from(matchedRouteCounts.entries())
.sort((a, b) => b[1] - a[1])
.slice(0, 10)
@@ -362,7 +348,7 @@ async function trackArrivals() {
console.log(` [DEBUG] Matched route IDs: ${matchedSummary}`);
}
if (unmatchedRoutes.size > 0) {
if (VERBOSE_TRACKER_LOGS && unmatchedRoutes.size > 0) {
const topUnmatched = Array.from(unmatchedRoutes.entries())
.sort((a, b) => b[1] - a[1])
.slice(0, 8)
@@ -399,11 +385,11 @@ function printStats() {
async function main() {
console.log('\nStarting Background Bus Tracker for Popular Routes & Stops\n');
// Initialize database
// Initialize storage
try {
await initDatabase();
await initStorage();
} catch (error) {
console.error('Failed to initialize database:', error);
console.error('Failed to initialize storage:', error);
console.log('Continuing without data logging...');
}
@@ -429,14 +415,14 @@ async function main() {
process.on('SIGINT', async () => {
console.log('\n\nShutting down tracker...');
printStats();
await closeDatabase();
await closeStorage();
process.exit(0);
});
process.on('SIGTERM', async () => {
console.log('\n\nReceived SIGTERM, closing tracker...');
printStats();
await closeDatabase();
await closeStorage();
process.exit(0);
});
}