Graphley / app · graphley.ai / Sources / OwnerMetricsKit / OwnerMetrics.swift
| 1 | import Foundation |
| 2 | |
| 3 | public enum MetricKind: String, Codable, CaseIterable, Identifiable, Sendable { |
| 4 | case website, app |
| 5 | public var id: String { rawValue } |
| 6 | public var title: String { self == .website ? "Websites" : "Apps" } |
| 7 | public var countLabel: String { self == .website ? "Site visits" : "App counts" } |
| 8 | } |
| 9 | |
| 10 | public enum MetricWindow: Int, CaseIterable, Identifiable, Sendable { |
| 11 | case week = 7, month = 30, quarter = 90 |
| 12 | public var id: Int { rawValue } |
| 13 | public var title: String { "\(rawValue) days" } |
| 14 | } |
| 15 | |
| 16 | public struct DailyCount: Identifiable, Sendable, Equatable { |
| 17 | public let day: String |
| 18 | public let count: Int64 |
| 19 | public var id: String { day } |
| 20 | } |
| 21 | |
| 22 | public struct PropertyCount: Identifiable, Sendable, Equatable { |
| 23 | public let id: String |
| 24 | public let count: Int64 |
| 25 | public var title: String { |
| 26 | switch id { |
| 27 | case "projectatom": "Project ATOM" |
| 28 | case "protomota": "Protomota" |
| 29 | case "airesourcepro": "AI Resource Pro" |
| 30 | case "skillfarm": "Skill Farm" |
| 31 | case "portfolio": "Portfolio" |
| 32 | case "opentalon": "OpenTalon" |
| 33 | case "inkspring": "InkSpring" |
| 34 | case "jotley": "Jotley" |
| 35 | case "glimmer": "Glimmer" |
| 36 | case "pulsecanvas": "PulseCanvas" |
| 37 | case "graphley": "Graphley" |
| 38 | case "bucksley": "Bucksley" |
| 39 | case "jobley": "Jobley" |
| 40 | case "voxley": "Voxley" |
| 41 | case "textley": "Textley" |
| 42 | default: id |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public struct OwnerMetricsSnapshot: Sendable { |
| 48 | struct Payload: Decodable { |
| 49 | let since: String |
| 50 | let through: String |
| 51 | let rows: [Row] |
| 52 | } |
| 53 | struct Row: Decodable { |
| 54 | let kind: MetricKind |
| 55 | let id: String |
| 56 | let day: String |
| 57 | let count: Int64 |
| 58 | } |
| 59 | |
| 60 | public let since: String |
| 61 | public let through: String |
| 62 | private let days: [String] |
| 63 | private let rows: [Row] |
| 64 | |
| 65 | static func validated(_ data: Data, window: MetricWindow) throws -> Self { |
| 66 | let payload: Payload |
| 67 | do { payload = try JSONDecoder().decode(Payload.self, from: data) } |
| 68 | catch { throw MetricsError.invalidResponse } |
| 69 | |
| 70 | let iso = DateFormatter() |
| 71 | var calendar = Calendar(identifier: .gregorian) |
| 72 | let utc = TimeZone(secondsFromGMT: 0)! |
| 73 | calendar.timeZone = utc |
| 74 | iso.calendar = calendar |
| 75 | iso.timeZone = utc |
| 76 | iso.locale = Locale(identifier: "en_US_POSIX") |
| 77 | iso.dateFormat = "yyyy-MM-dd" |
| 78 | iso.isLenient = false |
| 79 | guard let start = iso.date(from: payload.since), iso.string(from: start) == payload.since, |
| 80 | let end = iso.date(from: payload.through), iso.string(from: end) == payload.through, |
| 81 | let length = calendar.dateComponents([.day], from: start, to: end).day, length + 1 == window.rawValue, |
| 82 | payload.rows.count <= window.rawValue * 64 else { throw MetricsError.invalidResponse } |
| 83 | |
| 84 | let days = (0...length).map { iso.string(from: calendar.date(byAdding: .day, value: $0, to: start)!) } |
| 85 | let allowedDays = Set(days) |
| 86 | var seen = Set<String>() |
| 87 | var summed: Int64 = 0 |
| 88 | for row in payload.rows { |
| 89 | let (next, overflow) = summed.addingReportingOverflow(row.count) |
| 90 | guard !overflow, row.count >= 0, allowedDays.contains(row.day), |
| 91 | row.id.range(of: #"\A[a-z0-9][a-z0-9_-]{0,63}\z"#, options: .regularExpression) != nil, |
| 92 | seen.insert("\(row.kind.rawValue)/\(row.id)/\(row.day)").inserted else { throw MetricsError.invalidResponse } |
| 93 | summed = next |
| 94 | } |
| 95 | return Self(since: payload.since, through: payload.through, days: days, rows: payload.rows) |
| 96 | } |
| 97 | |
| 98 | public func hasData(_ kind: MetricKind) -> Bool { rows.contains { $0.kind == kind } } |
| 99 | public func total(_ kind: MetricKind) -> Int64 { rows.lazy.filter { $0.kind == kind }.reduce(0) { $0 + $1.count } } |
| 100 | |
| 101 | public func daily(_ kind: MetricKind) -> [DailyCount] { |
| 102 | var counts: [String: Int64] = [:] |
| 103 | for row in rows where row.kind == kind { counts[row.day, default: 0] += row.count } |
| 104 | return days.map { DailyCount(day: $0, count: counts[$0] ?? 0) } |
| 105 | } |
| 106 | |
| 107 | public func properties(_ kind: MetricKind) -> [PropertyCount] { |
| 108 | var counts: [String: Int64] = [:] |
| 109 | for row in rows where row.kind == kind { counts[row.id, default: 0] += row.count } |
| 110 | return counts.map { PropertyCount(id: $0.key, count: $0.value) }.sorted { |
| 111 | $0.count == $1.count ? $0.id < $1.id : $0.count > $1.count |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | public enum MetricsError: LocalizedError, Equatable, Sendable { |
| 117 | case unauthorizedOrDisabled, unavailable, invalidResponse, keychain |
| 118 | case passkeyRejected, setupLinkInvalid, passkeyAlreadyAdded, lastPasskey |
| 119 | public var errorDescription: String? { |
| 120 | switch self { |
| 121 | case .unauthorizedOrDisabled: "Sign in with your passkey to see counts." |
| 122 | case .unavailable: "Project ATOM could not provide counts. Try again later." |
| 123 | case .invalidResponse: "Project ATOM returned an unexpected response." |
| 124 | case .keychain: "Graphley could not access this device’s Keychain." |
| 125 | case .passkeyRejected: "Project ATOM did not accept that passkey." |
| 126 | case .setupLinkInvalid: "This setup link was already used or has expired. Ask for a new one." |
| 127 | case .passkeyAlreadyAdded: "That passkey is already added." |
| 128 | case .lastPasskey: "Add another passkey before removing your last one." |
| 129 | } |
| 130 | } |
| 131 | } |