Add TFL train disruption alerts integration
Query TFL API for Tube and Elizabeth Line status, displaying disruptions as feed cards. Major disruptions (severity 1-6) appear as RIGHT_NOW spotlight cards, minor delays (7-9) as FYI items. - Add TFLDataSource with 2-min cache and severity classification - Add .transitAlert FeedItemType with 0.85 base weight - Wire up async fetch in ContextOrchestrator pipeline - Handle timeout and failure cases gracefully 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -24,6 +24,7 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
||||
@Published private(set) var lastCalendarDiagnostics: [String: String] = [:]
|
||||
@Published private(set) var lastPipelineElapsedMs: Int? = nil
|
||||
@Published private(set) var lastFetchFailed: Bool = false
|
||||
@Published private(set) var lastTFLDiagnostics: [String: String] = [:]
|
||||
@Published private(set) var musicAuthorization: MusicAuthorization.Status = .notDetermined
|
||||
@Published private(set) var nowPlaying: NowPlayingSnapshot? = nil
|
||||
@Published private(set) var spotifyNowPlaying: SpotifyNowPlaying? = nil
|
||||
@@ -43,6 +44,7 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
||||
private let calendarDataSource = CalendarDataSource()
|
||||
private let poiDataSource = POIDataSource()
|
||||
private let stockDataSource = StockDataSource()
|
||||
private let tflDataSource = TFLDataSource()
|
||||
private let ranker: HeuristicRanker
|
||||
private let store: FeedStore
|
||||
private let server: LocalServer
|
||||
@@ -258,11 +260,15 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
||||
async let stockResult = withTimeoutResult(seconds: 6) {
|
||||
try await self.stockDataSource.dataWithDiagnostics(symbols: stockSymbols, now: nowEpoch)
|
||||
}
|
||||
async let tflResult = withTimeoutResult(seconds: 6) {
|
||||
try await self.tflDataSource.dataWithDiagnostics(now: nowEpoch)
|
||||
}
|
||||
|
||||
let wxRes = await weatherResult
|
||||
let calRes = await calendarResult
|
||||
let poiRes = await poiResult
|
||||
let stockRes = await stockResult
|
||||
let tflRes = await tflResult
|
||||
|
||||
func calendarTTL(endAt: Int, now: Int) -> Int {
|
||||
let ttl = endAt - now
|
||||
@@ -279,6 +285,7 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
||||
var calendarItems: [FeedItem] = []
|
||||
var poiItems: [FeedItem] = []
|
||||
var stockItems: [FeedItem] = []
|
||||
var tflItems: [FeedItem] = []
|
||||
var weatherNowItem: FeedItem? = nil
|
||||
var fetchFailed = false
|
||||
var wxDiagnostics: [String: String] = [:]
|
||||
@@ -454,13 +461,49 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
||||
logger.warning("stock fetch failed: \(String(describing: error), privacy: .public)")
|
||||
}
|
||||
|
||||
var tflDiagnostics: [String: String] = [:]
|
||||
switch tflRes {
|
||||
case .success(let snapshot):
|
||||
tflDiagnostics = snapshot.diagnostics
|
||||
for disruption in snapshot.data.disruptions {
|
||||
let confidence: Double = disruption.isMajor ? 0.9 : 0.6
|
||||
let item = FeedItem(
|
||||
id: "tfl:\(disruption.lineId):\(nowEpoch / 300)",
|
||||
type: .transitAlert,
|
||||
title: tflDataSource.disruptionTitle(disruption).truncated(maxLength: TextConstraints.titleMax),
|
||||
subtitle: tflDataSource.disruptionSubtitle(disruption).truncated(maxLength: TextConstraints.subtitleMax),
|
||||
priority: confidence,
|
||||
ttlSec: tflDataSource.ttlSec,
|
||||
condition: nil,
|
||||
startsAt: nil,
|
||||
poiType: nil,
|
||||
bucket: disruption.isMajor ? .rightNow : .fyi,
|
||||
actions: ["DISMISS"]
|
||||
)
|
||||
tflItems.append(item)
|
||||
if disruption.isMajor {
|
||||
rightNowCandidates.append(.init(item: item, confidence: confidence, isEligibleForRightNow: true))
|
||||
}
|
||||
}
|
||||
if !snapshot.data.disruptions.isEmpty {
|
||||
logger.info("tfl disruptions fetched count=\(snapshot.data.disruptions.count)")
|
||||
}
|
||||
case .failure(let error):
|
||||
if case TimeoutError.timedOut = error {
|
||||
logger.warning("tfl fetch timeout")
|
||||
} else {
|
||||
logger.warning("tfl fetch failed: \(String(describing: error), privacy: .public)")
|
||||
}
|
||||
}
|
||||
|
||||
let elapsedMs = Int(Date().timeIntervalSince(start) * 1000)
|
||||
lastPipelineElapsedMs = elapsedMs
|
||||
lastFetchFailed = fetchFailed
|
||||
lastWeatherDiagnostics = wxDiagnostics
|
||||
lastCalendarDiagnostics = calDiagnostics
|
||||
lastTFLDiagnostics = tflDiagnostics
|
||||
|
||||
logger.info("pipeline right_now_candidates=\(rightNowCandidates.count) calendar_items=\(calendarItems.count) poi_items=\(poiItems.count) stock_items=\(stockItems.count) fetchFailed=\(fetchFailed) elapsed_ms=\(elapsedMs)")
|
||||
logger.info("pipeline right_now_candidates=\(rightNowCandidates.count) calendar_items=\(calendarItems.count) poi_items=\(poiItems.count) stock_items=\(stockItems.count) tfl_items=\(tflItems.count) fetchFailed=\(fetchFailed) elapsed_ms=\(elapsedMs)")
|
||||
|
||||
if fetchFailed, rightNowCandidates.isEmpty, calendarItems.isEmpty, weatherNowItem == nil {
|
||||
let fallbackFeed = store.getFeed(now: nowEpoch)
|
||||
@@ -556,6 +599,28 @@ final class ContextOrchestrator: NSObject, ObservableObject {
|
||||
)
|
||||
})
|
||||
|
||||
let fyiTFL = tflItems
|
||||
.filter { $0.bucket == .fyi }
|
||||
.filter { $0.id != winnerItem.id }
|
||||
.filter { !store.isSuppressed(id: $0.id, type: $0.type, now: nowEpoch) }
|
||||
.prefix(2)
|
||||
|
||||
fyi.append(contentsOf: fyiTFL.map { item in
|
||||
FeedItem(
|
||||
id: item.id,
|
||||
type: item.type,
|
||||
title: item.title.truncated(maxLength: TextConstraints.titleMax),
|
||||
subtitle: item.subtitle.truncated(maxLength: TextConstraints.subtitleMax),
|
||||
priority: min(max(item.priority, 0.0), 1.0),
|
||||
ttlSec: max(1, item.ttlSec),
|
||||
condition: item.condition,
|
||||
startsAt: item.startsAt,
|
||||
poiType: item.poiType,
|
||||
bucket: .fyi,
|
||||
actions: ["DISMISS"]
|
||||
)
|
||||
})
|
||||
|
||||
let items = [winnerItem] + fyi
|
||||
let feedEnvelope = FeedEnvelope(
|
||||
schema: 1,
|
||||
|
||||
Reference in New Issue
Block a user