Skip to content

Commit 7e02ab4

Browse files
aweylswilly22
authored andcommitted
Expose the statistics data as properties (#2)
Moved the ResultQuery class and exposed the statistics data as properties. (Those changes in redisgraph-py and redis-graph will work also without the changes in the C code because it will retrieve the data as 0s)
1 parent 69fc391 commit 7e02ab4

File tree

3 files changed

+80
-28
lines changed

3 files changed

+80
-28
lines changed

redisgraph/client.py

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import redis
21
import random
32
import string
4-
from prettytable import PrettyTable
3+
4+
from query_result import QueryResult
5+
56

67
def random_string(length=10):
78
"""
89
Returns a random N chracter long string.
910
"""
1011
return ''.join(random.choice(string.lowercase) for x in range(length))
1112

13+
1214
class Node(object):
1315
"""
1416
A node within the garph.
@@ -28,6 +30,7 @@ def __str__(self):
2830
label=self.label,
2931
properties=','.join(key+':'+str(val) for key, val in self.properties.iteritems()))
3032

33+
3134
class Edge(object):
3235
"""
3336
An edge connecting two nodes.
@@ -56,35 +59,12 @@ def __str__(self):
5659
relation=self.relation,
5760
dest_alias=self.dest_node.alias)
5861

62+
5963
class Graph(object):
6064
"""
6165
Graph, collection of nodes and edges.
6266
"""
6367

64-
class QueryResult(object):
65-
def __init__(self, result_set, statistics):
66-
self.result_set = result_set
67-
self.statistics = statistics
68-
69-
"""Prints the data from the query response:
70-
1. First row result_set contains the columns names. Thus the first row in PrettyTable
71-
will contain the columns.
72-
2. The row after that will contain the data returned, or 'No Data returned' if there is none.
73-
3. Prints the statistics of the query.
74-
"""
75-
def pretty_print(self):
76-
tbl = PrettyTable(self.result_set[0])
77-
for row in self.result_set[1:]:
78-
tbl.add_row(row)
79-
80-
if len(self.result_set) == 1:
81-
tbl.add_row(['No data returned.'])
82-
83-
print(str(tbl) + '\n')
84-
85-
for stat in self.statistics:
86-
print(stat)
87-
8868
def __init__(self, name, redis_con):
8969
"""
9070
Create a new graph.
@@ -137,7 +117,7 @@ def query(self, q):
137117
data = response[0]
138118
statistics = response[1]
139119
result_set = [res.split(',') for res in data]
140-
return self.QueryResult(result_set, statistics)
120+
return QueryResult(result_set, statistics)
141121

142122
def execution_plan(self, query):
143123
"""

redisgraph/query_result.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from prettytable import PrettyTable
2+
3+
4+
class QueryResult(object):
5+
6+
LABELS_ADDED = 'Labels added'
7+
NODES_CREATED = 'Nodes created'
8+
PROPERTIES_SET = 'Properties set'
9+
RELATIONSHIPS_CREATED = 'Relationships created'
10+
INTERNAL_EXECUTION_TIME = 'internal execution time'
11+
12+
def __init__(self, result_set, statistics):
13+
self.result_set = result_set
14+
self.statistics = statistics
15+
self.parsed_statistics = self._retrieve_data_from_statistics(statistics)
16+
17+
18+
"""Prints the data from the query response:
19+
1. First row result_set contains the columns names. Thus the first row in PrettyTable
20+
will contain the columns.
21+
2. The row after that will contain the data returned, or 'No Data returned' if there is none.
22+
3. Prints the statistics of the query.
23+
"""
24+
def pretty_print(self):
25+
tbl = PrettyTable(self.result_set[0])
26+
for row in self.result_set[1:]:
27+
tbl.add_row(row)
28+
29+
if len(self.result_set) == 1:
30+
tbl.add_row(['No data returned.'])
31+
32+
print(str(tbl) + '\n')
33+
34+
for stat in self.statistics:
35+
print(stat)
36+
37+
def _retrieve_data_from_statistics(self, statistics):
38+
return {
39+
self.LABELS_ADDED: self._get_value(self.LABELS_ADDED, statistics),
40+
self.NODES_CREATED: self._get_value(self.NODES_CREATED, statistics),
41+
self.PROPERTIES_SET: self._get_value(self.PROPERTIES_SET, statistics),
42+
self.RELATIONSHIPS_CREATED: self._get_value(self.RELATIONSHIPS_CREATED, statistics),
43+
self.INTERNAL_EXECUTION_TIME: self._get_value(self.INTERNAL_EXECUTION_TIME, statistics)
44+
}
45+
46+
@staticmethod
47+
def _get_value(prop, statistics):
48+
for stat in statistics:
49+
if prop in stat:
50+
return float(stat.split(': ')[1].split(' ')[0])
51+
52+
return 0
53+
54+
@property
55+
def labels_added(self):
56+
return self.parsed_statistics[self.LABELS_ADDED]
57+
58+
@property
59+
def nodes_created(self):
60+
return self.parsed_statistics[self.NODES_CREATED]
61+
62+
@property
63+
def properties_set(self):
64+
return self.parsed_statistics[self.PROPERTIES_SET]
65+
66+
@property
67+
def relationships_created(self):
68+
return self.parsed_statistics[self.RELATIONSHIPS_CREATED]
69+
70+
@property
71+
def run_time_ms(self):
72+
return self.parsed_statistics[self.INTERNAL_EXECUTION_TIME]

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from setuptools import setup, find_packages
22
setup(
33
name='redisgraph',
4-
version='0.4',
4+
version='0.5',
55

66
description='RedisGraph Python Client',
77
url='https://github.com/swilly22/redisgraph-py',

0 commit comments

Comments
 (0)