-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPmgModel.js
More file actions
193 lines (174 loc) · 5.9 KB
/
Copy pathPmgModel.js
File metadata and controls
193 lines (174 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
.pragma library
// PmgModel is the pure logic core. It turns PMG event-log lines into the shapes
// the QML binds to. It has no framework code, so plain JS tests cover it
// (test/model.test.mjs).
// Map each verdict to a display severity: block (red), hold (amber),
// allow (green), info (muted).
var SEVERITY = {
malware_blocked: "block",
malware_confirmed: "block",
install_insecure_bypass: "block",
dependency_cooldown: "hold",
install_allowed: "allow",
install_trusted_allowed: "allow",
sandbox_override: "info",
};
var LABEL = {
malware_blocked: "blocked",
malware_confirmed: "allowed by user",
install_insecure_bypass: "insecure bypass",
dependency_cooldown: "cooldown hold",
install_allowed: "allowed",
install_trusted_allowed: "trusted",
sandbox_override: "sandbox override",
};
// True for events the feed shows. Lifecycle and bookkeeping events are excluded.
function isDisplayable(ev) {
return !!ev && SEVERITY.hasOwnProperty(ev.event_type);
}
// True only for a real block. malware_confirmed is a user override, not a block.
function isBlock(ev) {
return !!ev && ev.event_type === "malware_blocked";
}
function severityOf(ev) {
return (ev && SEVERITY[ev.event_type]) || "info";
}
function labelOf(ev) {
return (ev && LABEL[ev.event_type]) || (ev ? ev.event_type : "");
}
// Parse one JSONL line to an event, or null for a blank or bad line.
function parseLine(line) {
var trimmed = String(line || "").trim();
if (!trimmed) return null;
try {
var ev = JSON.parse(trimmed);
return ev && typeof ev === "object" ? ev : null;
} catch (e) {
return null;
}
}
// parseLog parses a whole log file body into events, oldest-first as written.
function parseLog(text) {
var out = [];
var lines = String(text || "").split("\n");
for (var i = 0; i < lines.length; i++) {
var ev = parseLine(lines[i]);
if (ev) out.push(ev);
}
return out;
}
function timestampMillis(ev) {
if (!ev || !ev.timestamp) return 0;
var t = Date.parse(ev.timestamp);
return isNaN(t) ? 0 : t;
}
// Turn the proto enum ecosystem into a label, so ECOSYSTEM_PYPI becomes pypi.
// Unspecified or unknown becomes "" so the UI shows nothing.
function humanizeEcosystem(eco) {
var s = String(eco || "").replace(/^ECOSYSTEM_/i, "").toLowerCase();
if (s === "unspecified" || s === "unknown") return "";
return s;
}
// feedRows projects displayable events into newest-first view rows. It merges
// all identical rows into one row with a count, keeping the newest occurrence,
// then caps the result. Merging is by key, not by adjacency, so alternating
// duplicates (A, B, A, B) still collapse to A and B.
function feedRows(events, limit, nowMillis) {
var rows = [];
for (var i = 0; i < events.length; i++) {
var ev = events[i];
if (!isDisplayable(ev)) continue;
rows.push({
severity: severityOf(ev),
verdict: labelOf(ev),
ecosystem: humanizeEcosystem(ev.ecosystem),
pkg: ev.package_name || "",
version: ev.version || "",
ts: timestampMillis(ev),
ago: relativeTime(timestampMillis(ev), nowMillis),
count: 1,
});
}
rows.reverse();
var groups = {};
var order = [];
for (var j = 0; j < rows.length; j++) {
var r = rows[j];
var key = feedRowKey(r);
if (groups[key]) {
groups[key].count++;
} else {
groups[key] = r;
order.push(key);
}
}
var collapsed = order.map(function (k) { return groups[k]; });
if (limit && collapsed.length > limit) collapsed = collapsed.slice(0, limit);
return collapsed;
}
function feedRowKey(r) {
return [r.pkg, r.version, r.ecosystem, r.severity, r.verdict].join("