Skip to content

Commit

Permalink
hub: replace ResultGroup.defects_count with direct queries
Browse files Browse the repository at this point in the history
... to fix the following Traceback:
```
Scan failed due to: Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.DataError: smallint out of range

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/covscanhub/scan/xmlrpc_helper.py", line 35, in finish_scan
    process_scan(sb)
  File "/usr/lib/python3.6/site-packages/covscanhub/waiving/results_loader.py", line 219, in process_scan
    rl.process()
  File "/usr/lib/python3.6/site-packages/covscanhub/waiving/results_loader.py", line 198, in process
    self.store_defects(self.all.get_defects(), DEFECT_STATES['NEW'])
  File "/usr/lib/python3.6/site-packages/covscanhub/waiving/results_loader.py", line 174, in store_defects
    rg.save()
  File "/usr/lib/python3.6/site-packages/django/db/models/base.py", line 744, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/lib/python3.6/site-packages/django/db/models/base.py", line 782, in save_base
    force_update, using, update_fields,
  File "/usr/lib/python3.6/site-packages/django/db/models/base.py", line 854, in _save_table
    forced_update)
  File "/usr/lib/python3.6/site-packages/django/db/models/base.py", line 903, in _do_update
    return filtered._update(values) > 0
  File "/usr/lib/python3.6/site-packages/django/db/models/query.py", line 760, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "/usr/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1471, in execute_sql
    cursor = super().execute_sql(result_type)
  File "/usr/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1142, in execute_sql
    cursor.execute(sql, params)
  File "/usr/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File "/usr/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/usr/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/usr/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.DataError: smallint out of range
```

Resolves: https://issues.redhat.com/browse/OSH-242
  • Loading branch information
lzaoral committed Jan 15, 2024
1 parent fd5b374 commit 2da39a2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.2.20 on 2024-01-09 10:39

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('waiving', '0007_remove_checker_severity'),
]

operations = [
migrations.RemoveField(
model_name='resultgroup',
name='defects_count',
),
]
12 changes: 5 additions & 7 deletions osh/hub/waiving/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ class Meta:
def get_defects_count(self, defect_type):
rgs = ResultGroup.objects.filter(result=self,
defect_type=defect_type)
count = 0
for rg in rgs:
count += rg.defects_count
return count
return Defect.objects.filter(result_group__in=rgs).count()

def new_defects_count(self):
return self.get_defects_count(DEFECT_STATES['NEW'])
Expand Down Expand Up @@ -311,9 +308,6 @@ class ResultGroup(models.Model):
default=DEFECT_STATES["UNKNOWN"],
choices=DEFECT_STATES.get_mapping(),
help_text="Type of defects that are associated with this group.")
defects_count = models.PositiveSmallIntegerField(
default=0, blank=True, null=True, verbose_name="Number of defects \
associated with this group.")

objects = ResultGroupManager()

Expand Down Expand Up @@ -376,6 +370,10 @@ def is_marked_as_bug(self):
else:
return False

@property
def defects_count(self):
return Defect.objects.filter(result_group=self).count()

def get_state_to_display(self):
"""
return state for CSS class
Expand Down
1 change: 0 additions & 1 deletion osh/hub/waiving/results_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ def store_defects(self, defects, defect_state):
elif defect_state == DEFECT_STATES['FIXED']:
rg.state = RESULT_GROUP_STATES['INFO']

rg.defects_count += 1
rg.save()

d.checker = checker
Expand Down
3 changes: 1 addition & 2 deletions osh/hub/waiving/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import logging

import pycsdiff
from django.db.models import Sum

from .models import (DEFECT_STATES, RESULT_GROUP_STATES, Defect, ResultGroup,
Waiver, WaivingLog)
Expand Down Expand Up @@ -156,7 +155,7 @@ def get_scans_new_defects_count(scan_id):
"""Return number of newly introduced bugs for particular scan"""
rgs = ResultGroup.objects.filter(result__scanbinding__scan__id=scan_id,
defect_type=DEFECT_STATES['NEW'])
return rgs.aggregate(sum=Sum("defects_count"))['sum'] or 0
return Defect.objects.filter(result_group__in=rgs).count()


def get_waivers_for_rg(rg):
Expand Down

0 comments on commit 2da39a2

Please sign in to comment.