Merge lp:~cjohnston/qa-dashboard/sru-attention into lp:qa-dashboard

Proposed by Chris Johnston
Status: Merged
Approved by: Joe Talbott
Approved revision: 285
Merged at revision: 295
Proposed branch: lp:~cjohnston/qa-dashboard/sru-attention
Merge into: lp:qa-dashboard
Diff against target: 269 lines (+105/-81)
1 file modified
sru/management/commands/jenkins_pull_sru.py (+105/-81)
To merge this branch: bzr merge lp:~cjohnston/qa-dashboard/sru-attention
Reviewer Review Type Date Requested Status
Joe Talbott Approve
Review via email: mp+153041@code.launchpad.net

Commit message

Fixes the pulling bugs in SRU

To post a comment you must log in.
282. By Chris Johnston

remove code

Revision history for this message
Andy Doan (doanac) wrote :

On 03/12/2013 08:40 PM, Chris Johnston wrote:
> + bug = None
> + try:
> + bug = Bug.objects.get(bug_no=bug_no)
> + except Bug.DoesNotExist:
> + bug = Bug.objects.create(bug_no=bug_no, status='unknown')
> + if bug not in list(sruresult.bugs.all()):
> + sruresult.bugs.add(bug)
> + sruresult.save()

I noticed that pattern used a bit in this module. Is there a reason you
aren't using "get_or_create"[1]? I think that method is more fool-proof
and race-condition safe.

1:
<https://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create>

283. By Chris Johnston

Changes from feedback

284. By Chris Johnston

More sru work

285. By Chris Johnston

Switches all instances of get to get_or_create

Revision history for this message
Andy Doan (doanac) wrote :

+1

Revision history for this message
Joe Talbott (joetalbott) wrote :

