SPB Git

Repository Public

ChartProvenanceView.swift 7.8 KB 120 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Features/Charting/ChartProvenanceView.swift3// Description: Where the charted series comes from: the provenance chip of the chart (kind, provider, as-of, with marks for a cached copy, a fallback provider, the 1.x route, truncation and gaps) and the popover that states everything the data layer said.4import SwiftUI5import VQuantCore6import VQuantKit7import VQuantUI89extension ChartModel {10    /// The series' freshness in the kit's vocabulary. The 1.x route states none: a bar series is historical.11    var vqFreshness: VQFreshness? {12        guard let p = provenance else { return nil }13        let kind = p.freshnessKind.flatMap(VQFreshness.Kind.init(rawValue:)) ?? .historical14        // a daily bar is a session date, not an instant: the chip carries no clock time for it (the date is shown beside it)15        return VQFreshness(kind: kind, asOf: isIntraday ? (p.asOf ?? bars.last?.time) : nil, provider: p.provider, retrievedAt: p.retrievedAt ?? fetchedAt)16    }17}1819struct ChartProvenanceButton: View {20    let chart: ChartModel21    @Binding var isPresented: Bool22    let theme: VQTheme2324    var body: some View {25        Button { isPresented = true } label: {26            HStack(spacing: 5) {27                if let f = chart.vqFreshness, let p = chart.provenance {28                    VQProvenanceChip(f, timeZone: chart.displayZone)29                    if !chart.isIntraday, let last = chart.bars.last {30                        Text("through " + chart.dateText(last.time)).font(VQTokens.Typography.numSm.font).foregroundStyle(VQTokens.Palette.text2).fixedSize()31                    }32                    if chart.fromCache { mark("Cached", VQTokens.Palette.warn, "The server could not be reached: this is the last series this Mac received") }33                    if p.route == .legacy { mark("1.x route", VQTokens.Palette.warn, "The v2 data layer could not serve this series; it comes from the 1.x provider proxy, without provenance details") }34                    if p.fallbackFrom != nil { mark("Fallback", VQTokens.Palette.info, "Served by the fallback provider") }35                    if p.truncated { mark("Truncated", VQTokens.Palette.text2, "More bars matched than were returned") }36                    if let g = p.gaps, g.missingSessions > 0 { mark("\(g.missingSessions) gap\(g.missingSessions == 1 ? "" : "s")", VQTokens.Palette.warn, "Sessions missing between the first and last bar") }37                    Image(systemName: "info.circle").font(.system(size: 11)).foregroundStyle(VQTokens.Palette.text3)38                } else if chart.loading {39                    Text("Loading…").font(VQTokens.Typography.caption.font).foregroundStyle(VQTokens.Palette.text3)40                } else {41                    Text("No series").font(VQTokens.Typography.caption.font).foregroundStyle(VQTokens.Palette.text3)42                }43            }44            .contentShape(Rectangle())45        }46        .buttonStyle(.plain)47        .disabled(chart.provenance == nil)48        .help("Where this series comes from")49        .accessibilityLabel("Data provenance" + (chart.vqFreshness.map { ": " + $0.displayText(.en, timeZone: chart.displayZone) } ?? ""))50        .accessibilityIdentifier("chart.provenance")51        .popover(isPresented: $isPresented, arrowEdge: .bottom) { ChartProvenancePopover(chart: chart).vqTheme(theme) }52    }5354    private func mark(_ text: String, _ color: Color, _ help: String) -> some View {55        Text(text).font(.system(size: 10, weight: .semibold)).foregroundStyle(color)56            .padding(.horizontal, 6).padding(.vertical, 2)57            .overlay(Capsule().strokeBorder(color.opacity(0.55), lineWidth: 1))58            .help(help)59    }60}6162struct ChartProvenancePopover: View {63    let chart: ChartModel6465    var body: some View {66        VStack(alignment: .leading, spacing: 10) {67            VStack(alignment: .leading, spacing: 2) {68                Text("Data provenance").font(VQTokens.Typography.title.font).foregroundStyle(VQTokens.Palette.text1)69                Text("\(chart.symbol) · \(chart.interval.label) bars · \(chart.range.rawValue)").font(VQTokens.Typography.numSm.font).foregroundStyle(VQTokens.Palette.text2)70            }71            if let p = chart.provenance {72                if chart.fromCache, let f = chart.fetchedAt {73                    notice("Offline copy. The server could not be reached; this is the series this Mac received \(Fmt.relative(f)).", VQTokens.Palette.warn)74                }75                if p.route == .legacy {76                    notice("Served by the 1.x provider proxy because \(p.routeReason ?? "the v2 data layer did not answer"). That route states no adjustment, time zone, currency or gap check.", VQTokens.Palette.warn)77                }78                Grid(alignment: .leadingFirstTextBaseline, horizontalSpacing: 14, verticalSpacing: 5) {79                    ForEach(Array(p.rows().enumerated()), id: \.offset) { _, row in80                        GridRow {81                            Text(row.label).font(VQTokens.Typography.caption.font).foregroundStyle(VQTokens.Palette.text3).gridColumnAlignment(.leading)82                            Text(row.value).font(VQTokens.Typography.numSm.font).foregroundStyle(VQTokens.Palette.text1).textSelection(.enabled).fixedSize(horizontal: false, vertical: true)83                        }84                    }85                    GridRow {86                        Text("Retrieved").font(VQTokens.Typography.caption.font).foregroundStyle(VQTokens.Palette.text3)87                        Text(Fmt.dateTime(p.retrievedAt ?? chart.fetchedAt)).font(VQTokens.Typography.numSm.font).foregroundStyle(VQTokens.Palette.text1)88                    }89                }90                if let n = p.gaps?.note, !n.isEmpty { Text(n).font(VQTokens.Typography.caption.font).foregroundStyle(VQTokens.Palette.text2).fixedSize(horizontal: false, vertical: true) }91                if !p.notes.isEmpty {92                    Rectangle().fill(VQTokens.Palette.line).frame(height: 1)93                    VStack(alignment: .leading, spacing: 3) {94                        Text("NOTES").font(VQTokens.Typography.overline.font).tracking(VQTokens.Typography.overline.tracking).foregroundStyle(VQTokens.Palette.text3)95                        ForEach(p.notes, id: \.self) { n in96                            HStack(alignment: .firstTextBaseline, spacing: 6) { Text("·").foregroundStyle(VQTokens.Palette.text3); Text(n).fixedSize(horizontal: false, vertical: true) }97                                .font(VQTokens.Typography.caption.font).foregroundStyle(VQTokens.Palette.text2)98                        }99                    }100                }101                if chart.dropped > 0 { Text("\(chart.dropped) row\(chart.dropped == 1 ? "" : "s") without a close were ignored, not filled in.").font(VQTokens.Typography.caption.font).foregroundStyle(VQTokens.Palette.warn) }102                Text("Indicators and drawings are computed on this Mac from these bars. Nothing here is a streaming feed.").font(VQTokens.Typography.caption.font).foregroundStyle(VQTokens.Palette.text3).fixedSize(horizontal: false, vertical: true)103            }104        }105        .padding(16)106        .frame(width: 400, alignment: .leading)107        .accessibilityElement(children: .contain)108        .accessibilityIdentifier("chart.provenance.popover")109    }110111    private func notice(_ text: String, _ color: Color) -> some View {112        HStack(alignment: .firstTextBaseline, spacing: 6) {113            Image(systemName: "exclamationmark.triangle").font(.system(size: 11)).foregroundStyle(color)114            Text(text).font(VQTokens.Typography.caption.font).foregroundStyle(VQTokens.Palette.text1).fixedSize(horizontal: false, vertical: true)115        }116        .padding(8).frame(maxWidth: .infinity, alignment: .leading)117        .background(color.opacity(0.10), in: RoundedRectangle(cornerRadius: VQTokens.Radius.sm))118    }119}120
Files main
Menu

Theme