| 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 | 33 | public init(folder: String, totals: RunTotals) { |
| 9 | 33 | self.folder = folder |
| 10 | 33 | self.totals = totals |
| 11 | 33 | } |
| 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 | 16 | public init(title: String, runs: [HistoryRun]) throws { |
| 29 | 16 | guard !runs.isEmpty else { throw HistoryPageError.noRuns } |
| 30 | 15 | self.title = title |
| 31 | 20 | self.newestFirst = runs.sorted { |
| 32 | 20 | ($0.totals.date, $1.folder) > ($1.totals.date, $0.folder) |
| 33 | 20 | } |
| 34 | 15 | } |
| 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 | 15 | public var html: String { |
| 43 | 15 | let latest = newestFirst[0] |
| 44 | 15 | let header = SiteHeader( |
| 45 | 15 | title: title, currentTab: .history, hasTestsPage: latest.totals.hasTestsPage == true, |
| 46 | 15 | rootPath: String(Self.reportLink(latest).dropLast(IndexPage.sitePath.count)), |
| 47 | 15 | run: ReportHeader(historyPath: "../index.html")) |
| 48 | 28 | let branches = newestFirst.contains { $0.totals.branches != nil } |
| 49 | 28 | let conditions = newestFirst.contains { $0.totals.conditions != nil } |
| 50 | 28 | let trend = CoverageTrend(runs: newestFirst.map(\.totals)) |
| 51 | 15 | return |
| 52 | 15 | """ |
| 53 | 15 | <!doctype html> |
| 54 | 15 | <html lang="en"> |
| 55 | 15 | <head> |
| 56 | 15 | <meta charset="utf-8"> |
| 57 | 15 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 58 | 15 | <title>History · \(escapingHTML(title))</title> |
| 59 | 15 | <link rel="stylesheet" href="\(Self.stylesheetPath)"> |
| 60 | 15 | </head> |
| 61 | 15 | <body class="history-page"> |
| 62 | 15 | <header class="page-header"> |
| 63 | 15 | \(header.html) |
| 64 | 15 | <p class="totals">\(groupingDigits(newestFirst.count)) \ |
| 65 | 15 | \(newestFirst.count == 1 ? "commit" : "commits"), newest first</p> |
| 66 | 15 | </header> |
| 67 | 15 | <main> |
| 68 | 15 | <section class="history-chart" aria-label="Coverage trend"> |
| 69 | 15 | \(trend.chart) |
| 70 | 15 | <p class="trend-legend">Lines\(branches ? " · Branches" : "")\(conditions ? " · Conditions" : "") · Red marks: no data\(trend.scaleText.map { " · Scale \($0)" } ?? "")</p> |
| 71 | 15 | </section> |
| 72 | 15 | <table class="history"> |
| 73 | 15 | <thead><tr><th>Commit</th><th>Subject</th><th>Lines</th>\(branches ? "<th>Branches</th>" : "")\(conditions ? "<th>Conditions</th>" : "")<th class="date">Date</th></tr></thead> |
| 74 | 15 | <tbody> |
| 75 | 15 | \(rows(branches: branches, conditions: conditions)) |
| 76 | 15 | </tbody> |
| 77 | 15 | </table> |
| 78 | 15 | </main> |
| 79 | 15 | </body> |
| 80 | 15 | </html> |
| 81 | 15 | |
| 82 | 15 | """ |
| 83 | 15 | } |
| 84 | | |
| 85 | | /// Relative to the page, with the folder's name encoded so any name stays one link. |
| 86 | 43 | private static func reportLink(_ run: HistoryRun) -> String { |
| 87 | 43 | let folder = run.folder.addingPercentEncoding(withAllowedCharacters: folderCharacters) |
| 88 | 43 | return "\(folder ?? run.folder)/\(IndexPage.sitePath)" |
| 89 | 43 | } |
| 90 | | |
| 91 | | /// Unreserved characters only: `#`, `?`, `%` and spaces in a name must not end the path. |
| 92 | | private static let folderCharacters = CharacterSet( |
| 93 | | charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~") |
| 94 | | |
| 95 | 15 | private func rows(branches: Bool, conditions: Bool) -> String { |
| 96 | 28 | newestFirst.enumerated().map { index, run in |
| 97 | 28 | let previous = newestFirst.dropFirst(index + 1).first { $0.totals.unavailable == nil } |
| 98 | 28 | return rowMarkup( |
| 99 | 28 | run, previous: previous?.totals, branches: branches, conditions: conditions) |
| 100 | 28 | }.joined(separator: "\n") |
| 101 | 15 | } |
| 102 | | |
| 103 | | private func rowMarkup( |
| 104 | | _ run: HistoryRun, previous: RunTotals?, branches: Bool, conditions: Bool |
| 105 | 28 | ) -> String { |
| 106 | 28 | let totals = run.totals |
| 107 | 28 | let subject = escapingHTML(totals.subject ?? "") |
| 108 | 28 | var cells = |
| 109 | 28 | #"<td class="commit"><a href="\#(escapingHTML(Self.reportLink(run)))">"# |
| 110 | 28 | + "\(escapingHTML(run.folder))</a></td>" |
| 111 | 28 | + #"<td class="subject">\#(subject)</td>"# |
| 112 | 28 | if let reason = totals.unavailable { |
| 113 | 3 | cells += |
| 114 | 3 | #"<td class="lines"><span class="unavailable">\#(escapingHTML(reason))</span></td>"# |
| 115 | 25 | } else { |
| 116 | 25 | cells += |
| 117 | 25 | #"<td class="lines">\#(Self.metricMarkup(totals.lines, before: previous?.lines))</td>"# |
| 118 | 25 | } |
| 119 | 28 | if branches { |
| 120 | 1 | cells += |
| 121 | 1 | #"<td class="branches">\#(Self.metricMarkup(totals.unavailable == nil ? totals.branches : nil, before: previous?.branches))</td>"# |
| 122 | 1 | } |
| 123 | 28 | if conditions { |
| 124 | 1 | cells += |
| 125 | 1 | #"<td class="conditions">\#(Self.metricMarkup(totals.unavailable == nil ? totals.conditions : nil, before: previous?.conditions))</td>"# |
| 126 | 1 | } |
| 127 | 28 | return "<tr>\(cells)" + #"<td class="date">\#(Self.dateMarkup(totals.date))</td></tr>"# |
| 128 | 28 | } |
| 129 | | |
| 130 | | private static func metricMarkup(_ count: RunTotals.Lines?, before: RunTotals.Lines?) -> String |
| 131 | 27 | { |
| 132 | 27 | guard let count else { return "—" } |
| 133 | 27 | let summary = LineSummary(count: count.total, covered: count.covered) |
| 134 | 27 | var text = |
| 135 | 27 | #"<span class="percent \#(summary.tier.rawValue)">\#(summary.percentText)</span>"# |
| 136 | 27 | if let before { |
| 137 | 11 | let previous = LineSummary(count: before.total, covered: before.covered) |
| 138 | 11 | let change = CoverageChange.points(summary.percent - previous.percent) |
| 139 | 11 | text += #" <span class="change \#(change.styleName)">\#(change.text)</span>"# |
| 140 | 11 | } |
| 141 | 27 | return text |
| 142 | 27 | } |
| 143 | | |
| 144 | | /// The machine-readable date with a short text beside it, `2026-12-01 08:30 UTC`. |
| 145 | 28 | private static func dateMarkup(_ date: Date) -> String { |
| 146 | 28 | let iso = date.formatted(.iso8601) |
| 147 | 28 | let shown = iso.prefix(shownDateLength).replacingOccurrences(of: "T", with: " ") |
| 148 | 28 | return #"<time datetime="\#(iso)">\#(shown) UTC</time>"# |
| 149 | 28 | } |
| 150 | | |
| 151 | | /// `2026-12-01T08:30`: up to the minute. |
| 152 | | private static let shownDateLength = 16 |
| 153 | | } |