Skip to content

Commit c1d6375

Browse files
committed
Defer unused fields to optimize pipeline queries
Signed-off-by: Keshav Priyadarshi <[email protected]>
1 parent 5c4e7c1 commit c1d6375

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

vulnerabilities/models.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,7 +1947,20 @@ def run_succeeded(self):
19471947
def run_failed(self):
19481948
"""Return True if the execution failed."""
19491949
fail_exitcode = self.run_exitcode and self.run_exitcode > 0
1950-
return fail_exitcode or self.job_status == "failed"
1950+
1951+
if not fail_exitcode:
1952+
job = self.job
1953+
if job.is_failed:
1954+
# Job was killed externally.
1955+
end_date = job.ended_at.replace(tzinfo=datetime.timezone.utc)
1956+
self.set_run_ended(
1957+
exitcode=1,
1958+
output=f"Killed from outside, exc_info={job.latest_result().exc_string}",
1959+
end_date=end_date,
1960+
)
1961+
return True
1962+
1963+
return fail_exitcode
19511964

19521965
@property
19531966
def run_stopped(self):
@@ -2001,11 +2014,11 @@ def set_run_started(self):
20012014
self.run_start_date = timezone.now()
20022015
self.save(update_fields=["run_start_date"])
20032016

2004-
def set_run_ended(self, exitcode, output=""):
2017+
def set_run_ended(self, exitcode, output="", end_date=None):
20052018
"""Set the run-related fields after the run execution."""
20062019
self.run_exitcode = exitcode
20072020
self.run_output = output
2008-
self.run_end_date = timezone.now()
2021+
self.run_end_date = end_date or timezone.now()
20092022
self.save(update_fields=["run_exitcode", "run_output", "run_end_date"])
20102023

20112024
def set_run_staled(self):
@@ -2030,9 +2043,12 @@ def stop_run(self):
20302043
return
20312044

20322045
if self.job_status == JobStatus.FAILED:
2046+
job = self.job
2047+
end_date = job.ended_at.replace(tzinfo=datetime.timezone.utc)
20332048
self.set_run_ended(
20342049
exitcode=1,
2035-
output=f"Killed from outside, latest_result={self.job.latest_result()}",
2050+
output=f"Killed from outside, exc_info={job.latest_result().exc_string}",
2051+
end_date=end_date,
20362052
)
20372053
return
20382054

@@ -2143,19 +2159,22 @@ def pipeline_class(self):
21432159
@property
21442160
def all_runs(self):
21452161
"""Return all the previous run instances for this pipeline."""
2146-
return self.pipelineruns.all().order_by("-created_date")
2162+
return self.pipelineruns.all()
21472163

21482164
@property
21492165
def latest_run(self):
2150-
return self.pipelineruns.latest("created_date") if self.pipelineruns.exists() else None
2166+
return self.pipelineruns.first() if self.pipelineruns.exists() else None
21512167

21522168
@property
21532169
def earliest_run(self):
21542170
return self.pipelineruns.earliest("created_date") if self.pipelineruns.exists() else None
21552171

21562172
@property
21572173
def latest_run_date(self):
2158-
return self.latest_run.run_start_date if self.latest_run else None
2174+
if not self.pipelineruns.exists():
2175+
return
2176+
latest_run = self.pipelineruns.values("created_date").first()
2177+
return latest_run["created_date"]
21592178

21602179
@property
21612180
def next_run_date(self):
@@ -2175,8 +2194,9 @@ def status(self):
21752194
if not self.is_active:
21762195
return
21772196

2178-
if self.latest_run:
2179-
return self.latest_run.status
2197+
if self.pipelineruns.exists():
2198+
latest = self.pipelineruns.only("pk").first()
2199+
return latest.status
21802200

21812201
def create_new_job(self, execute_now=False):
21822202
"""

vulnerabilities/views.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ class PipelineScheduleListView(ListView, FormMixin):
355355
model = PipelineSchedule
356356
context_object_name = "schedule_list"
357357
template_name = "pipeline_schedule_list.html"
358-
paginate_by = 30
358+
paginate_by = 20
359359
form_class = PipelineSchedulePackageForm
360360

361361
def get_queryset(self):
@@ -371,7 +371,7 @@ class PipelineRunListView(ListView):
371371
model = PipelineRun
372372
context_object_name = "run_list"
373373
template_name = "pipeline_run_list.html"
374-
paginate_by = 30
374+
paginate_by = 20
375375
slug_url_kwarg = "pipeline_id"
376376
slug_field = "pipeline_id"
377377

@@ -380,7 +380,7 @@ def get_queryset(self):
380380
PipelineSchedule,
381381
pipeline_id=self.kwargs["pipeline_id"],
382382
)
383-
return pipeline.all_runs
383+
return pipeline.pipelineruns.defer("log")
384384

385385
def get_context_data(self, **kwargs):
386386
context = super().get_context_data(**kwargs)

0 commit comments

Comments
 (0)