| 1 | | import Foundation |
| 2 | | |
| 3 | | /// One published run: the folder it sits in (the commit's short hash) and its numbers. |
| 4 | | public struct HistoryRun: Sendable, Equatable { |
| 5 | | public let folder: String |
| 6 | | public let totals: RunTotals |
| 7 | | |
| 8 | 42 | public init(folder: String, totals: RunTotals) { |
| 9 | 42 | self.folder = folder |
| 10 | 42 | self.totals = totals |
| 11 | 42 | } |
| 12 | | } |
| 13 | | |
| 14 | | public enum HistoryPageError: Error, Equatable { |
| 15 | | case noRuns |
| 16 | | } |
| 17 | | |
| 18 | | /// Coverage history above the published runs (DESIGN.md D7), newest first. |
| 19 | | public struct HistoryPage: Sendable { |
| 20 | | /// The stylesheet's name; it sits beside the page. |
| 21 | | public static let stylesheetPath = SiteStylesheet.fileName |
| 22 | | |
| 23 | | /// - Parameters: |
| 24 | | /// - title: the project's name, shown as given. |
| 25 | | /// - runs: in any order; each run's report is `<folder>/index.html` beside the page. |
| 26 | | /// - Throws: `HistoryPageError.noRuns` when `runs` is empty, since there is nothing |
| 27 | | /// to display. |
| 28 | 29 | public init(title: String, runs: [HistoryRun]) throws { |
| 29 | 29 | guard !runs.isEmpty else { throw HistoryPageError.noRuns } |
| 30 | 28 | self.title = title |
| 31 | 46 | self.newestFirst = runs.sorted { |
| 32 | 46 | ($0.totals.date, $1.folder) > ($1.totals.date, $0.folder) |
| 33 | 46 | } |
| 34 | 28 | } |
| 35 | | |
| 36 | | private let title: String |
| 37 | | private let newestFirst: [HistoryRun] |
| 38 | | |
| 39 | | /// The newest run, including unavailable runs, for the site root redirect. |
| 40 | 0 | public var newestRunFolder: String { newestFirst[0].folder } |
| 41 | | |
| 42 | 28 | public var html: String { |
| 43 | 28 | let latest = newestFirst[0] |
| 44 | 28 | let header = SiteHeader( |
| 45 | 28 | title: title, currentTab: .history, hasTestsPage: latest.totals.hasTestsPage == true, |
| 46 | 28 | rootPath: String(Self.reportLink(latest).dropLast(IndexPage.sitePath.count)), |
| 47 | 28 | run: ReportHeader(historyPath: "../index.html")) |
| 48 | 57 | let regions = newestFirst.contains { $0.totals.regions != nil } |
| 49 | 59 | let branches = newestFirst.contains { $0.totals.branches != nil } |
| 50 | 59 | let conditions = newestFirst.contains { $0.totals.conditions != nil } |
| 51 | 28 | let columns = Columns(regions: regions, branches: branches, conditions: conditions) |
| 52 | 59 | let trend = CoverageTrend(runs: newestFirst.map(\.totals)) |
| 53 | 28 | return |
| 54 | 28 | """ |
| 55 | 28 | <!doctype html> |
| 56 | 28 | <html lang="en"> |
| 57 | 28 | <head> |
| 58 | 28 | <meta charset="utf-8"> |
| 59 | 28 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 60 | 28 | <title>History · \(escapingHTML(title))</title> |
| 61 | 28 | <link rel="stylesheet" href="\(Self.stylesheetPath)"> |
| 62 | 28 | </head> |
| 63 | 28 | <body class="history-page"> |
| 64 | 28 | <header class="page-header"> |
| 65 | 28 | \(header.html) |
| 66 | 28 | </header> |
| 67 | 28 | <main> |
| 68 | 28 | <section class="history-chart" aria-label="Coverage trend"> |
| 69 | 28 | \(trend.chart) |
| 70 | 28 | \(Self.legendMarkup(columns)) |
| 71 | 28 | </section> |
| 72 | 28 | <table class="history"> |
| 73 | 28 | <thead><tr><th>Commit</th><th>Message</th><th>Lines</th>\(regions ? #"<th class="regions">Regions</th>"# : "")\(branches ? "<th>Branches</th>" : "")\(conditions ? "<th>Conditions</th>" : "")<th class="date">Date</th></tr></thead> |
| 74 | 28 | <tbody> |
| 75 | 28 | \(rows(columns)) |
| 76 | 28 | </tbody> |
| 77 | 28 | </table> |
| 78 | 28 | </main> |
| 79 | 28 | <script>\(Self.localDateScript)</script> |
| 80 | 28 | <script>\(ChartTooltip.script)</script> |
| 81 | 28 | </body> |
| 82 | 28 | </html> |
| 83 | 28 | |
| 84 | 28 | """ |
| 85 | 28 | } |
| 86 | | |
| 87 | | /// Keys each recorded series, and the no-data marker, to its colour in the chart. A |
| 88 | | /// series' key is a checkbox, which the stylesheet reads to hide it without a script. |
| 89 | 28 | private static func legendMarkup(_ columns: Columns) -> String { |
| 90 | 28 | var series = [("lines", "Lines")] |
| 91 | 28 | if columns.regions { series.append(("regions", "Regions")) } |
| 92 | 28 | if columns.branches { series.append(("branches", "Branches")) } |
| 93 | 28 | if columns.conditions { series.append(("conditions", "Conditions")) } |
| 94 | 34 | let toggles = series.map { name, label in |
| 95 | 34 | #"<li><label><input type="checkbox" data-toggle="\#(name)" checked>"# |
| 96 | 34 | + #"<span class="swatch trend-\#(name)-swatch"></span>\#(label)</label></li>"# |
| 97 | 34 | } |
| 98 | 28 | let noData = #"<li><span class="swatch no-data-swatch"></span>Grey marks: no data</li>"# |
| 99 | 28 | return #"<ul class="trend-legend">\#(toggles.joined())\#(noData)</ul>"# |
| 100 | 28 | } |
| 101 | | |
| 102 | | /// Relative to the page, with the folder's name encoded so any name stays one link. |
| 103 | 87 | private static func reportLink(_ run: HistoryRun) -> String { |
| 104 | 87 | let folder = run.folder.addingPercentEncoding(withAllowedCharacters: folderCharacters) |
| 105 | 87 | return "\(folder ?? run.folder)/\(IndexPage.sitePath)" |
| 106 | 87 | } |
| 107 | | |
| 108 | | /// Unreserved characters only: `#`, `?`, `%` and spaces in a name must not end the path. |
| 109 | | private static let folderCharacters = CharacterSet( |
| 110 | | charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~") |
| 111 | | |
| 112 | | /// The optional metrics any run recorded, which the table and legend show. |
| 113 | | private struct Columns { |
| 114 | | let regions: Bool |
| 115 | | let branches: Bool |
| 116 | | let conditions: Bool |
| 117 | | } |
| 118 | | |
| 119 | 28 | private func rows(_ columns: Columns) -> String { |
| 120 | 59 | newestFirst.enumerated().map { index, run in |
| 121 | 59 | let previous = newestFirst.dropFirst(index + 1).first { $0.totals.unavailable == nil } |
| 122 | 59 | return rowMarkup(run, previous: previous?.totals, columns: columns) |
| 123 | 59 | }.joined(separator: "\n") |
| 124 | 28 | } |
| 125 | | |
| 126 | 59 | private func rowMarkup(_ run: HistoryRun, previous: RunTotals?, columns: Columns) -> String { |
| 127 | 59 | let totals = run.totals |
| 128 | 59 | let subject = escapingHTML(totals.subject ?? "") |
| 129 | 59 | var cells = |
| 130 | 59 | #"<td class="commit"><a href="\#(escapingHTML(Self.reportLink(run)))">"# |
| 131 | 59 | + "\(escapingHTML(run.folder))</a></td>" |
| 132 | 59 | + #"<td class="subject">\#(subject)</td>"# |
| 133 | 59 | if let reason = totals.unavailable { |
| 134 | 3 | cells += |
| 135 | 3 | #"<td class="lines"><span class="unavailable">\#(escapingHTML(reason))</span></td>"# |
| 136 | 56 | } else { |
| 137 | 56 | cells += |
| 138 | 56 | #"<td class="lines">\#(Self.metricMarkup(totals.lines, before: previous?.lines))</td>"# |
| 139 | 56 | } |
| 140 | 59 | if columns.regions { |
| 141 | 6 | cells += |
| 142 | 6 | #"<td class="regions">\#(Self.metricMarkup(totals.unavailable == nil ? totals.regions : nil, before: previous?.regions))</td>"# |
| 143 | 6 | } |
| 144 | 59 | if columns.branches { |
| 145 | 1 | cells += |
| 146 | 1 | #"<td class="branches">\#(Self.metricMarkup(totals.unavailable == nil ? totals.branches : nil, before: previous?.branches))</td>"# |
| 147 | 1 | } |
| 148 | 59 | if columns.conditions { |
| 149 | 1 | cells += |
| 150 | 1 | #"<td class="conditions">\#(Self.metricMarkup(totals.unavailable == nil ? totals.conditions : nil, before: previous?.conditions))</td>"# |
| 151 | 1 | } |
| 152 | 59 | return "<tr>\(cells)" + #"<td class="date">\#(Self.dateMarkup(totals.date))</td></tr>"# |
| 153 | 59 | } |
| 154 | | |
| 155 | | private static func metricMarkup(_ count: RunTotals.Lines?, before: RunTotals.Lines?) -> String |
| 156 | 64 | { |
| 157 | 64 | guard let count else { return "—" } |
| 158 | 63 | let summary = LineSummary(count: count.total, covered: count.covered) |
| 159 | 63 | var text = |
| 160 | 63 | #"<span class="percent \#(summary.tier.rawValue)">\#(summary.percentText)</span>"# |
| 161 | 63 | if let before { |
| 162 | 30 | let previous = LineSummary(count: before.total, covered: before.covered) |
| 163 | 30 | let change = CoverageChange.points(summary.percent - previous.percent) |
| 164 | 30 | text += #" <span class="change \#(change.styleName)">\#(change.text)</span>"# |
| 165 | 30 | } |
| 166 | 63 | return text |
| 167 | 64 | } |
| 168 | | |
| 169 | | /// Rewrites each date in the viewer's time zone, which the build can't know. Without |
| 170 | | /// scripts the dates stay in UTC and say so. |
| 171 | | private static let localDateScript = """ |
| 172 | | (() => { |
| 173 | | const two = number => String(number).padStart(2, "0"); |
| 174 | | for (const time of document.querySelectorAll("time[datetime]")) { |
| 175 | | const date = new Date(time.dateTime); |
| 176 | | time.textContent = `${date.getFullYear()}-${two(date.getMonth() + 1)}-${two(date.getDate())} ` |
| 177 | | + `${two(date.getHours())}:${two(date.getMinutes())}`; |
| 178 | | } |
| 179 | | })(); |
| 180 | | """ |
| 181 | | |
| 182 | | /// The machine-readable date with a short text beside it, `2026-12-01 08:30 UTC`. |
| 183 | 59 | private static func dateMarkup(_ date: Date) -> String { |
| 184 | 59 | let iso = date.formatted(.iso8601) |
| 185 | 59 | let shown = iso.prefix(shownDateLength).replacingOccurrences(of: "T", with: " ") |
| 186 | 59 | return #"<time datetime="\#(iso)">\#(shown) UTC</time>"# |
| 187 | 59 | } |
| 188 | | |
| 189 | | /// `2026-12-01T08:30`: up to the minute. |
| 190 | | private static let shownDateLength = 16 |
| 191 | | } |