def get(user_name, contrib_ref): db = get_db() user = db.users.find_one({"contribs.ref": contrib_ref}, {"contribs.$": 1}) contrib_ref = db.contribs_v2.find_one({"_id": ObjectId(contrib_ref)}) contrib = user["contribs"][0] contrib["items"] = contrib_ref.get("items", [])[::-1] if "schemes" in contrib: contrib["schemes"] = list( db.contribs.scheme.find({"_id": { "$in": contrib["schemes"] }})) """ for item in contrib["items"]: item["_id"] = str(item["_id"]) item["name_1"] = item["object"] item["name_2"] = item["subject"] item["relations"] = item["predicates"] """ items = [] for i in contrib["items"]: item = { "_id": str(i["_id"]), "name_1": i["object"], "name_2": i["subject"], "scheme": i["scheme"], "relations": i["predicates"], "url": i["url"], "date": i.get("date", None), "dateTo": i.get("dateTo", None), "source": i.get("source", None) } items.append(item) contrib["items"] = items return contrib
def convert(): db = get_db() user = db.users.find_one({"_id": "twitter@baio1980"}) for c in user["contribs"]: old_contrib = db.contribs.find_one({"_id": ObjectId(c["ref"])}) new_contrib = {"_id": ObjectId(c["ref"]), "items": []} if old_contrib: for i in old_contrib["items"]: item = { "_id": i["_id"], "scheme": "person-person.ru", "object": " ".join(i["name_1"].split(" ")[::-1]), "subject": " ".join(i["name_2"].split(" ")[::-1]), "url": c["url"], "predicates": [] } for t in i["tags"]: predicate = { "type": "pp-" + t["type"], "val": t["name"] } item["predicates"].append(predicate) new_contrib["items"].append(item) db.contribs_v2.save(new_contrib) c["schemes"] = ["person-person.ru", "person-org.ru", "org-org.ru"] db.users.save(user)
def get(user_name, contrib_ref): db = get_db() user = db.users.find_one({"contribs.ref": contrib_ref}, {"contribs.$" : 1}) contrib_ref = db.contribs_v2.find_one({"_id" : ObjectId(contrib_ref)}) contrib = user["contribs"][0] contrib["items"] = contrib_ref.get("items", [])[::-1] if "schemes" in contrib: contrib["schemes"] = list(db.contribs.scheme.find({"_id": { "$in" : contrib["schemes"]}})) """ for item in contrib["items"]: item["_id"] = str(item["_id"]) item["name_1"] = item["object"] item["name_2"] = item["subject"] item["relations"] = item["predicates"] """ items = [] for i in contrib["items"]: item = { "_id": str(i["_id"]), "name_1": i["object"], "name_2": i["subject"], "scheme": i["scheme"], "relations": i["predicates"], "url": i["url"], "date": i.get("date", None), "dateTo": i.get("dateTo", None), "source": i.get("source", None) } items.append(item) contrib["items"] = items return contrib
def copy(user_id, contrib_ref_from): db = get_db() src_user = db.users.find_one({"contribs.ref": contrib_ref_from}, { "name": 1, "contribs.$": 1 }) if src_user["_id"] == user_id: raise ValueError("Contib couldn't be copied to the same user") user_contrib = src_user["contribs"][0] contrib = db.contribs_v2.find_one({"_id": ObjectId(contrib_ref_from)}) contrib["_id"] = ObjectId() user_contrib.update({ "ref": str(contrib["_id"]), "date": dt.datetime.now(), "copied_from": { "user": src_user["_id"], "contrib": contrib_ref_from } }) db.contribs_v2.insert(contrib) db.users.update({"_id": user_id}, {"$push": {"contribs": user_contrib}})
def get_graphs(user_name, contrib_id): db = get_db() user = db.users.find_one({"_id": user_name, "graphs.contribs": contrib_id}, {"graphs.ref": 1}) if user: res = map(lambda x: x["ref"], user["graphs"]) return res else: return []
def patch(user_name, contrib_id, data): _wrangling(data) _validate(data) db = get_db() contrib_ref = ObjectId(contrib_id) res = map(lambda x: {"_id": x["_id"], "err": [], "warn": []}, data) crt_items = [] for i, item in enumerate(data): if item["_id"] is None: dom = _json2dom(item) crt_items.append(dom) res[i]["_id"] = str(dom["_id"]) upd_items = [] for item in filter( lambda x: x["_id"] is not None and ("_isRemoved" not in x or not x["_isRemoved"]), data): dom = _json2dom(item) upd_items.append(dom) rm_ids = [] for item in filter(lambda x: "_isRemoved" in x and x["_isRemoved"], data): rm_ids.append(ObjectId(item["_id"])) if len(crt_items) > 0: db.contribs_v2.update({"_id": contrib_ref}, {"$pushAll": { "items": crt_items }}) if len(upd_items) > 0: for upd_item in upd_items: db.contribs_v2.update( { "_id": contrib_ref, "items._id": upd_item["_id"] }, {"$set": { "items.$": upd_item }}) #Waiting for the miracle to come, https://jira.mongodb.org/browse/SERVER-831 #cnt = db.user.find({"_id": user_name, "contribs.name": contrib_name}, {"contribs.$.data": {"$elemMatch": {"_id" : "name_1_name_3"}}}) if len(rm_ids) > 0: db.contribs_v2.update({"_id": contrib_ref}, {"$pull": { "items": { "_id": { "$in": rm_ids } } }}) res = {"data": res, "graphs": get_graphs(user_name, contrib_id)} return res
def get_scheme(user_name, contrib_id): db = get_db() user = db.users.find_one({"_id": user_name, "contribs.ref": contrib_id}, {"contribs.$.scheme_ref" : 1}) if user: res = db.contribs.scheme.find_one({"_id": user["contribs"][0]["scheme_ref"]}) del res["_id"] return res else: return None
def get_shortest_path(name_1, name_2): name_1 = name_1.encode("utf8") name_2 = name_2.encode("utf8") #name_1 = "володин валерий" #name_2 = "карманов александр" db = get_db() graph_db = neo4j.GraphDatabaseService(os.getenv("NEO4J_URI")) query = "START n=node:person(name=\"{}\"), m=node:person(name=\"{}\") MATCH p = shortestPath(n-[*]-m) RETURN p;"\ .format(name_1, name_2) data, metadata = cypher.execute(graph_db, query) if len(data) == 0: return {"id": None, "isYours": False, "owner" : None, "name": "{}-{}".format(name_1, name_2), "nodes": [], "edges": []} refs = map(lambda x: x.get_properties()["refs"], data[0][0].relationships) plain_refs = sum(refs, []) plain_refs = map(lambda x: ObjectId(x), plain_refs) agg = db.contribs_v2.aggregate([{"$match" : {"items._id": {"$in" : plain_refs}}}, {"$unwind" : "$items"}, {"$match" : {"items._id": {"$in": plain_refs}}}]) items = map(lambda x: x["items"], agg["result"]) i = 0 res = [] for ref in refs: res.append(items[i:i+len(ref)]) i += len(ref) print res def map_node(node_name): return {"id": node_name, "name": node_name, "meta" : {"pos" : [-1, -1]}} nodes = dict() edges = dict() for r in res: for item in r: node_name_1 = node_name = item["object"] if node_name not in nodes: nodes[node_name] = map_node(node_name) node_name_2 = node_name = item["subject"] if node_name not in nodes: nodes[node_name] = map_node(node_name) edge_name = node_name_1 + " " + node_name_2 if edge_name in edges: edge = edges[edge_name] for item_tag in item["predicates"]: edge_tag = filter(lambda x: x["val"] == item_tag["val"] and x["type"] == item_tag["type"], edge["tags"]) if len(edge_tag) > 0: edge_tag = edge_tag[0] if item["url"] not in edge_tag["urls"]: edge_tag["urls"].append(item["url"]) else: item_tag["urls"] = [item["url"]] edge["tags"].append(item_tag) else: edge = {"id": edge_name, "source_id": node_name_1, "target_id": node_name_2, "tags" : item["predicates"]} for tag in edge["tags"]: tag["urls"] = [item["url"]] edges[edge_name] = edge return {"id": None, "isYours": False, "owner" : None, "name": "{}-{}".format(name_1, name_2), "nodes": nodes.values(), "edges": edges.values()}
def get_graphs(user_name, contrib_id): db = get_db() user = db.users.find_one({ "_id": user_name, "graphs.contribs": contrib_id }, {"graphs.ref": 1}) if user: res = map(lambda x: x["ref"], user["graphs"]) return res else: return []
def get_linked_nodes(name): name = name.encode("utf8") db = get_db() graph_db = neo4j.GraphDatabaseService(os.getenv("NEO4J_URI")) query = "start n=node:person(name=\"{}\") match n-[r]-m return n, r, m;"\ .format(name) data, metadata = cypher.execute(graph_db, query) if len(data) == 0: return {"nodes": [], "edges": []} refs = map(lambda x: x[1].get_properties()["refs"], data) plain_refs = sum(refs, []) plain_refs = map(lambda x: ObjectId(x), plain_refs) agg = db.contribs_v2.aggregate([{"$match" : {"items._id": {"$in" : plain_refs}}}, {"$unwind" : "$items"}, {"$match" : {"items._id": {"$in": plain_refs}}}]) items = map(lambda x: x["items"], agg["result"]) i = 0 res = [] for ref in refs: res.append(items[i:i+len(ref)]) i += len(ref) print res def map_node(node_name): return {"id": node_name, "name": node_name, "meta" : {"pos" : [-1, -1]}} nodes = dict() edges = dict() for r in res: for item in r: node_name_1 = node_name = item["object"] if node_name not in nodes: nodes[node_name] = map_node(node_name) node_name_2 = node_name = item["subject"] if node_name not in nodes: nodes[node_name] = map_node(node_name) edge_name = node_name_1 + " " + node_name_2 if edge_name in edges: edge = edges[edge_name] for item_tag in item["predicates"]: edge_tag = filter(lambda x: x["val"] == item_tag["val"] and x["type"] == item_tag["type"], edge["tags"]) if len(edge_tag) > 0: edge_tag = edge_tag[0] if item["url"] not in edge_tag["urls"]: edge_tag["urls"].append(item["url"]) else: item_tag["urls"] = [item["url"]] edge["tags"].append(item_tag) else: edge = {"id": edge_name, "source_id": node_name_1, "target_id": node_name_2, "tags" : item["predicates"]} for tag in edge["tags"]: tag["urls"] = [item["url"]] edges[edge_name] = edge return {"nodes": nodes.values(), "edges": edges.values()}
def get_scheme(user_name, contrib_id): db = get_db() user = db.users.find_one({ "_id": user_name, "contribs.ref": contrib_id }, {"contribs.$.scheme_ref": 1}) if user: res = db.contribs.scheme.find_one( {"_id": user["contribs"][0]["scheme_ref"]}) del res["_id"] return res else: return None
def get(user_name, contrib_ref): db = get_db() user = db.users.find_one({"_id": user_name, "contribs.ref": contrib_ref}, {"contribs.$": 1}) contrib_ref = db.contribs.find_one({"_id": ObjectId(contrib_ref)}) contrib = user["contribs"][0] contrib["items"] = contrib_ref.get("items", [])[::-1] if "schemes" in contrib: contrib["schemes"] = list(db.contribs.scheme.find({"_id": {"$in": contrib["schemes"]}})) for item in contrib["items"]: item["_id"] = str(item["_id"]) item["relations"] = [{"val": u"друг", "type": "pp-family"}] for tag in item["tags"]: item["{}_rel".format(tag["type"])] = tag["name"] del item["tags"] return contrib
def patch(user_name, graph_id, nodes): #TODO: check user ownn graph db = get_db() if db.users.find_one({"_id": user_name, "graphs.ref": graph_id}, {"_id" : 1}) is None: return 401 def map_node_meta(node): return {"id": node["id"], "pos" : [node["x"], node["y"]]} nodes_meta = map(map_node_meta, nodes) for meta in nodes_meta: f = db.graphs.meta.find({"_id": ObjectId(graph_id), "nodes.id": meta["id"]}).count() if f == 0: db.graphs.meta.update({"_id": ObjectId(graph_id)}, {"$push" : {"nodes": meta}},upsert=True) else: db.graphs.meta.update({"_id": ObjectId(graph_id), "nodes.id": meta["id"]}, {"$set" : {"nodes.$": meta}}) return 200
def update_via_scheme(items): db = get_db() schemes = dict() upd_index_docs = [] for item in items: if "scheme" in item: if item["scheme"] not in schemes: scheme = db.contribs.scheme.find_one({"_id": item["scheme"]}) schemes[item["scheme"]] = scheme else: scheme = schemes[item["scheme"]] del scheme["_id"] upd_index_docs += item_2_index_docs(item, scheme) upd_index_docs = utils_array.unique(upd_index_docs, lambda x: x[2]) es.bset(upd_index_docs)
def convert(): db = get_db() user = db.users.find_one({"_id": "twitter@baio1980"}) names = [] tags = [] for c in user["contribs"]: contrib = db.contribs_v2.find_one({"_id": ObjectId(c["ref"])}) if contrib and "items" in contrib: for i in contrib["items"]: n = i["object"] names.append(n) n = i["subject"] names.append(n) tags += i["predicates"] names = map(lambda x: (x, x), set(names)) tags = unique(tags, lambda x: x["type"] + "_" + x["val"]) tags = map(lambda x: ("relations.ru", x["type"], x["val"], {"val": x["val"]}), tags) es.mset("person-names.ru", "politic-rus", names) es.bset(tags)
def get(user_name, contrib_ref): db = get_db() user = db.users.find_one({ "_id": user_name, "contribs.ref": contrib_ref }, {"contribs.$": 1}) contrib_ref = db.contribs.find_one({"_id": ObjectId(contrib_ref)}) contrib = user["contribs"][0] contrib["items"] = contrib_ref.get("items", [])[::-1] if "schemes" in contrib: contrib["schemes"] = list( db.contribs.scheme.find({"_id": { "$in": contrib["schemes"] }})) for item in contrib["items"]: item["_id"] = str(item["_id"]) item["relations"] = [{"val": u"друг", "type": "pp-family"}] for tag in item["tags"]: item["{}_rel".format(tag["type"])] = tag["name"] del item["tags"] return contrib
def patch(user_name, contrib_id, data): _wrangling(data) _validate(data) db = get_db() contrib_ref = ObjectId(contrib_id) res = map(lambda x: {"_id": x["_id"], "err": [], "warn": []}, data) crt_items = [] for i, item in enumerate(data): if item["_id"] is None: dom = _json2dom(item) crt_items.append(dom) res[i]["_id"] = str(dom["_id"]) upd_items = [] for item in filter(lambda x: x["_id"] is not None and ("_isRemoved" not in x or not x["_isRemoved"]), data): dom = _json2dom(item) upd_items.append(dom) rm_ids = [] for item in filter(lambda x: "_isRemoved" in x and x["_isRemoved"], data): rm_ids.append(ObjectId(item["_id"])) if len(crt_items) > 0: db.contribs_v2.update({"_id": contrib_ref}, {"$pushAll" : {"items" : crt_items}}) if len(upd_items) > 0: for upd_item in upd_items: db.contribs_v2.update({"_id": contrib_ref, "items._id": upd_item["_id"]}, {"$set": {"items.$" : upd_item}}) #Waiting for the miracle to come, https://jira.mongodb.org/browse/SERVER-831 #cnt = db.user.find({"_id": user_name, "contribs.name": contrib_name}, {"contribs.$.data": {"$elemMatch": {"_id" : "name_1_name_3"}}}) if len(rm_ids) > 0: db.contribs_v2.update({"_id" : contrib_ref}, {"$pull": {"items" : {"_id" : {"$in" : rm_ids}}}}) res = {"data": res, "graphs": get_graphs(user_name, contrib_id)} return res
def convert(): db = get_db() user = db.users.find_one({"_id": "twitter@baio1980"}) names = [] tags = [] for c in user["contribs"]: contrib = db.contribs_v2.find_one({"_id": ObjectId(c["ref"])}) if contrib and "items" in contrib: for i in contrib["items"]: n = i["object"] names.append(n) n = i["subject"] names.append(n) tags += i["predicates"] names = map(lambda x: (x, x), set(names)) tags = unique(tags, lambda x: x["type"] + "_" + x["val"]) tags = map( lambda x: ("relations.ru", x["type"], x["val"], { "val": x["val"] }), tags) es.mset("person-names.ru", "politic-rus", names) es.bset(tags)
def convert(): db = get_db() user = db.users.find_one({"_id": "twitter@baio1980"}) for c in user["contribs"]: old_contrib = db.contribs.find_one({"_id": ObjectId(c["ref"])}) new_contrib = {"_id": ObjectId(c["ref"]), "items": []} if old_contrib: for i in old_contrib["items"]: item = { "_id": i["_id"], "scheme": "person-person.ru", "object": " ".join(i["name_1"].split(" ")[::-1]), "subject": " ".join(i["name_2"].split(" ")[::-1]), "url": c["url"], "predicates": [] } for t in i["tags"]: predicate = {"type": "pp-" + t["type"], "val": t["name"]} item["predicates"].append(predicate) new_contrib["items"].append(item) db.contribs_v2.save(new_contrib) c["schemes"] = ["person-person.ru", "person-org.ru", "org-org.ru"] db.users.save(user)
def get_linked_nodes(name): name = name.encode("utf8") db = get_db() graph_db = neo4j.GraphDatabaseService(os.getenv("NEO4J_URI")) query = "start n=node:person(name=\"{}\") match n-[r]-m return n, r, m;"\ .format(name) data, metadata = cypher.execute(graph_db, query) if len(data) == 0: return {"nodes": [], "edges": []} refs = map(lambda x: x[1].get_properties()["refs"], data) plain_refs = sum(refs, []) plain_refs = map(lambda x: ObjectId(x), plain_refs) agg = db.contribs_v2.aggregate([{ "$match": { "items._id": { "$in": plain_refs } } }, { "$unwind": "$items" }, { "$match": { "items._id": { "$in": plain_refs } } }]) items = map(lambda x: x["items"], agg["result"]) i = 0 res = [] for ref in refs: res.append(items[i:i + len(ref)]) i += len(ref) print res def map_node(node_name): return {"id": node_name, "name": node_name, "meta": {"pos": [-1, -1]}} nodes = dict() edges = dict() for r in res: for item in r: node_name_1 = node_name = item["object"] if node_name not in nodes: nodes[node_name] = map_node(node_name) node_name_2 = node_name = item["subject"] if node_name not in nodes: nodes[node_name] = map_node(node_name) edge_name = node_name_1 + " " + node_name_2 if edge_name in edges: edge = edges[edge_name] for item_tag in item["predicates"]: edge_tag = filter( lambda x: x["val"] == item_tag["val"] and x["type"] == item_tag["type"], edge["tags"]) if len(edge_tag) > 0: edge_tag = edge_tag[0] if item["url"] not in edge_tag["urls"]: edge_tag["urls"].append(item["url"]) else: item_tag["urls"] = [item["url"]] edge["tags"].append(item_tag) else: edge = { "id": edge_name, "source_id": node_name_1, "target_id": node_name_2, "tags": item["predicates"] } for tag in edge["tags"]: tag["urls"] = [item["url"]] edges[edge_name] = edge return {"nodes": nodes.values(), "edges": edges.values()}
def get_shortest_path(name_1, name_2): name_1 = name_1.encode("utf8") name_2 = name_2.encode("utf8") #name_1 = "володин валерий" #name_2 = "карманов александр" db = get_db() graph_db = neo4j.GraphDatabaseService(os.getenv("NEO4J_URI")) query = "START n=node:person(name=\"{}\"), m=node:person(name=\"{}\") MATCH p = shortestPath(n-[*]-m) RETURN p;"\ .format(name_1, name_2) data, metadata = cypher.execute(graph_db, query) if len(data) == 0: return { "id": None, "isYours": False, "owner": None, "name": "{}-{}".format(name_1, name_2), "nodes": [], "edges": [] } refs = map(lambda x: x.get_properties()["refs"], data[0][0].relationships) plain_refs = sum(refs, []) plain_refs = map(lambda x: ObjectId(x), plain_refs) agg = db.contribs_v2.aggregate([{ "$match": { "items._id": { "$in": plain_refs } } }, { "$unwind": "$items" }, { "$match": { "items._id": { "$in": plain_refs } } }]) items = map(lambda x: x["items"], agg["result"]) i = 0 res = [] for ref in refs: res.append(items[i:i + len(ref)]) i += len(ref) print res def map_node(node_name): return {"id": node_name, "name": node_name, "meta": {"pos": [-1, -1]}} nodes = dict() edges = dict() for r in res: for item in r: node_name_1 = node_name = item["object"] if node_name not in nodes: nodes[node_name] = map_node(node_name) node_name_2 = node_name = item["subject"] if node_name not in nodes: nodes[node_name] = map_node(node_name) edge_name = node_name_1 + " " + node_name_2 if edge_name in edges: edge = edges[edge_name] for item_tag in item["predicates"]: edge_tag = filter( lambda x: x["val"] == item_tag["val"] and x["type"] == item_tag["type"], edge["tags"]) if len(edge_tag) > 0: edge_tag = edge_tag[0] if item["url"] not in edge_tag["urls"]: edge_tag["urls"].append(item["url"]) else: item_tag["urls"] = [item["url"]] edge["tags"].append(item_tag) else: edge = { "id": edge_name, "source_id": node_name_1, "target_id": node_name_2, "tags": item["predicates"] } for tag in edge["tags"]: tag["urls"] = [item["url"]] edges[edge_name] = edge return { "id": None, "isYours": False, "owner": None, "name": "{}-{}".format(name_1, name_2), "nodes": nodes.values(), "edges": edges.values() }
def get(user_name, graph_id): orig_user_id = user_name if not user_name and not graph_id: user_name, graph_id = _get_default_graph() db = get_db() user = db.users.find_one({"_id": user_name}) if user_name and not graph_id: if user and "graphs" in user and len(user["graphs"]) > 0: user_graph = user["graphs"][0] graph_id = user_graph["ref"] else: user_name, graph_id = _get_default_graph() user = db.users.find_one({"_id": user_name}) user_graph = filter(lambda x: x["ref"] == graph_id, user["graphs"])[0] else: if not user: user = db.users.find_one({"graphs.ref": graph_id}) user_graph = filter(lambda x: x["ref"] == graph_id, user["graphs"])[0] is_yours = user["_id"] == orig_user_id graph_name = user_graph["name"] graph_meta = db.graphs.meta.find_one({"_id" : ObjectId(graph_id)}) nodes_meta = graph_meta["nodes"] if graph_meta else None nodes = dict() edges = dict() for contrib_id in user_graph["contribs"]: contrib = db.contribs_v2.find_one({"_id" : ObjectId(contrib_id)}) #contrib_url = filter(lambda x: x["ref"] == contrib_id, user["contribs"])[0]["url"] items = contrib["items"] if ("items" in contrib) else [] def map_node(node_name): pos = [-1, -1] if nodes_meta: node_meta = filter(lambda x: x["id"] == node_name, nodes_meta) if len(node_meta) > 0: pos = node_meta[0]["pos"] return {"id": node_name, "name": node_name, "meta" : {"pos" : pos}} for item in items: node_name_1 = node_name = item["object"] if node_name not in nodes: nodes[node_name] = map_node(node_name) node_name_2 = node_name = item["subject"] if node_name not in nodes: nodes[node_name] = map_node(node_name) edge_name = node_name_1 + " " + node_name_2 if edge_name in edges: edge = edges[edge_name] for item_tag in item["predicates"]: edge_tag = filter(lambda x: x["val"] == item_tag["val"] and x["type"] == item_tag["type"], edge["tags"]) if len(edge_tag) > 0: edge_tag = edge_tag[0] if item["url"] not in edge_tag["urls"]: edge_tag["urls"].append(item["url"]) else: item_tag["urls"] = [item["url"]] edge["tags"].append(item_tag) else: edge = {"id": edge_name, "source_id": node_name_1, "target_id": node_name_2, "tags" : item["predicates"]} for tag in edge["tags"]: tag["urls"] = [item["url"]] edges[edge_name] = edge return {"id": graph_id, "isYours": is_yours, "owner" : user["_id"], "name": graph_name, "nodes": nodes.values(), "edges": edges.values()}