Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _latest_uid_filter(self, session, query):
# Create a sub query of latest uid (by updated) per (project,key)
subq = session.query(
Artifact.uid,
Artifact.project,
Artifact.key,
func.max(Artifact.updated),
).group_by(
Artifact.project,
Artifact.key.label("key"),
).subquery("max_key")
# Join curreny query with sub query on (project, key, uid)
return query.join(
subq,
and_(
Artifact.project == subq.c.project,
Artifact.key == subq.c.key,
Artifact.uid == subq.c.uid,
)
# Create a sub query of latest uid (by updated) per (project,key)
subq = session.query(
Artifact.uid,
Artifact.project,
Artifact.key,
func.max(Artifact.updated),
).group_by(
Artifact.project,
Artifact.key.label("key"),
).subquery("max_key")
# Join curreny query with sub query on (project, key, uid)
return query.join(
subq,
and_(
Artifact.project == subq.c.project,
Artifact.key == subq.c.key,
Artifact.uid == subq.c.uid,
)
def _find_artifacts(self, session, project, uid, labels, since, until):
labels = label_set(labels)
query = self._query(session, Artifact, project=project)
if uid != "*":
if uid == "latest":
query = self._latest_uid_filter(session, query)
else:
query = query.filter(Artifact.uid == uid)
query = self._add_labels_filter(session, query, Artifact, labels)
if since or until:
since = since or datetime.min
until = until or datetime.max
query = query.filter(and_(
Artifact.updated >= since,
Artifact.updated <= until
))
return query
def _latest_uid_filter(self, session, query):
# Create a sub query of latest uid (by updated) per (project,key)
subq = session.query(
Artifact.uid,
Artifact.project,
Artifact.key,
func.max(Artifact.updated),
).group_by(
Artifact.project,
Artifact.key.label("key"),
).subquery("max_key")
# Join curreny query with sub query on (project, key, uid)
return query.join(
subq,
and_(
Artifact.project == subq.c.project,
Artifact.key == subq.c.key,
Artifact.uid == subq.c.uid,
)
def store_artifact(
self, session, key, artifact, uid, iter=None, tag="", project=""):
project = project or config.default_project
self._create_project_if_not_exists(session, project)
artifact = artifact.copy()
updated = artifact.get("updated")
if not updated:
updated = artifact["updated"] = datetime.now(timezone.utc)
if iter:
key = "{}-{}".format(iter, key)
art = self._get_artifact(session, uid, project, key)
labels = artifact.get("labels", {})
if not art:
art = Artifact(
key=key,
uid=uid,
updated=updated,
project=project)
update_labels(art, labels)
art.struct = artifact
self._upsert(session, art)
if tag:
self.tag_objects(session, [art], project, tag)
def _get_artifact(self, session, uid, project, key):
try:
resp = self._query(
session, Artifact, uid=uid, project=project, key=key).one_or_none()
return resp
finally:
pass
def _find_artifacts(self, session, project, uid, labels, since, until):
labels = label_set(labels)
query = self._query(session, Artifact, project=project)
if uid != "*":
if uid == "latest":
query = self._latest_uid_filter(session, query)
else:
query = query.filter(Artifact.uid == uid)
query = self._add_labels_filter(session, query, Artifact, labels)
if since or until:
since = since or datetime.min
until = until or datetime.max
query = query.filter(and_(
Artifact.updated >= since,
Artifact.updated <= until
))
return query
def _find_artifacts(self, session, project, uid, labels, since, until):
labels = label_set(labels)
query = self._query(session, Artifact, project=project)
if uid != "*":
if uid == "latest":
query = self._latest_uid_filter(session, query)
else:
query = query.filter(Artifact.uid == uid)
query = self._add_labels_filter(session, query, Artifact, labels)
if since or until:
since = since or datetime.min
until = until or datetime.max
query = query.filter(and_(
Artifact.updated >= since,
Artifact.updated <= until
))