def print_profile(self, profile): planner = profile.arguments["planner"] version = profile.arguments["version"] runtime = profile.arguments["runtime"] print("") print("Planner: {}".format(planner)) print("Version: {}".format(version)) print("Runtime: {}".format(runtime)) print("") headers = [ "Operator", "Estimated Rows", "Rows", "DB Hits", "Variables" ] rows = [] for n in reversed(walk(profile)): operator = n.operator_type estimated_rows = int(n.arguments["EstimatedRows"]) rows_ = n.arguments["Rows"] db_hits = n.arguments["DbHits"] variables = n.identifiers rows.append([operator, estimated_rows, rows_, db_hits, variables]) print(pretty_table(headers, rows))
def write_to_logfile(self, query, response): headers = response["headers"] rows = response["rows"] duration = response["duration"] error = response["error"] self.logfile.write("> {}\n".format(query)) self.logfile.write("{}\n".format(pretty_table(headers, rows))) if error is False: self.logfile.write("{} ms\n\n".format(duration))
def print_schema(self): headers = ["Labels", "Relationship Types", "Constraints", "Indexes"] columns = [self.get_labels()[:]] columns.append(self.get_relationship_types()[:]) columns.append(self.format_constraints_indexes(self.get_constraints()[:])) columns.append(self.format_constraints_indexes(self.get_indexes()[:])) max_length = len(max(columns, key=len)) [x.extend([""] * (max_length - len(x))) for x in columns] rows = [[x[i] for x in columns] for i in range(max_length)] print(pretty_table(headers, rows))
def print_schema(self): headers = ["Labels", "Relationship Types", "Constraints", "Indexes"] columns = [self.get_labels()[:]] columns.append(self.get_relationship_types()[:]) columns.append( self.format_constraints_indexes(self.get_constraints()[:])) columns.append(self.format_constraints_indexes(self.get_indexes()[:])) max_length = len(max(columns, key=len)) [x.extend([""] * (max_length - len(x))) for x in columns] rows = [[x[i] for x in columns] for i in range(max_length)] print(pretty_table(headers, rows))
def print_help(): headers = ["Keyword", "Description"] rows = [["quit", "Exit cycli."], ["exit", "Exit cycli."], ["help", "Display this text."], ["refresh", "Refresh schema cache."], ["run-n", "Run a Cypher query n times."], ["export", "Set a parameter with export key=value."], ["save-csv", "Save the query results to a CSV file."], [ "schema", "Display indexes, constraints, labels, and relationship types." ], ["schema-indexes", "Display indexes."], ["schema-constraints", "Display constraints."], ["schema-labels", "Display labels."], ["schema-rels", "Display relationship types."], ["CTRL-D", "Exit cycli if the input is blank."], ["CTRL-C", "Abort and rollback the currently-running query."]] print(pretty_table(headers, rows))
def print_help(): headers = ["Keyword", "Description"] rows = [ ["quit", "Exit cycli."], ["exit", "Exit cycli."], ["help", "Display this text."], ["refresh", "Refresh schema cache."], ["run-n", "Run a Cypher query n times."], ["export", "Set a parameter with export key=value."], ["save-csv", "Save the query results to a CSV file."], ["schema", "Display indexes, constraints, labels, and relationship types."], ["schema-indexes", "Display indexes."], ["schema-constraints", "Display constraints."], ["schema-labels", "Display labels."], ["schema-rels", "Display relationship types."], ["CTRL-D", "Exit cycli if the input is blank."], ["CTRL-C", "Abort and rollback the currently-running query."] ] print(pretty_table(headers, rows))
def print_profile(self, profile): planner = profile.arguments["planner"] version = profile.arguments["version"] runtime = profile.arguments["runtime"] print("") print("Planner: {}".format(planner)) print("Version: {}".format(version)) print("Runtime: {}".format(runtime)) print("") headers = ["Operator", "Estimated Rows", "Rows", "DB Hits", "Variables"] rows = [] for n in reversed(walk(profile)): operator = n.operator_type estimated_rows = int(n.arguments["EstimatedRows"]) rows_ = n.arguments["Rows"] db_hits = n.arguments["DbHits"] variables = n.identifiers rows.append([operator, estimated_rows, rows_, db_hits, variables]) print(pretty_table(headers, rows))
def handle_query(self, query): run_n = re.match('run-([0-9]+) (.*)', query, re.DOTALL) save_csv = query.startswith("save-csv ") if self.cypher.is_a_write_query(query) and self.read_only: print("Query aborted. You are in read-only mode.") elif query in ["quit", "exit"]: raise UserWantsOut elif query == "help": print_help() elif query == "refresh": self.neo4j.refresh() elif query == "schema": self.neo4j.print_schema() elif query == "schema-indexes": self.neo4j.print_indexes() elif query == "schema-constraints": self.neo4j.print_constraints() elif query == "schema-labels": self.neo4j.print_labels() elif query == "schema-rels": self.neo4j.print_relationship_types() elif query.startswith("env"): if query == "env": for key, value in self.neo4j.parameters.items(): print("{0}={1}".format(key, value)) else: key = query[3:] key = key.strip("'\"[]") value = self.neo4j.parameters.get(key) if value is not None: print(value) elif query.startswith("export "): if "=" not in query: print("Set parameters with export key=value.") else: params = query.replace("export ", "").strip() key, value = params.split("=", 1) key = key.strip() value = value.strip() try: value = eval(value) self.neo4j.update_parameters(key, value) except Exception as e: print(e) else: count = int(run_n.group(1)) if run_n else 1 query = run_n.group(2) if run_n else query query = query[len("save-csv "):] if save_csv else query if count <= 0 or not query: print("Check your syntax. cycli expects run-{n} {query} where {n} is an integer > 0 and {query} is a Cypher query.") return error = False total_duration = 0 index = 0 while index < count: response = self.neo4j.cypher(query) headers = response["headers"] rows = response["rows"] duration = response["duration"] error = response["error"] profile = response.get("profile") if error is False: print(pretty_table(headers, rows)) ms = "Run {}: {} ms\n".format(index + 1, duration) if run_n else "{} ms".format(duration) print(ms) if profile: self.neo4j.print_profile(profile) if save_csv: self.write_to_csvfile(headers, rows) else: print(error) if self.logfile: self.write_to_logfile(query, response) total_duration += duration index += 1 if run_n and error is False: print("Total duration: {} ms".format(total_duration))
def print_indexes(self): headers = ["Indexes"] indexes = self.get_indexes() rows = [[x] for x in self.format_constraints_indexes(indexes)] print(pretty_table(headers, rows))
def print_relationship_types(self): headers = ["Relationship Types"] rows = [[x] for x in self.get_relationship_types()] print(pretty_table(headers, rows))
def print_labels(self): headers = ["Labels"] rows = [[x] for x in self.get_labels()] print(pretty_table(headers, rows))
def handle_query(self, query): run_n = re.match('run-([0-9]+) (.*)', query, re.DOTALL) save_csv = query.startswith("save-csv ") if self.cypher.is_a_write_query(query) and self.read_only: print("Query aborted. You are in read-only mode.") elif query in ["quit", "exit"]: raise UserWantsOut elif query == "help": print_help() elif query == "refresh": self.neo4j.refresh() elif query == "schema": self.neo4j.print_schema() elif query == "schema-indexes": self.neo4j.print_indexes() elif query == "schema-constraints": self.neo4j.print_constraints() elif query == "schema-labels": self.neo4j.print_labels() elif query == "schema-rels": self.neo4j.print_relationship_types() elif query.startswith("env"): if query == "env": for key, value in self.neo4j.parameters.items(): print("{0}={1}".format(key, value)) else: key = query[3:] key = key.strip("'\"[]") value = self.neo4j.parameters.get(key) if value is not None: print(value) elif query.startswith("export "): if "=" not in query: print("Set parameters with export key=value.") else: params = query.replace("export ", "").strip() key, value = params.split("=", 1) key = key.strip() value = value.strip() try: value = eval(value) self.neo4j.update_parameters(key, value) except Exception as e: print(e) else: count = int(run_n.group(1)) if run_n else 1 query = run_n.group(2) if run_n else query query = query[len("save-csv "):] if save_csv else query if count <= 0 or not query: print("Check your syntax. cycli expects run-{n} {query} where {n} is an integer > 0 and {query} is a Cypher query.") return total_duration = 0 index = 0 while index < count: response = self.neo4j.cypher(query) headers = response["headers"] rows = response["rows"] duration = response["duration"] error = response["error"] profile = response.get("profile") if error is False: print(pretty_table(headers, rows)) ms = "Run {}: {} ms\n".format(index + 1, duration) if run_n else "{} ms".format(duration) print(ms) if profile: self.neo4j.print_profile(profile) if save_csv: self.write_to_csvfile(headers, rows) else: print(error) if self.logfile: self.write_to_logfile(query, response) total_duration += duration index += 1 if run_n and error is False: print("Total duration: {} ms".format(total_duration))