Repository Public
1import Foundation23/// What the repositories need to talk to the server: reads go straight to the API; mutations go through the4/// step-up helper, because only the server knows whether a recent passkey verification is required.5public struct Gateway: Sendable {6 public let api: APIClient7 public let auth: AuthService8 public let cache: OfflineCache?910 public init(api: APIClient, auth: AuthService, cache: OfflineCache?) {11 self.api = api; self.auth = auth; self.cache = cache12 }1314 public func read<T: Sendable>(_ operation: @Sendable (Client) async throws -> T) async throws -> T {15 try await api.call(operation)16 }1718 /// Runs a mutation; on `403 step_up_required` performs the passkey ceremony and retries once.19 public func mutate<T: Sendable>(_ operation: @escaping @Sendable (Client) async throws -> T) async throws -> T {20 try await auth.withStepUp { [api] in try await api.call(operation) }21 }2223 /// Follows `next_cursor` until the last page (bounded: `maxPages` × 200 items).24 public func collect<Item: Sendable>(maxPages: Int = 10,25 _ page: @Sendable (Client, String?) async throws -> (items: [Item], next: String?)) async throws -> [Item] {26 var out: [Item] = []27 var cursor: String?28 for _ in 0..<maxPages {29 let c = cursor30 let (items, next) = try await api.call { try await page($0, c) }31 out.append(contentsOf: items)32 guard let next, !next.isEmpty else { break }33 cursor = next34 }35 return out36 }3738 @MainActor39 func resource<V: Codable & Sendable>(_ key: String?, _ loader: @escaping @Sendable (Gateway) async throws -> V) -> Resource<V> {40 Resource(cache: cache, cacheKey: key) { [self] in try await loader(self) }41 }42}4344public extension API.TimeRange {45 var seconds: TimeInterval {46 switch self {47 case ._1h: 360048 case ._24h: 86_40049 case ._7d: 7 * 86_40050 case ._30d: 30 * 86_40051 }52 }53}5455public struct MetricQuery: Hashable, Sendable {56 public var nodeID: String57 public var metrics: [String]58 public var range: API.TimeRange59 public var label: String?60 public init(nodeID: String, metrics: [String], range: API.TimeRange, label: String? = nil) {61 self.nodeID = nodeID; self.metrics = metrics; self.range = range; self.label = label62 }63}6465public struct CompareQuery: Hashable, Sendable {66 public var nodeIDs: [String]67 public var metric: String68 public var range: API.TimeRange69 public var label: String?70 public init(nodeIDs: [String], metric: String, range: API.TimeRange, label: String? = nil) {71 self.nodeIDs = nodeIDs; self.metric = metric; self.range = range; self.label = label72 }73}7475public struct TrendQuery: Hashable, Sendable {76 public var metric: String77 public var range: API.TimeRange78 public var groupBy: API.TrendGroupBy79 public init(metric: String, range: API.TimeRange, groupBy: API.TrendGroupBy = .cluster) {80 self.metric = metric; self.range = range; self.groupBy = groupBy81 }82}8384public struct HealthQuery: Hashable, Sendable {85 public var nodeID: String86 public var range: API.TimeRange87 public init(nodeID: String, range: API.TimeRange) { self.nodeID = nodeID; self.range = range }88}8990/// A calendar interval, aligned by the caller (month grid, week...).91public struct DateWindow: Hashable, Sendable {92 public var from: Date93 public var to: Date94 public var nodeID: String?95 public init(from: Date, to: Date, nodeID: String? = nil) { self.from = from; self.to = to; self.nodeID = nodeID }96 var cacheKey: String { "maintenance.\(Int(from.timeIntervalSince1970))-\(Int(to.timeIntervalSince1970)).\(nodeID ?? "all")" }97}9899public struct NotesQuery: Hashable, Sendable {100 public var text: String?101 public var windowID: String?102 public var nodeID: String?103 public var occurrence: Date?104 public init(text: String? = nil, windowID: String? = nil, nodeID: String? = nil, occurrence: Date? = nil) {105 self.text = text; self.windowID = windowID; self.nodeID = nodeID; self.occurrence = occurrence106 }107}108109public struct AuditFilter: Hashable, Sendable {110 public var actorID: String?111 public var action: String?112 public var targetKind: String?113 public var targetID: String?114 public var outcome: API.AuditOutcome?115 public var from: Date?116 public var to: Date?117 public var correlationID: String?118 public init() {}119}120121public struct ActionFilter: Hashable, Sendable {122 public var state: API.ActionState?123 public var type: String?124 public var criticality: API.Criticality?125 public var nodeID: String?126 public init() {}127}128