fix(memgraph): compute get_graph_metrics from graph data#123
Open
RajdeepKushwaha5 wants to merge 2 commits into
topoteretes:maintopoteretes/cognee-community:mainfrom
RajdeepKushwaha5:fix/memgraph-graph-metricsRajdeepKushwaha5/cognee-community:fix/memgraph-graph-metricsCopy head branch name to clipboard
Open
fix(memgraph): compute get_graph_metrics from graph data#123RajdeepKushwaha5 wants to merge 2 commits intotopoteretes:maintopoteretes/cognee-community:mainfrom RajdeepKushwaha5:fix/memgraph-graph-metricsRajdeepKushwaha5/cognee-community:fix/memgraph-graph-metricsCopy head branch name to clipboard
RajdeepKushwaha5 wants to merge 2 commits into
topoteretes:maintopoteretes/cognee-community:mainfrom
RajdeepKushwaha5:fix/memgraph-graph-metricsRajdeepKushwaha5/cognee-community:fix/memgraph-graph-metricsCopy head branch name to clipboard
Conversation
MemgraphAdapter.get_graph_metrics() filtered on :Node/:EDGE labels that this adapter never writes (the queries were copied from the Kuzu adapter, which uses a fixed :Node/:EDGE schema) and indexed dict-shaped query results positionally (node_count[0][0]). Both made the method raise and fall through to the all-zeros fallback, so every metric was wrong for any non-empty graph. The connected-components query additionally collapsed all nodes into a single component via a broken COLLECT aggregation. Compute the metrics in-process from get_graph_data() instead: - num_nodes, num_edges, mean_degree, edge_density - connected components via a self-contained union-find helper - num_selfloops from edge endpoints diameter, avg_shortest_path_length and avg_clustering require all-pairs traversals that are prohibitively expensive, so they are reported as unsupported (-1), matching the Neptune adapter. Add unit tests covering the component helper and get_graph_metrics with a stubbed get_graph_data() (no live Memgraph required). Signed-off-by: Rajdeep Singh <rajdeep.kuswaha@s.amity.edu>
… the fix The memgraph CI workflow runs examples/example.py (not pytest), and the example never called get_graph_metrics(), so the metrics fix had no end-to-end coverage in CI. Print get_graph_metrics(include_optional=True) after fetching the graph data so a live Memgraph run exercises the corrected code path. Signed-off-by: Rajdeep Singh <rajdeep.kuswaha@s.amity.edu>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
MemgraphAdapter.get_graph_metrics()returned all-zero / fallback metrics for any non-empty graph. This PR computes the metrics correctly from the live graph and adds both unit tests and end-to-end CI coverage.Closes #122.
Root causes (all three fixed):
node_count[0][0]raisedKeyError(query()returns dicts keyed by theRETURNname), which the broadexceptswallowed,returning the all-zeros fallback.MATCH (n:Node)/[:EDGE](copied from the Kuzu adapter), but this adapter writes unlabeled nodes and real relationship-typed edges, so every labelled query matched nothing.WITH n.id AS node_id,nwent out of scope and was rebound to the whole graph, collapsing every node into a single component.What changed:
memgraph_adapter.py— compute metrics in-process fromget_graph_data():num_nodes,num_edges,mean_degree,edge_densityweakly_connected_component_sizes)num_selfloopsfrom edge endpointsdiameter,avg_shortest_path_length,avg_clusteringreported as unsupported (-1), matching the Neptune adapter (cognee#3171), since they need prohibitively expensive all-pairs traversals.tests/test_graph_metrics.py— unit tests for the component helper andget_graph_metrics()with a stubbedget_graph_data()(no live Memgraph required).examples/example.py— callget_graph_metrics(include_optional=True)so the Memgraph CI workflow (which runsexamples/example.py) exercises the corrected path end-to-end against a live instance.Testing:
pytest tests/test_graph_metrics.py→ 9 passed (against cognee 1.2.x).ruff check→ passed on all changed files.examples/example.pynow prints real, non-zero metrics on a live Memgraph run.DCO Affirmation
I affirm that all code in every commit of this pull request conforms to the terms of the Topoteretes Developer Certificate of Origin.