Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graph - add counters for removed labels and properties #2292

Merged
merged 11 commits into from
Jul 26, 2022
24 changes: 24 additions & 0 deletions redis/commands/graph/query_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
from .path import Path

LABELS_ADDED = "Labels added"
LABELS_REMOVED = "Labels removed"
NODES_CREATED = "Nodes created"
NODES_DELETED = "Nodes deleted"
RELATIONSHIPS_DELETED = "Relationships deleted"
PROPERTIES_SET = "Properties set"
PROPERTIES_REMOVED = "Properties removed"
RELATIONSHIPS_CREATED = "Relationships created"
INDICES_CREATED = "Indices created"
INDICES_DELETED = "Indices deleted"
Expand All @@ -21,8 +23,10 @@

STATS = [
LABELS_ADDED,
LABELS_REMOVED,
NODES_CREATED,
PROPERTIES_SET,
PROPERTIES_REMOVED,
RELATIONSHIPS_CREATED,
NODES_DELETED,
RELATIONSHIPS_DELETED,
Expand Down Expand Up @@ -323,40 +327,60 @@ def _get_stat(self, stat):

@property
def labels_added(self):
"""Returns the number of labels added in the query"""
return self._get_stat(LABELS_ADDED)

@property
def labels_removed(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add docstrings to each of these

"""Returns the number of labels removed in the query"""
return self._get_stat(LABELS_REMOVED)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though they're "gimmes" we should have unit tests, covering each property - this way we don't break them in the future.


@property
def nodes_created(self):
"""Returns the number of nodes created in the query"""
return self._get_stat(NODES_CREATED)

@property
def nodes_deleted(self):
"""Returns the number of nodes deleted in the query"""
return self._get_stat(NODES_DELETED)

@property
def properties_set(self):
"""Returns the number of properties set in the query"""
return self._get_stat(PROPERTIES_SET)

@property
def properties_removed(self):
"""Returns the number of properties removed in the query"""
return self._get_stat(PROPERTIES_REMOVED)

@property
def relationships_created(self):
"""Returns the number of relationships created in the query"""
return self._get_stat(RELATIONSHIPS_CREATED)

@property
def relationships_deleted(self):
"""Returns the number of relationships deleted in the query"""
return self._get_stat(RELATIONSHIPS_DELETED)

@property
def indices_created(self):
"""Returns the number of indices created in the query"""
return self._get_stat(INDICES_CREATED)

@property
def indices_deleted(self):
"""Returns the number of indices deleted in the query"""
return self._get_stat(INDICES_DELETED)

@property
def cached_execution(self):
"""Returns whether or not the query execution plan was cached"""
return self._get_stat(CACHED_EXECUTION) == 1

@property
def run_time_ms(self):
"""Returns the server execution time of the query"""
return self._get_stat(INTERNAL_EXECUTION_TIME)
47 changes: 47 additions & 0 deletions tests/test_graph.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
from unittest.mock import patch

import pytest

from redis.commands.graph import Edge, Node, Path
from redis.commands.graph.execution_plan import Operation
from redis.commands.graph.query_result import (
CACHED_EXECUTION,
INDICES_CREATED,
INDICES_DELETED,
INTERNAL_EXECUTION_TIME,
LABELS_ADDED,
LABELS_REMOVED,
NODES_CREATED,
NODES_DELETED,
PROPERTIES_REMOVED,
PROPERTIES_SET,
RELATIONSHIPS_CREATED,
RELATIONSHIPS_DELETED,
QueryResult,
)
from redis.exceptions import ResponseError
from tests.conftest import skip_if_redis_enterprise

Expand Down Expand Up @@ -575,3 +592,33 @@ def test_explain(client):
assert result.structured_plan == expected

redis_graph.delete()


@pytest.mark.redismod
def test_resultset_statistics(client):
with patch.object(target=QueryResult, attribute="_get_stat") as mock_get_stats:
result = client.graph().query("RETURN 1")
result.labels_added
mock_get_stats.assert_called_with(LABELS_ADDED)
result.labels_removed
mock_get_stats.assert_called_with(LABELS_REMOVED)
result.nodes_created
mock_get_stats.assert_called_with(NODES_CREATED)
result.nodes_deleted
mock_get_stats.assert_called_with(NODES_DELETED)
result.properties_set
mock_get_stats.assert_called_with(PROPERTIES_SET)
result.properties_removed
mock_get_stats.assert_called_with(PROPERTIES_REMOVED)
result.relationships_created
mock_get_stats.assert_called_with(RELATIONSHIPS_CREATED)
result.relationships_deleted
mock_get_stats.assert_called_with(RELATIONSHIPS_DELETED)
result.indices_created
mock_get_stats.assert_called_with(INDICES_CREATED)
result.indices_deleted
mock_get_stats.assert_called_with(INDICES_DELETED)
result.cached_execution
mock_get_stats.assert_called_with(CACHED_EXECUTION)
result.run_time_ms
mock_get_stats.assert_called_with(INTERNAL_EXECUTION_TIME)