Example #1
0
def _table_search_query(graph: GraphTraversalSource,
                        tag_filter: str) -> List[Dict]:
    traversal = graph.V().hasLabel(TableMetadata.TABLE_NODE_LABEL)
    if tag_filter:
        traversal = traversal.has('published_tag', tag_filter)
    traversal = traversal.project('database', 'cluster', 'schema',
                                  'schema_description', 'name', 'key',
                                  'description', 'last_updated_timestamp',
                                  'column_names', 'column_descriptions',
                                  'total_usage', 'unique_usage', 'tags',
                                  'badges', 'programmatic_descriptions')
    traversal = traversal.by(
        __.out(TableMetadata.TABLE_SCHEMA_RELATION_TYPE).out(
            SCHEMA_REVERSE_RELATION_TYPE).out(
                CLUSTER_REVERSE_RELATION_TYPE).values('name'))  # database
    traversal = traversal.by(
        __.out(TableMetadata.TABLE_SCHEMA_RELATION_TYPE).out(
            SCHEMA_REVERSE_RELATION_TYPE).values('name'))  # cluster
    traversal = traversal.by(
        __.out(
            TableMetadata.TABLE_SCHEMA_RELATION_TYPE).values('name'))  # schema
    traversal = traversal.by(
        __.coalesce(
            __.out(TableMetadata.TABLE_SCHEMA_RELATION_TYPE).out(
                DescriptionMetadata.DESCRIPTION_RELATION_TYPE).values(
                    'description'), __.constant('')))  # schema_description
    traversal = traversal.by('name')  # name
    traversal = traversal.by(T.id)  # key
    traversal = traversal.by(
        __.coalesce(
            __.out(DescriptionMetadata.DESCRIPTION_RELATION_TYPE).values(
                'description'), __.constant('')))  # description
    traversal = traversal.by(
        __.coalesce(
            __.out(LASTUPDATED_RELATION_TYPE).values(TIMESTAMP_PROPERTY),
            __.constant('')))  # last_updated_timestamp
    traversal = traversal.by(
        __.out(TableMetadata.TABLE_COL_RELATION_TYPE).values(
            'name').fold())  # column_names
    traversal = traversal.by(
        __.out(TableMetadata.TABLE_COL_RELATION_TYPE).out(
            DescriptionMetadata.DESCRIPTION_RELATION_TYPE).values(
                'description').fold())  # column_descriptions
    traversal = traversal.by(
        __.coalesce(
            __.outE(READ_REVERSE_RELATION_TYPE).values('read_count'),
            __.constant(0)).sum())  # total_usage
    traversal = traversal.by(
        __.outE(READ_REVERSE_RELATION_TYPE).count())  # unique_usage
    traversal = traversal.by(
        __.inE(TableMetadata.TAG_TABLE_RELATION_TYPE).outV().values(
            METADATA_KEY_PROPERTY_NAME).fold())  # tags
    traversal = traversal.by(
        __.out('HAS_BADGE').values('keys').dedup().fold())  # badges
    traversal = traversal.by(
        __.out(DescriptionMetadata.PROGRAMMATIC_DESCRIPTION_NODE_LABEL).values(
            'description').fold())  # programmatic_descriptions
    traversal = traversal.order().by(__.select('name'), Order.asc)
    return traversal.toList()
Example #2
0
def _user_search_query(graph: GraphTraversalSource,
                       tag_filter: str) -> List[Dict]:
    traversal = graph.V().hasLabel(User.USER_NODE_LABEL)
    traversal = traversal.has(User.USER_NODE_FULL_NAME)
    if tag_filter:
        traversal = traversal.where('published_tag', tag_filter)
    traversal = traversal.project('email', 'first_name', 'last_name',
                                  'full_name', 'github_username', 'team_name',
                                  'employee_type', 'manager_email', 'slack_id',
                                  'is_active', 'role_name', 'total_read',
                                  'total_own', 'total_follow')
    traversal = traversal.by('email')  # email
    traversal = traversal.by('first_name')  # first_name
    traversal = traversal.by('last_name')  # last_name
    traversal = traversal.by('full_name')  # full_name
    traversal = traversal.by('github_username')  # github_username
    traversal = traversal.by('team_name')  # team_name
    traversal = traversal.by('employee_type')  # employee_type
    traversal = traversal.by(
        __.coalesce(
            __.out(User.USER_MANAGER_RELATION_TYPE).values('email'),
            __.constant('')))  # manager_email
    traversal = traversal.by('slack_id')  # slack_id
    traversal = traversal.by('is_active')  # is_active
    traversal = traversal.by('role_name')  # role_name
    traversal = traversal.by(
        __.coalesce(
            __.outE(READ_RELATION_TYPE).values('read_count'),
            __.constant(0)).sum())  # total_read
    traversal = traversal.by(
        __.outE(OWNER_OF_OBJECT_RELATION_TYPE).fold().count())  # total_own
    traversal = traversal.by(
        __.outE('FOLLOWED_BY').fold().count())  # total_follow
    traversal = traversal.order().by(__.select('email'), Order.asc)
    return traversal.toList()
