|
| 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] |
0 commit comments