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 | 5x 5x 5x 61x 83x 5x 154x 5x 58x 48x 58x 17x 17x 15x 17x 13x 11x 11x 43x 26x 5x 50x 63x 15x 15x 15x 63x 63x 15x 38x 12x 12x 30x 23x 21x 21x 21x 42x 15x 14x 12x 12x 105x 85x 25x 25x 62x 38x 38x 38x 38x 54x 25x 16x 2x 4x | import type {
KnowledgeStats,
SourceMap,
SourceMapEdge,
SourceMapEvidence,
SourceMapKind,
SourceMapNode,
} from "@/objects/Knowledge";
import { sourceHealth } from "../figures";
import type { ForceLink, ForceNode, Ring } from "./forceLayout";
/**
* What the source map draws, read from svc-core's map (`GET
* /repositories/{id}/map`) — pure, so the canvas, the evidence panel and the
* Connections table read one model and the rules are tested without a
* canvas.
*/
/** A tie this strong or more is "strong" (the filter, the table's word). */
export const STRONG = 0.6;
export const MODERATE = 0.35;
export const strengthWord = (weight: number) =>
weight >= STRONG ? "Strong" : weight >= MODERATE ? "Moderate" : "Weak";
export const percent = (weight: number) => `${Math.round(weight * 100)}%`;
export const edgeId = (edge: Pick<SourceMapEdge, "source" | "target">) =>
`${edge.source}~${edge.target}`;
const RING: Record<string, Ring> = { source: 0, suggestion: 1, agent: 2 };
/** A node's drawn diameter: three sizes by how much it holds. */
export function sizeOf(node: SourceMapNode): number {
if (node.kind === "agent") return 40;
const items = typeof node.meta.items === "number" ? node.meta.items : 0;
return items >= 500 ? 64 : items >= 10 ? 52 : 42;
}
/** Where a source stands (`SOURCE_HEALTH`): from its full figures when the
* page has them (a member), else from what the map carries (a suggestion). */
export function nodeHealth(node: SourceMapNode, figures?: KnowledgeStats | null): string {
const type = String(node.meta.source_type ?? "");
if (figures) return sourceHealth(type, figures);
const attention = Array.isArray(node.meta.attention) ? node.meta.attention : [];
if (attention.length) return "attention";
if (type === "db" || type === "api") return "in_place";
return (typeof node.meta.items === "number" ? node.meta.items : 0) > 0 ? "ready" : "empty";
}
/** The layout's input: every node and tie of the map, whatever is shown. */
export function layoutInput(map: SourceMap): { nodes: ForceNode[]; links: ForceLink[] } {
return {
nodes: map.nodes.map((n) => ({ id: n.id, ring: RING[n.kind] ?? 0, size: sizeOf(n) })),
links: map.edges.map((e) => ({ source: e.source, target: e.target, weight: e.weight })),
};
}
export interface MapFilters {
/** The kinds of evidence whose connections are shown. */
kinds: ReadonlySet<string>;
agents: boolean;
suggestions: boolean;
strongOnly: boolean;
}
export const allKinds = (map: SourceMap | null | undefined): Set<string> =>
new Set((map?.kinds ?? []).map((k) => k.key));
export interface Visible {
nodes: Set<string>;
edges: Set<string>;
}
/**
* What the filters leave showing. A connection shows when it rests on a
* kind that is on (and is strong, when only strong ones are asked for); a
* suggestion shows only with a connection showing, since that tie is why it
* is suggested. Nothing moves: hiding is all a filter does.
*/
export function visibleIn(map: SourceMap, filters: MapFilters): Visible {
const kindOf = new Map(map.nodes.map((n) => [n.id, n.kind]));
const nodes = new Set<string>();
const edges = new Set<string>();
for (const n of map.nodes) {
if (n.kind === "source") nodes.add(n.id);
if (n.kind === "agent" && filters.agents) nodes.add(n.id);
}
for (const e of map.edges) {
if (e.kind === "cited_by") {
if (filters.agents) edges.add(edgeId(e));
continue;
}
if (!e.evidence.some((p) => filters.kinds.has(p.kind))) continue;
if (filters.strongOnly && e.weight < STRONG) continue;
const ends = [e.source, e.target];
Iif (!filters.suggestions && ends.some((id) => kindOf.get(id) === "suggestion")) continue;
edges.add(edgeId(e));
for (const id of ends) nodes.add(id);
}
return { nodes, edges };
}
/** A count in words, by its kind's unit: "370 documents in both". */
export function countPhrase(part: SourceMapEvidence, kind?: SourceMapKind): string | null {
if (!kind?.unit_one || !kind.unit_many) return null;
const n = Math.round(part.count);
return `${n.toLocaleString()} ${n === 1 ? kind.unit_one : kind.unit_many}`;
}
export interface ConnectionRow {
id: string;
edge: SourceMapEdge;
a: SourceMapNode;
b: SourceMapNode;
/** One end is a suggestion — a source outside the repository. */
suggestion: SourceMapNode | null;
kinds: string[];
}
/** Every connection showing, strongest first: the map as a table. */
export function connectionRows(map: SourceMap, visible: Visible): ConnectionRow[] {
const byId = new Map(map.nodes.map((n) => [n.id, n]));
const labels = new Map(map.kinds.map((k) => [k.key, k.label]));
const rows: ConnectionRow[] = [];
for (const edge of map.edges) {
if (edge.kind !== "evidence" || !visible.edges.has(edgeId(edge))) continue;
const a = byId.get(edge.source);
const b = byId.get(edge.target);
Iif (!a || !b) continue;
rows.push({
id: edgeId(edge),
edge,
a: a.kind === "suggestion" ? b : a,
b: a.kind === "suggestion" ? a : b,
suggestion: a.kind === "suggestion" ? a : b.kind === "suggestion" ? b : null,
kinds: edge.evidence.map((p) => labels.get(p.kind) ?? p.kind),
});
}
return rows.sort(
(x, y) =>
Number(!!x.suggestion) - Number(!!y.suggestion) ||
y.edge.weight - x.edge.weight ||
x.id.localeCompare(y.id),
);
}
/** The ties of one node that are showing, strongest first. */
export function tiesOf(map: SourceMap, nodeId: string, visible: Visible): ConnectionRow[] {
return connectionRows(map, visible).filter(
(row) => row.a.id === nodeId || row.b.id === nodeId,
);
}
|