Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | 2x 2x 2x 2x 42x 2x 2x 2x 2x 2x 2x 2x 2x 42x 42x 2x 2x 2x 10x 2x 10x 2x 34x 68x 4x 34x 2x 42x 2x 2x 2x 2x 34x 42x 2x 10x 8x 2x 2x 42x 34x 2x 10x 2x 2x 2x 2x 2x 6x 2x 4x 22x 2x 2x | /**
* One imported transfer as a 3D overview (#876): only the import, its
* relations and where it came from. /overview?import=<transfer id>&spe=SPE-1
* builds it from GET /api/spe-imports/[id]/graph, so the retired graph
* explorer is no longer needed to look at an import.
*/
import { didToName } from "@/lib/participant-name";
import type {
Fact,
OverviewLayer,
OverviewLink,
OverviewNode,
OverviewSignal,
OverviewView,
} from "./types";
/** What GET /api/spe-imports/[id]/graph answers. */
export interface ImportGraph {
dataset: Record<string, unknown>;
nodes: Array<{
id: string;
label: string;
name: string;
group?: string;
properties?: Record<string, string | number | boolean>;
}>;
links: Array<{ source: string; target: string; type: string }>;
truncated?: boolean;
}
const LAYERS: OverviewLayer[] = [
{
id: "origin",
name: "Where it came from: provider and contract",
z: 1,
color: "#94a3b8",
},
{
id: "spe",
name: "The secure processing environment and the import",
z: 0,
color: "#a78bfa",
},
{ id: "records", name: "The imported records", z: -1, color: "#22c55e" },
];
const LINK_KIND: Record<string, string> = {
LOADED_INTO: "loaded into",
CONTAINS: "contains",
REFERS_TO: "refers to",
PROVIDED_BY: "provided by",
UNDER_AGREEMENT: "under the contract",
};
const RECORD_COLOR: Record<string, string> = {
Patient: "#22c55e",
Condition: "#f59e0b",
Observation: "#38bdf8",
MedicationRequest: "#f472b6",
Encounter: "#a3e635",
};
const facts = (p: Record<string, string | number | boolean> = {}): Fact[] =>
Object.entries(p).map(([k, v]) => [k, String(v)]);
/** Above this many records the scene names them on hover only. */
const LABELLED_RECORDS = 20;
export function importOverview(graph: ImportGraph): OverviewView {
const d = graph.dataset;
const speId = String(d.speId ?? "");
const importedAt = String(d.importedAt ?? "");
const provider = String(d.provider ?? "");
const providerName = provider ? didToName(provider) : "";
const datasetNodeId =
graph.nodes.find((n) => n.label === "ImportedDataset")?.id ?? "";
const recordCount = graph.nodes.filter(
(n) => n.label === "ImportedRecord",
).length;
const nameOf = new Map(graph.nodes.map((n) => [n.id, n.name]));
// What each record refers to and what refers to it: a patient's
// observations, an observation's patient (#876).
const refersTo = new Map<string, string[]>();
const referredBy = new Map<string, string[]>();
for (const l of graph.links) {
if (l.type !== "REFERS_TO") continue;
refersTo.set(l.source, [...(refersTo.get(l.source) ?? []), l.target]);
referredBy.set(l.target, [...(referredBy.get(l.target) ?? []), l.source]);
}
const related = (id: string): Fact[] => {
const list = (ids: string[] | undefined, title: string): Fact[] =>
ids?.length
? [
[
`${title} (${ids.length})`,
ids
.slice(0, 8)
.map((x) => nameOf.get(x) ?? x)
.join("; ") + (ids.length > 8 ? "; …" : ""),
],
]
: [];
return [
...list(refersTo.get(id), "Refers to"),
...list(referredBy.get(id), "Referred to by"),
];
};
const nodes: OverviewNode[] = graph.nodes.map((n) => {
switch (n.label) {
case "ImportedDataset":
return {
id: n.id,
label: n.name,
layer: "spe",
kind: "Imported dataset",
size: 4,
color: "#2dd4bf",
pulse: true,
title: n.name,
facts: facts(n.properties),
article: "Art. 73",
};
case "SpeWorkspace":
return {
id: n.id,
label: n.name,
layer: "spe",
kind: "Secure processing environment",
size: 3,
color: "#a78bfa",
title: n.name,
facts: facts(n.properties),
article: "Art. 73",
};
case "Participant":
return {
id: n.id,
label: providerName || n.name,
layer: "origin",
kind: "Provider",
size: 3,
color: "#f97316",
title: providerName || n.name,
facts: facts(n.properties),
};
case "Contract":
return {
id: n.id,
label: n.name.slice(0, 8),
layer: "origin",
kind: "Contract",
size: 2,
color: "#94a3b8",
title: n.name,
facts: facts(n.properties),
};
default:
return {
id: n.id,
label: recordCount > LABELLED_RECORDS ? "" : n.name,
layer: "records",
kind: n.group ?? "Record",
size: 1,
color: RECORD_COLOR[n.group ?? ""] ?? "#93c5fd",
title: n.name,
sub: n.group,
facts: [...facts(n.properties), ...related(n.id)],
};
}
});
const known = new Set(nodes.map((n) => n.id));
const links: OverviewLink[] = graph.links
.filter((l) => known.has(l.source) && known.has(l.target))
.map((l) => ({
source: l.source,
target: l.target,
kind: LINK_KIND[l.type] ?? l.type.toLowerCase(),
arrow: true,
particles: l.type === "LOADED_INTO" ? 2 : 0,
}));
const byType = new Map<string, number>();
for (const n of graph.nodes) {
if (n.label === "ImportedRecord") {
byType.set(
n.group ?? "Record",
(byType.get(n.group ?? "Record") ?? 0) + 1,
);
}
}
const records = Number(d.records ?? 0);
const refs = graph.links.filter((l) => l.type === "REFERS_TO").length;
const signals: OverviewSignal[] = [
{
severity: "ok",
nodeId: datasetNodeId,
code: "import-done",
text: `${records} records imported into ${speId}${
importedAt ? ` on ${importedAt.slice(0, 10)}` : ""
}, with ${refs} references between them.`,
article: "Art. 73",
},
];
Eif (d.pseudonymised === true) {
signals.push({
severity: "ok",
nodeId: datasetNodeId,
code: "import-pseudonymised",
text: "Patients are pseudonymised: no names, addresses or exact birth dates.",
article: "Art. 66(3)",
});
}
Eif (providerName) {
signals.push({
severity: "info",
nodeId: nodes.find((n) => n.kind === "Provider")?.id ?? datasetNodeId,
code: "import-provider",
text: `Provided by ${providerName}${
d.agreementId
? ` under contract ${String(d.agreementId).slice(0, 8)}…`
: ""
}.`,
});
}
for (const [type, n] of byType) {
signals.push({
severity: "info",
nodeId: nodes.find((x) => x.kind === type)?.id ?? datasetNodeId,
code: "import-type",
text: `${type}: ${n}`,
});
}
Iif (graph.truncated) {
signals.push({
severity: "warn",
nodeId: datasetNodeId,
code: "import-truncated",
text: `Showing ${
byType.size ? [...byType.values()].reduce((a, b) => a + b, 0) : 0
} of ${records} records.`,
});
}
return {
persona: "researcher",
asOf: importedAt.slice(0, 10),
me: { id: String(d.consumer ?? ""), name: "" },
title: "My import, in one view",
question:
"What did this transfer bring into the SPE, from whom, and under which contract?",
article: "Regulation (EU) 2025/327 Art. 68, 73",
layers: LAYERS,
nodes,
links,
signals,
legend: [
{ color: "#2dd4bf", text: "the imported dataset" },
{ color: "#a78bfa", text: "the SPE it is in" },
{ color: "#f97316", text: "the provider" },
{ color: "#94a3b8", text: "the contract" },
{ color: "#22c55e", text: "patients" },
{ color: "#38bdf8", text: "observations" },
],
dataNote: "Every organisation and study shown is fictional.",
};
}
|