diff --git a/README.md b/README.md index 9f906f3..9b0ed07 100644 --- a/README.md +++ b/README.md @@ -727,6 +727,15 @@ await gtfs.fetchRealtimeData(['./path/to/feed.pb']); // Update configuration gtfs.setRealtimeFeedUrls(['https://example.com/new-feed']); gtfs.setStalenessThreshold(60); // 60 seconds + +// Check when realtime data was last fetched +const lastFetch = gtfs.getLastRealtimeFetchTimestamp(); +if (lastFetch) { + const ageSeconds = Math.floor(Date.now() / 1000) - lastFetch; + console.log(`RT data is ${ageSeconds} seconds old`); +} else { + console.log('No RT data has been fetched yet'); +} ``` #### Querying Alerts @@ -1164,6 +1173,7 @@ All methods support flexible filtering with both single values and arrays: - `getRealtimeFeedUrls()` - Get configured RT feed URLs - `setStalenessThreshold(seconds)` - Set staleness threshold (default: 120 seconds) - `getStalenessThreshold()` - Get current staleness threshold +- `getLastRealtimeFetchTimestamp()` - Get Unix timestamp (seconds) of last successful RT fetch, or null if never fetched - `getAlerts(filters?)` - Get alerts (filters: alertId, routeId, stopId, tripId, activeOnly, cause, effect, limit) - `getVehiclePositions(filters?)` - Get vehicle positions (filters: tripId, routeId, vehicleId, limit) - `getTripUpdates(filters?)` - Get trip updates (filters: tripId, routeId, limit) diff --git a/src/gtfs-sqljs.ts b/src/gtfs-sqljs.ts index 3e2c1f5..4006d86 100644 --- a/src/gtfs-sqljs.ts +++ b/src/gtfs-sqljs.ts @@ -142,6 +142,7 @@ export class GtfsSqlJs { private SQL: SqlJsStatic | null = null; private realtimeFeedUrls: string[] = []; private stalenessThreshold: number = 120; + private lastRealtimeFetchTimestamp: number | null = null; /** * Private constructor - use static factory methods instead @@ -875,6 +876,14 @@ export class GtfsSqlJs { return this.stalenessThreshold; } + /** + * Get timestamp of the last successful realtime data fetch and insertion + * @returns Unix timestamp in seconds, or null if no realtime data has been fetched + */ + getLastRealtimeFetchTimestamp(): number | null { + return this.lastRealtimeFetchTimestamp; + } + /** * Fetch and load GTFS Realtime data from configured feed URLs or provided URLs * @param urls - Optional array of feed URLs. If not provided, uses configured feed URLs @@ -888,6 +897,7 @@ export class GtfsSqlJs { } await loadRealtimeData(this.db, feedUrls); + this.lastRealtimeFetchTimestamp = Math.floor(Date.now() / 1000); } /**