| 1 | | import Foundation |
| 2 | | |
| 3 | | /// Coverage over time, including gaps for missing metrics and runs without data. |
| 4 | | public struct CoverageTrend: Sendable { |
| 5 | | private let runs: [RunTotals] |
| 6 | | private static let padding = 4.0 |
| 7 | | private static let sparklineWidth = 120.0 |
| 8 | | private static let sparklineHeight = 32.0 |
| 9 | | private static let chartWidth = 640.0 |
| 10 | | private static let chartHeight = 160.0 |
| 11 | | private static let pointRadius = 2 |
| 12 | | private static let unavailableRadius = 3 |
| 13 | | /// The least share of coverage the vertical axis spans, so a 0.1-point change isn't |
| 14 | | /// drawn as a cliff. |
| 15 | | private static let minimumSpan = 0.035 |
| 16 | | |
| 17 | | /// Accepts runs in any order; points are ordered by date, then commit. |
| 18 | 66 | public init(runs: [RunTotals]) { |
| 19 | 66 | self.runs = runs.sorted { ($0.date, $0.commit ?? "") < ($1.date, $1.commit ?? "") } |
| 20 | 66 | } |
| 21 | | |
| 22 | | /// A compact line-only chart; empty when no runs were supplied. |
| 23 | 48 | public var sparkline: String { |
| 24 | 48 | svg( |
| 25 | 48 | width: Self.sparklineWidth, height: Self.sparklineHeight, |
| 26 | 48 | metrics: [("lines", { $0.lines })]) |
| 27 | 48 | } |
| 28 | | |
| 29 | | /// Lines and any recorded branch/condition series, with no-data markers. |
| 30 | 22 | public var chart: String { |
| 31 | 22 | svg( |
| 32 | 22 | width: Self.chartWidth, height: Self.chartHeight, |
| 33 | 22 | metrics: [ |
| 34 | 36 | ("lines", { $0.lines }), ("branches", { $0.branches }), |
| 35 | 36 | ("conditions", { $0.conditions }), |
| 36 | 22 | ]) |
| 37 | 22 | } |
| 38 | | |
| 39 | | /// The vertical axis as text, for example `89.8%–99.9%`; nil when nothing is plotted. |
| 40 | | /// The axis fits the plotted values rather than starting at 0%, so readers need it. |
| 41 | 17 | public var scaleText: String? { |
| 42 | 17 | guard let axis else { return nil } |
| 43 | 13 | return "\(Self.percent(axis.lowerBound))–\(Self.percent(axis.upperBound))" |
| 44 | 17 | } |
| 45 | | |
| 46 | | /// Fractions covered that every series plots, widened to the minimum span and kept |
| 47 | | /// inside 0...1. |
| 48 | 60 | private var axis: ClosedRange<Double>? { |
| 49 | 60 | var fractions: [Double] = [] |
| 50 | 93 | for run in runs where run.unavailable == nil { |
| 51 | 279 | for count in [run.lines, run.branches, run.conditions] { |
| 52 | 279 | guard let count, count.total > 0 else { continue } |
| 53 | 103 | fractions.append(Double(count.covered) / Double(count.total)) |
| 54 | 103 | } |
| 55 | 93 | } |
| 56 | 60 | guard let low = fractions.min(), let high = fractions.max() else { return nil } |
| 57 | 52 | let extra = max(0, Self.minimumSpan - (high - low)) / 2 |
| 58 | 52 | let upper = min(1, high + extra) |
| 59 | 52 | let lower = max(0, min(low - extra, upper - Self.minimumSpan)) |
| 60 | 52 | return lower...upper |
| 61 | 60 | } |
| 62 | | |
| 63 | | private func svg( |
| 64 | | width: Double, height: Double, metrics: [(String, (RunTotals) -> RunTotals.Lines?)] |
| 65 | 70 | ) -> String { |
| 66 | 70 | guard !runs.isEmpty else { return "" } |
| 67 | 43 | let plotWidth = width - Self.padding * 2 |
| 68 | 43 | let plotHeight = height - Self.padding * 2 |
| 69 | 75 | let x = { (index: Int) in |
| 70 | 75 | Self.padding |
| 71 | 75 | + (self.runs.count == 1 |
| 72 | 75 | ? plotWidth / 2 : Double(index) / Double(self.runs.count - 1) * plotWidth) |
| 73 | 75 | } |
| 74 | 43 | let axis = self.axis ?? 0...1 |
| 75 | 85 | let series = metrics.map { name, metric in |
| 76 | 85 | var segments: [[String]] = [] |
| 77 | 85 | var points: [String] = [] |
| 78 | 154 | for (index, run) in runs.enumerated() { |
| 79 | 154 | if run.unavailable == nil, let count = metric(run), count.total > 0 { |
| 80 | 69 | let percentage = Double(count.covered) / Double(count.total) |
| 81 | 69 | points.append( |
| 82 | 69 | "\(Self.number(x(index))),\(Self.number(Self.padding + (axis.upperBound - percentage) / (axis.upperBound - axis.lowerBound) * plotHeight))" |
| 83 | 69 | ) |
| 84 | 85 | } else if !points.isEmpty { |
| 85 | 6 | segments.append(points) |
| 86 | 6 | points = [] |
| 87 | 6 | } |
| 88 | 154 | } |
| 89 | 85 | if !points.isEmpty { segments.append(points) } |
| 90 | 85 | return segments.map { segment in |
| 91 | 51 | if segment.count == 1, let point = segment.first { |
| 92 | 39 | let coordinates = point.split(separator: ",") |
| 93 | 39 | return |
| 94 | 39 | #"<circle class="trend-\#(name)" cx="\#(coordinates[0])" cy="\#(coordinates[1])" r="\#(Self.pointRadius)"/>"# |
| 95 | 39 | } |
| 96 | 12 | return |
| 97 | 12 | #"<polyline class="trend-\#(name)" points="\#(segment.joined(separator: " "))"/>"# |
| 98 | 51 | }.joined() |
| 99 | 85 | }.joined() |
| 100 | 70 | let missing = runs.enumerated().filter { $0.element.unavailable != nil }.map { index, _ in |
| 101 | 6 | #"<circle class="trend-unavailable" cx="\#(Self.number(x(index)))" cy="\#(Self.number(height - Self.padding))" r="\#(Self.unavailableRadius)"><title>No coverage data</title></circle>"# |
| 102 | 6 | }.joined() |
| 103 | 43 | return |
| 104 | 43 | #"<svg class="coverage-trend" viewBox="0 0 \#(Self.number(width)) \#(Self.number(height))" role="img" aria-label="Coverage history"><title>Coverage history</title>\#(series)\#(missing)</svg>"# |
| 105 | 70 | } |
| 106 | | |
| 107 | 26 | private static func percent(_ fraction: Double) -> String { |
| 108 | 26 | String(format: "%.1f%%", locale: Locale(identifier: "en_US_POSIX"), fraction * 100) |
| 109 | 26 | } |
| 110 | | |
| 111 | 236 | private static func number(_ value: Double) -> String { |
| 112 | 236 | String(format: "%.1f", locale: Locale(identifier: "en_US_POSIX"), value) |
| 113 | 236 | } |
| 114 | | } |