Skip to content

Commit a246f40

Browse files
authored
Graph - add counters for removed labels and properties (#2292)
* grpah - add counters for removed labels and properties * added mock graph result set statistics * docstrings for graph result set statistics * format * isort * moved docstrings into functions
1 parent 6f20821 commit a246f40

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

redis/commands/graph/query_result.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
from .path import Path
1010

1111
LABELS_ADDED = "Labels added"
12+
LABELS_REMOVED = "Labels removed"
1213
NODES_CREATED = "Nodes created"
1314
NODES_DELETED = "Nodes deleted"
1415
RELATIONSHIPS_DELETED = "Relationships deleted"
1516
PROPERTIES_SET = "Properties set"
17+
PROPERTIES_REMOVED = "Properties removed"
1618
RELATIONSHIPS_CREATED = "Relationships created"
1719
INDICES_CREATED = "Indices created"
1820
INDICES_DELETED = "Indices deleted"
@@ -21,8 +23,10 @@
2123

2224
STATS = [
2325
LABELS_ADDED,
26+
LABELS_REMOVED,
2427
NODES_CREATED,
2528
PROPERTIES_SET,
29+
PROPERTIES_REMOVED,
2630
RELATIONSHIPS_CREATED,
2731
NODES_DELETED,
2832
RELATIONSHIPS_DELETED,
@@ -323,40 +327,60 @@ def _get_stat(self, stat):
323327

324328
@property
325329
def labels_added(self):
330+
"""Returns the number of labels added in the query"""
326331
return self._get_stat(LABELS_ADDED)
327332

333+
@property
334+
def labels_removed(self):
335+
"""Returns the number of labels removed in the query"""
336+
return self._get_stat(LABELS_REMOVED)
337+
328338
@property
329339
def nodes_created(self):
340+
"""Returns the number of nodes created in the query"""
330341
return self._get_stat(NODES_CREATED)
331342

332343
@property
333344
def nodes_deleted(self):
345+
"""Returns the number of nodes deleted in the query"""
334346
return self._get_stat(NODES_DELETED)
335347

336348
@property
337349
def properties_set(self):
350+
"""Returns the number of properties set in the query"""
338351
return self._get_stat(PROPERTIES_SET)
339352

353+
@property
354+
def properties_removed(self):
355+
"""Returns the number of properties removed in the query"""
356+
return self._get_stat(PROPERTIES_REMOVED)
357+
340358
@property
341359
def relationships_created(self):
360+
"""Returns the number of relationships created in the query"""
342361
return self._get_stat(RELATIONSHIPS_CREATED)
343362

344363
@property
345364
def relationships_deleted(self):
365+
"""Returns the number of relationships deleted in the query"""
346366
return self._get_stat(RELATIONSHIPS_DELETED)
347367

348368
@property
349369
def indices_created(self):
370+
"""Returns the number of indices created in the query"""
350371
return self._get_stat(INDICES_CREATED)
351372

352373
@property
353374
def indices_deleted(self):
375+
"""Returns the number of indices deleted in the query"""
354376
return self._get_stat(INDICES_DELETED)
355377

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

360383
@property
361384
def run_time_ms(self):
385+
"""Returns the server execution time of the query"""
362386
return self._get_stat(INTERNAL_EXECUTION_TIME)

tests/test_graph.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
1+
from unittest.mock import patch
2+
13
import pytest
24

35
from redis.commands.graph import Edge, Node, Path
46
from redis.commands.graph.execution_plan import Operation
7+
from redis.commands.graph.query_result import (
8+
CACHED_EXECUTION,
9+
INDICES_CREATED,
10+
INDICES_DELETED,
11+
INTERNAL_EXECUTION_TIME,
12+
LABELS_ADDED,
13+
LABELS_REMOVED,
14+
NODES_CREATED,
15+
NODES_DELETED,
16+
PROPERTIES_REMOVED,
17+
PROPERTIES_SET,
18+
RELATIONSHIPS_CREATED,
19+
RELATIONSHIPS_DELETED,
20+
QueryResult,
21+
)
522
from redis.exceptions import ResponseError
623
from tests.conftest import skip_if_redis_enterprise
724

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

577594
redis_graph.delete()
595+
596+
597+
@pytest.mark.redismod
598+
def test_resultset_statistics(client):
599+
with patch.object(target=QueryResult, attribute="_get_stat") as mock_get_stats:
600+
result = client.graph().query("RETURN 1")
601+
result.labels_added
602+
mock_get_stats.assert_called_with(LABELS_ADDED)
603+
result.labels_removed
604+
mock_get_stats.assert_called_with(LABELS_REMOVED)
605+
result.nodes_created
606+
mock_get_stats.assert_called_with(NODES_CREATED)
607+
result.nodes_deleted
608+
mock_get_stats.assert_called_with(NODES_DELETED)
609+
result.properties_set
610+
mock_get_stats.assert_called_with(PROPERTIES_SET)
611+
result.properties_removed
612+
mock_get_stats.assert_called_with(PROPERTIES_REMOVED)
613+
result.relationships_created
614+
mock_get_stats.assert_called_with(RELATIONSHIPS_CREATED)
615+
result.relationships_deleted
616+
mock_get_stats.assert_called_with(RELATIONSHIPS_DELETED)
617+
result.indices_created
618+
mock_get_stats.assert_called_with(INDICES_CREATED)
619+
result.indices_deleted
620+
mock_get_stats.assert_called_with(INDICES_DELETED)
621+
result.cached_execution
622+
mock_get_stats.assert_called_with(CACHED_EXECUTION)
623+
result.run_time_ms
624+
mock_get_stats.assert_called_with(INTERNAL_EXECUTION_TIME)

0 commit comments

Comments
 (0)