def check_ri_without_rt(g):
    """routing-instance that doesn't have any route-target (that crashes schema)
    """
    return g.V().hasLabel("routing_instance") \
        .not_(__.has('fq_name', within(["default-domain", "default-project", "ip-fabric", "__default__"],
                                       ["default-domain", "default-project", "__link_local__", "__link_local__"]))) \
        .not_(__.out().hasLabel("route_target"))
Example #4
0
def query_02(g, alerts_unique):
    """
    Calculate the  directed longest chain in the graph, vertex represented by name
    :param g: graph traversal source object
    :param alerts_unique: valid unique alerts name, without None
    :return: None
    """
    # statics.load_statics(globals())
    print("{0:}QUERY_02{0:}".format(23 * '='))
    longest_chains = []
    max_chain_len = 1
    for elem in alerts_unique:
        chain = g.V().has("name", elem).repeat(__.out().simplePath()).emit().path().by("name").toList()
        if len(chain) != 0 :
            temp = chain[-1] # the returned paths are sorted in ascending order
            if len(temp) > max_chain_len:
                max_chain_len = len(temp)
                longest_chains = [temp]
            elif len(temp) == max_chain_len:
                longest_chains.append(temp)

    print("Longest chain length is {}.\nLongest Chains:".format(max_chain_len))
    for elem in longest_chains:
        print(elem)
    print('\n')
    return
Example #5
0
 def getNextDetectionsById(self, id, limit):
     #  This can be used to return self
     vehIds = self.g.V(self.b.of(TrajectoryGraph.OUT_V,
                                 id)).emit().repeat(__.out()).times(
                                     self.b.of(TrajectoryGraph.LIMIT,
                                               limit)).id().toList()
     logging.info("NextDetections from V[{}]: {}".format(id, vehIds))
     return vehIds
Example #6
0
def get_nodes_of_type(g, id_):
    """
        return nodes that have an link "rdf:type" to type node identified by id_
        :param g: gremlin graph
        :param id_: the type node label, URI, node id in the graph or gremlin Vertex object
    """
    type_id = get_node(g, id_)
    return g.V().as_("node").where(__.out("rdf:type").hasId(
        type_id.id)).select("node").dedup().toList()
Example #7
0
    def test_get_top_collections(self):
        """
            This test works with Version LAM_metadata_05_ECO of the source file
        :return:
        """
        known_label = "skos:Collection"
        type_id = rdf2g.get_node(self.g, known_label)
        # 17 expoected
        collections = self.g.V().as_("node").where(__.out("rdf:type").hasId(type_id.id)).select("node").dedup().toList()
        assert len(collections) > 10, "Did not retrieve the expected  17 collections"
        collections = self.g.V().match(__.as_("a").out("rdf:type").hasId(type_id.id),
                                       __.as_("a").in_("skos:member")).select("a").properties(
            "skos:prefLabel").toList()
        assert len(collections) > 5, "Did not retrieve the expected 9 collections"

        collections = self.g.V().match(__.as_("a").out("rdf:type").hasId(type_id.id),
                                       __.not_(__.as_("a").in_("skos:member"))).select("a").toList()
        assert len(collections) > 5, "Did not retrieve the expected 8 collections"
Example #8
0
def check_duplicate_default_sg(g):
    """duplicate default security groups
    """
    r = g.V().hasLabel('project').flatMap(
        __.out().hasLabel('security_group').has('display_name', 'default').group().by(
            __.in_().hasLabel('project').id()
        ).unfold()
        .filter(lambda: "it.get().value.size() > 1")
    ).toList()
    if len(r) > 0:
        printo('Found %d %s:' % (len(r), check_duplicate_default_sg.__doc__.strip()))
    projects = []
    for dup in r:
        for p, sgs in dup.items():
            projects.append(v_to_r(p))
            printo("  %s:" % projects[-1])
            for sg in sgs:
                printo("    - %s" % sg)
    return projects
