226 lines · 8.4 KB · Swift
RawDownload
import CodexBarCoreimport Foundation /// Shared, lock-guarded ISO8601 formatters for status feeds. Allocating a fresh/// `ISO8601DateFormatter` per decoded date field is a measurable share of decoding the/// Google Workspace incidents feed, which can run to hundreds of kilobytes (#1399).private final class StatusISO8601FormatterBox: @unchecked Sendable {    let lock = NSLock()    let withFractional: ISO8601DateFormatter = {        let fmt = ISO8601DateFormatter()        fmt.formatOptions = [.withInternetDateTime, .withFractionalSeconds]        return fmt    }()     let plain: ISO8601DateFormatter = {        let fmt = ISO8601DateFormatter()        fmt.formatOptions = [.withInternetDateTime]        return fmt    }()} private enum StatusFeedDateParser {    static let box = StatusISO8601FormatterBox()     static func parse(_ text: String) -> Date? {        self.box.lock.lock()        defer { self.box.lock.unlock() }        return self.box.withFractional.date(from: text) ?? self.box.plain.date(from: text)    }     static func decodingStrategy() -> JSONDecoder.DateDecodingStrategy {        .custom { decoder in            let container = try decoder.singleValueContainer()            let raw = try container.decode(String.self)            guard let date = StatusFeedDateParser.parse(raw) else {                throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid ISO8601 date")            }            return date        }    }} extension UsageStore {    /// Status feeds decode off the main actor: the Google Workspace incidents payload alone    /// can be hundreds of kilobytes and cost 150-340ms to decode (#1399), and these helpers    /// touch no store state.    @concurrent    nonisolated static func fetchStatus(        from baseURL: URL,        transport: any ProviderHTTPTransport = ProviderHTTPClient.shared)        async throws -> ProviderStatus    {        let apiURL = baseURL.appendingPathComponent("api/v2/status.json")        var request = URLRequest(url: apiURL)        request.timeoutInterval = 10         let (data, _) = try await transport.data(for: request)         struct Response: Decodable {            struct Status: Decodable {                let indicator: String                let description: String?            }             struct Page: Decodable {                let updatedAt: Date?                 private enum CodingKeys: String, CodingKey {                    case updatedAt = "updated_at"                }            }             let page: Page?            let status: Status        }         let decoder = JSONDecoder()        decoder.dateDecodingStrategy = StatusFeedDateParser.decodingStrategy()         let response = try decoder.decode(Response.self, from: data)        let indicator = ProviderStatusIndicator(rawValue: response.status.indicator) ?? .unknown        return ProviderStatus(            indicator: indicator,            description: response.status.description,            updatedAt: response.page?.updatedAt)    }     @concurrent    nonisolated static func fetchWorkspaceStatus(        productID: String,        transport: any ProviderHTTPTransport = ProviderHTTPClient.shared,        beforeDecoding: (@Sendable () -> Void)? = nil)        async throws -> ProviderStatus    {        guard let url = URL(string: "https://www.google.com/appsstatus/dashboard/incidents.json") else {            throw URLError(.badURL)        }        var request = URLRequest(url: url)        request.timeoutInterval = 10        let (data, _) = try await transport.data(for: request)        beforeDecoding?()        return try Self.parseGoogleWorkspaceStatus(data: data, productID: productID)    }     nonisolated static func parseGoogleWorkspaceStatus(data: Data, productID: String) throws -> ProviderStatus {        let decoder = JSONDecoder()        decoder.keyDecodingStrategy = .convertFromSnakeCase        decoder.dateDecodingStrategy = StatusFeedDateParser.decodingStrategy()         let incidents = try decoder.decode([GoogleWorkspaceIncident].self, from: data)        let active = incidents.filter { $0.isRelevant(productID: productID) && $0.isActive }        guard !active.isEmpty else {            return ProviderStatus(indicator: .none, description: nil, updatedAt: nil)        }         var best: (            indicator: ProviderStatusIndicator,            incident: GoogleWorkspaceIncident,            update: GoogleWorkspaceUpdate?)        best = (indicator: .none, incident: active[0], update: active[0].mostRecentUpdate ?? active[0].updates?.last)         for incident in active {            let update = incident.mostRecentUpdate ?? incident.updates?.last            let indicator = Self.workspaceIndicator(                status: update?.status ?? incident.statusImpact,                severity: incident.severity)            if Self.indicatorRank(indicator) <= Self.indicatorRank(best.indicator) { continue }            best = (indicator: indicator, incident: incident, update: update)        }         let description = Self.workspaceSummary(from: best.update?.text ?? best.incident.externalDesc)        let updatedAt = best.update?.when ?? best.incident.modified ?? best.incident.begin        return ProviderStatus(indicator: best.indicator, description: description, updatedAt: updatedAt)    }     private nonisolated static func indicatorRank(_ indicator: ProviderStatusIndicator) -> Int {        switch indicator {        case .none: 0        case .maintenance: 1        case .minor: 2        case .major: 3        case .critical: 4        case .unknown: 1        }    }     private nonisolated static func workspaceIndicator(status: String?, severity: String?) -> ProviderStatusIndicator {        switch status?.uppercased() {        case "AVAILABLE": return .none        case "SERVICE_INFORMATION": return .minor        case "SERVICE_DISRUPTION": return .major        case "SERVICE_OUTAGE": return .critical        case "SERVICE_MAINTENANCE", "SCHEDULED_MAINTENANCE": return .maintenance        default: break        }         switch severity?.lowercased() {        case "low": return .minor        case "medium": return .major        case "high": return .critical        default: return .minor        }    }     private nonisolated static func workspaceSummary(from text: String?) -> String? {        guard let text else { return nil }        let normalized = text            .replacingOccurrences(of: "\r\n", with: "\n")            .replacingOccurrences(of: "\r", with: "\n")        let lines = normalized.split(separator: "\n", omittingEmptySubsequences: true)        for rawLine in lines {            let trimmed = rawLine.trimmingCharacters(in: .whitespacesAndNewlines)            if trimmed.isEmpty { continue }            let lower = trimmed.lowercased()            if lower.hasPrefix("**summary") || lower.hasPrefix("**description") || lower == "summary" {                continue            }            var cleaned = trimmed.replacingOccurrences(of: "**", with: "")            cleaned = cleaned.replacingOccurrences(                of: #"\[([^\]]+)\]\([^)]+\)"#,                with: "$1",                options: .regularExpression)            if cleaned.hasPrefix("- ") {                cleaned.removeFirst(2)            }            cleaned = cleaned.trimmingCharacters(in: .whitespacesAndNewlines)            if !cleaned.isEmpty { return cleaned }        }        return nil    }     private struct GoogleWorkspaceIncident: Decodable {        let begin: Date?        let end: Date?        let modified: Date?        let externalDesc: String?        let statusImpact: String?        let severity: String?        let affectedProducts: [GoogleWorkspaceProduct]?        let currentlyAffectedProducts: [GoogleWorkspaceProduct]?        let mostRecentUpdate: GoogleWorkspaceUpdate?        let updates: [GoogleWorkspaceUpdate]?         var isActive: Bool {            self.end == nil        }         func isRelevant(productID: String) -> Bool {            if let current = currentlyAffectedProducts {                return current.contains(where: { $0.id == productID })            }            return self.affectedProducts?.contains(where: { $0.id == productID }) ?? false        }    }     private struct GoogleWorkspaceProduct: Decodable {        let title: String?        let id: String    }     private struct GoogleWorkspaceUpdate: Decodable {        let when: Date?        let status: String?        let text: String?    }}