Merge lp:~qzhang/lava-scheduler/simplify-sql-query into lp:lava-scheduler

Proposed by Spring Zhang
Status: Merged
Merged at revision: 182
Proposed branch: lp:~qzhang/lava-scheduler/simplify-sql-query
Merge into: lp:lava-scheduler
Diff against target: 72 lines (+25/-11)
1 file modified
lava_scheduler_app/views.py (+25/-11)
To merge this branch: bzr merge lp:~qzhang/lava-scheduler/simplify-sql-query
Reviewer Review Type Date Requested Status
Michael Hudson-Doyle (community) Approve
Andy Doan (community) Approve
Review via email: mp+110450@code.launchpad.net

Description of the change

1. Change health job pass and total number statistics method.
2. Simplify the SQL query in DeviceTypeTable by mwhudson's code

To post a comment you must log in.
Revision history for this message
Andy Doan (doanac) :
review: Approve
Revision history for this message
Michael Hudson-Doyle (mwhudson) wrote :

Thanks, I'll merge this now. Sorry for not noticing the branch before making a release earlier...

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lava_scheduler_app/views.py'
2--- lava_scheduler_app/views.py 2012-06-14 05:28:44 +0000
3+++ lava_scheduler_app/views.py 2012-06-15 03:35:24 +0000
4@@ -25,6 +25,7 @@
5 from django.template import defaultfilters as filters
6 from django.utils.html import escape
7 from django.utils.safestring import mark_safe
8+from django.db import models
9
10 from django_tables2 import Attrs, Column
11
12@@ -166,7 +167,8 @@
13
14 def health_jobs_in_hr(hr=-24):
15 return TestJob.objects.filter(health_check=True,
16- start_time__gte=(datetime.datetime.now() + relativedelta(hours=hr)))
17+ start_time__gte=(datetime.datetime.now()
18+ + relativedelta(hours=hr))).exclude(status__in=[TestJob.SUBMITTED, TestJob.RUNNING])
19
20 def _online_total():
21 ''' returns a tuple of (num_online, num_not_retired) '''
22@@ -213,20 +215,31 @@
23 raise PermissionDenied()
24 return job
25
26+class SumIfSQL(models.sql.aggregates.Aggregate):
27+ is_ordinal = True
28+ sql_function = 'SUM'
29+ sql_template = 'SUM((%(condition)s)::int)'
30+
31+class SumIf(models.Aggregate):
32+ name = 'SumIf'
33+ def add_to_query(self, query, alias, col, source, is_summary):
34+ aggregate = SumIfSQL(
35+ col, source=source, is_summary=is_summary, **self.extra)
36+ query.aggregates[alias] = aggregate
37+
38 class DeviceTypeTable(DataTablesTable):
39
40 def get_queryset(self):
41- return DeviceType.objects.all()
42+ return DeviceType.objects.all().annotate(
43+ idle=SumIf('device', condition='status=%s' % Device.IDLE),
44+ offline=SumIf('device', condition='status in (%s,%s)' % (
45+ Device.OFFLINE, Device.OFFLINING)),
46+ busy=SumIf('device', condition='status=%s' % Device.RUNNING),
47+ ).order_by('name')
48
49 def render_status(self, record):
50- idle_num = Device.objects.filter(device_type=record.name,
51- status=Device.IDLE).count()
52- offline_num = Device.objects.filter(device_type=record.name,
53- status__in=[Device.OFFLINE, Device.OFFLINING]).count()
54- running_num = Device.objects.filter(device_type=record.name,
55- status=Device.RUNNING).count()
56- return "%s idle, %s offline, %s busy" % (idle_num, offline_num,
57- running_num)
58+ return "%s idle, %s offline, %s busy" % (record.idle, record.offline,
59+ record.busy)
60
61 name = IDLinkColumn("name")
62 status = Column()
63@@ -255,7 +268,8 @@
64 device_type = self.params[0]
65 num = health_jobs_in_hr(record).filter(
66 actual_device__in=Device.objects.filter(
67- device_type=device_type), status=TestJob.INCOMPLETE).count()
68+ device_type=device_type), status__in=[TestJob.INCOMPLETE,
69+ TestJob.CANCELED, TestJob.CANCELING]).count()
70 return num
71
72 name = Column()

Subscribers

People subscribed via source and target branches