I am new to Gremlin and am trying to convert a SQL query to Gremlin. I have two vertex types, labeled host and repo, and here is the Gremlin script to create the vertices and the edges:

g.addV('asset').property(id, 'a1').property('ip', '127.4.8.51').property('scanDate', '2020-09-10').property('repoId', 1)
g.addV('asset').property(id, 'a2').property('ip', '127.4.8.55').property('scanDate', '2020-09-20').property('repoId', 1)
g.addV('asset').property(id, 'a3').property('ip', '127.4.8.57').property('scanDate', '2020-09-21').property('repoId', 1)
g.addV('asset').property(id, 'a4').property('ip', '127.4.10.36').property('scanDate', '2020-09-12').property('repoId', 2)
g.addV('asset').property(id, 'a5').property('ip', '127.4.10.75').property('scanDate', '2020-09-14').property('repoId', 2)
g.addV('repo').property(id, 'r1').property('repoName', 'repo1').property('assetAge', 10).property('repoId', 1)
g.addV('repo').property(id, 'r2').property('repoName', 'repo2').property('assetAge', 9).property('repoId', 2)
g.V('a1').addE('has').to(g.V('r1'))
g.V('a2').addE('has').to(g.V('r1'))
g.V('a3').addE('has').to(g.V('r1'))
g.V('a4').addE('has').to(g.V('r2'))
g.V('a5').addE('has').to(g.V('r2'))

I would like to write down a query in Gremlin that does the same thing as the bellow SQL query:

SELCET *
FROM asset a
JOIN repo r ON a.repoId = r.repoId
WHERE a.scanDate >= CURDATE() - INTERVAL (r.assetAge + 1) DAY

I have so far tried the bellow code in python:

from datetime import datetime, timedelta
from gremlin_python.process.traversal import gte

d = datetime.today() - timedelta(days=10) # here I have hard coded the days
traversal = g.V().hasLabel("asset").has("scanDate", gte(d))
traversal.valueMap().toList()

But I do not know how I can pass the repo.assetAge from the mapped repo vertices to the days parameter in timedelta(). Any help is really appreciated. Thanks.

0

There are 0 best solutions below