perf: batch export graph database queries

This commit is contained in:
2026-08-05 12:55:37 +02:00
parent 80ef1be239
commit f5ad77b430
10 changed files with 331 additions and 128 deletions
+18 -5
View File
@@ -12,21 +12,34 @@ from .codec import serialize_object
from .graph import ObjectGraph, seed_scope
def export_scope(scope_type: str, scope_id: int, *, max_objects: int, signing_key: str = ""):
def export_scope(
scope_type: str,
scope_id: int,
*,
max_objects: int,
query_batch_size: int = 500,
signing_key: str = "",
):
root, seeds = seed_scope(scope_type, scope_id)
graph = ObjectGraph(max_objects=max_objects).collect(seeds)
graph = ObjectGraph(max_objects=max_objects, query_batch_size=query_batch_size).collect(seeds)
objects = graph.objects
exported_keys = set(objects)
assets = {}
records = [
serialize_object(obj, set(objects), assets)
for _, obj in sorted(objects.items())
serialize_object(
obj,
exported_keys,
assets,
custom_fields=graph.custom_fields_for_model(type(obj)),
)
for obj in graph.iter_loaded_objects()
]
counts = Counter(record["model"] for record in records)
manifest = {
"created_at": datetime.now(UTC).isoformat(),
"source_instance": str(InstanceIdentity.local_id()),
"source_netbox_version": getattr(getattr(settings, "RELEASE", None), "version", "4.6"),
"plugin_version": "0.2.0",
"plugin_version": "0.3.0",
"scope": {
"type": scope_type,
"source_pk": str(scope_id),