forked from Mirantis/launchpad-reports-summary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncdb.py
290 lines (242 loc) · 8.3 KB
/
syncdb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import time
from Queue import Empty
from datetime import date
from itertools import ifilter
from multiprocessing import Process
from multiprocessing import Queue
from multiprocessing import Event
import pytz
from datetime import datetime
from dateutil import relativedelta, parser
from launchpad_reporting.launchpad import LaunchpadClient
from launchpad_reporting.db import db
NUM_PROCESSES = 5
PROJECTS_LIST = [
"fuel",
"mos",
"murano",
"mistral",
"sahara",
"ceilometer"
]
SUBPROJECTS_LIST = [
"murano",
"sahara",
"nova",
"neutron",
"keystone",
"heat",
"glance",
"horizon",
"ceilometer",
"oslo",
"cinder"
]
class Bug(object):
def __init__(self, dictionary):
for key, value in dictionary.iteritems():
if 'date' in key and value:
dt = datetime.strptime(
value, '%Y-%m-%dT%H:%M:%S.%f+00:00').replace(tzinfo=pytz.UTC)
self.__setattr__(key, dt)
else:
self.__setattr__(key, value)
def younger_than(bug_time, **kwargs):
if (bug_time == None):
return True
bug_time = parser.parse(bug_time.ctime())
threshold = date.today() - relativedelta.relativedelta(**kwargs)
threshold = parser.parse(threshold.ctime())
return bug_time > threshold
def process_date(bug_date):
if bug_date == None:
return None
else:
return bug_date.replace(tzinfo=pytz.UTC)
def serialize_bug(bug, task=None):
print("Loading bug: {0}".format(bug.web_link))
bug_dates = {
'date_assigned': process_date(bug.date_assigned),
'date_closed': process_date(bug.date_closed),
'date_confirmed': process_date(bug.date_confirmed),
'date_created': process_date(bug.date_created),
'date_fix_committed': process_date(bug.date_fix_committed),
'date_fix_released': process_date(bug.date_fix_released),
'date_in_progress': process_date(bug.date_in_progress),
'date_incomplete': process_date(bug.date_incomplete),
'date_left_closed': process_date(bug.date_left_closed),
'date_left_new': process_date(bug.date_left_new),
'date_triaged': process_date(bug.date_triaged)
}
if task:
bug_item = task.bug
bug_assignee = str(bug.assignee_link).split("~")[1] \
if bug.assignee_link else None
bug_assignee_link = bug.assignee_link \
if bug.assignee_link else None
bug_milestone = str(bug.milestone_link).split("/")[-1]
bug_milestone_link = bug.milestone_link
bug_owner = str(bug.owner_link).split("~")[1]
bug_owner_link = bug.owner_link
else:
bug_item = bug.bug
bug_assignee = bug.assignee.name if bug.assignee else None
bug_assignee_link = bug.assignee.web_link \
if bug.assignee else None
bug_milestone = bug.milestone.name
bug_milestone_link = bug.milestone.web_link
bug_owner = bug.owner.name
bug_owner_link = bug.owner.web_link
return {
'id': bug_item.id,
'target_name': bug.bug_target_name,
'web_link': bug.web_link,
'milestone': (
bug_milestone if bug_milestone else None
),
'milestone_link': (
bug_milestone_link if bug_milestone else None,
),
'status': bug.status,
'tags': bug_item.tags,
'title': bug_item.title,
'importance': bug.importance,
'owner': bug_owner,
'owner_link': bug_owner_link,
'assignee': (
bug_assignee if bug_assignee else None
),
'assignee_link': (
bug_assignee_link if bug_assignee else bug_assignee
),
'date_assigned': bug_dates["date_assigned"],
'date_closed': bug_dates["date_closed"],
'date_confirmed': bug_dates["date_confirmed"],
'date_created': bug_dates["date_created"],
'date_fix_committed': bug_dates["date_fix_committed"],
'date_fix_released': bug_dates["date_fix_released"],
'date_in_progress': bug_dates["date_in_progress"],
'date_incomplete': bug_dates["date_incomplete"],
'date_left_closed': bug_dates["date_left_closed"],
'date_left_new': bug_dates["date_left_new"],
'date_triaged': bug_dates["date_triaged"],
'created less than week': younger_than(bug_dates["date_created"], weeks=1),
'created less than month': younger_than(bug_dates["date_created"], months=1),
'fixed less than week': younger_than(bug_dates["date_fix_committed"], weeks=1),
'fixed less than month': younger_than(bug_dates['date_fix_committed'], months=1),
}
def load_project_bugs(project_name, queue, stop_event):
launchpad = LaunchpadClient()
project = launchpad._get_project(project_name)
milestone_series = {}
for m in project.active_milestones:
milestone_series[str(m)] = str(m.series_target)
counter = 0
for bug in launchpad.get_all_bugs(project):
db.bugs[
str(bug.bug_target_name).split('/')[0]
].remove({'id': bug.bug.id})
bug_milestone = str(bug.milestone)
rts = bug.related_tasks.entries
if rts:
rts_milestones = []
for rt in rts:
rts_milestones.append(rt["target_link"])
if milestone_series[bug_milestone] not in rts_milestones:
queue.put(serialize_bug(bug))
for rt in rts:
counter += 1
rt = Bug(rt)
queue.put(serialize_bug(rt, task=bug))
else:
counter += 1
queue.put(serialize_bug(bug))
print(
"No more bugs for project '{0}' ({1} processed)".format(
project_name,
counter
)
)
stop_event.set()
def process_bugs(queue, stop_events):
while True:
try:
bug = queue.get_nowait()
db.bugs[
str(bug['target_name']).split('/')[0]
].update({"$and": [
{'id': bug['id']},
{'milestone': bug['milestone']}]}, bug, upsert=True)
except Empty:
if all([e.is_set() for e in stop_events]):
break
time.sleep(0.1)
if __name__ == "__main__":
launchpad = LaunchpadClient()
milestones = db.bugs.milestones
db.bugs.drop_collection(milestones)
projects = db.bugs.projects
subprojects = db.bugs.subprojects
mos = launchpad.get_project("mos")
fuel = launchpad.get_project("fuel")
milestones_list = launchpad.common_milestone(
mos.active_milestones,
fuel.active_milestones
)
print("Creating milestones...")
milestones.insert({"Milestone": milestones_list})
print("Creating projects...")
collection_names = db.bugs.collection_names()
projects.update(
{
"Project": projects.find_one()["Project"]
if "Project" in collection_names else ""
},
{"Project": PROJECTS_LIST},
upsert=True
)
print("Creating subprojects...")
subprojects.update(
{
"Subproject": subprojects.find_one()["Subproject"]
if "Subproject" in collection_names else ""
},
{"Subproject": SUBPROJECTS_LIST},
upsert=True
)
project_names = projects.find_one()["Project"]
print("Creating collections...")
map(
lambda pr: db.bugs.create_collection(pr),
ifilter(
lambda pr: pr not in collection_names,
project_names
)
)
queue = Queue()
stop_events = [Event() for _ in project_names]
loaders = [
Process(
target=load_project_bugs,
args=(pname, queue, stop_events[i])
) for i, pname in enumerate(project_names)
]
processes = map(
lambda num: Process(
target=process_bugs,
args=(queue, stop_events)
),
xrange(NUM_PROCESSES)
)
print("Spawning loader processes...")
map(lambda p: p.start(), loaders)
print("Spawning processes...")
map(lambda p: p.start(), processes)
map(lambda p: p.join(), loaders)
map(lambda p: p.join(), processes)
db.bugs.drop_collection("update_date")
db.bugs.create_collection("update_date")
db.bugs.update_date.insert({"Update_date": time.time()})