Skip to content

Commit

Permalink
Merge pull request #1049 from apolkosnik/master
Browse files Browse the repository at this point in the history
Replacing len() on Querysets with .count() to make the DB do the heavy lifting
  • Loading branch information
mgoffin committed Jun 12, 2019
2 parents ec1c6b2 + 4a632bc commit 7b3a3c5
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion crits/certificates/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def handle_cert_file(filename, data, source_name, user=None,
cert.reload()

# run certificate triage
if len(AnalysisResult.objects(object_id=str(cert.id))) < 1 and data:
if AnalysisResult.objects(object_id=str(cert.id)).count() < 1 and data:
run_triage(cert, user)

# update relationship if a related top-level object is supplied
Expand Down
3 changes: 2 additions & 1 deletion crits/core/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ def does_source_exist(source, active=False):
query = {'name': source}
if active:
query['active'] = 'on'
if len(SourceAccess.objects(__raw__=query)) > 0:
if SourceAccess.objects(__raw__=query).count() > 0:
return True
else:
return False
Expand Down Expand Up @@ -982,6 +982,7 @@ def alter_bucket_list(obj, buckets, val):
# We are using mongo_connector here because mongoengine does not have
# support for a setOnInsert option. If mongoengine were to gain support
# for this we should switch to using it instead of pymongo here.
###TODO: SetOnInsert is supported since mongoengine 0.8.0 (see commits #308/#309)
buckets_col = mongo_connector(settings.COL_BUCKET_LISTS)
for name in buckets:
buckets_col.update({'name': name},
Expand Down
6 changes: 3 additions & 3 deletions crits/core/management/commands/create_default_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def populate_actions(drop):
actions = ['Blocked Outbound At Firewall', 'Blocked Outbound At Desktop Firewall']
if drop:
Action.drop_collection()
if len(Action.objects()) < 1:
if Action.objects().count() < 1:
for action in actions:
ia = Action()
ia.name = action
Expand All @@ -90,7 +90,7 @@ def populate_raw_data_types(drop):
data_types = ['Text', 'JSON']
if drop:
RawDataType.drop_collection()
if len(RawDataType.objects()) < 1:
if RawDataType.objects().count() < 1:
for data_type in data_types:
dt = RawDataType()
dt.name = data_type
Expand All @@ -112,7 +112,7 @@ def populate_signature_types(drop):
data_types = ['Bro', 'Snort', 'Yara']
if drop:
SignatureType.drop_collection()
if len(SignatureType.objects()) < 1:
if SignatureType.objects().count() < 1:
for data_type in data_types:
dt = SignatureType()
dt.name = data_type
Expand Down
16 changes: 9 additions & 7 deletions crits/notifications/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,14 +511,16 @@ def get_user_notifications(username, count=False, newer_than=None):
n = None

if newer_than is None or newer_than == None:
n = Notification.objects(users=username).order_by('-created')
else:
n = Notification.objects(Q(users=username) & Q(created__gt=newer_than)).order_by('-created')

if count:
return len(n)
if count:
n = Notification.objects(users=username).order_by('-created').count()
else:
n = Notification.objects(users=username).order_by('-created')
else:
return n
if count:
n = Notification.objects(Q(users=username) & Q(created__gt=newer_than)).order_by('-created').count()
else:
n = Notification.objects(Q(users=username) & Q(created__gt=newer_than)).order_by('-created')
return n

__supported_notification_types__ = {
'Actor': 'name',
Expand Down
2 changes: 1 addition & 1 deletion crits/objects/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def delete_object_file(value):
query = {'objects.value': value}
for obj in obj_list:
obj_class = class_from_type(obj)
count += len(obj_class.objects(__raw__=query))
count += obj_class.objects(__raw__=query).count()
if count > 1:
break
else:
Expand Down
4 changes: 2 additions & 2 deletions crits/raw_data/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def get_raw_data_details(_id, user):
'value': raw_data.id
}

versions = len(RawData.objects(link_id=raw_data.link_id).only('id'))
versions = RawData.objects(link_id=raw_data.link_id).only('id').count()

#comments
comments = {'comments': raw_data.get_comments(),
Expand Down Expand Up @@ -437,7 +437,7 @@ def handle_raw_data_file(data, source_name, user=None,
analyst=user.username)


raw_data.version = len(RawData.objects(link_id=link_id)) + 1
raw_data.version = RawData.objects(link_id=link_id).count() + 1

if bucket_list:
raw_data.add_bucket_list(bucket_list, user)
Expand Down
2 changes: 1 addition & 1 deletion crits/samples/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ def handle_file(filename, data, source, source_method='', source_reference='',
sample.reload()

# run sample triage:
if len(AnalysisResult.objects(object_id=str(sample.id))) < 1:
if AnalysisResult.objects(object_id=str(sample.id)).count() < 1:
run_triage(sample, user)

if is_return_only_md5 == False:
Expand Down
4 changes: 2 additions & 2 deletions crits/signatures/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def get_signature_details(_id, user):
'value': signature.id
}

versions = len(Signature.objects(link_id=signature.link_id).only('id'))
versions = Signature.objects(link_id=signature.link_id).only('id').count()

#comments
comments = {'comments': signature.get_comments(),
Expand Down Expand Up @@ -397,7 +397,7 @@ def handle_signature_file(data, source_name, user=None,
if isinstance(s, EmbeddedSource):
signature.add_source(s, method=source_method, reference=source_reference, source_tlp=source_tlp)

signature.version = len(Signature.objects(link_id=link_id)) + 1
signature.version = Signature.objects(link_id=link_id).count() + 1

if link_id:
signature.link_id = link_id
Expand Down

0 comments on commit 7b3a3c5

Please sign in to comment.