def delete(panel_id): if Panel.find_one(id=panel_id): db.run("MATCH ()-[r]->() WHERE r.id=\"{}\" DELETE r".format( panel_id)).evaluate() return True log("Panel not found") return False
def delete(by_id=None, by_name=None): props_json = {} if by_id: props_json["id"] = by_id if by_name: props_json["name"] = by_name props = json_to_properties(props_json) db.run("MATCH (a:Rack {}) DETACH DELETE a".format(props)).evaluate() return True
def find_shortest_path(rack1_props_json, rack2_props_json, media_type): r1_props_str = json_to_properties(rack1_props_json) r2_props_str = json_to_properties(rack2_props_json) r1 = db.run("MATCH (a:Rack {}) RETURN a".format(r1_props_str)).data() r2 = db.run("MATCH (a:Rack {}) RETURN a".format(r2_props_str)).data() if r1 == r2: log("Can't find shortest path for same nodes") return False cypher_query = """ MATCH (a:Rack {}),(b:Rack {}), path = shortestPath((a)-[:{}*]-(b)) WHERE ALL (r IN relationships(path) WHERE r.status="active") RETURN Nodes(path) """.format(r1_props_str, r2_props_str, media_type.upper()) r = db.run(cypher_query).data() return [x["Nodes(path)"] for x in r]
def deactivate(panel_id): if Panel.find_one(id=panel_id): return db.run( "MATCH ()-[r]->() WHERE r.id=\"{}\" SET r.status=\"inactive\" RETURN r" .format(panel_id)).evaluate() log("Not found") return False
def create(source_id, destination_id, name, panel_type, num_of_ports, connector, height=-1): source_rack = Rack.find_one(by_id=source_id) destination_rack = Rack.find_one(by_id=destination_id) if not source_rack: log("Source rack not found") return False if not destination_rack: log("Destination rack not found") return False json_props = dict(id=uuid.uuid4(), name=name, panel_type=panel_type, num_of_ports=num_of_ports, connector=connector, status="active", height=height) source_rack_props = "{ id: \"" + str(source_id) + "\" }" destination_rack_props = "{ id: \"" + str(destination_id) + "\" }" cypher_query = """ MATCH (a:Rack {}), (b:Rack {}) CREATE (a)-[c:{} {}]->(b) RETURN c """.format(source_rack_props, destination_rack_props, panel_type.upper(), json_to_properties(json_props)) return db.run(cypher_query).evaluate()
def find_one(by_id=None, by_name=None): props_json = {} if by_id: props_json["id"] = by_id if by_name: props_json["name"] = by_name props = json_to_properties(props_json) return db.run("MATCH (a:Rack {}) RETURN a".format(props)).evaluate()
def create(name, site, building, room): json_props = dict( id = uuid.uuid4(), name=name, site=site, building=building, room=room ) props = json_to_properties(json_props) return db.run("CREATE (a:Rack {}) RETURN a".format(props)).evaluate()
def find_all_between_racks(rack1_id, rack2_id): if not Rack.find_one(by_id=rack1_id): log("Rack 1 not found") return False if not Rack.find_one(by_id=rack2_id): log("Rack 2 not found") return False cypher_query = """ MATCH (a)-[r]->(b) WHERE a.id=\"{}\" AND b.id=\"{}\" return r """.format(rack1_id, rack2_id) r1 = db.run(cypher_query).data() cypher_query = """ MATCH (a)-[r]->(b) WHERE a.id=\"{}\" AND b.id=\"{}\" return r """.format(rack2_id, rack1_id) r2 = db.run(cypher_query).data() if r1 and r2: return [i["r"] for i in r1] + [i["r"] for i in r2] if r1: return [i["r"] for i in r1] return [i["r"] for i in r2]
def find_one(**kwargs): props = [] if kwargs: print kwargs for key, value in kwargs.iteritems(): if type(value) in [int, float]: props.append("r.{}={}".format(key, value)) else: props.append("r.{}=\"{}\"".format(key, value)) where_props = " AND ".join(props) cypher_query = """ MATCH ()-[r]->() WHERE {} return r """.format(where_props) return db.run(cypher_query).evaluate()
def free_find(**kwargs): props = [] where_props = "" if kwargs: for key, value in kwargs.iteritems(): if type(value) in [int, float]: props.append("a.{}={}".format(key, value)) else: props.append("a.{}=\"{}\"".format(key, value)) where_props = "WHERE "+ " AND ".join(props) cypher_query = """ MATCH (a: Rack) {} return a """.format(where_props) r = db.run(cypher_query).data() return [i["a"] for i in r]
def find(by_id=None, by_name=None, by_site=None, by_building=None, by_room=None): props_json = {} if by_id: props_json["id"] = by_id if by_name: props_json["name"] = by_name if by_site: props_json["site"] = by_site if by_building: props_json["building"] = by_building if by_room: props_json["room"] = by_room props = json_to_properties(props_json) r = db.run("MATCH (a:Rack {}) RETURN a".format(props)).data() return [i["a"] for i in r]
def update(rack_id=None, name=None, site=None, building=None, room=None): if rack_id: if not Rack.find_one(by_id=rack_id): log("Rack not found") return False props = [] if name: props.append("a.name=\"{}\"".format(name)) if site: props.append("a.site=\"{}\"".format(site)) if building: props.append("a.building=\"{}\"".format(building)) if room: props.append("a.room=\"{}\"".format(room)) if props: props_str = "SET " + ", ".join(props) else: log("Nothing to update") return False cypher_query = "MATCH (a: Rack { id: \"" + str(rack_id) + "\"}" + ") {} RETURN a".format(props_str) return db.run(cypher_query).evaluate() log("Must provide rack id") return False
def update(panel_id, **kwargs): props = [] p = Panel.find_one(id=panel_id) if not p: log("Panel not found") return False for key, value in kwargs.iteritems(): if key in dict(p): if key == "id": log("Panel id is not changeable") continue if type(value) in [int, float]: props.append("r.{}={}".format(key, value)) else: props.append("r.{}=\"{}\"".format(key, value)) if props: props_str = ", ".join(props) cypher_query = """ MATCH ()-[r]->() WHERE r.id=\"{}\" SET {} RETURN r """.format(panel_id, props_str) return db.run(cypher_query).evaluate() log("No updates were made") return False
def get_all_racks(): racks = db.run("MATCH (a:Rack) RETURN a.name, a.id").data() return [{x["a.id"]: x["a.name"]} for x in racks if x["a.id"]]