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