Repository navigation
Expand file tree
/
Copy pathgraph_audit.py
More file actions
281 lines (255 loc) · 10.5 KB
/
Copy pathgraph_audit.py
File metadata and controls
281 lines (255 loc) · 10.5 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
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
271
272
273
274
275
276
277
278
279
280
281
# Structural integrity audit of a generated knowledge graph (issue #646).
# Validates recorded node and relationship batches against the documented
# schema (types_defs.NODE_SCHEMAS / RELATIONSHIP_SCHEMAS): orphan nodes,
# required-property completeness, and undocumented labels, properties, and
# relationship endpoint triples.
from collections.abc import Callable, Sequence
from . import constants as cs
from . import cypher_queries as cq
from .types_defs import (
NODE_SCHEMAS,
RELATIONSHIP_SCHEMAS,
AuditViolation,
GraphNodeRecord,
GraphRelRecord,
PropertyValue,
ResultRow,
)
def documented_node_properties() -> dict[str, dict[str, bool]]:
"""Parse NODE_SCHEMAS property strings into {label: {prop: required}}."""
parsed: dict[str, dict[str, bool]] = {}
for schema in NODE_SCHEMAS:
props: dict[str, bool] = {}
for entry in schema.properties.strip(cs.SCHEMA_PROPS_BRACES).split(
cs.CHAR_COMMA
):
# A trailing comma or stray whitespace in a schema string must
# not register an empty-named required property.
if not (entry := entry.strip()):
continue
name, _, type_part = entry.partition(cs.CHAR_COLON)
props[name.strip()] = not type_part.strip().endswith(
cs.SCHEMA_OPTIONAL_SUFFIX
)
parsed[schema.label.value] = props
return parsed
def documented_relationship_triples() -> frozenset[tuple[str, str, str]]:
return frozenset(
(source.value, schema.rel_type.value, target.value)
for schema in RELATIONSHIP_SCHEMAS
for source in schema.sources
for target in schema.targets
)
def _node_key(node: GraphNodeRecord) -> PropertyValue:
if key_prop := cs.NODE_UNIQUE_CONSTRAINTS.get(node.label):
return node.properties.get(key_prop)
return None
def merge_node_records(
nodes: Sequence[GraphNodeRecord],
) -> list[GraphNodeRecord]:
"""Collapse repeated ensures of one node, merging properties.
Mirrors the ingestor's MERGE ... SET n += props semantics: the stored
node carries the union of all batched property dicts.
"""
merged: dict[tuple[str, PropertyValue], GraphNodeRecord] = {}
for node in nodes:
identity = (node.label, _node_key(node))
if existing := merged.get(identity):
existing.properties.update(node.properties)
else:
merged[identity] = GraphNodeRecord(node.label, dict(node.properties))
return list(merged.values())
def _node_identities(
nodes: Sequence[GraphNodeRecord],
) -> set[tuple[str, PropertyValue]]:
return {(node.label, _node_key(node)) for node in merge_node_records(nodes)}
def find_orphans(
nodes: Sequence[GraphNodeRecord], relationships: Sequence[GraphRelRecord]
) -> list[GraphNodeRecord]:
identities = _node_identities(nodes)
connected: set[tuple[str, PropertyValue]] = set()
for rel in relationships:
# The database MERGEs a relationship by MATCHing both endpoints, so a
# dangling edge is silently dropped and must not count as connectivity
# for the endpoint that does exist.
endpoints = [(label, value) for label, _, value in (rel.from_spec, rel.to_spec)]
if all(endpoint in identities for endpoint in endpoints):
connected.update(endpoints)
return [
node
for node in merge_node_records(nodes)
# A repo with no indexable content is just its Project root, so a
# zero-degree Project is valid rather than a construction failure.
if node.label != cs.NodeLabel.PROJECT.value
and (node.label, _node_key(node)) not in connected
]
def find_dangling_relationships(
nodes: Sequence[GraphNodeRecord], relationships: Sequence[GraphRelRecord]
) -> list[AuditViolation]:
identities = _node_identities(nodes)
violations: list[AuditViolation] = []
seen: set[tuple[str, PropertyValue, str, str, PropertyValue]] = set()
for rel in relationships:
from_label, _, from_key = rel.from_spec
to_label, _, to_key = rel.to_spec
if (from_label, from_key) in identities and (to_label, to_key) in identities:
continue
signature = (from_label, from_key, rel.rel_type, to_label, to_key)
if signature in seen:
continue
seen.add(signature)
violations.append(
AuditViolation(
cs.AuditCheck.DANGLING_RELATIONSHIP,
cs.AUDIT_DETAIL_DANGLING.format(
from_label=from_label,
from_key=from_key,
rel_type=rel.rel_type,
to_label=to_label,
to_key=to_key,
),
)
)
return violations
def find_property_violations(
nodes: Sequence[GraphNodeRecord],
) -> list[AuditViolation]:
documented = documented_node_properties()
violations: list[AuditViolation] = []
for node in merge_node_records(nodes):
schema_props = documented.get(node.label)
if schema_props is None:
violations.append(
AuditViolation(
cs.AuditCheck.UNDOCUMENTED_LABEL,
cs.AUDIT_DETAIL_UNDOCUMENTED_LABEL.format(label=node.label),
)
)
continue
key = _node_key(node)
for prop in node.properties:
if prop not in schema_props:
violations.append(
AuditViolation(
cs.AuditCheck.UNDOCUMENTED_PROPERTY,
cs.AUDIT_DETAIL_UNDOCUMENTED_PROPERTY.format(
label=node.label, key=key, prop=prop
),
)
)
for prop, required in schema_props.items():
if required and node.properties.get(prop) is None:
violations.append(
AuditViolation(
cs.AuditCheck.MISSING_REQUIRED_PROPERTY,
cs.AUDIT_DETAIL_MISSING_REQUIRED.format(
label=node.label, key=key, prop=prop
),
)
)
return violations
def find_relationship_violations(
relationships: Sequence[GraphRelRecord],
) -> list[AuditViolation]:
documented = documented_relationship_triples()
violations: list[AuditViolation] = []
seen: set[tuple[str, str, str]] = set()
for rel in relationships:
triple = (rel.from_spec[0], rel.rel_type, rel.to_spec[0])
if triple in documented or triple in seen:
continue
seen.add(triple)
violations.append(
AuditViolation(
cs.AuditCheck.UNDOCUMENTED_RELATIONSHIP,
cs.AUDIT_DETAIL_UNDOCUMENTED_RELATIONSHIP.format(
from_label=triple[0], rel_type=triple[1], to_label=triple[2]
),
)
)
return violations
def build_missing_required_query(label: str, required_props: Sequence[str]) -> str:
conditions = cq.CYPHER_AUDIT_OR.join(
cq.CYPHER_AUDIT_IS_NULL.format(prop=prop) for prop in required_props
)
return cq.CYPHER_AUDIT_MISSING_REQUIRED.format(label=label, conditions=conditions)
def collect_live_violations(
fetch_all: Callable[[str], Sequence[ResultRow]],
) -> list[AuditViolation]:
"""Run the structural audit against a live graph via Cypher (doctor)."""
violations: list[AuditViolation] = []
for row in fetch_all(cq.CYPHER_AUDIT_ORPHANS):
violations.append(
AuditViolation(
cs.AuditCheck.ORPHAN_NODE,
cs.AUDIT_DETAIL_ORPHAN_COUNT.format(
count=row["orphans"], label=row["label"]
),
)
)
documented_props = documented_node_properties()
for row in fetch_all(cq.CYPHER_AUDIT_LABELS):
if row["label"] not in documented_props:
violations.append(
AuditViolation(
cs.AuditCheck.UNDOCUMENTED_LABEL,
cs.AUDIT_DETAIL_UNDOCUMENTED_LABEL.format(label=row["label"]),
)
)
documented_triples = documented_relationship_triples()
for row in fetch_all(cq.CYPHER_AUDIT_REL_TRIPLES):
if (row["src"], row["rel"], row["dst"]) not in documented_triples:
violations.append(
AuditViolation(
cs.AuditCheck.UNDOCUMENTED_RELATIONSHIP,
cs.AUDIT_DETAIL_UNDOCUMENTED_RELATIONSHIP.format(
from_label=row["src"],
rel_type=row["rel"],
to_label=row["dst"],
),
)
)
for row in fetch_all(cq.CYPHER_AUDIT_LABEL_PROPS):
# Unknown labels are already reported above; only grade documented ones.
if (schema_props := documented_props.get(str(row["label"]))) and str(
row["key"]
) not in schema_props:
violations.append(
AuditViolation(
cs.AuditCheck.UNDOCUMENTED_PROPERTY,
cs.AUDIT_DETAIL_UNDOCUMENTED_PROPERTY_LIVE.format(
label=row["label"], prop=row["key"]
),
)
)
for label, schema_props in documented_props.items():
required = [prop for prop, is_required in schema_props.items() if is_required]
# An all-optional schema would render an empty WHERE clause, which
# is a Cypher syntax error.
if not required:
continue
rows = fetch_all(build_missing_required_query(label, required))
if rows and (count := rows[0]["missing"]):
violations.append(
AuditViolation(
cs.AuditCheck.MISSING_REQUIRED_PROPERTY,
cs.AUDIT_DETAIL_MISSING_REQUIRED_LIVE.format(
count=count, label=label
),
)
)
return violations
def collect_violations(
nodes: Sequence[GraphNodeRecord], relationships: Sequence[GraphRelRecord]
) -> list[AuditViolation]:
violations = [
AuditViolation(
cs.AuditCheck.ORPHAN_NODE,
cs.AUDIT_DETAIL_ORPHAN.format(label=node.label, key=_node_key(node)),
)
for node in find_orphans(nodes, relationships)
]
violations.extend(find_property_violations(nodes))
violations.extend(find_relationship_violations(relationships))
violations.extend(find_dangling_relationships(nodes, relationships))
return violations