Best Value: baseline on the closest eligible price and gate on freshness
- Add FuelPriceFreshness: selected-fuel-aware eligibility plus a shared comparison confidence, so a saving is presented as verified only when both the candidate and the baseline pass the gate for the fuel on screen. - Rank against the closest station with an eligible selected-fuel price instead of the physically closest station, so a single stale neighbour can no longer invalidate every comparison in the list. Ordinary Closest sorting still includes every priced station. - Keep ineligible prices visible, fall back to pump-price order, and label Best Value unavailable when no comparison qualifies. - Widget: same baseline and confidence path as the app. - Tests: stale-closest baseline, alert baseline, eligibility policy, boundary and missing-timestamp regressions.
This commit is contained in:
@@ -38,7 +38,9 @@ enum FuelPriceProvider {
|
||||
lat: relay.lat ?? 0,
|
||||
lng: relay.lng ?? 0,
|
||||
prices: relay.allPrices,
|
||||
priceUpdated: nil
|
||||
priceUpdated: relay.priceUpdatedDate?.timeIntervalSince1970,
|
||||
priceUpdatedByFuel: relay.priceUpdatedByFuel,
|
||||
priceUpdatedByFuelRaw: relay.priceUpdatedByFuelRaw
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -65,6 +67,15 @@ enum FuelPriceProvider {
|
||||
return result
|
||||
}
|
||||
|
||||
static func fuelType(for grade: String) -> FuelType? {
|
||||
switch grade.uppercased() {
|
||||
case "E10": return .e10
|
||||
case "E5": return .e5
|
||||
case "DIESEL", "B7", "B7S", "B7P", "B10": return .diesel
|
||||
default: return nil
|
||||
}
|
||||
}
|
||||
|
||||
/// Decodes mirror envelope metadata (source, dataset update time) — the
|
||||
/// About section shows these so the user can see which data source is
|
||||
/// live and how fresh the GOV.UK data itself is. The fields are additive
|
||||
@@ -106,6 +117,41 @@ private struct MirrorResponse: Codable {
|
||||
let lng: Double?
|
||||
let price: Double?
|
||||
let prices: [String: Double]?
|
||||
let priceUpdated: String?
|
||||
let priceUpdatedByGrade: [String: String]?
|
||||
let priceUpdatedByFuelRawValues: [String: String]?
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case id, name, brand, address, postcode, lat, lng, price, prices
|
||||
case priceUpdated = "price_updated"
|
||||
case priceUpdatedByGrade = "price_updated_by_grade"
|
||||
case priceUpdatedByFuelRawValues = "price_updated_by_fuel"
|
||||
}
|
||||
|
||||
var priceUpdatedDate: Date? {
|
||||
FuelPriceTimestamp.parse(priceUpdated)
|
||||
}
|
||||
|
||||
var priceUpdatedByFuelRaw: [FuelType: String]? {
|
||||
let raw = priceUpdatedByGrade ?? priceUpdatedByFuelRawValues
|
||||
guard let raw else { return nil }
|
||||
let parsed = raw.reduce(into: [FuelType: String]()) { result, item in
|
||||
guard let fuel = FuelPriceProvider.fuelType(for: item.key) else { return }
|
||||
result[fuel] = item.value
|
||||
}
|
||||
return parsed.isEmpty ? nil : parsed
|
||||
}
|
||||
|
||||
var priceUpdatedByFuel: [FuelType: TimeInterval]? {
|
||||
let raw = priceUpdatedByGrade ?? priceUpdatedByFuelRawValues
|
||||
guard let raw else { return nil }
|
||||
let parsed = raw.reduce(into: [FuelType: TimeInterval]()) { result, item in
|
||||
guard let fuel = FuelPriceProvider.fuelType(for: item.key),
|
||||
let date = FuelPriceTimestamp.parse(item.value) else { return }
|
||||
result[fuel] = date.timeIntervalSince1970
|
||||
}
|
||||
return parsed.isEmpty ? nil : parsed
|
||||
}
|
||||
|
||||
/// Map mirror grade keys (E5/E10/DIESEL) to FuelType for ALL fuels the
|
||||
/// station sells — one fetch populates every fuel tab. Defensive guard:
|
||||
|
||||
Reference in New Issue
Block a user