Cotton code
119 lines · 6.0 KB · SwiftRaw
// The serving Mac's off-Mac backup, as one notice on the board.//// WHY: the owner works from the phone, and the Mac's copy to its bucket was// dead for two days with nothing on the phone saying so. So a Mac whose backup// FAILED, or fell BEHIND past its own lag allowance, is one notice in the// board's notices part — on Home and on the database page — until the Mac says// it caught up.//// What is judged where://   * failing or not is the MAC's judgement: the relay's status is `failed`, or//     `behind` once it has not caught up for longer than the Mac's//     `COTTON_BACKUP_LAG_MINUTES` (`cotton_client::backup::lag`), and the core//     maps both to `failing` (`methods/host_settings.rs` `backup_section`). No//     second lag constant lives on this side to drift from the Mac's.//   * since when, the same: the Mac's `since` (when it fell behind, or the//     last sync it saw before a failure).//   * here: ONE notice per Mac however many of its databases fail, its sentence,//     and that silence changes nothing — a phone that cannot reach the Mac//     keeps what the Mac last said rather than inventing "not backed up".//   * ON HOME ONLY PAST A DAY (owner, 11 Oct 2026): a Mac a few minutes//     behind is the stream catching up, not news — the card said "not backed//     up since 1:48 am" half an hour later and pushed the board down. So it is//     raised only when the last copy that got through (`lastAt`, the backup's//     last-applied time) is more than `staleAfter` old, or none ever did; the//     Mac's own state lives on its page either way.//   * the rule is "the owner's databases are not being backed up off the Mac",//     so HALF A CONFIGURATION (a bucket named, its settings not adding up —//     the relay's `invalid`) raises it too, with its own sentence. Starting is//     not raised (the first copy is on its way), nor is no bucket named at all//     (`off`: a Mac set up to hold the only copy).import CottonKitimport CottonValuesimport Foundation/// One Mac's backup that is not getting through.public struct BackupNotice: Equatable, Sendable {    /// The Mac that said it, by device id — its page is where the notice opens.    public let deviceId: String    /// The Mac's own name off the device rows; nil when the row is not here.    public let macName: String?    /// Since when nothing got through (ISO-8601); nil when the Mac does not know.    public let since: String?    /// The Mac's own sentence for why — kept for the Mac's page, never said    /// on Home: a script path or a relay's error is no news to a person.    public let reason: String?    /// The last copy that got through (ISO-8601); nil when none ever did.    public var lastAt: String? = nil    /// Why nothing is copied: the copy stopped getting through, or the    /// backup is only half set up.    public var kind: Kind = .failing    /// The backup's one line, resolved by the core (`records::backup`) — the    /// same Feedback and the Mac's page say; nil when the core said none.    public var line: String? = nil    public enum Kind: Equatable, Sendable {        case failing        case halfSetUp    }    /// What one `read.servingBackup` answer means for the board.    public enum Reading: Equatable, Sendable {        /// A Mac answered: its notice, or nil when its backup is getting through.        case answered(BackupNotice?)        /// No Mac answered — nothing new to say either way.        case unanswered    }    /// Read one answer. Only an answer a Mac gave (`this_mac`, `serving_mac`)    /// says anything; a database of that Mac's failing is enough, and however    /// many fail it is still the one Mac's notice.    public static func read(_ answer: JSONValue, now: Date = Date()) -> Reading {        guard let answered = DeviceBackup.answered(answer),              let deviceId = answer.untrimmed("deviceId"), !deviceId.isEmpty        else { return .unanswered }        let whole = answered.backup        if let last = whole.lastAt.flatMap({ CottonDate.parse($0) }), now.timeIntervalSince(last) < staleAfter {            return .answered(nil)        }        let databases = (answer["backup"]?.list("databases") ?? []).compactMap { DeviceBackup(line: $0) }        let name = answer.text("deviceName")        let failing = databases.filter { $0.state == .failing }        guard whole.state == .failing || !failing.isEmpty else {            guard whole.isHalfSetUp else { return .answered(nil) }            return .answered(BackupNotice(deviceId: deviceId, macName: name, since: nil, reason: whole.lastError, kind: .halfSetUp,                                          line: answer.text("backupLine")))        }        let since = whole.since ?? failing.compactMap(\.since).min { earlier($0, $1) }        let reason = (whole.state == .failing ? whole.lastError : nil) ?? failing.lazy.compactMap(\.lastError).first        return .answered(BackupNotice(deviceId: deviceId, macName: name, since: since, reason: reason, lastAt: whole.lastAt,                                      line: answer.text("backupLine")))    }    /// How old the last copy that got through must be before Home says so.    public static let staleAfter: TimeInterval = 24 * 60 * 60    private static func earlier(_ one: String, _ other: String) -> Bool {        switch (CottonDate.parse(one), CottonDate.parse(other)) {        case let (a?, b?): return a < b        case (.some, nil): return true        default: return false        }    }}/// The notice's label over its sentence.enum BackupNoticeWords {    static var label: String { ShellLines.line("board", "backup.label") }}/// The backup receipt's words, as the board's `notices` part sets them/// (`backup_behind` with `{device}` and `{behind}`, `backup_unset`,/// `backup_open`; resolved by the core, `board::default_of`). The core fills/// the line (`records::backup`); the app draws it and the button's word.public struct BackupWords: Hashable, Sendable, Codable {    public let behind: String    public let unset: String    public let open: String}