/
contributor.py
468 lines (443 loc) · 23.8 KB
/
contributor.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#SPDX-License-Identifier: MIT
"""
Metrics that provides data about contributors & their associated activity
"""
import datetime
import sqlalchemy as s
import pandas as pd
from augur.api.util import register_metric
import uuid
from ..server import engine
@register_metric()
def contributors(repo_group_id, repo_id=None, period='day', begin_date=None, end_date=None):
"""
Returns a timeseries of all the contributions to a project.
DataFrame has these columns:
date
commits
pull_requests
issues
commit_comments
pull_request_comments
issue_comments
total
:param repo_id: The repository's id
:param repo_group_id: The repository's group id
:param period: To set the periodicity to 'day', 'week', 'month' or 'year', defaults to 'day'
:param begin_date: Specifies the begin date, defaults to '1970-1-1 00:00:00'
:param end_date: Specifies the end date, defaults to datetime.now()
:return: DataFrame of persons/period
"""
# In this version, pull request, pr request comments,issue comments haven't be calculated
if not begin_date:
begin_date = '1970-1-1 00:00:01'
if not end_date:
end_date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
if repo_id:
contributorsSQL = s.sql.text("""
SELECT id::text AS user_id,
SUM(commits) AS commits,
SUM(issues) AS issues,
SUM(commit_comments) AS commit_comments,
SUM(issue_comments) AS issue_comments,
SUM(pull_requests) AS pull_requests,
SUM(pull_request_comments) AS pull_request_comments,
SUM(a.commits + a.issues + a.commit_comments + a.issue_comments + a.pull_requests +
a.pull_request_comments) AS total,
a.repo_id, repo.repo_name
FROM (
(SELECT reporter_id AS id,
0 AS commits,
COUNT(*) AS issues,
0 AS commit_comments,
0 AS issue_comments,
0 AS pull_requests,
0 AS pull_request_comments,
repo_id
FROM issues
WHERE repo_id = :repo_id
AND created_at BETWEEN :begin_date AND :end_date
AND reporter_id IS NOT NULL
AND pull_request IS NULL
GROUP BY reporter_id, repo_id)
UNION ALL
(SELECT cmt_ght_author_id AS id,
COUNT(*) AS commits,
0 AS issues,
0 AS commit_comments,
0 AS issue_comments,
0 AS pull_requests,
0 AS pull_request_comments,
repo_id
FROM commits
WHERE repo_id = :repo_id
AND cmt_ght_author_id IS NOT NULL
AND cmt_committer_date BETWEEN :begin_date AND :end_date
GROUP BY cmt_ght_author_id, repo_id)
UNION ALL
(SELECT cntrb_id AS id,
0 AS commits,
0 AS issues,
COUNT(*) AS commit_comments,
0 AS issue_comments,
0 AS pull_requests,
0 AS pull_request_comments,
commits.repo_id as repo_id
FROM commit_comment_ref,
commits,
message
WHERE commit_comment_ref.cmt_id = commit_comment_ref.cmt_id
AND message.msg_id = commit_comment_ref.msg_id
AND commits.repo_id = :repo_id
AND created_at BETWEEN :begin_date AND :end_date
GROUP BY id, commits.repo_id)
UNION ALL
(
SELECT message.cntrb_id AS id,
0 AS commits,
0 AS issues,
0 AS commit_comments,
count(*) AS issue_comments,
0 AS pull_requests,
0 AS pull_request_comments,
issues.repo_id as repo_id
FROM issues,
issue_message_ref,
message
WHERE issues.repo_id = :repo_id
AND reporter_id IS NOT NULL
AND issues.issue_id = issue_message_ref.issue_id
AND issue_message_ref.msg_id = message.msg_id
AND issues.pull_request IS NULL
AND created_at BETWEEN :begin_date AND :end_date
GROUP BY id, issues.repo_id
)
) a, repo
WHERE a.repo_id = repo.repo_id
GROUP BY a.id, a.repo_id, repo_name
ORDER BY total DESC
""")
with engine.connect() as conn:
results = pd.read_sql(contributorsSQL, conn, params={'repo_id': repo_id, 'period': period,
'begin_date': begin_date, 'end_date': end_date})
else:
contributorsSQL = s.sql.text("""
SELECT id::text AS user_id,
SUM(commits) AS commits,
SUM(issues) AS issues,
SUM(commit_comments) AS commit_comments,
SUM(issue_comments) AS issue_comments,
SUM(pull_requests) AS pull_requests,
SUM(pull_request_comments) AS pull_request_comments,
SUM(a.commits + a.issues + a.commit_comments + a.issue_comments + a.pull_requests +
a.pull_request_comments) AS total, a.repo_id, repo_name
FROM (
(SELECT reporter_id AS id,
repo_id,
0 AS commits,
COUNT(*) AS issues,
0 AS commit_comments,
0 AS issue_comments,
0 AS pull_requests,
0 AS pull_request_comments
FROM issues
WHERE repo_id in (SELECT repo_id FROM repo WHERE repo_group_id=:repo_group_id)
AND created_at BETWEEN :begin_date AND :end_date
AND reporter_id IS NOT NULL
AND pull_request IS NULL
GROUP BY reporter_id, issues.repo_id)
UNION ALL
(SELECT cmt_ght_author_id AS id,
repo_id,
COUNT(*) AS commits,
0 AS issues,
0 AS commit_comments,
0 AS issue_comments,
0 AS pull_requests,
0 AS pull_request_comments
FROM commits
WHERE repo_id in (SELECT repo_id FROM repo WHERE repo_group_id=:repo_group_id)
AND cmt_ght_author_id IS NOT NULL
AND cmt_committer_date BETWEEN :begin_date AND :end_date
GROUP BY cmt_ght_author_id, repo_id)
UNION ALL
(SELECT cntrb_id AS id,
commits.repo_id as repo_id,
0 AS commits,
0 AS issues,
COUNT(*) AS commit_comments,
0 AS issue_comments,
0 AS pull_requests,
0 AS pull_request_comments
FROM commit_comment_ref,
commits,
message
WHERE commit_comment_ref.cmt_id = commit_comment_ref.cmt_id
AND message.msg_id = commit_comment_ref.msg_id
AND commits.repo_id in (SELECT repo_id FROM repo WHERE repo_group_id=:repo_group_id)
AND created_at BETWEEN :begin_date AND :end_date
GROUP BY id, commits.repo_id)
UNION ALL
(
SELECT message.cntrb_id AS id,
issues.repo_id as repo_id,
0 AS commits,
0 AS issues,
0 AS commit_comments,
count(*) AS issue_comments,
0 AS pull_requests,
0 AS pull_request_comments
FROM issues,
issue_message_ref,
message
WHERE issues.repo_id in (SELECT repo_id FROM repo WHERE repo_group_id=:repo_group_id)
AND issues.reporter_id IS NOT NULL
AND issues.issue_id = issue_message_ref.issue_id
AND issue_message_ref.msg_id = message.msg_id
AND issues.pull_request IS NULL
AND created_at BETWEEN :begin_date AND :end_date
GROUP BY id, issues.repo_id
)
) a, repo
WHERE a.repo_id = repo.repo_id
GROUP BY a.id, a.repo_id, repo_name
ORDER BY total DESC
""")
with engine.connect() as conn:
results = pd.read_sql(contributorsSQL, conn, params={'repo_group_id': repo_group_id, 'period': period,
'begin_date': begin_date, 'end_date': end_date})
return results
@register_metric()
def contributors_new(repo_group_id, repo_id=None, period='day', begin_date=None, end_date=None):
"""
Returns a timeseries of new contributions to a project.
:param repo_id: The repository's id
:param repo_group_id: The repository's group id
:param period: To set the periodicity to 'day', 'week', 'month' or 'year', defaults to 'day'
:param begin_date: Specifies the begin date, defaults to '1970-1-1 00:00:00'
:param end_date: Specifies the end date, defaults to datetime.now()
:return: DataFrame of persons/period
"""
# In this version, pull request, pr request comments,issue comments haven't be calculated
if not begin_date:
begin_date = '1970-1-1 00:00:01'
if not end_date:
end_date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
if repo_id:
contributorsNewSQL = s.sql.text("""
SELECT date_trunc(:period, b.created_at::DATE) AS date, COUNT(id) AS new_contributors, repo.repo_id, repo_name
FROM (
SELECT id as id, MIN(created_at) AS created_at, a.repo_id
FROM (
(SELECT reporter_id AS id, MIN(created_at) AS created_at, repo_id
FROM issues
WHERE repo_id = :repo_id
AND created_at BETWEEN :begin_date AND :end_date
AND reporter_id IS NOT NULL
AND pull_request IS NULL
GROUP BY reporter_id, repo_id)
UNION ALL
(SELECT cmt_ght_author_id AS id,
MIN(TO_TIMESTAMP(cmt_author_date, 'YYYY-MM-DD')) AS created_at,
repo_id
FROM commits
WHERE repo_id = :repo_id
AND cmt_ght_author_id IS NOT NULL
AND TO_TIMESTAMP(cmt_author_date, 'YYYY-MM-DD') BETWEEN :begin_date AND :end_date
GROUP BY cmt_ght_author_id, repo_id)
UNION ALL
(SELECT cntrb_id as id, MIN(created_at) AS created_at, commits.repo_id
FROM commit_comment_ref,
commits,
message
where commits.cmt_id = commit_comment_ref.cmt_id
and commits.repo_id = :repo_id
and commit_comment_ref.msg_id = message.msg_id
group by id, commits.repo_id)
UNION ALL
(SELECT issue_events.cntrb_id AS id, MIN(issue_events.created_at) AS created_at, issues.repo_id
FROM issue_events, issues
WHERE issues.repo_id = :repo_id
AND issues.issue_id = issue_events.issue_id
AND issues.pull_request IS NULL
AND issue_events.created_at BETWEEN :begin_date AND :end_date
AND issue_events.cntrb_id IS NOT NULL
AND action = 'closed'
GROUP BY issue_events.cntrb_id, issues.repo_id)
) a
GROUP BY a.id, a.repo_id) b, repo
WHERE repo.repo_id = b.repo_id
GROUP BY date, repo.repo_id, repo_name
""")
with engine.connect() as conn:
results = pd.read_sql(contributorsNewSQL, conn, params={'repo_id': repo_id, 'period': period,
'begin_date': begin_date, 'end_date': end_date})
else:
contributorsNewSQL = s.sql.text("""
SELECT date_trunc(:period, b.created_at::DATE) AS date, COUNT(id) AS new_contributors, repo.repo_id, repo_name
FROM (
SELECT id as id, MIN(created_at) AS created_at, a.repo_id
FROM (
(SELECT reporter_id AS id, MIN(created_at) AS created_at, repo_id
FROM issues
WHERE repo_id in (SELECT repo_id FROM repo WHERE repo_group_id=:repo_group_id)
AND created_at BETWEEN :begin_date AND :end_date
AND reporter_id IS NOT NULL
AND pull_request IS NULL
GROUP BY reporter_id, repo_id)
UNION ALL
(SELECT cmt_ght_author_id AS id,
MIN(TO_TIMESTAMP(cmt_author_date, 'YYYY-MM-DD')) AS created_at,
repo_id
FROM commits
WHERE repo_id in (SELECT repo_id FROM repo WHERE repo_group_id=:repo_group_id)
AND cmt_ght_author_id IS NOT NULL
AND TO_TIMESTAMP(cmt_author_date, 'YYYY-MM-DD') BETWEEN :begin_date AND :end_date
GROUP BY cmt_ght_author_id, repo_id)
UNION ALL
(SELECT cntrb_id as id, MIN(created_at) AS created_at, commits.repo_id
FROM commit_comment_ref,
commits,
message
where commits.cmt_id = commit_comment_ref.cmt_id
and commits.repo_id in (SELECT repo_id FROM repo WHERE repo_group_id=:repo_group_id)
and commit_comment_ref.msg_id = message.msg_id
group by id, commits.repo_id)
UNION ALL
(SELECT issue_events.cntrb_id AS id, MIN(issue_events.created_at) AS created_at, issue_events.repo_id
FROM issue_events, issues
WHERE issues.repo_id in (SELECT repo_id FROM repo WHERE repo_group_id=:repo_group_id)
AND issues.issue_id = issue_events.issue_id
AND issues.pull_request IS NULL
AND issue_events.created_at BETWEEN :begin_date AND :end_date
AND issue_events.cntrb_id IS NOT NULL
AND action = 'closed'
GROUP BY issue_events.cntrb_id, issue_events.repo_id)
) a
GROUP BY a.id, a.repo_id) b, repo
WHERE repo.repo_id = b.repo_id
GROUP BY date, repo.repo_id, repo_name
""")
with engine.connect() as conn:
results = pd.read_sql(contributorsNewSQL, conn, params={'repo_group_id': repo_group_id, 'period': period,
'begin_date': begin_date, 'end_date': end_date})
return results
@register_metric()
def lines_changed_by_author(repo_group_id, repo_id=None):
"""
Returns number of lines changed per author per day
:param repo_url: the repository's URL
"""
if repo_id:
linesChangedByAuthorSQL = s.sql.text("""
SELECT cmt_author_email, date_trunc('week', cmt_author_date::date) as cmt_author_date, cmt_author_affiliation as affiliation,
SUM(cmt_added) as additions, SUM(cmt_removed) as deletions, SUM(cmt_whitespace) as whitespace, repo_name
FROM commits JOIN repo ON commits.repo_id = repo.repo_id
WHERE commits.repo_id = :repo_id
GROUP BY commits.repo_id, date_trunc('week', cmt_author_date::date), cmt_author_affiliation, cmt_author_email, repo_name
ORDER BY date_trunc('week', cmt_author_date::date) ASC;
""")
with engine.connect() as conn:
results = pd.read_sql(linesChangedByAuthorSQL, conn, params={"repo_id": repo_id})
return results
else:
linesChangedByAuthorSQL = s.sql.text("""
SELECT cmt_author_email, date_trunc('week', cmt_author_date::date) as cmt_author_date, cmt_author_affiliation as affiliation,
SUM(cmt_added) as additions, SUM(cmt_removed) as deletions, SUM(cmt_whitespace) as whitespace
FROM commits
WHERE repo_id in (SELECT repo_id FROM repo WHERE repo_group_id=:repo_group_id)
GROUP BY repo_id, date_trunc('week', cmt_author_date::date), cmt_author_affiliation, cmt_author_email
ORDER BY date_trunc('week', cmt_author_date::date) ASC;
""")
with engine.connect() as conn:
results = pd.read_sql(linesChangedByAuthorSQL, conn, params={"repo_group_id": repo_group_id})
return results
@register_metric()
def contributors_code_development(repo_group_id, repo_id=None, period='all', begin_date=None, end_date=None):
"""
Returns a timeseries of all the contributions to a project.
DataFrame has these columns:
date
commits
:param repo_id: The repository's id
:param repo_group_id: The repository's group id
----- :param period: To set the periodicity to 'all', day', 'week', 'month' or 'year', defaults to 'all'
----- :param begin_date: Specifies the begin date, defaults to '1970-1-1 00:00:00'
----- :param end_date: Specifies the end date, defaults to datetime.now()
:return: DataFrame of persons/period
"""
# In this version, pull request, pr request comments,issue comments haven't be calculated
if not begin_date:
begin_date = '1970-1-1 00:00:01'
if not end_date:
end_date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
if repo_id:
contributorsSQL = s.sql.text("""
SELECT
email AS email,
SUM(commits) AS commits,
SUM(lines_added) AS lines_added,
a.repo_id, repo.repo_name
FROM (
(
SELECT repo_id, email, SUM(patches)::int as commits, 0 as lines_added
FROM
(SELECT repo_id, email, patches
FROM dm_repo_annual
WHERE repo_id = :repo_id
ORDER BY patches DESC) a
GROUP BY email, a.repo_id
)
UNION ALL
(
SELECT repo_id, cmt_author_email as email, 0 as commits, SUM(cmt_added) as lines_added
-- cmt_author_affiliation as affiliation,
-- SUM(cmt_added) as additions, SUM(cmt_removed) as deletions, SUM(cmt_whitespace) as whitespace,
FROM commits
WHERE commits.repo_id = :repo_id
GROUP BY commits.repo_id, cmt_author_date, cmt_author_affiliation, cmt_author_email
ORDER BY cmt_author_date ASC
)
) a, repo
WHERE a.repo_id = repo.repo_id
GROUP BY a.email, a.repo_id, repo_name
""")
with engine.connect() as conn:
results = pd.read_sql(contributorsSQL, conn, params={'repo_id': repo_id, 'period': period,
'begin_date': begin_date, 'end_date': end_date})
else:
contributorsSQL = s.sql.text("""
SELECT
email AS email,
SUM(commits) AS commits,
SUM(lines_added) AS lines_added,
a.repo_id, repo.repo_name
FROM (
(
SELECT repo_id, email, SUM(patches)::INT AS commits, 0 AS lines_added
FROM
(SELECT dm_repo_annual.repo_id, email, patches
FROM dm_repo_annual JOIN repo ON repo.repo_id = dm_repo_annual.repo_id
WHERE repo_group_id = :repo_group_id
ORDER BY patches DESC) a
GROUP BY email, a.repo_id
)
UNION ALL
(
SELECT commits.repo_id, cmt_author_email AS email, 0 AS commits, SUM(cmt_added) AS lines_added
-- cmt_author_affiliation as affiliation,
-- SUM(cmt_added) as additions, SUM(cmt_removed) as deletions, SUM(cmt_whitespace) as whitespace,
FROM commits JOIN repo ON repo.repo_id = commits.repo_id
WHERE repo_group_id = :repo_group_id
GROUP BY commits.repo_id, cmt_author_date, cmt_author_affiliation, cmt_author_email
ORDER BY cmt_author_date ASC
)
) a, repo
WHERE a.repo_id = repo.repo_id
GROUP BY a.email, a.repo_id, repo_name
ORDER BY commits desc, email
""")
with engine.connect() as conn:
results = pd.read_sql(contributorsSQL, conn, params={'repo_group_id': repo_group_id, 'period': period,
'begin_date': begin_date, 'end_date': end_date})
return results