Looks good to me.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'sru/management/commands/jenkins_pull_sru.py'
2--- sru/management/commands/jenkins_pull_sru.py 2013-03-21 18:59:36 +0000
3+++ sru/management/commands/jenkins_pull_sru.py 2013-03-23 16:02:22 +0000
4@@ -13,27 +13,22 @@
5 # You should have received a copy of the GNU Affero General Public License
6 # along with this program. If not, see <http://www.gnu.org/licenses/>.
7
8-import re
9 import logging
10 from datetime import datetime, timedelta
11 from optparse import make_option
12-from django.core.management.base import BaseCommand, CommandError
13+from django.core.management.base import BaseCommand
14
15 from common.management import (
16- jenkins_get,
17- skip_build,
18- BUILD_COUNT_MAX,
19- JOB_COUNT_MAX,
20- DEBUG_LIMITS,
21- )
22+ jenkins_get,
23+ skip_build,
24+ BUILD_COUNT_MAX,
25+ JOB_COUNT_MAX,
26+ DEBUG_LIMITS,
27+)
28
29 from common.utils import regexes
30-
31-from sru.models import (
32- Kernel,
33- SRUResult,
34- SRUResultLog,
35- )
36+from common.models import Bug
37+from sru.models import Kernel
38
39
40 def _get_kernel_version(description, default="Unknown"):
41@@ -54,15 +49,35 @@
42
43 return version
44
45+
46+def _parse_bugs(lp_bugs, result):
47+ """
48+ Add bugs to the sruresult.
49+ """
50+ sruresult = result
51+ status = 'unknown'
52+ for bug_no in lp_bugs:
53+ bug = None
54+ bug, new_bug = Bug.objects.get_or_create(
55+ bug_no=bug_no,
56+ defaults={'status': status}
57+ )
58+ if bug not in list(sruresult.bugs.all()):
59+ sruresult.bugs.add(bug)
60+ sruresult.save()
61+
62+
63 class Command(BaseCommand):
64 option_list = BaseCommand.option_list + (
65- make_option('-I', '--initial-run',
66- action='store_true',
67- dest='initial_run',
68- default=False,
69- help='Initial run processes all data (takes longer)'),
70- )
71-
72+ make_option(
73+ '-I', '--initial-run',
74+ action='store_true',
75+ dest='initial_run',
76+ default=False,
77+ help='Initial run processes all data (takes longer)'
78+ ),
79+ )
80+
81 help = 'Pull data from jenkins'
82
83 def handle(self, *args, **options):
84@@ -77,7 +92,7 @@
85
86 jobs = jobs_top['jobs']
87
88- job_count = 0 # XXX: DEBUG
89+ job_count = 0 # XXX: DEBUG
90 for job in jobs:
91 name = job['name']
92 m = regexes['kernel_sru'].match(name)
93@@ -92,12 +107,11 @@
94
95 variant, arch, gpu = m.group(5, 6, 7)
96
97-
98 # XXX: debugging limit
99 job_count += 1
100 if DEBUG_LIMITS and job_count > JOB_COUNT_MAX:
101 break
102-
103+
104 logging.info("job: {}\n".format(name))
105 logging.info("\tjob_type: {}\n".format(job_type))
106 logging.info("\trelease: {}\n".format(release))
107@@ -112,18 +126,24 @@
108 # ignore jobs that haven't been run yet.
109 if job_info['lastBuild'] is None:
110 continue
111- last_build = datetime.fromtimestamp(job_info['lastBuild']['timestamp']/1000)
112+ last_build = datetime.fromtimestamp(
113+ job_info['lastBuild']['timestamp']/1000
114+ )
115 # for update runs only check jobs that have built in the
116 # last week.
117- if not initial_run and datetime.now() - last_build > timedelta(days=7):
118+ if not initial_run and datetime.now() - last_build > timedelta(
119+ days=7
120+ ):
121 continue
122
123- build_count = 0 # XXX: DEBUG
124+ build_count = 0 # XXX: DEBUG
125 for build in builds:
126 if skip_build(build):
127- logging.info("skipping build: {} {}".format(
128- job['name'], build['number']
129- ))
130+ logging.info(
131+ "skipping build: %s %s",
132+ job['name'],
133+ build['number']
134+ )
135 continue
136
137 # XXX: debugging limit
138@@ -132,76 +152,80 @@
139 break
140
141 build_number = build['number']
142- build_url = build['url']
143- #build_info = jenkins_get("%s/api/json" % build_url)
144- building = build['building']
145- build_desc = build['description']
146- build_date = datetime.fromtimestamp(build['timestamp']/1000)
147- artifacts = build['artifacts']
148- actions = build['actions']
149+ build_url = build['url']
150+ building = build['building']
151+ build_desc = build['description']
152+ build_date = datetime.fromtimestamp(
153+ build['timestamp']/1000
154+ )
155+ artifacts = build['artifacts']
156+ actions = build['actions']
157
158 if build_desc is None:
159 build_desc = ''
160
161- kernel_version = _get_kernel_version(build_desc,
162- "Unknown. Ran on {}".format(build_date.strftime("%Y-%m-%d %H:%M")))
163+ kernel_version = _get_kernel_version(
164+ build_desc,
165+ "Unknown. Ran on {}".format(
166+ build_date.strftime("%Y-%m-%d %H:%M")
167+ )
168+ )
169
170 # Skip this Jenkins build if it hasn't finished yet
171 if building:
172 continue
173
174- # XXX: find out what TZ jenkins uses and use correct one here
175+ # XXX: find out what TZ jenkins uses
176+ # and use correct one here
177 if not initial_run and datetime.now() - build_date > timedelta(days=7):
178 continue
179
180- lp_bugs = []
181+ lp_bugs = []
182
183- if build_desc != None:
184+ if build_desc is not None:
185 b = build_desc.split(',')
186 b = regexes['build_desc'].findall(build_desc)
187 for word in b:
188 word = word.strip()
189- if regexes['buildid'].match(word) and build_no is None:
190+ regex = regexes['buildid'].match(word)
191+ if regex and build_no is None:
192 # It's a build number
193 build_no = "{} ?".format(word)
194 elif regexes['lpbug'].match(word):
195 lp_bugs.append(word[4:])
196
197- try:
198- kernel = Kernel.objects.get(release=release,
199- variant=variant,
200- arch=arch,
201- gpu=gpu,
202- version=kernel_version,
203- job_type=job_type)
204- except Kernel.DoesNotExist:
205- kernel = Kernel.objects.create(release=release,
206- variant=variant,
207- arch=arch,
208- gpu=gpu,
209- version=kernel_version,
210- job_type=job_type)
211-
212- result = None
213- try:
214- result = kernel.sruresult_set.get(name=name, jenkins_build=build_number)
215- # break
216- except SRUResult.DoesNotExist:
217- fail_count, skip_count, pass_count, total_count = (0, 0, 0, 0)
218- for action in actions:
219- try:
220- fail_count, skip_count, total_count = (action['failCount'], action['skipCount'], action['totalCount'])
221- pass_count = total_count - fail_count - skip_count
222- break
223- except KeyError:
224- continue
225-
226- result = kernel.sruresult_set.create(name=name,
227- jenkins_build=build_number,
228- ran_at=build_date,
229- jenkins_url=build_url,
230- fail_count=fail_count,
231- skip_count=skip_count,
232- pass_count=pass_count,
233- total_count=total_count
234- )
235+ kernel, new_kernel = Kernel.objects.get_or_create(
236+ release=release,
237+ variant=variant,
238+ arch=arch,
239+ gpu=gpu,
240+ version=kernel_version,
241+ job_type=job_type,
242+ )
243+
244+ fail_count = 0
245+ skip_count = 0
246+ pass_count = 0
247+ total_count = 0
248+ for action in actions:
249+ try:
250+ fail_count = action['failCount']
251+ skip_count = action['skipCount']
252+ total_count = action['totalCount']
253+ pass_count = total_count - fail_count - skip_count
254+ break
255+ except KeyError:
256+ continue
257+ result, new_result = kernel.sruresult_set.get_or_create(
258+ name=name,
259+ jenkins_build=build_number,
260+ defaults={
261+ 'ran_at': build_date,
262+ 'jenkins_url': build_url,
263+ 'fail_count': fail_count,
264+ 'skip_count': skip_count,
265+ 'pass_count': pass_count,
266+ 'total_count': total_count,
267+ }
268+ )
269+ _parse_bugs(lp_bugs, result)

Subscribers

People subscribed via source and target branches