Example #9
0
def _dashboard_search_query(graph: GraphTraversalSource,
                            tag_filter: str) -> List[Dict]:
    traversal = graph.V().hasLabel(DashboardMetadata.DASHBOARD_NODE_LABEL)
    traversal = traversal.has('name')
    if tag_filter:
        traversal = traversal.where('published_tag', tag_filter)

    traversal = traversal.project('group_name', 'name', 'cluster',
                                  'description', 'group_description',
                                  'group_url', 'url', 'uri',
                                  'last_successful_run_timestamp',
                                  'query_names', 'chart_names', 'total_usage',
                                  'tags', 'badges')
    traversal = traversal.by(
        __.out(
            DashboardMetadata.DASHBOARD_DASHBOARD_GROUP_RELATION_TYPE).values(
                'name'))  # group_name
    traversal = traversal.by('name')  # name
    traversal = traversal.by(
        __.out(DashboardMetadata.DASHBOARD_DASHBOARD_GROUP_RELATION_TYPE).out(
            DashboardMetadata.DASHBOARD_GROUP_CLUSTER_RELATION_TYPE).values(
                'name'))  # cluster
    traversal = traversal.by(
        __.coalesce(
            __.out(
                DashboardMetadata.DASHBOARD_DESCRIPTION_RELATION_TYPE).values(
                    'description'), __.constant('')))  # description
    traversal = traversal.by(
        __.coalesce(
            __.out(
                DashboardMetadata.DASHBOARD_DASHBOARD_GROUP_RELATION_TYPE).out(
                    DashboardMetadata.DASHBOARD_DESCRIPTION_RELATION_TYPE).
            values('description'), __.constant('')))  # group_description
    traversal = traversal.by(
        __.out(
            DashboardMetadata.DASHBOARD_DASHBOARD_GROUP_RELATION_TYPE).values(
                'dashboard_group_url'))  # group_url
    traversal = traversal.by('dashboard_url')  # dashboard_url
    traversal = traversal.by('key')  # uri

    traversal = traversal.by(
        __.coalesce(
            __.out('EXECUTED').has(
                'key', TextP.endingWith(
                    '_last_successful_execution')).values('timestamp'),
            __.constant('')))  # last_successful_run_timestamp
    traversal = traversal.by(
        __.out(DashboardQuery.DASHBOARD_QUERY_RELATION_TYPE).values(
            'name').dedup().fold())  # query_names
    traversal = traversal.by(
        __.out(DashboardQuery.DASHBOARD_QUERY_RELATION_TYPE).out(
            DashboardChart.CHART_RELATION_TYPE).values(
                'name').dedup().fold())  # chart_names
    traversal = traversal.by(
        __.coalesce(
            __.outE(READ_REVERSE_RELATION_TYPE).values(
                READ_RELATION_COUNT_PROPERTY),
            __.constant(0)).sum())  # total_usage
    traversal = traversal.by(
        __.out('TAGGED_BY').has(
            'tag_type', 'default').values('keys').dedup().fold())  # tags
    traversal = traversal.by(
        __.out('HAS_BADGE').values('keys').dedup().fold())  # badges

    traversal = traversal.order().by(__.select('name'), Order.asc)

    dashboards = traversal.toList()
    for dashboard in dashboards:
        dashboard['product'] = dashboard['uri'].split('_')[0]

    return dashboards
Example #10
0
    def test_traversals(self, remote_connection):
        statics.load_statics(globals())
        g = traversal().withRemote(remote_connection)

        assert long(6) == g.V().count().toList()[0]
        # #
        assert Vertex(1) == g.V(1).next()
        assert Vertex(1) == g.V(Vertex(1)).next()
        assert 1 == g.V(1).id_().next()
        assert Traverser(Vertex(1)) == g.V(1).nextTraverser()
        assert 1 == len(g.V(1).toList())
        assert isinstance(g.V(1).toList(), list)
        results = g.V().repeat(__.out()).times(2).name
        results = results.toList()
        assert 2 == len(results)
        assert "lop" in results
        assert "ripple" in results
        # #
        assert 10 == g.V().repeat(__.both()).times(5)[0:10].count().next()
        assert 1 == g.V().repeat(__.both()).times(5)[0:1].count().next()
        assert 0 == g.V().repeat(__.both()).times(5)[0:0].count().next()
        assert 4 == g.V()[2:].count().next()
        assert 2 == g.V()[:2].count().next()
        # #
        results = g.withSideEffect(
            'a', ['josh', 'peter'
                  ]).V(1).out('created').in_('created').values('name').where(
                      P.within('a')).toList()
        assert 2 == len(results)
        assert 'josh' in results
        assert 'peter' in results
        # #
        results = g.V().out().profile().toList()
        assert 1 == len(results)
        assert 'metrics' in results[0]
        assert 'dur' in results[0]
        # #
        results = g.V().has('name',
                            'peter').as_('a').out('created').as_('b').select(
                                'a', 'b').by(__.valueMap()).toList()
        assert 1 == len(results)
        assert 'peter' == results[0]['a']['name'][0]
        assert 35 == results[0]['a']['age'][0]
        assert 'lop' == results[0]['b']['name'][0]
        assert 'java' == results[0]['b']['lang'][0]
        assert 2 == len(results[0]['a'])
        assert 2 == len(results[0]['b'])
        # #
        results = g.V(1).inject(g.V(2).next()).values('name').toList()
        assert 2 == len(results)
        assert 'marko' in results
        assert 'vadas' in results
        # #
        results = g.V().has('person', 'name', 'marko').map(
            lambda: ("it.get().value('name')", "gremlin-groovy")).toList()
        assert 1 == len(results)
        assert 'marko' in results
        # #
        # this test just validates that the underscored versions of steps conflicting with Gremlin work
        # properly and can be removed when the old steps are removed - TINKERPOP-2272
        results = g.V().filter_(
            __.values('age').sum_().and_(__.max_().is_(P.gt(0)),
                                         __.min_().is_(P.gt(0)))).range_(
                                             0, 1).id_().next()
        assert 1 == results
        # #
        # test binding in P
        results = g.V().has('person', 'age',
                            Bindings.of('x', P.lt(30))).count().next()
        assert 2 == results
        # #
        # test dict keys which can only work on GraphBinary and GraphSON3 which include specific serialization
        # types for dict
        if not isinstance(remote_connection._client._message_serializer,
                          GraphSONSerializersV2d0):
            results = g.V().has(
                'person', 'name',
                'marko').elementMap("name").groupCount().next()
            assert {
                HashableDict.of({
                    T.id: 1,
                    T.label: 'person',
                    'name': 'marko'
                }): 1
            } == results
        if not isinstance(remote_connection._client._message_serializer,
                          GraphSONSerializersV2d0):
            results = g.V().has('person', 'name',
                                'marko').both('knows').groupCount().by(
                                    __.values('name').fold()).next()
            assert {tuple(['vadas']): 1, tuple(['josh']): 1} == results
