Example #1
0
    def _build_dependencies_query(cls,
                                  team_id,
                                  topic,
                                  label,
                                  node,
                                  filter_on_config=False):
        """Build a Cypher query based on given parameters."""
        where = ""
        query = ("MATCH(n:{topic}_{label}{{name: '{name}'}}) "
                 "OPTIONAL MATCH (n)-[r]->(m) {where}"
                 "RETURN n,r,m ORDER BY m.name LIMIT 10")

        # Filter the dependencies using the labels declared in the configuration
        if filter_on_config:
            label_config = ConfigController.get_label_config(team_id, label)
            regex = r"^.*(\[[A-Za-z]+(, [A-Za-z]+)*?\])$"

            match = re.search(regex, label_config["qos"])
            if match:
                deps = match.group(1)[1:-1].split(", ")
                where += "WHERE '{}_{}' IN LABELS(m) ".format(topic, deps[0])

                for dep in deps[1:]:
                    where += "OR '{}_{}' IN LABELS(m) ".format(topic, dep)

        return query.format(where=where, topic=topic, label=label, name=node)
Example #2
0
    def get_node_dependencies(cls,
                              team_id,
                              label,
                              node,
                              filter_on_config=False):
        team = TeamController._get({"Team": {"id": team_id}})
        query = """
        MATCH(n:{topic}_{label}{{name: "{name}"}})
        OPTIONAL MATCH (n)-[r]->(m) {where}
        RETURN n,r,m
        ORDER BY m.name LIMIT 10
        """

        # Filter the dependencies with the labels declared in the configuration
        where = ""
        if filter_on_config:
            label_config = ConfigController.get_label_config(team_id, label)
            regex = r"^.*(\[[A-Za-z]+(, [A-Za-z]+)*?\])$"
            match = re.search(regex, label_config["qos"])
            if match:
                deps = match.group(1)[1:-1].split(", ")
                where += "WHERE '{}_{}' IN LABELS(m)".format(
                    team.kafka_topic, deps[0])
                for dep in deps[1:]:
                    where += " OR '{}_{}' IN LABELS(m)".format(
                        team.kafka_topic, dep)

        neo = Neo4jClient()
        query = query.format(topic=team.kafka_topic,
                             label=label,
                             name=node,
                             where=where)
        sequence = neo.query(query, data_contents=DATA_GRAPH)

        dependencies = {
            "dependencies": {},
            "graph": {
                "nodes": [],
                "relationships": []
            }
        }

        if sequence.graph:
            for g in sequence.graph:

                # Handle the nodes
                for node in g["nodes"]:
                    title = node["labels"][0][len(team.kafka_topic) + 1:]
                    if title not in dependencies["dependencies"]:
                        dependencies["dependencies"][title] = []

                    # Id is required to make data unique later
                    node["properties"]["id"] = node["id"]
                    dependencies["dependencies"][title].append(
                        node["properties"])

                    # Add the node data in the whole graph
                    dependencies["graph"]["nodes"].append({
                        "id":
                        node["id"],
                        "label":
                        node["properties"].get("name", "unknown"),
                        "title":
                        title,
                    })

                # Handle the relationships
                for rel in g["relationships"]:
                    dependencies["graph"]["relationships"].append({
                        "id":
                        rel["id"],
                        "from":
                        rel["startNode"],
                        "to":
                        rel["endNode"],
                        "arrows":
                        "to",
                    })

        dependencies = cls.make_unique(dependencies)
        return dependencies