Skip to content

Commit

Permalink
add parameter to allow SROs to return non-latest relationships (#56)
Browse files Browse the repository at this point in the history
* add parameter to allow SROs to return non-latest relationships needed in #47

* only return 1 object in `$OBJECTS/<object_id>` #57
  • Loading branch information
fqrious authored Feb 4, 2025
1 parent 2e3b5c5 commit 976fbe1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
28 changes: 13 additions & 15 deletions dogesec_commons/objects/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def positive_int(integer_string, cutoff=None, default=1):
class ArangoDBHelper:
max_page_size = conf.MAXIMUM_PAGE_SIZE
page_size = conf.DEFAULT_PAGE_SIZE
SRO_OBJECTS_ONLY_LATEST = getattr(settings, 'SRO_OBJECTS_ONLY_LATEST', True)

@staticmethod
def get_like_literal(str: str):
Expand Down Expand Up @@ -405,7 +406,7 @@ def get_sdos(self):

if term := self.query.get('name'):
bind_vars['name'] = "%" + self.get_like_literal(term) + '%'
search_filters.append("LIKE(doc.name, @name)")
search_filters.append("doc.name LIKE @name")

if other_filters:
other_filters = "FILTER " + " AND ".join(other_filters)
Expand All @@ -430,7 +431,8 @@ def get_objects_by_id(self, id):
query = """
FOR doc in @@view
SEARCH doc.id == @id AND doc._is_latest == TRUE
LIMIT @offset, @count
LET _unused = [@offset, @count]
LIMIT 1
RETURN KEEP(doc, KEYS(doc, true))
"""
return self.execute_query(query, bind_vars=bind_vars)
Expand Down Expand Up @@ -458,27 +460,21 @@ def get_sros(self):
"@collection": self.collection,
}

other_filters = []
search_filters = ['doc._is_latest == TRUE']

if terms := self.query_as_array('source_ref_type'):
bind_vars['source_ref_type'] = terms
other_filters.append('SPLIT(doc.source_ref, "--")[0] IN @source_ref_type')
search_filters.append('doc._source_type IN @source_ref_type')

if terms := self.query_as_array('target_ref_type'):
bind_vars['target_ref_type'] = terms
other_filters.append('SPLIT(doc.target_ref, "--")[0] IN @target_ref_type')
search_filters.append('doc._target_type IN @target_ref_type')

if term := self.query.get('relationship_type'):
bind_vars['relationship_type'] = term
other_filters.append("CONTAINS(doc.relationship_type, @relationship_type)")
bind_vars['relationship_type'] = '%' + self.get_like_literal(term) + '%'
search_filters.append("doc.relationship_type LIKE @relationship_type")


if other_filters:
other_filters = "FILTER " + " AND ".join(other_filters)
else:
other_filters = ""

search_filters = ['doc._is_latest == TRUE']
if not self.query_as_bool('include_embedded_refs', True):
search_filters.append('doc._is_ref != TRUE')

Expand All @@ -490,10 +486,13 @@ def get_sros(self):
bind_vars['source_ref'] = term
search_filters.append('doc.source_ref == @source_ref')

if not self.SRO_OBJECTS_ONLY_LATEST:
search_filters[0] = '(doc._is_latest == TRUE OR doc._target_type IN @sco_types OR doc._source_type IN @sco_types)'
bind_vars['sco_types'] = list(SCO_TYPES)

query = f"""
FOR doc in @@collection
SEARCH doc.type == 'relationship' AND { ' AND '.join(search_filters) }
{other_filters}
{self.get_sort_stmt(SRO_SORT_FIELDS)}
LIMIT @offset, @count
Expand Down Expand Up @@ -521,7 +520,6 @@ def get_post_objects(self, post_id, feed_id):
LIMIT @offset, @count
RETURN KEEP(doc, KEYS(doc, true))
"""

return self.execute_query(query, bind_vars=bind_vars)


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "dogesec_commons"
version = "0.0.4-pre0"
version = "0.0.4-pre1"
authors = [
{ name="DOGESEC", email="[email protected]" },
]
Expand Down

0 comments on commit 976fbe1

Please sign in to comment.