Skip to content

Commit

Permalink
Replace the fixed_packages_length annotation by a generated field #95
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Aug 23, 2024
1 parent 1deee0e commit e64fbf9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions component_catalog/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ class VulnerabilityFilterSet(DataspacedFilterSet):
fields=[
"vulnerability_id",
"affected_packages_count",
"fixed_packages_length",
"created_date",
"last_modified_date",
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.6 on 2024-08-23 10:09

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('component_catalog', '0006_vulnerability_model_and_missing_indexes'),
]

operations = [
migrations.AddField(
model_name='vulnerability',
name='fixed_packages_length',
field=models.GeneratedField(db_persist=True, expression=models.Func(models.F('fixed_packages'), function='jsonb_array_length'), output_field=models.IntegerField()),
),
]
8 changes: 8 additions & 0 deletions component_catalog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2603,6 +2603,14 @@ class Vulnerability(HistoryDateFieldsMixin, DataspacedModel):
blank=True,
help_text=_("A list of packages that are not affected by this vulnerability."),
)
fixed_packages_length = models.GeneratedField(
expression=models.Func(
models.F('fixed_packages'),
function='jsonb_array_length'
),
output_field=models.IntegerField(),
db_persist=True,
)

class Meta:
verbose_name_plural = "Vulnerabilities"
Expand Down
8 changes: 1 addition & 7 deletions component_catalog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2498,26 +2498,20 @@ class VulnerabilityListView(
)

def get_queryset(self):
from django.db.models import Func
from django.db.models import IntegerField

return (
super()
.get_queryset()
.only(
"uuid",
"vulnerability_id",
"aliases",
"fixed_packages_length",
"created_date",
"last_modified_date",
"dataspace",
)
.annotate(
affected_packages_count=Count("affected_packages"),
# TODO: Consider computing this on save()
fixed_packages_length=Func(
"fixed_packages", function="jsonb_array_length", output_field=IntegerField()
),
)
.order_by(
"-last_modified_date",
Expand Down

0 comments on commit e64fbf9

Please sign in to comment.