@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.
- package/LICENSE +9 -0
- package/README.md +32 -2
- package/integration.d.mts +13 -0
- package/integration.mjs +44 -0
- package/package.json +57 -3
- package/src/ReportApp.vue +220 -0
- package/src/ReportContent.vue +13 -0
- package/src/ReportView.vue +88 -0
- package/src/layouts/DefaultLayout.astro +17 -0
- package/src/lib/bars.ts +290 -0
- package/src/lib/basis.ts +157 -0
- package/src/lib/campaigns.ts +410 -0
- package/src/lib/compare.ts +204 -0
- package/src/lib/dom.ts +43 -0
- package/src/lib/kinds.ts +84 -0
- package/src/lib/levels.ts +61 -0
- package/src/lib/rings.ts +244 -0
- package/src/lib/routes.ts +118 -0
- package/src/lib/rules.ts +143 -0
- package/src/lib/status.ts +150 -0
- package/src/lib/summary.ts +231 -0
- package/src/lib/text.ts +57 -0
- package/src/lib/tracks.ts +264 -0
- package/src/lib/words.ts +72 -0
- package/src/report-view.ts +37 -0
- package/src/report.astro +27 -0
- package/src/report.css +359 -0
- package/src/types.ts +141 -0
- package/src/views/campaigns.ts +265 -0
- package/src/views/catalog.ts +456 -0
- package/src/views/compare.ts +676 -0
- package/src/views/entity.ts +443 -0
- package/src/views/overview.ts +639 -0
- package/src/views/rule.ts +153 -0
- package/src/views/scorecards.ts +947 -0
- package/src/views/tracks.ts +294 -0
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
import type { ReportData } from "../types";
|
|
2
|
+
import type { Row } from "./summary";
|
|
3
|
+
import { levelOrder, medal, provisionalOf, unassessedOf } from "./bars";
|
|
4
|
+
import { el, enc, link } from "./dom";
|
|
5
|
+
import { kindCount, scorecardSplit } from "./kinds";
|
|
6
|
+
import { known } from "./levels";
|
|
7
|
+
import { isTop, shortId, title } from "./rings";
|
|
8
|
+
import { LADDER, floorOf, isComparative, rankLevel, ruleTitle } from "./rules";
|
|
9
|
+
import { blockers } from "./status";
|
|
10
|
+
import { unassessedIn } from "./tracks";
|
|
11
|
+
import { fmt, plural } from "./words";
|
|
12
|
+
|
|
13
|
+
// The first scorecard on a track that can still rise, with the rule most of its lowest entities need.
|
|
14
|
+
// A scorecard's own level name ("ten", "clean") is quoted so it reads as a name, not a stray word.
|
|
15
|
+
// "Ten" is an OpenSSF check id. In a next-step sentence it reads as a stray word, and as a link it is under 24 px wide.
|
|
16
|
+
function ruleWords(name, rule) {
|
|
17
|
+
var t = ruleTitle(name, rule);
|
|
18
|
+
return t === "Ten" ? "a score of 10" : t;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function levelName(l) {
|
|
22
|
+
return l === "none" || l === "not scored" || LADDER.indexOf(l) >= 0 ? l : "“" + l + "”";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// One summary of a scorecard row wherever a list shows it: scope, share at its own top level, and the
|
|
26
|
+
// levels that may not hold, so a list never reads better than the scorecard's page.
|
|
27
|
+
export function scCaption(D: ReportData, r, split?) {
|
|
28
|
+
var head = (r.sc.kind ? kindCount(D, r.sc.kind, r.total) + (split ? scorecardSplit(D, r.sc.kind) : "") : plural(r.total, "entity", "entities")) + (r.total > r.scored ? ", " + fmt(r.total - r.scored) + " not in scope" : "");
|
|
29
|
+
var prov = provisionalOf(D, r.name);
|
|
30
|
+
var unj = unassessedOf(D, r.name);
|
|
31
|
+
var idle = idleLowestMark(D, r.name);
|
|
32
|
+
return [
|
|
33
|
+
head + (r.scored ? " · " + r.topShare + "% at the top level (" + levelName(r.top || "top") + ")" : " · not scored yet"),
|
|
34
|
+
prov ? el("span", { className: "provisional", title: "Their levels hold only if the checks not run in this build pass; drawn dotted in the bar", textContent: " · " + fmt(prov) + " provisional (checks not run)" }) : null,
|
|
35
|
+
unj ? el("span", { className: "unjudged-n", title: "No agent has assessed them yet, so their agent-judged rules pass or fail for want of anything to read; drawn hatched in the bar", textContent: " · " + fmt(unj) + " not assessed by an agent (hatched)" }) : null,
|
|
36
|
+
idle ? el("span", { textContent: " · " + idle }) : null,
|
|
37
|
+
];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// The weakest scorecard on a track, when it sits more than one level below its top.
|
|
41
|
+
// Lifting it does not move the track's percentage, so the list says so beside the row the Next box names.
|
|
42
|
+
export function idleLowestMark(D: ReportData, name) {
|
|
43
|
+
var sc = (D.scorecards || {})[name];
|
|
44
|
+
if (!sc || !sc.track || isComparative(D, name)) return null;
|
|
45
|
+
var peers = Object.keys(D.scorecards || {}).filter(function (n) {
|
|
46
|
+
return ((D.scorecards || {})[n] || {}).track === sc.track && !isComparative(D, n);
|
|
47
|
+
});
|
|
48
|
+
var ranked = peers
|
|
49
|
+
.map(function (n) {
|
|
50
|
+
return { n: n, floor: floorOf(D, n) };
|
|
51
|
+
})
|
|
52
|
+
.filter(function (x) {
|
|
53
|
+
return x.floor && !isTop(D, x.n, x.floor);
|
|
54
|
+
});
|
|
55
|
+
ranked.sort(function (a, b) {
|
|
56
|
+
return rankLevel(a.floor) - rankLevel(b.floor) || title(D, a.n).localeCompare(title(D, b.n));
|
|
57
|
+
});
|
|
58
|
+
if (!ranked.length || ranked[0].n !== name) return null;
|
|
59
|
+
var order = ["none"].concat((sc.levels || []).filter(function (l) { return l !== "none"; }));
|
|
60
|
+
var idx = order.indexOf(ranked[0].floor as string);
|
|
61
|
+
if (idx < 0 || idx >= order.length - 2) return null;
|
|
62
|
+
return "Lowest on this track; lifting it adds no points until those entities reach the top level.";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function nextMedal(D: ReportData, cards) {
|
|
66
|
+
for (var i = 0; i < cards.length; i++) {
|
|
67
|
+
var c = cards[i];
|
|
68
|
+
var sc = (D.scorecards || {})[c.row.name] || {};
|
|
69
|
+
var up = c.floor && nextLevel(sc, c.floor);
|
|
70
|
+
if (!up) continue;
|
|
71
|
+
var held: Record<string, number> = {};
|
|
72
|
+
var low = 0;
|
|
73
|
+
Object.values((D.scores || {})[c.row.name] || {}).forEach(function (s) {
|
|
74
|
+
if (s.level !== c.floor) return;
|
|
75
|
+
low++;
|
|
76
|
+
blockers(sc, s, up).forEach(function (n) {
|
|
77
|
+
held[n] = (held[n] || 0) + 1;
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
var best = Object.entries(held).sort(function (a, b) {
|
|
81
|
+
return b[1] - a[1] || a[0].localeCompare(b[0]);
|
|
82
|
+
})[0];
|
|
83
|
+
return { name: c.row.name, floor: c.floor, up: up, low: low, rule: best && best[0], n: best ? best[1] : 0 };
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// A decision file's basename is an id. The next step leads with what it is; the filename follows in small type.
|
|
89
|
+
function entityLead(id) {
|
|
90
|
+
var file = shortId(id);
|
|
91
|
+
if (/^D0[0-9A-Z]{8,}\.md$/.test(file)) return { label: "Decision record", file: file };
|
|
92
|
+
return { label: file, file: null };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// The entities sitting at `level` on a scorecard, named when a reader can hold the list (three or fewer).
|
|
96
|
+
function namedAt(D: ReportData, name, level) {
|
|
97
|
+
var ids = Object.keys((D.scores || {})[name] || {})
|
|
98
|
+
.filter(function (id) {
|
|
99
|
+
return D.scores[name][id].level === level;
|
|
100
|
+
})
|
|
101
|
+
.sort();
|
|
102
|
+
if (!ids.length || ids.length > 3) return null;
|
|
103
|
+
var bits: (HTMLElement | string)[] = [];
|
|
104
|
+
ids.forEach(function (id, i) {
|
|
105
|
+
if (i) bits.push(i === ids.length - 1 ? " and " : ", ");
|
|
106
|
+
var lead = entityLead(id);
|
|
107
|
+
bits.push(link("entity/" + enc(id), "", lead.label));
|
|
108
|
+
if (lead.file) bits.push(el("small", { className: "sub", textContent: " (" + lead.file + ")" }));
|
|
109
|
+
});
|
|
110
|
+
return bits;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// A track's next step is the one that moves its percentage most; the next medal on its weakest
|
|
114
|
+
// scorecard follows, with its gain stated, because a step below a scorecard's top level adds nothing.
|
|
115
|
+
// The basis badge stays off this sentence: it belongs on the rule, and in the sentence it splits the
|
|
116
|
+
// rule from the verb.
|
|
117
|
+
export function nextMedalEl(D: ReportData, cards, label?, why?) {
|
|
118
|
+
var nm = nextMedal(D, cards);
|
|
119
|
+
var tracked = Object.keys(D.tracks || {}).length > 0;
|
|
120
|
+
var movers = tracked ? ringMovers(D, cards) : [];
|
|
121
|
+
var verified = movers.find(function (m) {
|
|
122
|
+
return !m.waits;
|
|
123
|
+
});
|
|
124
|
+
var unverified = movers.find(function (m) {
|
|
125
|
+
return m.waits && (!verified || m.gain > verified.gain);
|
|
126
|
+
});
|
|
127
|
+
if (!nm && !verified) return null;
|
|
128
|
+
var medalBody = function () {
|
|
129
|
+
if (!nm) return [];
|
|
130
|
+
var card = link("scorecard/" + enc(nm.name), "", title(D, nm.name));
|
|
131
|
+
var rule = nm.rule ? [link("rule/" + enc(nm.name) + "/" + enc(nm.rule), "", ruleWords(nm.name, nm.rule))] : [];
|
|
132
|
+
var named = nm.rule && nm.n === nm.low ? namedAt(D, nm.name, nm.floor) : null;
|
|
133
|
+
var entities = plural(nm.low, "entity", "entities") + " at " + nm.floor;
|
|
134
|
+
if (named) return [...named, " on ", card, nm.low === 1 ? " needs " : " need ", ...rule, "."];
|
|
135
|
+
return nm.rule && nm.n === nm.low
|
|
136
|
+
? [entities + " on ", card, nm.low === 1 ? " needs " : " need ", ...rule, "."]
|
|
137
|
+
: ["lift " + entities + " to " + nm.up + " on ", card, ...(nm.rule ? [". " + nm.n + (nm.n === 1 ? " needs " : " need "), ...rule, " first."] : ["."])];
|
|
138
|
+
};
|
|
139
|
+
var top = nm ? (((D.scorecards || {})[nm.name] || {}).levels || []).slice(-1)[0] : "";
|
|
140
|
+
var medalAtTop = nm ? isTop(D, nm.name, nm.up) : false;
|
|
141
|
+
var medalNote = !nm ? "" : medalAtTop ? " That is its top level." : " Its top level is " + top + (tracked ? ", so this adds no points to the track's percentage until they reach it." : ".");
|
|
142
|
+
var leadVerified = !!(verified && !(medalAtTop && verified.r.name === nm?.name));
|
|
143
|
+
var extras = [
|
|
144
|
+
leadVerified && nm ? el("div", { className: "sub mover" }, el("b", { textContent: "Next medal: " }), ...medalBody(), medalNote) : null,
|
|
145
|
+
unverified ? moverEl(D, unverified) : null,
|
|
146
|
+
].filter(Boolean);
|
|
147
|
+
var more = extras.length ? el("details", { className: "sub", style: "margin-top:6px" }, el("summary", { textContent: "Other steps" }), ...extras) : null;
|
|
148
|
+
if (leadVerified && verified) {
|
|
149
|
+
return el(
|
|
150
|
+
"div",
|
|
151
|
+
{ className: "nextmedal" },
|
|
152
|
+
el("b", { textContent: label || "Next: " }),
|
|
153
|
+
...moverBody(D, verified),
|
|
154
|
+
why ? el("span", { className: "sub" }, why) : null,
|
|
155
|
+
more,
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
if (!nm) return null;
|
|
159
|
+
return el(
|
|
160
|
+
"div",
|
|
161
|
+
{ className: "nextmedal" },
|
|
162
|
+
el("b", { textContent: label || "Next medal: " }),
|
|
163
|
+
...medalBody(),
|
|
164
|
+
el("span", { className: "sub" }, medalNote + (why || "")),
|
|
165
|
+
more,
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
type Mover = { r: Row; n: number; below: string; top: string; gain: number; rule: [string, number] | undefined; waits: [string, number] | false | undefined };
|
|
170
|
+
|
|
171
|
+
// Steps that raise a track's ring, largest first: lifting a scorecard's entities one level below its
|
|
172
|
+
// top to the top, since the ring averages each scorecard's share at its top level.
|
|
173
|
+
function ringMovers(D: ReportData, cards): Mover[] {
|
|
174
|
+
var rows: Row[] = cards
|
|
175
|
+
.map(function (c) {
|
|
176
|
+
return c.row;
|
|
177
|
+
})
|
|
178
|
+
.filter(function (r) {
|
|
179
|
+
return !r.compare && r.scored;
|
|
180
|
+
});
|
|
181
|
+
var out: Mover[] = [];
|
|
182
|
+
rows.forEach(function (r) {
|
|
183
|
+
var lv = (r.sc.levels || []).filter(function (l) {
|
|
184
|
+
return l !== "none";
|
|
185
|
+
});
|
|
186
|
+
if (lv.length < 1 || r.topShare >= 100) return;
|
|
187
|
+
var below = lv.length > 1 ? lv[lv.length - 2] : "none";
|
|
188
|
+
var p = (r.parts || []).find(function (x) {
|
|
189
|
+
return x.level === below;
|
|
190
|
+
});
|
|
191
|
+
var n = p ? p.n : 0;
|
|
192
|
+
var gain = n ? (n / r.scored) * 100 / rows.length : 0;
|
|
193
|
+
if (gain < 1) return;
|
|
194
|
+
var top = lv[lv.length - 1];
|
|
195
|
+
var sc = (D.scorecards || {})[r.name] || {};
|
|
196
|
+
var held: Record<string, number> = {};
|
|
197
|
+
var unrun: Record<string, number> = {};
|
|
198
|
+
var order = sc.levels || [];
|
|
199
|
+
Object.values((D.scores || {})[r.name] || {}).forEach(function (s) {
|
|
200
|
+
if (s.level !== below) return;
|
|
201
|
+
blockers(sc, s, top).forEach(function (k) {
|
|
202
|
+
held[k] = (held[k] || 0) + 1;
|
|
203
|
+
});
|
|
204
|
+
Object.keys(s.rules || {}).forEach(function (k) {
|
|
205
|
+
if (s.rules[k] === "unknown" && order.indexOf(((sc.rules || {})[k] || {}).level) <= order.indexOf(top)) unrun[k] = (unrun[k] || 0) + 1;
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
var byCount = function (x, y) {
|
|
209
|
+
return y[1] - x[1] || x[0].localeCompare(y[0]);
|
|
210
|
+
};
|
|
211
|
+
var waits = !Object.keys(held).length && (Object.entries(unrun).sort(byCount)[0] as [string, number] | undefined);
|
|
212
|
+
var rule = Object.entries(held).sort(byCount)[0] as [string, number] | undefined;
|
|
213
|
+
out.push({ r: r, n: n, below: below, top: top, gain: gain, rule: rule, waits: waits });
|
|
214
|
+
});
|
|
215
|
+
return out.sort(function (a, b) {
|
|
216
|
+
return b.gain - a.gain || a.r.name.localeCompare(b.r.name);
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function points(g: number) {
|
|
221
|
+
var p = Math.round(g);
|
|
222
|
+
return p + (p === 1 ? " point" : " points");
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// The verified step that moves a track's percentage most. The same step leads the overview and the track card.
|
|
226
|
+
export function verifiedMover(D: ReportData, cards): Mover | null {
|
|
227
|
+
if (!Object.keys(D.tracks || {}).length) return null;
|
|
228
|
+
return (
|
|
229
|
+
ringMovers(D, cards).find(function (m) {
|
|
230
|
+
return !m.waits;
|
|
231
|
+
}) || null
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export function moverBits(D: ReportData, b: Mover) {
|
|
236
|
+
return moverBody(D, b);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function moverBody(D: ReportData, b: Mover) {
|
|
240
|
+
var rule = b.rule;
|
|
241
|
+
var entities = plural(b.n, "entity", "entities") + " at " + levelName(b.below);
|
|
242
|
+
var card = link("scorecard/" + enc(b.r.name), "", title(D, b.r.name));
|
|
243
|
+
var ruleEl = rule ? [link("rule/" + enc(b.r.name) + "/" + enc(rule[0]), "", ruleWords(b.r.name, rule[0]))] : [];
|
|
244
|
+
var named = rule && rule[1] === b.n ? namedAt(D, b.r.name, b.below) : null;
|
|
245
|
+
var gain = " The track gains about " + points(b.gain) + " on this track's ring.";
|
|
246
|
+
// Naming the entities dropped the count, so a reader had to open the campaign to learn how many.
|
|
247
|
+
var kind = ((D.scorecards || {})[b.r.name] || {}).kind;
|
|
248
|
+
var count = kind ? kindCount(D, kind, b.n) : plural(b.n, "entity", "entities");
|
|
249
|
+
if (named) return [count + ", ", ...named, ", on ", card, b.n === 1 ? " needs " : " need ", ...ruleEl, ".", gain];
|
|
250
|
+
return [
|
|
251
|
+
...(rule && rule[1] === b.n
|
|
252
|
+
? [entities + " on ", card, b.n === 1 ? " needs " : " need ", ...ruleEl, "."]
|
|
253
|
+
: ["Lift " + entities + " to " + levelName(b.top) + " on ", card, ...(rule ? [". " + rule[1] + (rule[1] === 1 ? " needs " : " need "), ...ruleEl, " first."] : ["."])]),
|
|
254
|
+
gain,
|
|
255
|
+
];
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function moverEl(D: ReportData, b: Mover) {
|
|
259
|
+
var waits = b.waits as [string, number];
|
|
260
|
+
var card = link("scorecard/" + enc(b.r.name), "", title(D, b.r.name));
|
|
261
|
+
return el(
|
|
262
|
+
"div",
|
|
263
|
+
{ className: "sub mover" },
|
|
264
|
+
el("b", { textContent: "Unverified, run a check first: " }),
|
|
265
|
+
plural(b.n, "entity", "entities") + " at " + levelName(b.below) + " on ",
|
|
266
|
+
card,
|
|
267
|
+
" may already meet " + levelName(b.top) + ", but its check ",
|
|
268
|
+
link("rule/" + enc(b.r.name) + "/" + enc(waits[0]), "", ruleWords(b.r.name, waits[0])),
|
|
269
|
+
" was not run in this build, so the gain is unknown until it runs.",
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export function ownerName(o) {
|
|
274
|
+
return String(o || "").replace(/^(group|user):/, "");
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// A standard capped at its top level is unblocked by whoever owns it adding the next level.
|
|
278
|
+
export function stuckNote(D: ReportData, name, next) {
|
|
279
|
+
var owner = ownerName(((D.scorecards || {})[name] || {}).owner);
|
|
280
|
+
return el("span", { className: "sub" }, " needs a " + next + " level defined" + (owner ? " by its owner, " + owner : "") + ": ", link("scorecard/" + enc(name), "", "see its levels →"));
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// Campaigns measure progress to a target, so they get a bar with a target tick; rings are kept for
|
|
284
|
+
// a track's share at the top level.
|
|
285
|
+
export function goalWords(each) {
|
|
286
|
+
return (
|
|
287
|
+
each.filter(function (p) {
|
|
288
|
+
return p.met;
|
|
289
|
+
}).length +
|
|
290
|
+
" of " +
|
|
291
|
+
plural(each.length, "scorecard") +
|
|
292
|
+
" at target"
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export function goalBar(state, pct, target, big, each?) {
|
|
297
|
+
var empty = state === "empty";
|
|
298
|
+
var parts = (each || []).length;
|
|
299
|
+
var met = (each || []).filter(function (p) {
|
|
300
|
+
return p.met;
|
|
301
|
+
}).length;
|
|
302
|
+
var g = el(
|
|
303
|
+
"div",
|
|
304
|
+
{ className: "cprog" + (big ? " big" : "") + (state === "on" || (empty && parts && met === parts) ? " on" : "") },
|
|
305
|
+
el("b", { textContent: empty ? (parts ? met + " of " + parts + (big ? " scorecards" : "") : "—") : pct + "%" }),
|
|
306
|
+
empty ? null : el("div", { className: "gb" }, el("span", { style: "width:" + Math.max(0, Math.min(100, pct)) + "%" }), el("i", { style: "left:calc(" + target + "% - 1px)" })),
|
|
307
|
+
el("small", { textContent: empty ? (parts ? (met === parts ? "at target" : "target " + target + "%") : "no single score") : "target " + target + "%" }),
|
|
308
|
+
);
|
|
309
|
+
g.setAttribute("role", "img");
|
|
310
|
+
g.setAttribute("aria-label", empty ? (parts ? met + " of " + plural(parts, "scorecard") + (met === parts ? " at the target of " : " toward the target of ") + target + "%, each scored on its own" : "no single score") : pct + "% of entities meet the goal; target " + target + "%");
|
|
311
|
+
return g;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// hollowPct is the part of pct that passes only because no agent has assessed it; it is hatched.
|
|
315
|
+
// Pass, fail, not run and not assessed, each a segment, so the four counts are the whole bar.
|
|
316
|
+
export function checkBar(parts: { cls: string; n: number }[], label) {
|
|
317
|
+
var total = parts.reduce(function (s, p) {
|
|
318
|
+
return s + p.n;
|
|
319
|
+
}, 0) || 1;
|
|
320
|
+
var g = el(
|
|
321
|
+
"div",
|
|
322
|
+
{ className: "kpi-bar parts" },
|
|
323
|
+
...parts.filter(function (p) { return p.n > 0; }).map(function (p) {
|
|
324
|
+
return el("span", { className: p.cls, style: "width:" + (100 * p.n) / total + "%" });
|
|
325
|
+
}),
|
|
326
|
+
);
|
|
327
|
+
g.setAttribute("role", "img");
|
|
328
|
+
g.setAttribute("aria-label", label);
|
|
329
|
+
return g;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export function kpiBar(pct, label, short?, hollowPct?) {
|
|
333
|
+
var clamp = function (v) {
|
|
334
|
+
return Math.max(0, Math.min(100, v || 0));
|
|
335
|
+
};
|
|
336
|
+
var g = el(
|
|
337
|
+
"div",
|
|
338
|
+
{ className: "kpi-bar" + (short ? " short" : "") },
|
|
339
|
+
el("span", { style: "width:" + clamp(pct - (hollowPct || 0)) + "%" }),
|
|
340
|
+
hollowPct ? el("span", { className: "hollow", style: "width:" + clamp(hollowPct) + "%" }) : null,
|
|
341
|
+
);
|
|
342
|
+
g.setAttribute("role", "img");
|
|
343
|
+
g.setAttribute("aria-label", label);
|
|
344
|
+
return g;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
export function kpiPips(on, total, label) {
|
|
348
|
+
var g = el(
|
|
349
|
+
"div",
|
|
350
|
+
{ className: "kpi-pips" },
|
|
351
|
+
...Array.from({ length: Math.min(total, 12) }, function (_, i) {
|
|
352
|
+
return el("span", { className: i < on ? "on" : "" });
|
|
353
|
+
}),
|
|
354
|
+
);
|
|
355
|
+
g.setAttribute("role", "img");
|
|
356
|
+
g.setAttribute("aria-label", label);
|
|
357
|
+
return g;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
export function nextLevel(sc, level) {
|
|
361
|
+
var order = sc.levels || [];
|
|
362
|
+
if (level === "none") return order[0] || null;
|
|
363
|
+
var i = order.indexOf(level);
|
|
364
|
+
return i >= 0 && i < order.length - 1 ? order[i + 1] : null;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export function campaignState(c) {
|
|
368
|
+
if (!c.inScope && c.measure == null) return "empty";
|
|
369
|
+
return c.onTarget ? "on" : "off";
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// A campaign whose scorecards rate different kinds has no shared scope, so each included scorecard is
|
|
373
|
+
// measured against the campaign's target on its own.
|
|
374
|
+
export function includeProgress(D: ReportData, c) {
|
|
375
|
+
var target = c.targetPercent == null ? 100 : c.targetPercent;
|
|
376
|
+
return (c.includes || []).map(function (i) {
|
|
377
|
+
var sc = (D.scorecards || {})[i.scorecard];
|
|
378
|
+
var order = levelOrder(sc && sc.levels);
|
|
379
|
+
var goal = i.atLeast ? order.indexOf(i.atLeast) : order.length - 2;
|
|
380
|
+
var ratedIds = Object.keys((D.scores || {})[i.scorecard] || {}).filter(function (id) {
|
|
381
|
+
return D.scores[i.scorecard][id].level !== "skipped";
|
|
382
|
+
});
|
|
383
|
+
var rated = ratedIds.map(function (id) {
|
|
384
|
+
return D.scores[i.scorecard][id];
|
|
385
|
+
});
|
|
386
|
+
var below = rated.filter(function (x) {
|
|
387
|
+
return order.indexOf(x.level) < goal;
|
|
388
|
+
}).length;
|
|
389
|
+
var hollow = ratedIds.filter(function (id) {
|
|
390
|
+
return order.indexOf(D.scores[i.scorecard][id].level) >= goal && unassessedIn(D, i.scorecard, id);
|
|
391
|
+
}).length;
|
|
392
|
+
var pct = rated.length ? Math.floor((100 * (rated.length - below)) / rated.length) : 0;
|
|
393
|
+
return { name: i.scorecard, title: title(D, i.scorecard), known: !!sc, goal: order[goal], rated: rated.length, below: below, hollow: hollow, pct: pct, met: !!sc && pct >= target };
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// "on", "off" or "empty" for every campaign, judging one scored per scorecard by its scorecards.
|
|
398
|
+
export function campaignStanding(D: ReportData, c) {
|
|
399
|
+
var state = campaignState(c);
|
|
400
|
+
if (state !== "empty") return state;
|
|
401
|
+
var each = includeProgress(D, c).filter(function (p) {
|
|
402
|
+
return p.known;
|
|
403
|
+
});
|
|
404
|
+
if (!each.length) return "empty";
|
|
405
|
+
return each.every(function (p) {
|
|
406
|
+
return p.met;
|
|
407
|
+
})
|
|
408
|
+
? "on"
|
|
409
|
+
: "off";
|
|
410
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import type { ReportData } from "../types";
|
|
2
|
+
import { BASIS, basisBadge, basisCounts, basisIcon } from "./basis";
|
|
3
|
+
import { el } from "./dom";
|
|
4
|
+
import { title } from "./rings";
|
|
5
|
+
import { ruleTitle } from "./rules";
|
|
6
|
+
import { blockers } from "./status";
|
|
7
|
+
|
|
8
|
+
// The setup facts no rule reads, as one line: "corpus 10,000 · runs 5 · version Seikika … vs Backstage …".
|
|
9
|
+
export var TIMED_UNITS = ["ms", "s", "rps", "seconds", "minutes", "req/s"];
|
|
10
|
+
|
|
11
|
+
export function setupBrief(context, ids, facts, who) {
|
|
12
|
+
var show = function (v) {
|
|
13
|
+
return typeof v === "number" ? num(v) : typeof v === "boolean" ? (v ? "yes" : "no") : String(v);
|
|
14
|
+
};
|
|
15
|
+
return Object.keys(context)
|
|
16
|
+
.sort()
|
|
17
|
+
.map(function (f) {
|
|
18
|
+
var vals = ids.map(function (id) {
|
|
19
|
+
return (facts[id] || {})[f];
|
|
20
|
+
});
|
|
21
|
+
var same = vals.every(function (v) {
|
|
22
|
+
return JSON.stringify(v) === JSON.stringify(vals[0]);
|
|
23
|
+
});
|
|
24
|
+
if (same) return humanize(f).toLowerCase() + " " + show(vals[0]);
|
|
25
|
+
return (
|
|
26
|
+
humanize(f).toLowerCase() +
|
|
27
|
+
" " +
|
|
28
|
+
ids
|
|
29
|
+
.map(function (id, i) {
|
|
30
|
+
return vals[i] == null ? null : who(id) + " " + show(vals[i]);
|
|
31
|
+
})
|
|
32
|
+
.filter(Boolean)
|
|
33
|
+
.join(" vs ")
|
|
34
|
+
);
|
|
35
|
+
})
|
|
36
|
+
.filter(Boolean);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function humanize(s) {
|
|
40
|
+
var t = String(s || "")
|
|
41
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
42
|
+
.replace(/[-_]+/g, " ")
|
|
43
|
+
.toLowerCase();
|
|
44
|
+
return t.charAt(0).toUpperCase() + t.slice(1);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function num(v) {
|
|
48
|
+
return Number(v).toLocaleString("en-US", { maximumFractionDigits: 2 });
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// A comparison rule in the reader's words: the description of the one fact it reads.
|
|
52
|
+
export function measureLabel(c, r) {
|
|
53
|
+
var fs = (c.reads || {})[r] || [];
|
|
54
|
+
var d = fs.length === 1 ? (c.fields || {})[fs[0]] : null;
|
|
55
|
+
return d ? d.replace(/\.$/, "") : humanize(r);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function ruleValue(c, r, id) {
|
|
59
|
+
var s = (c.scores || {})[id];
|
|
60
|
+
var v = s && s.values ? s.values[r] : undefined;
|
|
61
|
+
if (v != null) return v;
|
|
62
|
+
var fs = (c.reads || {})[r] || [];
|
|
63
|
+
return fs.length === 1 ? ((c.facts || {})[id] || {})[fs[0]] : undefined;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function valueLine(c, r, rd, ids, who) {
|
|
67
|
+
var vals = ids.map(function (id) {
|
|
68
|
+
return ruleValue(c, r, id);
|
|
69
|
+
});
|
|
70
|
+
var numeric = vals.every(function (v) {
|
|
71
|
+
return v == null || typeof v === "number";
|
|
72
|
+
});
|
|
73
|
+
var peer = (c.peers || [])[0];
|
|
74
|
+
var st = (c.rules[r] || {})[peer];
|
|
75
|
+
var leader = st === "ahead" ? c.subject : st === "behind" ? peer : null;
|
|
76
|
+
var max = Math.max.apply(
|
|
77
|
+
null,
|
|
78
|
+
vals
|
|
79
|
+
.filter(function (v) {
|
|
80
|
+
return typeof v === "number";
|
|
81
|
+
})
|
|
82
|
+
.concat([0]),
|
|
83
|
+
);
|
|
84
|
+
var yesNo = vals.every(function (v) {
|
|
85
|
+
return typeof v === "boolean";
|
|
86
|
+
});
|
|
87
|
+
return el(
|
|
88
|
+
"div",
|
|
89
|
+
{ className: "vals" + (yesNo ? " yn" : "") },
|
|
90
|
+
...ids.map(function (id, i) {
|
|
91
|
+
var outcome = (((c.scores || {})[id] || {}).rules || {})[r] || "unknown";
|
|
92
|
+
var v = vals[i];
|
|
93
|
+
var color = id === leader ? "var(--pass)" : st === "parity" ? "var(--muted)" : "var(--faint)";
|
|
94
|
+
var text =
|
|
95
|
+
v == null
|
|
96
|
+
? outcome === "pass"
|
|
97
|
+
? "✓ meets it"
|
|
98
|
+
: outcome === "fail"
|
|
99
|
+
? "✗ misses it"
|
|
100
|
+
: "not measured"
|
|
101
|
+
: typeof v === "boolean"
|
|
102
|
+
? v
|
|
103
|
+
? "✓ yes"
|
|
104
|
+
: "✗ no"
|
|
105
|
+
: typeof v === "number"
|
|
106
|
+
? num(v) + (rd.unit ? " " + rd.unit : "")
|
|
107
|
+
: String(v);
|
|
108
|
+
var fillEl: HTMLElement | null = null;
|
|
109
|
+
if (numeric && typeof v === "number" && max > 0) {
|
|
110
|
+
var fill = el("span", { style: "background:" + color });
|
|
111
|
+
fillEl = fill;
|
|
112
|
+
var w = Math.max(2, (v / max) * 100);
|
|
113
|
+
requestAnimationFrame(function () {
|
|
114
|
+
requestAnimationFrame(function () {
|
|
115
|
+
fill.style.width = w + "%";
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
return el(
|
|
120
|
+
"div",
|
|
121
|
+
{ className: "val" },
|
|
122
|
+
el("span", { className: "who", textContent: who(id), title: who(id) }),
|
|
123
|
+
fillEl ? el("span", { className: "vb" }, fillEl) : el("span"),
|
|
124
|
+
// Colour marks the leader only; the rule's absolute bar, when it has one, is in the hover text.
|
|
125
|
+
el("span", { className: "v " + (id === leader ? "pass" : ""), textContent: text, title: outcome === "pass" ? "Meets the rule's bar" : outcome === "fail" ? "Misses the rule's bar" : "" }),
|
|
126
|
+
);
|
|
127
|
+
}),
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export var COUNT_UNITS = ["files", "lines", "packages", "steps", "vulnerabilities", "contributors", "bytes"];
|
|
132
|
+
export var SPAN_UNITS = ["ms", "s", "seconds", "minutes"];
|
|
133
|
+
export var SIZE_UNITS = ["MB", "KB", "GB"];
|
|
134
|
+
// The comparative a reader would say for a unit: fewer files, faster seconds, smaller megabytes.
|
|
135
|
+
export function cmpWord(rd, lower) {
|
|
136
|
+
var u = (rd && rd.unit) || "";
|
|
137
|
+
if (COUNT_UNITS.indexOf(u) >= 0) return lower ? "fewer" : "more";
|
|
138
|
+
if (SPAN_UNITS.indexOf(u) >= 0) return lower ? "faster" : "slower";
|
|
139
|
+
if (SIZE_UNITS.indexOf(u) >= 0) return lower ? "smaller" : "larger";
|
|
140
|
+
return lower ? "lower" : "higher";
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// How far apart two measured values are, in the reader's terms ("8.6× faster").
|
|
144
|
+
export function magnitude(c, r, rd, who) {
|
|
145
|
+
var peer = (c.peers || [])[0];
|
|
146
|
+
var a = ruleValue(c, r, c.subject);
|
|
147
|
+
var b = ruleValue(c, r, peer);
|
|
148
|
+
if (typeof a !== "number" || typeof b !== "number") return null;
|
|
149
|
+
if (a === b) return el("span", { className: "mag", textContent: "same value" });
|
|
150
|
+
if (a <= 0 || b <= 0) return null;
|
|
151
|
+
var ratio = Math.max(a, b) / Math.min(a, b);
|
|
152
|
+
var x = ratio >= 10 ? Math.round(ratio).toLocaleString("en-US") : ratio.toFixed(1);
|
|
153
|
+
var lead = rd.better === "lower" ? (a < b ? c.subject : peer) : rd.better === "higher" ? (a > b ? c.subject : peer) : c.subject;
|
|
154
|
+
var la = lead === c.subject ? a : b;
|
|
155
|
+
var lb = lead === c.subject ? b : a;
|
|
156
|
+
return el("span", { className: "mag", textContent: who(lead) + ": " + x + "× " + cmpWord(rd, la < lb) });
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function gapText(D: ReportData, g, id) {
|
|
160
|
+
var m = /^(.+?):atLeast (\S+) \((\S+)\)$/.exec(g);
|
|
161
|
+
if (m) {
|
|
162
|
+
var sc = (D.scorecards || {})[m[1]];
|
|
163
|
+
var score = ((D.scores || {})[m[1]] || {})[id];
|
|
164
|
+
var need = sc && score ? blockers(sc, score, m[2]) : [];
|
|
165
|
+
var card = m[1];
|
|
166
|
+
return (need.length ? "Needs " + need.map(function (r) { return "“" + ruleTitle(card, r) + "”"; }).join(", ") + " for " + m[2] : m[2].charAt(0).toUpperCase() + m[2].slice(1) + " is the goal") + " on " + title(D, m[1]) + "; " + m[3] + " now.";
|
|
167
|
+
}
|
|
168
|
+
m = /^(.+?):rule (.+) \((\S+)\)$/.exec(g);
|
|
169
|
+
if (m) return title(D, m[1]) + ": " + ({ fail: "fails", pass: "passes", unknown: "has not run" }[m[3]] || "is " + m[3]) + " “" + ruleTitle(m[1], m[2]) + "”.";
|
|
170
|
+
m = /^(.+?): unknown scorecard$/.exec(g);
|
|
171
|
+
if (m) return "Scorecard " + m[1] + " is unknown.";
|
|
172
|
+
return g;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Beside a scorecard's name in any list: whether an agent's judgement decides some of its rules.
|
|
176
|
+
export function judgedMark(D: ReportData, name, iconOnly?) {
|
|
177
|
+
var n = basisCounts(((D.scorecards || {})[name] || {}).rules);
|
|
178
|
+
if (!n.judged && !n.measured) return null;
|
|
179
|
+
var b = n.judged ? "judged" : "measured";
|
|
180
|
+
var text = n.judged ? (n.measured ? n.judged + " of " + (n.judged + n.measured) + " rules are" : "Every rule is") + " agent-judged: " + BASIS.judged.desc : "Every rule is measured: " + BASIS.measured.desc;
|
|
181
|
+
if (iconOnly) {
|
|
182
|
+
var i = el("span", { className: "bicon " + b, title: text, role: "img" }, basisIcon(b));
|
|
183
|
+
i.setAttribute("aria-label", n.judged ? "has agent-judged rules" : "every rule measured");
|
|
184
|
+
return i;
|
|
185
|
+
}
|
|
186
|
+
var m = basisBadge(b, true);
|
|
187
|
+
m.title = text;
|
|
188
|
+
return m;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// A trailing glyph never wraps onto a line of its own: it stays with the last word.
|
|
192
|
+
export function keepWithLast(text, node) {
|
|
193
|
+
if (!node) return [text];
|
|
194
|
+
var words = text.split(" ");
|
|
195
|
+
var last = words.pop();
|
|
196
|
+
return [words.length ? words.join(" ") + " " : "", el("span", { className: "nowrap" }, last + "\u00a0", node)];
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function cardName(D: ReportData, name) {
|
|
200
|
+
var t = title(D, name);
|
|
201
|
+
var mark = judgedMark(D, name);
|
|
202
|
+
var head = el("span", { className: "t" }, t, mark ? " " : null, mark);
|
|
203
|
+
return t === name ? [head] : [head, el("code", { className: "c", textContent: name })];
|
|
204
|
+
}
|
package/src/lib/dom.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export var SVGNS = "http://www.w3.org/2000/svg";
|
|
2
|
+
export var KNOWN = ["none", "bronze", "silver", "gold", "skipped"];
|
|
3
|
+
|
|
4
|
+
// What a view may hand to el(), svg() or link() as a child: falsy values and "" are skipped.
|
|
5
|
+
export type Kid = Node | string | number | null | undefined | false;
|
|
6
|
+
export type Props = Record<string, unknown> | null | undefined;
|
|
7
|
+
|
|
8
|
+
export var TAG = /^[a-z][a-z0-9]*$/;
|
|
9
|
+
export function el<K extends keyof HTMLElementTagNameMap>(t: K, a?: Props, ...kids: Kid[]): HTMLElementTagNameMap[K];
|
|
10
|
+
export function el(t: string, a?: Props, ...kids: Kid[]): HTMLElement;
|
|
11
|
+
export function el(t: string, a?: Props, ...kids: Kid[]): HTMLElement {
|
|
12
|
+
if (!TAG.test(t)) throw new Error("bad tag name");
|
|
13
|
+
var e = document.createElement(t);
|
|
14
|
+
Object.assign(e, a || {});
|
|
15
|
+
kids.forEach(function (k) {
|
|
16
|
+
if (k === "" || k == null || k === false) return;
|
|
17
|
+
e.append(typeof k === "number" ? String(k) : k);
|
|
18
|
+
});
|
|
19
|
+
return e;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function svg(t: string, attrs?: Record<string, string | number> | null, ...kids: Kid[]): SVGElement {
|
|
23
|
+
var e = document.createElementNS(SVGNS, t) as SVGElement;
|
|
24
|
+
Object.keys(attrs || {}).forEach(function (k) {
|
|
25
|
+
e.setAttribute(k, String((attrs as Record<string, string | number>)[k]));
|
|
26
|
+
});
|
|
27
|
+
kids.forEach(function (k) {
|
|
28
|
+
if (k) e.append(typeof k === "number" ? String(k) : k);
|
|
29
|
+
});
|
|
30
|
+
return e;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function link(path: string, cls?: string | null, kids?: Kid | Kid[]): HTMLAnchorElement {
|
|
34
|
+
var a = el("a", { href: "#" + path, className: cls || "" });
|
|
35
|
+
(Array.isArray(kids) ? kids : [kids]).forEach(function (k) {
|
|
36
|
+
if (k != null && k !== "" && k !== false) a.append(typeof k === "number" ? String(k) : k);
|
|
37
|
+
});
|
|
38
|
+
return a;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function enc(s: string | number): string {
|
|
42
|
+
return encodeURIComponent(s);
|
|
43
|
+
}
|