@seikika/astro-report 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,264 @@
1
+ import type { Summary } from "./summary";
2
+ import type { ReportData } from "../types";
3
+ import type { Kid } from "./dom";
4
+
5
+ export type Tally = { ahead: number; parity: number; behind: number; unknown: number };
6
+ import { medal } from "./bars";
7
+ import { basisBadge, basisOf } from "./basis";
8
+ import { levelName } from "./campaigns";
9
+ import { el } from "./dom";
10
+ import { capsRepo, ring, title } from "./rings";
11
+ import { LADDER, floorOf, isComparative, rankLevel, ruleLink } from "./rules";
12
+ import { fmt } from "./words";
13
+
14
+ export function trackTitle(D: ReportData, name) {
15
+ var t = (D.tracks || {})[name];
16
+ return (t && t.title) || name;
17
+ }
18
+
19
+ // A track's standing: for level scorecards, the share of entities at the top level averaged
20
+ // over its scorecards; for comparisons, the factors at parity or ahead.
21
+ export function trackStats(D: ReportData, S: Summary, name) {
22
+ var rows = S.rows.filter(function (r) {
23
+ return (r.sc.track || null) === (name || null);
24
+ });
25
+ var compare =
26
+ rows.length > 0 &&
27
+ rows.every(function (r) {
28
+ return isComparative(D, r.name);
29
+ });
30
+ var noneIds: Record<string, boolean> = {};
31
+ var noneRows = 0;
32
+ var shares: number[] = [];
33
+ var scoredRows = 0;
34
+ var capped: string[] = [];
35
+ var short: string[] = [];
36
+ // Every scored scorecard counts at its own top level, in every portal; one whose ladder stops
37
+ // below gold is named, and one that also caps the repository level gets a lock note.
38
+ var cards = rows.map(function (r) {
39
+ if (!r.compare) {
40
+ var scoredHere = (D.scores || {})[r.name] || {};
41
+ Object.keys(scoredHere).forEach(function (id) {
42
+ if (scoredHere[id].level === "none") noneIds[id] = true;
43
+ });
44
+ // The scorecard cards list a row each. Their sum is larger when one entity is at none on more than one.
45
+ noneRows += r.none || 0;
46
+ if (r.scored) {
47
+ scoredRows++;
48
+ shares.push(r.topShare);
49
+ if (capsRepo(D, r.name, floorOf(D, r.name))) capped.push(r.name);
50
+ var lv = ((D.scorecards || {})[r.name] || {}).levels || [];
51
+ var top = lv[lv.length - 1];
52
+ if (top && LADDER.indexOf(top) > 0 && top !== LADDER[LADDER.length - 1]) short.push(r.name);
53
+ }
54
+ }
55
+ return { row: r, floor: r.compare ? null : floorOf(D, r.name) };
56
+ });
57
+ cards.sort(function (a, b) {
58
+ return rankLevel(a.floor) - rankLevel(b.floor) || (a.row.topShare || 0) - (b.row.topShare || 0) || b.row.none - a.row.none || title(D, a.row.name).localeCompare(title(D, b.row.name));
59
+ });
60
+ var verdicts: Tally & { rules?: { ahead: number; parity: number; behind: number; total: number }; both?: Both; majority?: boolean } = { ahead: 0, parity: 0, behind: 0, unknown: 0 };
61
+ var behind: string[] = [];
62
+ rows.forEach(function (r) {
63
+ if (!r.compare) return;
64
+ var c = (D.comparisons || {})[r.name];
65
+ var v = (c.verdict || {})[(c.peers || [])[0]] || "unknown";
66
+ verdicts[v] = (verdicts[v] || 0) + 1;
67
+ if (v === "behind") behind.push(title(D, r.name));
68
+ var rules = verdicts.rules || (verdicts.rules = { ahead: 0, parity: 0, behind: 0, total: 0 });
69
+ var k = { ahead: 0, parity: 0, behind: 0, unknown: 0 };
70
+ Object.keys(c.rules || {}).forEach(function (rule) {
71
+ var x = (c.rules[rule] || {})[(c.peers || [])[0]];
72
+ if (x in rules) rules[x]++;
73
+ rules.total++;
74
+ k[x in k ? x : "unknown"]++;
75
+ });
76
+ var both = verdicts.both || (verdicts.both = { strict: { ahead: 0, parity: 0, behind: 0, unknown: 0 }, majority: { ahead: 0, parity: 0, behind: 0, unknown: 0 } });
77
+ both.strict[standingBy(k, false)]++;
78
+ both.majority[standingBy(k, true)]++;
79
+ });
80
+ var pct = compare
81
+ ? Math.round(((verdicts.ahead + verdicts.parity) / rows.length) * 100)
82
+ : shares.length
83
+ ? Math.round(
84
+ shares.reduce(function (a, b) {
85
+ return a + b;
86
+ }, 0) / shares.length,
87
+ )
88
+ : null;
89
+ var campaigns = Object.keys(D.campaigns || {}).filter(function (n) {
90
+ return (D.campaigns || {})[n].track === name;
91
+ });
92
+ var majority = compare && byMajority(D, rows.map(function (r) {
93
+ return r.name;
94
+ }));
95
+ if (compare) verdicts.majority = majority;
96
+ return { name: name, def: (D.tracks || {})[name] || {}, rows: rows, cards: cards, compare: compare, majority: majority, none: Object.keys(noneIds).length, noneRows: noneRows, pct: pct, averaged: shares.length, scoredRows: scoredRows, capped: capped, short: short, verdicts: verdicts, behind: behind, campaigns: campaigns };
97
+ }
98
+
99
+ export function tracksSorted(D: ReportData, S: Summary) {
100
+ return Object.keys(D.tracks || {})
101
+ .map(function (n) {
102
+ return trackStats(D, S, n);
103
+ })
104
+ .filter(function (t) {
105
+ return t.rows.length || t.campaigns.length;
106
+ })
107
+ .sort(function (a, b) {
108
+ return (a.compare ? 1 : 0) - (b.compare ? 1 : 0) || (a.pct == null ? 101 : a.pct) - (b.pct == null ? 101 : b.pct) || b.none - a.none || a.name.localeCompare(b.name);
109
+ });
110
+ }
111
+
112
+ export function unassessedNote(D: ReportData, card, scored) {
113
+ var ids = (D.unassessed || {})[card] || [];
114
+ var inScope = ids.filter(function (id) {
115
+ return ((D.scores[card] || {})[id] || {}).level !== "skipped";
116
+ }).length;
117
+ if (!inScope) return null;
118
+ var rules = ((D.scorecards || {})[card] || {}).rules || {};
119
+ var passing: string[] = [];
120
+ var failing: string[] = [];
121
+ Object.keys(rules)
122
+ .filter(function (r) {
123
+ return basisOf(rules[r]) === "judged";
124
+ })
125
+ .forEach(function (r) {
126
+ var fails = ids.some(function (id) {
127
+ var o = (((D.scores[card] || {})[id] || {}).rules || {})[r];
128
+ return o && o !== "pass";
129
+ });
130
+ (fails ? failing : passing).push(r);
131
+ });
132
+ var names = function (rs) {
133
+ return rs.map(function (r) {
134
+ return ruleLink(card, r);
135
+ });
136
+ };
137
+ var list = function (rs) {
138
+ var out: Kid[] = [];
139
+ names(rs).forEach(function (a, i) {
140
+ if (i) out.push(i === rs.length - 1 ? " and " : ", ");
141
+ out.push(a);
142
+ });
143
+ return out;
144
+ };
145
+ var why: Kid[] = [];
146
+ if (passing.length) why.push(...list(passing), passing.length === 1 ? " passes" : " pass", " only because there is nothing to fail on");
147
+ if (passing.length && failing.length) why.push("; ");
148
+ if (failing.length) why.push(...list(failing), failing.length === 1 ? " fails" : " fail", " until an assessment exists");
149
+ why.push(passing.length ? ". A level they reach means nothing has been found, not that a review found nothing." : ".");
150
+ return el(
151
+ "div",
152
+ { className: "callout judged-note" },
153
+ el("span", {}, el("b", { textContent: (inScope === scored ? "No entity here has an" : fmt(inScope) + " of " + fmt(scored) + " entities have no") + " agent's assessment yet. " }), "On those, of the ", basisBadge("judged", true), " rules, ", ...why),
154
+ );
155
+ }
156
+
157
+ export function unassessedIn(D: ReportData, card, id) {
158
+ return ((D.unassessed || {})[card] || []).indexOf(id) >= 0;
159
+ }
160
+
161
+ // What a track card's ring colours and a comparison's segments mean, in place of a paragraph.
162
+ export function trackKey(tracks) {
163
+ var sw = function (c, t) {
164
+ return el("span", {}, el("span", { className: "dot", style: "background:" + c }), t);
165
+ };
166
+ var compare = tracks.some(function (t) {
167
+ return t.compare;
168
+ });
169
+ var quoted = null;
170
+ tracks.forEach(function (t) {
171
+ (t.cards || []).forEach(function (c) {
172
+ if (!quoted && c.floor && c.floor !== "none" && LADDER.indexOf(c.floor) < 0) quoted = c.floor;
173
+ });
174
+ });
175
+ var key = el(
176
+ "div",
177
+ { className: "legend trackkey" },
178
+ el("b", { textContent: "Ring, share at the top level:" }),
179
+ sw(RAMP, "the green arc's length is the share; grey is not scored"),
180
+ el(
181
+ "span",
182
+ { className: "slot-key" },
183
+ el("b", { textContent: "Bars:" }),
184
+ el(
185
+ "span",
186
+ { className: "slots mini", ariaHidden: "true", style: "--avg:55%" },
187
+ ...[80, 30, 55].map(function (p) {
188
+ return el("span", { className: "slot", style: "--h:" + p + "%;--c:" + RAMP });
189
+ }),
190
+ el("span", { className: "avg" }),
191
+ ),
192
+ "one per scorecard, its share at the top level (point at, tap or arrow through a bar to name it under the strip); dashed: their average, the ring",
193
+ ),
194
+ compare ? el("b", { textContent: "Segmented ring, one per factor:" }) : null,
195
+ compare ? sw(standingColor("ahead"), "ahead") : null,
196
+ compare ? sw(standingColor("parity"), "at parity") : null,
197
+ compare ? sw(standingColor("behind"), "behind") : null,
198
+ el("span", { className: "medal-key" }, el("b", { textContent: "Medal:" }), medal("bronze", 16), "its weakest entity's level ·", medal("none", 16), "none"),
199
+ quoted ? el("span", {}, "A quoted level such as " + levelName(quoted) + " is its scorecard's own name for a level.") : null,
200
+ );
201
+ return el("details", { className: "keyd" }, el("summary", { textContent: "How to read: one bar per scorecard, and the ring is their average" }), key);
202
+ }
203
+
204
+ export function furthestTrack(D: ReportData, S: Summary) {
205
+ var t = tracksSorted(D, S).find(function (t) {
206
+ return !t.compare && t.pct != null && t.pct < 100;
207
+ });
208
+ return t ? t.name : null;
209
+ }
210
+
211
+ // One hue for a share at the top level, so arc length alone carries it. Grey stays "not scored" and
212
+ // parity, red the none level and "behind", and bronze, silver and gold belong to medals.
213
+ export var RAMP = "var(--pass)";
214
+
215
+ export function trackColor(t) {
216
+ if (t.compare) return "var(--accent)";
217
+ if (t.pct == null) return "var(--faint)";
218
+ return RAMP;
219
+ }
220
+
221
+ export var STANDING_MARK = { ahead: "▲", parity: "=", behind: "▼", unknown: "?" };
222
+
223
+ export function standingChip(s, text) {
224
+ var v = s || "unknown";
225
+ return el("span", { className: "standing " + v, textContent: STANDING_MARK[v] + " " + (text || (v === "unknown" ? "not measured" : v)) });
226
+ }
227
+
228
+ // Whether every named comparison tallies its factors by the majority of rules rather than behind-on-any
229
+ // (D01M87WF2QZVPTD8CGDZKY133CD).
230
+ export function byMajority(D: ReportData, names) {
231
+ return (
232
+ names.length > 0 &&
233
+ names.every(function (n) {
234
+ return ((D.comparisons || {})[n] || {}).verdictRule === "majority";
235
+ })
236
+ );
237
+ }
238
+
239
+ export type Both = { strict: Tally; majority: Tally };
240
+
241
+ // A factor's standing from its rules' standings under either counting rule (D01M87WF2QZVPTD8CGDZKY133CD):
242
+ // strict is behind if any rule is behind, majority follows whichever side has more rules.
243
+ export function standingBy(k: Tally, majority: boolean): keyof Tally {
244
+ if (majority) return k.unknown ? "unknown" : k.ahead > k.behind ? "ahead" : k.behind > k.ahead ? "behind" : "parity";
245
+ return k.behind ? "behind" : k.unknown ? "unknown" : k.ahead ? "ahead" : "parity";
246
+ }
247
+
248
+ // The chip for a factor whose rules split both ways, or that leads on under half of them.
249
+ export function factorChip(v, counts, total) {
250
+ if (counts.ahead && counts.behind) return v + ", mixed";
251
+ return v === "ahead" && counts.ahead * 2 < total ? "narrowly ahead" : null;
252
+ }
253
+
254
+ // "Seikika is behind Backstage", in words that read as a sentence.
255
+ export function standingPhrase(a, s, b) {
256
+ if (s === "ahead") return a + " ahead of " + b;
257
+ if (s === "behind") return a + " behind " + b;
258
+ if (s === "parity") return a + " at parity with " + b;
259
+ return a + " vs " + b + ": not measured";
260
+ }
261
+
262
+ export function standingColor(s) {
263
+ return s === "ahead" ? "var(--pass)" : s === "behind" ? "var(--fail)" : s === "parity" ? "var(--muted)" : "var(--unknown)";
264
+ }
@@ -0,0 +1,72 @@
1
+ import { el } from "./dom";
2
+ import { rich } from "./text";
3
+
4
+ // The first whole sentence of a description, for cards: a card shows a complete thought, never a cut one.
5
+ // A reason's first sentence, with the rest in a span a clamped list hides, so nothing is cut mid-sentence.
6
+ export function whyFirst(text) {
7
+ var first = firstSentence(text);
8
+ var rest = String(text).trim().slice(first.length).trim();
9
+ return el("span", { className: "why" }, ...rich(first), rest ? el("span", { className: "rest" }, " ", ...rich(rest)) : null);
10
+ }
11
+
12
+ export function scPurpose(sc) {
13
+ var strip = function (t) {
14
+ return firstSentence(t).replace(/\s*\([^)]*\)/g, "");
15
+ };
16
+ return sc.rationale ? "Why: " + strip(sc.rationale) : strip(sc.description || "");
17
+ }
18
+
19
+ // The sentence of a kind's description that says why some of it is not scored, when it says so.
20
+ export function unscoredWhy(text) {
21
+ var rest = String(text || "").trim();
22
+ var first = firstSentence(rest);
23
+ while (first && rest) {
24
+ if (/not scored/.test(first) && first !== firstSentence(text)) return el("small", { className: "kwhy" }, ...rich(first));
25
+ rest = rest.slice(first.length).trim();
26
+ first = firstSentence(rest);
27
+ }
28
+ return null;
29
+ }
30
+
31
+ export function sentences(text) {
32
+ var out: string[] = [];
33
+ var t = String(text || "").trim();
34
+ while (t) {
35
+ var f = firstSentence(t);
36
+ out.push(f);
37
+ t = t.slice(f.length).trim();
38
+ }
39
+ return out;
40
+ }
41
+
42
+ export function firstSentence(text) {
43
+ var t = String(text || "").trim();
44
+ var depth = 0;
45
+ var code = false;
46
+ for (var i = 0; i < t.length - 2; i++) {
47
+ var ch = t.charAt(i);
48
+ if (ch === "`") code = !code;
49
+ else if (!code && (ch === "[" || ch === "(")) depth++;
50
+ else if (!code && (ch === "]" || ch === ")")) depth = Math.max(0, depth - 1);
51
+ else if (!code && !depth && (ch === "." || ch === "!" || ch === "?") && /\s/.test(t.charAt(i + 1)) && /[A-Z]/.test(t.charAt(i + 2))) return t.slice(0, i + 1);
52
+ }
53
+ return t;
54
+ }
55
+
56
+ // "Bytes of…" reads as "bytes of…" mid-sentence; "MCP…" keeps its capitals.
57
+ export function lowerFirst(t) {
58
+ // A capital later in the first word marks a name ("GitHub", "OpenSSF"), which keeps its case.
59
+ return /^[A-Z][a-z]/.test(t || "") && !/^[A-Z][a-z]*[A-Z]/.test(t) ? t.charAt(0).toLowerCase() + t.slice(1) : t;
60
+ }
61
+
62
+ export function andList(items) {
63
+ return items.length > 1 ? items.slice(0, -1).join(", ") + " and " + items[items.length - 1] : items.join("");
64
+ }
65
+
66
+ export function plural(n, word, many?) {
67
+ return fmt(n) + " " + (n === 1 ? word : many || word + "s");
68
+ }
69
+
70
+ export function fmt(n) {
71
+ return Number(n).toLocaleString("en-US");
72
+ }
@@ -0,0 +1,37 @@
1
+ import type { ReportData } from "./types";
2
+ import type { Summary } from "./lib/summary";
3
+ import { missingTarget, notFound, type Route } from "./lib/routes";
4
+ import { overviewViews } from "./views/overview";
5
+ import { scorecardsViews } from "./views/scorecards";
6
+ import { tracksViews } from "./views/tracks";
7
+ import { campaignsViews } from "./views/campaigns";
8
+ import { catalogViews } from "./views/catalog";
9
+ import { entityViews } from "./views/entity";
10
+ import { ruleViews } from "./views/rule";
11
+ import { compareViews } from "./views/compare";
12
+
13
+ const views = {
14
+ ...overviewViews, ...scorecardsViews, ...tracksViews, ...campaignsViews,
15
+ ...catalogViews, ...entityViews, ...ruleViews, ...compareViews,
16
+ };
17
+ type Methods = typeof overviewViews & typeof scorecardsViews & typeof tracksViews &
18
+ typeof campaignsViews & typeof catalogViews & typeof entityViews & typeof ruleViews & typeof compareViews;
19
+ export interface ReportViews extends Methods {}
20
+
21
+ export function buildView(data: ReportData, summary: Summary, route: Route): HTMLElement {
22
+ const missing = missingTarget(data, route);
23
+ if (missing) return notFound(missing);
24
+ switch (route.mode) {
25
+ case "overview": return views.overview(data, summary);
26
+ case "catalog": return views.catalog(data, summary, route.kind, route.query ?? {});
27
+ case "entity": return views.entity(data, summary, route.id);
28
+ case "campaigns":
29
+ case "campaign": return views.campaigns(data, summary, route.name);
30
+ case "compare": return views.compare(data, route.name);
31
+ case "tracks": return views.tracks(data, summary, route.name);
32
+ case "rule": return views.rule(data, summary, route.scorecard, route.rule);
33
+ case "scorecards": return views.scorecardIndex(data, summary, route.query ?? {});
34
+ case "scorecard": return views.scorecard(data, summary, route.name, route.query ?? {});
35
+ default: return notFound("This link could not be read.");
36
+ }
37
+ }
@@ -0,0 +1,27 @@
1
+ ---
2
+ import { readFile } from "node:fs/promises";
3
+ import { Layout, dataFile } from "virtual:seikika-report";
4
+ import "@seikika/ui/theme.css";
5
+
6
+ const snapshot = JSON.parse(await readFile(dataFile, "utf8"));
7
+ if (!snapshot || typeof snapshot.catalog !== "object" || !snapshot.catalog ||
8
+ typeof snapshot.scorecards !== "object" || !snapshot.scorecards ||
9
+ typeof snapshot.scores !== "object" || !snapshot.scores) {
10
+ throw new TypeError("The Seikika report expects a snapshot from seikika report -o web");
11
+ }
12
+ const title = snapshot.portal || "Seikika report";
13
+ const serialized = JSON.stringify(snapshot).replace(/</g, "\\u003c");
14
+ ---
15
+ <Layout title={title}>
16
+ <div id="seikika-report-root"></div>
17
+ <noscript><h1>{title}</h1><p>Enable JavaScript to browse the report.</p></noscript>
18
+ <script is:inline id="seikika-report-data" type="application/json" set:html={serialized}></script>
19
+ </Layout>
20
+ <script>
21
+ import { createApp } from "vue";
22
+ import ReportApp from "./ReportApp.vue";
23
+ import "./report.css";
24
+ const data = document.getElementById("seikika-report-data");
25
+ const root = document.getElementById("seikika-report-root");
26
+ if (data && root) createApp(ReportApp, { data: JSON.parse(data.textContent ?? "null") }).mount(root);
27
+ </script>