Example #11
0
        print("-", end="")
    print("")


# Create a SubgraphStrategy that only picks up airports in Texas
# and routes between them.
heading('SubgraphStrategy - just Texas airports')
strategy = SubgraphStrategy(vertices=__.has("region", "US-TX"),
                            edges=__.hasLabel('route'))
g2 = g.withStrategies(strategy)
verts = g2.V().count().next()
edges = g2.E().count().next()

routes = g2.V().\
         order().\
           by(__.out().count()).\
         group().\
           by('code').\
           by(__.out().count()).\
         order(Scope.local).by(Column.values).\
         next()

print("Found {} airports and {} routes.".format(verts, edges))
for k, v in routes.items():
    print(k, v)

# Create a ReadOnlyStrategy - any attempt to add or change an element
# using this traversal source should cause an exception to be thrown.
heading('ReadOnlyStrateggy')
g3 = g.withStrategies(ReadOnlyStrategy())
try:
def check_vmi_without_ri(g):
    """virtual-machine-interface without any routing-instance
    """
    return g.V().hasLabel('virtual_machine_interface').not_(
        __.out().hasLabel('routing_instance'))
Example #13
0
    def test_translations(self):
        g = traversal().withGraph(Graph())

        tests = list()
        # 0
        tests.append([g.V(),
                     "g.V()"])
        # 1
        tests.append([g.V('1', '2', '3', '4'),
                     "g.V('1','2','3','4')"])
        # 2
        tests.append([g.V('3').valueMap(True),
                     "g.V('3').valueMap(True)"])
        # 3
        tests.append([g.V().constant(5),
                     "g.V().constant(5)"])
        # 4
        tests.append([g.V().constant(1.5),
                     "g.V().constant(1.5)"])
        # 5
        tests.append([g.V().constant('Hello'),
                     "g.V().constant('Hello')"])
        # 6
        tests.append([g.V().hasLabel('airport').limit(5),
                     "g.V().hasLabel('airport').limit(5)"])
        # 7
        tests.append([g.V().hasLabel(within('a', 'b', 'c')),
                     "g.V().hasLabel(within(['a','b','c']))"])
        # 8
        tests.append([g.V().hasLabel('airport', 'continent').out().limit(5),
                     "g.V().hasLabel('airport','continent').out().limit(5)"])
        # 9
        tests.append([g.V().hasLabel('airport').out().values('code').limit(5),
                     "g.V().hasLabel('airport').out().values('code').limit(5)"])
        # 10
        tests.append([g.V('3').as_('a').out('route').limit(10).where(eq('a')).by('region'),
                     "g.V('3').as('a').out('route').limit(10).where(eq('a')).by('region')"])
        # 11
        tests.append([g.V('3').repeat(__.out('route').simplePath()).times(2).path().by('code'),
                     "g.V('3').repeat(__.out('route').simplePath()).times(2).path().by('code')"])
        # 12
        tests.append([g.V().hasLabel('airport').out().has('region', 'US-TX').values('code').limit(5),
                     "g.V().hasLabel('airport').out().has('region','US-TX').values('code').limit(5)"])
        # 13
        tests.append([g.V().hasLabel('airport').union(__.values('city'), __.values('region')).limit(5),
                     "g.V().hasLabel('airport').union(__.values('city'),__.values('region')).limit(5)"])
        # 14
        tests.append([g.V('3').as_('a').out('route', 'routes'),
                     "g.V('3').as('a').out('route','routes')"])
        # 15
        tests.append([g.V().where(__.values('runways').is_(5)),
                    "g.V().where(__.values('runways').is(5))"])
        # 16
        tests.append([g.V('3').repeat(__.out().simplePath()).until(__.has('code', 'AGR')).path().by('code').limit(5),
                     "g.V('3').repeat(__.out().simplePath()).until(__.has('code','AGR')).path().by('code').limit(5)"])
        # 17
        tests.append([g.V().hasLabel('airport').order().by(__.id()),
                     "g.V().hasLabel('airport').order().by(__.id())"])
        # 18
        tests.append([g.V().hasLabel('airport').order().by(T.id),
                     "g.V().hasLabel('airport').order().by(T.id)"])
        # 19
        tests.append([g.V().hasLabel('airport').order().by(__.id(),Order.desc),
                     "g.V().hasLabel('airport').order().by(__.id(),Order.desc)"])
        # 20
        tests.append([g.V().hasLabel('airport').order().by('code',Order.desc),
                     "g.V().hasLabel('airport').order().by('code',Order.desc)"])
        # 21
        tests.append([g.V('1', '2', '3').local(__.out().out().dedup().fold()),
                     "g.V('1','2','3').local(__.out().out().dedup().fold())"])
        # 22
        tests.append([g.V('3').out().path().count(Scope.local),
                     "g.V('3').out().path().count(Scope.local)"])
        # 23
        tests.append([g.E().count(),
                     "g.E().count()"])
        # 24
        tests.append([g.V('5').outE('route').inV().path().limit(10),
                     "g.V('5').outE('route').inV().path().limit(10)"])
        # 25
        tests.append([g.V('5').propertyMap().select(Column.keys),
                     "g.V('5').propertyMap().select(Column.keys)"])
        # 26
        tests.append([g.V('5').propertyMap().select(Column.values),
                     "g.V('5').propertyMap().select(Column.values)"])
        # 27
        tests.append([g.V('3').values('runways').math('_ + 1'),
                     "g.V('3').values('runways').math('_ + 1')"])
        # 28
        tests.append([g.V('3').emit().repeat(__.out().simplePath()).times(3).limit(5).path(),
                     "g.V('3').emit().repeat(__.out().simplePath()).times(3).limit(5).path()"])
        # 29
        tests.append([g.V().match(__.as_('a').has('code', 'LHR').as_('b')).select('b').by('code'),
                     "g.V().match(__.as('a').has('code','LHR').as('b')).select('b').by('code')"])
        # 30
        tests.append([g.V().has('test-using-keyword-as-property','repeat'),
                     "g.V().has('test-using-keyword-as-property','repeat')"])
        # 31
        tests.append([g.V('1').addE('test').to(__.V('4')),
                     "g.V('1').addE('test').to(__.V('4'))"])
        # 32
        tests.append([g.V().values('runways').max(),
                     "g.V().values('runways').max()"])
        # 33
        tests.append([g.V().values('runways').min(),
                     "g.V().values('runways').min()"])
        # 34
        tests.append([g.V().values('runways').sum(),
                     "g.V().values('runways').sum()"])
        # 35
        tests.append([g.V().values('runways').mean(),
                     "g.V().values('runways').mean()"])
        # 36
        tests.append([g.withSack(0).V('3', '5').sack(Operator.sum).by('runways').sack(),
                     "g.withSack(0).V('3','5').sack(Operator.sum).by('runways').sack()"])
        # 37
        tests.append([g.V('3').values('runways').store('x').V('4').values('runways').store('x').by(__.constant(1)).V('6').store('x').by(__.constant(1)).select('x').unfold().sum(),
                     "g.V('3').values('runways').store('x').V('4').values('runways').store('x').by(__.constant(1)).V('6').store('x').by(__.constant(1)).select('x').unfold().sum()"])
        # 38
        tests.append([g.inject(3, 4, 5),
                     "g.inject(3,4,5)"])
        # 39
        tests.append([g.inject([3, 4, 5]),
                     "g.inject([3, 4, 5])"])
        # 40
        tests.append([g.inject(3, 4, 5).count(),
                     "g.inject(3,4,5).count()"])
        # 41
        tests.append([g.V().has('runways', gt(5)).count(),
                     "g.V().has('runways',gt(5)).count()"])
        # 42
        tests.append([g.V().has('runways', lte(5.3)).count(),
                     "g.V().has('runways',lte(5.3)).count()"])
        # 43
        tests.append([g.V().has('code', within(123,124)),
                     "g.V().has('code',within([123,124]))"])
        # 44
        tests.append([g.V().has('code', within(123, 'abc')),
                     "g.V().has('code',within([123,'abc']))"])
        # 45
        tests.append([g.V().has('code', within('abc', 123)),
                     "g.V().has('code',within(['abc',123]))"])
        # 46
        tests.append([g.V().has('code', within('abc', 'xyz')),
                     "g.V().has('code',within(['abc','xyz']))"])
        # 47
        tests.append([g.V('1', '2').has('region', P.within('US-TX','US-GA')),
                     "g.V('1','2').has('region',within(['US-TX','US-GA']))"])
        # 48
        tests.append([g.V().and_(__.has('runways', P.gt(5)), __.has('region','US-TX')),
                     "g.V().and(__.has('runways',gt(5)),__.has('region','US-TX'))"])
        # 49
        tests.append([g.V().union(__.has('runways', gt(5)), __.has('region','US-TX')),
                     "g.V().union(__.has('runways',gt(5)),__.has('region','US-TX'))"])
        # 50
        tests.append([g.V('3').choose(__.values('runways').is_(3),__.constant('three'),__.constant('not three')),
                     "g.V('3').choose(__.values('runways').is(3),__.constant('three'),__.constant('not three'))"])
        # 51
        tests.append([g.V('3').choose(__.values('runways')).option(1,__.constant('three')).option(2,__.constant('not three')),
                     "g.V('3').choose(__.values('runways')).option(1,__.constant('three')).option(2,__.constant('not three'))"])
        # 52
        tests.append([g.V('3').choose(__.values('runways')).option(1.5,__.constant('one and a half')).option(2,__.constant('not three')),
                     "g.V('3').choose(__.values('runways')).option(1.5,__.constant('one and a half')).option(2,__.constant('not three'))"])
        # 53
        tests.append([g.V('3').repeat(__.out().simplePath()).until(__.loops().is_(1)).count(),
                     "g.V('3').repeat(__.out().simplePath()).until(__.loops().is(1)).count()"])
        # 54
        tests.append([g.V().hasLabel('airport').limit(20).group().by('region').by('code').order(Scope.local).by(Column.keys),
                     "g.V().hasLabel('airport').limit(20).group().by('region').by('code').order(Scope.local).by(Column.keys)"])
        # 55
        tests.append([g.V('1').as_('a').V('2').as_('a').select(Pop.all_, 'a'),
                     "g.V('1').as('a').V('2').as('a').select(Pop.all,'a')"])
        # 56
        tests.append([g.addV('test').property(Cardinality.set_, 'p1', 10),
                     "g.addV('test').property(Cardinality.set,'p1',10)"])
        # 57
        tests.append([g.addV('test').property(Cardinality.list_, 'p1', 10),
                     "g.addV('test').property(Cardinality.list,'p1',10)"])

        # 58
        tests.append([g.addV('test').property(Cardinality.single, 'p1', 10),
                     "g.addV('test').property(Cardinality.single,'p1',10)"])
        # 59
        tests.append([g.V().limit(5).order().by(T.label),
                     "g.V().limit(5).order().by(T.label)"])

        # 60
        tests.append([g.V().range(1, 5),
                     "g.V().range(1,5)"])

        # 61
        tests.append([g.addV('test').property('p1', 123),
                     "g.addV('test').property('p1',123)"])

        # 62
        tests.append([g.addV('test').property('date',datetime(2021, 2, 1, 9, 30)),
                     "g.addV('test').property('date',new Date(121,2,1,9,30,0))"])
        # 63
        tests.append([g.addV('test').property('date',datetime(2021, 2, 1)),
                     "g.addV('test').property('date',new Date(121,2,1,0,0,0))"])
        # 64
        tests.append([g.addE('route').from_(__.V('1')).to(__.V('2')),
                     "g.addE('route').from(__.V('1')).to(__.V('2'))"])
        # 65
        tests.append([g.withSideEffect('a', [1, 2]).V('3').select('a'),
                     "g.withSideEffect('a',[1, 2]).V('3').select('a')"])
        # 66
        tests.append([g.withSideEffect('a', 1).V('3').select('a'),
                     "g.withSideEffect('a',1).V('3').select('a')"])
        # 67
        tests.append([g.withSideEffect('a', 'abc').V('3').select('a'),
                     "g.withSideEffect('a','abc').V('3').select('a')"])
        # 68
        tests.append([g.V().has('airport', 'region', 'US-NM').limit(3).values('elev').fold().index(),
                     "g.V().has('airport','region','US-NM').limit(3).values('elev').fold().index()"])
        # 69
        tests.append([g.V('3').repeat(__.timeLimit(1000).out().simplePath()).until(__.has('code', 'AGR')).path(),
                     "g.V('3').repeat(__.timeLimit(1000).out().simplePath()).until(__.has('code','AGR')).path()"])

        # 70
        tests.append([g.V().hasLabel('airport').where(__.values('elev').is_(gt(14000))),
                     "g.V().hasLabel('airport').where(__.values('elev').is(gt(14000)))"])

        # 71
        tests.append([g.V().hasLabel('airport').where(__.out().count().is_(gt(250))).values('code'),
                     "g.V().hasLabel('airport').where(__.out().count().is(gt(250))).values('code')"])

        # 72
        tests.append([g.V().hasLabel('airport').filter(__.out().count().is_(gt(250))).values('code'),
                     "g.V().hasLabel('airport').filter(__.out().count().is(gt(250))).values('code')"])
        # 73
        tests.append([g.withSack(0).
                        V('3').
                        repeat(__.outE('route').sack(Operator.sum).by('dist').inV()).
                        until(__.has('code', 'AGR').or_().loops().is_(4)).
                        has('code', 'AGR').
                        local(__.union(__.path().by('code').by('dist'),__.sack()).fold()).
                        limit(10),
                     "g.withSack(0).V('3').repeat(__.outE('route').sack(Operator.sum).by('dist').inV()).until(__.has('code','AGR').or().loops().is(4)).has('code','AGR').local(__.union(__.path().by('code').by('dist'),__.sack()).fold()).limit(10)"])

        # 74
        tests.append([g.addV().as_('a').addV().as_('b').addE('knows').from_('a').to('b'),
                     "g.addV().as('a').addV().as('b').addE('knows').from('a').to('b')"])

        # 75
        tests.append([g.addV('Person').as_('a').addV('Person').as_('b').addE('knows').from_('a').to('b'),
                     "g.addV('Person').as('a').addV('Person').as('b').addE('knows').from('a').to('b')"])
        # 76
        tests.append([g.V('3').project('Out','In').by(__.out().count()).by(__.in_().count()),
                     "g.V('3').project('Out','In').by(__.out().count()).by(__.in().count())"])
        # 77
        tests.append([g.V('44').out().aggregate('a').out().where(within('a')).path(),
                     "g.V('44').out().aggregate('a').out().where(within(['a'])).path()"])
        # 78
        tests.append([g.V().has('date', datetime(2021, 2, 22)),
                     "g.V().has('date',new Date(121,2,22,0,0,0))"])
        # 79
        tests.append([g.V().has('date', within(datetime(2021, 2, 22), datetime(2021, 1, 1))),
                      "g.V().has('date',within([new Date(121,2,22,0,0,0),new Date(121,1,1,0,0,0)]))"])
        # 80
        tests.append([g.V().has('date', between(datetime(2021, 1, 1), datetime(2021, 2, 22))),
                                "g.V().has('date',between(new Date(121,1,1,0,0,0),new Date(121,2,22,0,0,0)))"])
        # 81
        tests.append([g.V().has('date', inside(datetime(2021, 1, 1),datetime(2021, 2, 22))),
                                "g.V().has('date',inside(new Date(121,1,1,0,0,0),new Date(121,2,22,0,0,0)))"])
        # 82
        tests.append([g.V().has('date', P.gt(datetime(2021, 1, 1, 9, 30))),
                     "g.V().has('date',gt(new Date(121,1,1,9,30,0)))"])
        # 83
        tests.append([g.V().has('runways', between(3,5)),
                     "g.V().has('runways',between(3,5))"])
        # 84
        tests.append([g.V().has('runways', inside(3,5)),
                     "g.V().has('runways',inside(3,5))"])
        # 85
        tests.append([g.V('44').outE().elementMap(),
                     "g.V('44').outE().elementMap()"])
        # 86
        tests.append([g.V('44').valueMap().by(__.unfold()),
                     "g.V('44').valueMap().by(__.unfold())"])
        # 87
        tests.append([g.V('44').valueMap().with_(WithOptions.tokens,WithOptions.labels),
                     "g.V('44').valueMap().with(WithOptions.tokens,WithOptions.labels)"])
        # 88
        tests.append([g.V('44').valueMap().with_(WithOptions.tokens),
                     "g.V('44').valueMap().with(WithOptions.tokens)"])
        # 89
        tests.append([g.withStrategies(ReadOnlyStrategy()).addV('test'),
                      "g.withStrategies(new ReadOnlyStrategy()).addV('test')"])
        # 90
        strategy = SubgraphStrategy(vertices=__.has('region', 'US-TX'), edges=__.hasLabel('route'))
        tests.append([g.withStrategies(strategy).V().count(),
                    "g.withStrategies(new SubgraphStrategy(vertices:__.has('region','US-TX'),edges:__.hasLabel('route'))).V().count()"])
        # 91
        strategy = SubgraphStrategy(vertex_properties=__.hasNot('runways'))
        tests.append([g.withStrategies(strategy).V().count(),
                      "g.withStrategies(new SubgraphStrategy(vertexProperties:__.hasNot('runways'))).V().count()"])
        # 92
        strategy = SubgraphStrategy(vertices=__.has('region', 'US-TX'),vertex_properties=__.hasNot('runways'))
        tests.append([g.withStrategies(strategy).V().count(),
                      "g.withStrategies(new SubgraphStrategy(vertices:__.has('region','US-TX'),vertexProperties:__.hasNot('runways'))).V().count()"])
        # 93
        strategy = SubgraphStrategy(vertices=__.has('region', 'US-TX'), edges=__.hasLabel('route'))
        tests.append([g.withStrategies(ReadOnlyStrategy(),strategy).V().count(),
                      "g.withStrategies(new ReadOnlyStrategy(),new SubgraphStrategy(vertices:__.has('region','US-TX'),edges:__.hasLabel('route'))).V().count()"])
        # 94
        strategy = SubgraphStrategy(vertices=__.has('region', 'US-TX'))
        tests.append([g.withStrategies(ReadOnlyStrategy(), strategy).V().count(),
                      "g.withStrategies(new ReadOnlyStrategy(),new SubgraphStrategy(vertices:__.has('region','US-TX'))).V().count()"])
        # 95
        tests.append([g.with_('evaluationTimeout', 500).V().count(),
                      "g.withStrategies(new OptionsStrategy(evaluationTimeout:500)).V().count()"])
        # 96
        tests.append([g.withStrategies(OptionsStrategy({'evaluationTimeout': 500})).V().count(),
                     "g.withStrategies(new OptionsStrategy(evaluationTimeout:500)).V().count()"])
        # 97
        tests.append([g.withStrategies(PartitionStrategy(partition_key="partition", write_partition="a", read_partitions=["a"])).addV('test'),
                     "g.withStrategies(new PartitionStrategy(partitionKey:'partition',writePartition:'a',readPartitions:['a'])).addV('test')"])
        # 98
        tests.append([g.withComputer().V().shortestPath().with_(ShortestPath.target, __.has('name','peter')),
                     "g.withStrategies(new VertexProgramStrategy()).V().shortestPath().with('~tinkerpop.shortestPath.target',__.has('name','peter'))"])

        tlr = Translator().of('g')

        for t in range(len(tests)):
            a = tlr.translate(tests[t][0].bytecode)
            assert a == tests[t][1]
    def get_suggested_for_user(self, user_id, page_size, paging_state):

        # Note: we're building a single graph traversal, but describing in three parts for readability

        # Part 1: finding "similar users"
        # - find the vertex for the user
        # - get all of the videos the user watched and store them
        # - go back to our current user
        # - for the video's I rated highly...
        # - what other users rated those videos highly? (this is like saying "what users share my taste")
        # - but don't grab too many, or this won't work OLTP, and "by('rating')" favors the higher ratings
        # - (except the current user)

        # Part 2: finding videos that were highly rated by similar users
        # - For those users who share my taste, grab N highly rated videos.
        # - Save the rating so we can sum the scores later, and use sack()
        # - because it does not require path information. (as()/select() was slow)
        # - excluding the videos the user has already watched
        # - Filter out the video if for some reason there is no uploaded edge to a user
        # - what are the most popular videos as calculated by the sum of all their ratings

        # Part 3: now that we have that big map of [video: score], let's order it
        # - then grab properties of the video and the user who uploaded each video using project()

        traversal = self.graph.V().has('user', 'userId', user_id).as_('^user') \
            .map(__.out('rated').dedup().fold()).as_('^watchedVideos') \
            .select('^user') \
            .outE('rated').has('rating', gte(MIN_RATING)).inV() \
            .inE('rated').has('rating', gte(MIN_RATING)) \
            .sample(NUM_RATINGS_TO_SAMPLE).by('rating').outV() \
            .where(neq('^user')) \
            .local(__.outE('rated').has('rating', gte(MIN_RATING)).limit(LOCAL_USER_RATINGS_TO_SAMPLE)) \
            .sack(Operator.assign).by('rating').inV() \
            .filter(__.in_('uploaded').hasLabel('user')) \
            .group().by().by(__.sack().sum()) \
            .order(Scope.local).by(Column.values, Order.decr) \
            .limit(Scope.local, NUM_RECOMMENDATIONS).select(Column.keys).unfold() \
            .project('video_id', 'added_date', 'name', 'preview_image_location', 'user_id') \
            .by('videoId').by('added_date').by('name').by('preview_image_location').by(__.in_('uploaded').values('userId'))

        # TODO: this step needs to be reinserted after .sack and before .filter
        #.not_(__.where(within('^watchedVideos'))) \

        logging.debug('Traversal: ' + str(traversal.bytecode))

        results = traversal.toList()
        logging.debug('Traversal generated ' + str(len(results)) + ' results')

        videos = list()
        for result in results:
            logging.debug('Traversal Result: ' + str(result))
            videos.append(
                VideoPreview(
                    video_id=result['video_id'],
                    added_date=result['added_date'],
                    user_id=result['user_id'],
                    name=result['name'],
                    preview_image_location=result['preview_image_location']))

        return SuggestedVideosResponse(user_id=user_id,
                                       videos=videos,
                                       paging_state=None)
Example #15
0
def check_iip_without_vmi(g):
    """instance-ip without any virtual-machine-interface
    """
    return g.V().hasLabel('instance_ip').not_(
        __.out().hasLabel('virtual_machine_interface'))