SPB Git

Repository Public

MonitorModels.swift 13.3 KB 323 lines swift

1// SP Wallet Core2// File: Sources/SPWalletCore/Domains/Ops/MonitorModels.swift3// Description: Models of the read-only monitoring screen « État du système » (services/api/src/monitor/routes.ts, aggregate.ts,4//              health.ts; services/api/src/capabilities.ts; the quote engine's statistics of quotes/engine.ts).5//              Three kinds of money are NEVER added together: the wallet (real, on chain), agent vaults (real when LIVE,6//              simulated in paper) and derivatives accounts (real on a venue's mainnet, test funds on testnet, simulated on7//              paper). Every row says which it is (`real`); the server's totals are already separated and stay so here.8//              Figures in USD are the server's own rounded estimates (Double); nothing here is an amount to approve.9//10// Author: Simon-Pierre Boucher11// Contact: contact@spboucher.ai12import Foundation1314// MARK: capital1516public struct MonitorCapital: Codable, Sendable, Hashable {17    public struct Agent: Codable, Sendable, Hashable, Identifiable {18        public let id: String19        public let name: String20        public let state: String21        public let network: String22        /** `LIVE` or `PAPER`. */23        public let mode: String24        /** True only for a LIVE agent: real funds. Anything else is simulated. */25        public let real: Bool26        public let capitalUsd: Double27        /** nil when no equity snapshot exists yet — never zero. */28        public let equityUsd: Double?29        public let asOf: Date?30        public let positions: Int31        public let lastBar: Date?32        public let stopReason: String?33        public let mandateExpiresAt: Date?34    }35    public struct Derivative: Codable, Sendable, Hashable, Identifiable {36        public let id: String37        public let label: String38        public let venue: String39        /** `mainnet`, `testnet`… */40        public let network: String41        /** `paper` (simulated) or `venue`. */42        public let kind: String43        /** True only for a venue account on mainnet. A testnet account holds test funds: shown, never counted as capital. */44        public let real: Bool45        public let mode: String46        public let equity: Double?47        public let collateral: Double?48        public let marginUsed: Double?49        public let unrealizedPnl: Double?50        public let asOf: Date?51        public let halt: String?52        public let divergence: String?53        public let positions: Int54        public let openOrders: Int55    }56    /** Five separate sums; there is deliberately no grand total. */57    public struct Totals: Codable, Sendable, Hashable {58        public let agentsLiveUsd: Double59        public let agentsPaperUsd: Double60        public let derivVenueUsd: Double61        public let derivPaperUsd: Double62        public let derivTestnetUsd: Double63    }64    public struct Split: Codable, Sendable, Hashable { public let agents: Int; public let derivatives: Int }65    public struct Counts: Codable, Sendable, Hashable {66        /** Agent state → number of agents. */67        public let agents: [String: Int]68        public let openPositions: Split69        public let openOrders: Split70    }71    /** The on-chain wallet, from the same read as GET /portfolio. `usd`/`cad` are nil when no price is available — never zero. */72    public struct Wallet: Codable, Sendable, Hashable {73        public let usd: Double?74        public let cad: Double?75        public let assets: Int76        /** Non-zero balances without a price: NOT in the total. */77        public let unpriced: Int78        /** Balances whose chain read failed: NOT in the total. */79        public let unavailable: Int80        /** False when the estimate leaves something out. */81        public var isComplete: Bool { usd != nil && unpriced == 0 && unavailable == 0 }82    }83    public let asOf: Date84    public let agents: [Agent]85    public let derivatives: [Derivative]86    public let totals: Totals87    public let counts: Counts88    public let wallet: Wallet89}9091// MARK: PnL9293/** Equity difference between two stored snapshots. A deposit or a withdrawal inside the window is counted as PnL (`note: flows_included`). */94public struct MonitorPnlFigure: Codable, Sendable, Hashable {95    public let abs: Double96    public let pct: Double?97    /** The series starts inside the window: the figure covers less than the window. */98    public let partial: Bool99    public let from: Date100}101102public struct MonitorPnl: Codable, Sendable, Hashable {103    public struct Agent: Codable, Sendable, Hashable, Identifiable {104        public let id: String105        public let name: String106        public let mode: String107        public let real: Bool108        public let state: String109        public let today: MonitorPnlFigure?110        public let d7: MonitorPnlFigure?111        public let d30: MonitorPnlFigure?112        public let equity: Double?113        public let asOf: Date?114    }115    public struct Derivative: Codable, Sendable, Hashable, Identifiable {116        public let id: String117        public let label: String118        public let kind: String119        public let real: Bool120        public let today: MonitorPnlFigure?121        public let d7: MonitorPnlFigure?122        public let d30: MonitorPnlFigure?123        public let equity: Double?124        public let asOf: Date?125    }126    public let asOf: Date127    /** The time zone the owner's day starts in (the quiet-hours zone of the alert settings). */128    public let tz: String129    /** `flows_included`: shown with the figures. */130    public let note: String131    public let agents: [Agent]132    public let derivatives: [Derivative]133}134135// MARK: exposure136137public struct MonitorExposure: Codable, Sendable, Hashable {138    public struct Row: Codable, Sendable, Hashable, Identifiable {139        public let asset: String140        public let spotUsd: Double141        public let perpLongUsd: Double142        public let perpShortUsd: Double143        /** An indication of direction, not a hedge ratio (spot and perpetuals are different instruments). */144        public let netUsd: Double145        public let grossUsd: Double146        /** False when a position of this asset had no price: the row is then incomplete. */147        public let priced: Bool148        public var id: String { asset }149    }150    public let asOf: Date151    /** `spot_and_perp_differ`: shown with the table. */152    public let caveat: String153    public let real: [Row]154    /** Paper accounts, paper agents and testnet accounts. Never merged with `real`. */155    public let simulated: [Row]156}157158// MARK: health159160public struct MonitorConnector: Codable, Sendable, Hashable, Identifiable {161    public let id: String162    /** `signer`, `rpc`, `market`, `venue`, `swap`, `bridge`, `database`. */163    public let group: String164    public let name: String165    /** nil = not measured (nothing in use that would tell) — never shown as healthy. */166    public let ok: Bool?167    public let lastSuccessAt: Date?168    public let detail: String169    public let ageSec: Double?170}171172public struct MonitorLoop: Codable, Sendable, Hashable, Identifiable {173    public let name: String174    public let everyMs: Double?175    public let runs: Int?176    public let lastOkAt: Date?177    public let lastErrorAt: Date?178    public let lastError: String?179    public let failures: Int?180    public let ageSec: Double?181    public let late: Bool182    public var id: String { name }183}184185/** One optional capability of the server and, when it is off, the NAMES of the variables that turn it off (never a value). */186public struct ServerCapability: Codable, Sendable, Hashable, Identifiable {187    public let id: String188    public let on: Bool189    public let missing: [String]190    public let why: String191}192193public struct AuditChainStatus: Codable, Sendable, Hashable {194    public let checkedAt: Date?195    public let total: Int196    public let intact: Bool197    /** Sequence number of the first broken event. */198    public let brokenAt: Int?199}200201public struct SignerReachability: Codable, Sendable, Hashable {202    public let reachable: Bool203    /** nil when the signer cannot be reached: its halt state is unknown. */204    public let halted: Bool?205}206207/** The quote engine as the health answer summarizes it (`QuoteEngine.summary()`). */208public struct QuoteEngineSummary: Codable, Sendable, Hashable {209    public struct Lag: Codable, Sendable, Hashable { public let samples: Int; public let p50: Double?; public let p95: Double? }210    public struct Venue: Codable, Sendable, Hashable, Identifiable {211        public let venue: String212        public let status: StreamVenueState213        public let ageMs: Double?214        public let transport: String215        public let feeds: Int216        public let reconnects: Int217        public let gaps: Int218        public var id: String { venue }219    }220    public let connections: Int221    public let subscriptions: Int222    public let messagesPerSec: Double223    public let conflated: Int224    public let tradesDropped: Int225    public let upstreamReconnects: Int226    /** State of the keyed, metered market-data sources (budget counters); provider-specific. */227    public let metered: JSONValue?228    public let lagMs: Lag229    public let venues: [Venue]230}231232public struct MonitorHealth: Codable, Sendable, Hashable {233    public struct MarketData: Codable, Sendable, Hashable {234        /** `{ provider, configured, needsKey, missing, … }` plus the daily credit counter when the key is set; provider-specific. */235        public let apibricks: JSONValue?236    }237    public struct Backup: Codable, Sendable, Hashable {238        /** False when the API cannot see the backup directory: nothing is known, which is not "no backup". */239        public let visible: Bool240        public let lastAt: Date?241        public let files: Int242        public let ageHours: Double?243    }244    public struct Disk: Codable, Sendable, Hashable { public let path: String; public let freePct: Double; public let freeGb: Double; public let totalGb: Double }245    public struct Versions: Codable, Sendable, Hashable {246        public let apiCommit: String?247        public let apiVersion: String248        public let node: String249        public let startedAt: Date?250        public let signerKeyId: String?251        public let signerPolicyVersion: Int?252    }253    /** The OS sandbox every strategy process runs in. `ok == false`: signals and backtests do not run (agents hold). */254    public struct Lab: Codable, Sendable, Hashable {255        /** `sandbox-exec` (confined), `off` (development switch: NOT confined), `unavailable` (nothing runs). */256        public let mode: String257        public let ok: Bool258        public let reason: String?259        public let checkedAt: Date?260        public let lastFailureAt: Date?261        public let lastFailure: String?262        public let calls: Int?263        public let refused: Int?264    }265    public let asOf: Date266    public let connectors: [MonitorConnector]267    public let loops: [MonitorLoop]268    /** nil before the quote engine exists in the server process. */269    public let quotes: QuoteEngineSummary?270    public let marketData: MarketData?271    public let backup: Backup272    /** nil when the server could not read its disk. */273    public let disk: Disk?274    public let audit: AuditChainStatus275    public let signer: SignerReachability276    public let versions: Versions277    public let capabilities: [ServerCapability]?278    public let lab: Lab?279280    public func capability(_ id: String) -> ServerCapability? { capabilities?.first { $0.id == id } }281}282283// MARK: quote engine statistics (GET /stream/stats)284285public struct QuoteStreamStats: Codable, Sendable, Hashable {286    public struct Connections: Codable, Sendable, Hashable { public let open: Int; public let byPrincipal: [String: Int]; public let total: Int; public let refused: Int }287    public struct Backpressure: Codable, Sendable, Hashable { public let queuedNow: Int; public let conflated: Int; public let tradesDropped: Int; public let bookResyncs: Int; public let slowConsumersClosed: Int }288    public struct Clients: Codable, Sendable, Hashable { public let deadPeers: Int; public let badFrames: Int; public let rateLimited: Int; public let restricted: Int; public let unauthorizedClosed: Int }289    public struct Tickets: Codable, Sendable, Hashable { public let issued: Int; public let used: Int; public let refused: Int; public let pending: Int }290    /** `StreamLimits` of the server. */291    public struct Limits: Codable, Sendable, Hashable {292        public let maxConnectionsOwner: Int293        public let maxConnectionsPerKey: Int294        public let maxConnectionsTotal: Int295        public let maxSubscriptions: Int296        public let maxFrameBytes: Int297        public let maxMessagesPerSec: Int298        public let idleTimeoutMs: Int299        public let heartbeatMs: Int300        public let recheckMs: Int301        public let ticketTtlMs: Int302        public let lowWaterBytes: Int303        public let highWaterBytes: Int304        public let overMs: Int305    }306    public let ts: Date307    public let `protocol`: Int308    public let connections: Connections309    public let subscriptions: Int310    public let messagesPerSec: Double311    public let messagesSent: Int312    public let bytesSent: Int313    public let backpressure: Backpressure314    public let clients: Clients315    public let tickets: Tickets316    public let upstreamReconnects: Int317    public let lagMs: QuoteEngineSummary.Lag318    public let limits: Limits319    /** Channels, feeds, counters and per-venue upstream state of the hub (`QuoteHub.stats()`); diagnostic, kept as served. */320    public let hub: JSONValue321    public let metered: JSONValue?322}323
Files main
Menu

Theme