OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import json | 5 import json |
6 | 6 |
7 | 7 |
8 def ratio_calculator(numerator, denominator): | 8 def ratio_calculator(numerator, denominator): |
9 """Computes the ratio of the counts in two lists | 9 """Computes the ratio of the counts in two lists |
10 | 10 |
(...skipping 21 matching lines...) Expand all Loading... |
32 def totaled_ratio_calculator(numerator, denominator): | 32 def totaled_ratio_calculator(numerator, denominator): |
33 """Computes the ratio of the counts in two lists | 33 """Computes the ratio of the counts in two lists |
34 | 34 |
35 Args: | 35 Args: |
36 numerator(int): a totaled count | 36 numerator(int): a totaled count |
37 denominator(int): a totaled count | 37 denominator(int): a totaled count |
38 | 38 |
39 Return: | 39 Return: |
40 ratio(float): a ratio rounded to three decimal places | 40 ratio(float): a ratio rounded to three decimal places |
41 """ | 41 """ |
42 if denominator != 0: | 42 if denominator != 0: |
43 ratio = round(float(numerator) / denominator, 3) | 43 ratio = round(float(numerator) / denominator, 3) |
44 else: | 44 else: |
45 raise ZeroDivisionError | 45 ratio = 0 |
46 return ratio | 46 return ratio |
47 | 47 |
48 | 48 |
49 # functions that return stats calculated as lists of lists by month | 49 # functions that return stats calculated as lists of lists by month |
50 def total_commits(cc): # pragma: no cover | 50 def total_commits(cc): # pragma: no cover |
51 """Counts all the git commits sorted by month and year | 51 """Counts all the git commits sorted by month and year |
52 | 52 |
53 Args: | 53 Args: |
54 cc(cursor) | 54 cc(cursor) |
55 | 55 |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 """ | 296 """ |
297 cc.execute("""SELECT COUNT(DISTINCT git_commit_hash) | 297 cc.execute("""SELECT COUNT(DISTINCT git_commit_hash) |
298 FROM commit_people | 298 FROM commit_people |
299 INNER JOIN git_commit | 299 INNER JOIN git_commit |
300 ON commit_people.git_commit_hash = git_commit.hash | 300 ON commit_people.git_commit_hash = git_commit.hash |
301 WHERE commit_people.type = 'tbr' AND %s""" % sql_time_specification) | 301 WHERE commit_people.type = 'tbr' AND %s""" % sql_time_specification) |
302 result = cc.fetchone() | 302 result = cc.fetchone() |
303 return int(result[0]) | 303 return int(result[0]) |
304 | 304 |
305 | 305 |
306 def totaled_tbr_no_lgtm(cc, sql_time_specification): # pragma: no cover | 306 def totaled_tbr_no_lgtm(cc, sql_time_specification): |
307 """Counts the number of commits with a TBR that have not been lgtm'ed | 307 """Counts the number of commits with a TBR that have not been lgtm'ed |
308 in a given timeframe | 308 in a given timeframe |
309 | 309 |
310 Args: | 310 Args: |
311 cc(cursor) | 311 cc(cursor) |
312 sql_time_specification(str): a sql command to limit the dates of the | 312 sql_time_specification(str): a sql command to limit the dates of the |
313 returned results | 313 returned results |
314 | 314 |
315 Return: | 315 Return: |
316 result(int): a count of all commits with a TBR and no lgtm | 316 count(int): a count of all commits with a TBR and no lgtm |
| 317 results(list): a list of lists with all tbr'ed commits with no lgtm in the |
| 318 format [rietveld_url, git_timestamp, git_subject, git_hash] |
317 """ | 319 """ |
318 cc.execute("""SELECT COUNT(*) | 320 cc.execute("""SELECT git_commit.review_url, git_commit.timestamp, |
| 321 git_commit.subject, git_commit.hash |
319 FROM review | 322 FROM review |
320 INNER JOIN git_commit | 323 INNER JOIN git_commit |
321 ON review.review_url = git_commit.review_url | 324 ON review.review_url = git_commit.review_url |
322 INNER JOIN commit_people | 325 INNER JOIN commit_people |
323 ON commit_people.git_commit_hash = git_commit.hash | 326 ON commit_people.git_commit_hash = git_commit.hash |
324 LEFT JOIN ( | 327 LEFT JOIN ( |
325 SELECT review_url, COUNT(*) AS c | 328 SELECT review_url, COUNT(*) AS c |
326 FROM review_people | 329 FROM review_people |
327 WHERE type = 'lgtm' | 330 WHERE type = 'lgtm' |
328 GROUP BY review_url) lgtm_count | 331 GROUP BY review_url) lgtm_count |
329 ON review.review_url = lgtm_count.review_url | 332 ON review.review_url = lgtm_count.review_url |
330 WHERE lgtm_count.c = 0 OR lgtm_count.c IS NULL | 333 WHERE lgtm_count.c = 0 OR lgtm_count.c IS NULL |
331 AND commit_people.type = 'tbr' AND %s""" % sql_time_specification) | 334 AND commit_people.type = 'tbr' AND %s""" % sql_time_specification) |
332 result = cc.fetchone() | 335 result = cc.fetchall() |
333 return int(result[0]) | 336 count = len(result) |
| 337 results = [[commit[0], commit[1].strftime("%Y-%m-%d %H:%M:%S"), |
| 338 commit[2], commit[3]] for commit in result] |
| 339 return count, results |
334 | 340 |
335 | 341 |
336 def totaled_blank_tbr(cc, sql_time_specification): # pragma: no cover | 342 def totaled_blank_tbr(cc, sql_time_specification): # pragma: no cover |
337 """Counts the number of occurences of TBR= with no reviewer listed in a | 343 """Counts the number of occurences of TBR= with no reviewer listed in a |
338 given timeframe | 344 given timeframe |
339 | 345 |
340 Args: | 346 Args: |
341 cc(cursor) | 347 cc(cursor) |
342 sql_time_specification(str): a sql command to limit the dates of the | 348 sql_time_specification(str): a sql command to limit the dates of the |
343 returned results | 349 returned results |
344 | 350 |
345 Return: | 351 Return: |
346 result(int): a count of all blank TBRs (TBR=) | 352 count(int): a count of all blank TBRs (TBR=) |
| 353 results(list): a list of lists with all tbr'ed commits with no lgtm in the |
| 354 format [rietveld_url, git_timestamp, git_subject, git_hash] |
347 """ | 355 """ |
348 cc.execute("""SELECT COUNT(*) | 356 cc.execute("""SELECT git_commit.review_url, git_commit.timestamp, |
| 357 git_commit.subject, git_commit.hash |
349 FROM commit_people | 358 FROM commit_people |
350 INNER JOIN git_commit | 359 INNER JOIN git_commit |
351 ON commit_people.git_commit_hash = git_commit.hash | 360 ON commit_people.git_commit_hash = git_commit.hash |
352 WHERE commit_people.people_email_address = 'NOBODY' | 361 WHERE commit_people.people_email_address = 'NOBODY' |
353 AND %s""" % sql_time_specification) | 362 AND %s""" % sql_time_specification) |
354 result = cc.fetchone() | 363 result = cc.fetchall() |
355 return int(result[0]) | 364 count = len(result) |
| 365 results = [[commit[0], commit[1].strftime("%Y-%m-%d %H:%M:%S"), |
| 366 commit[2], commit[3]] for commit in result] |
| 367 return count, results |
356 | 368 |
357 | 369 |
358 def totaled_no_review_url(cc, sql_time_specification): # pragma: no cover | 370 def totaled_no_review_url(cc, sql_time_specification): # pragma: no cover |
359 """Counts the number of commits with no review url in a given timeframe | 371 """Counts the number of commits with no review url in a given timeframe |
360 | 372 |
361 Args: | 373 Args: |
362 cc(cursor) | 374 cc(cursor) |
363 sql_time_specification(str): a sql command to limit the dates of the | 375 sql_time_specification(str): a sql command to limit the dates of the |
364 returned results | 376 returned results |
365 | 377 |
366 Return: | 378 Return: |
367 result(int): a count of all commits with no review_url | 379 count(int): a count of all commits with no review_url |
| 380 results(list): a list of lists with all tbr'ed commits with no lgtm in the |
| 381 format [rietveld_url, git_timestamp, git_subject, git_hash] |
368 """ | 382 """ |
369 cc.execute("""SELECT COUNT(*) | 383 cc.execute("""SELECT git_commit.review_url, git_commit.timestamp, |
| 384 git_commit.subject, git_commit.hash |
370 FROM git_commit | 385 FROM git_commit |
371 WHERE review_url IS NULL AND %s""" % sql_time_specification) | 386 WHERE review_url IS NULL AND %s""" % sql_time_specification) |
372 result = cc.fetchone() | 387 result = cc.fetchall() |
373 return int(result[0]) | 388 count = len(result) |
| 389 results = [[commit[0], commit[1].strftime("%Y-%m-%d %H:%M:%S"), |
| 390 commit[2], commit[3]] for commit in result] |
| 391 return count, results |
374 | 392 |
375 | 393 |
376 # TODO(keelerh): change the SQL query to avoid using temporary tables | 394 # TODO(keelerh): change the SQL query to avoid using temporary tables |
377 def tbr_to_total_people_ratio(cc, | 395 def tbr_to_total_people_ratio(cc, |
378 sql_time_specification=''): # pragma: no cover | 396 sql_time_specification=''): # pragma: no cover |
379 """Calculate TBR stats by author for the Antibody leaderboard and writes | 397 """Calculate TBR stats by author for the Antibody leaderboard and writes |
380 them to a json file | 398 them to a json file |
381 | 399 |
382 Args: | 400 Args: |
383 cc(cursor) | 401 cc(cursor) |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 | 501 |
484 def compute_stats_by_time(cc): # pragma: no cover | 502 def compute_stats_by_time(cc): # pragma: no cover |
485 """Computes the stats for the past 7 days, past 30 days, and all time | 503 """Computes the stats for the past 7 days, past 30 days, and all time |
486 | 504 |
487 Args: | 505 Args: |
488 cc(cursor) | 506 cc(cursor) |
489 | 507 |
490 Returns: | 508 Returns: |
491 output(list): three dictionaries containing stats for the past 7 days, 30 | 509 output(list): three dictionaries containing stats for the past 7 days, 30 |
492 days, and all time respectively, each including timeframe, | 510 days, and all time respectively, each including timeframe, |
493 suspicious_to_total_ratio, total_commits, tbr_no_lgtm, | 511 suspicious_to_total_ratio, a count of the number of commits |
494 no_review_url, blank_tbr | 512 for total_commits, tbr_no_lgtm, no_review_url, and blank_tbr, |
| 513 and a list of lists with the relevant commits for |
| 514 tbr_no_lgtm, no_review_url, and blank_tbr |
495 """ | 515 """ |
496 stats_7_days = {'timeframe': '7_days'} | 516 stats_7_days = {'timeframe': '7_days'} |
497 stats_30_days = {'timeframe': '30_days'} | 517 stats_30_days = {'timeframe': '30_days'} |
498 stats_all_time = {'timeframe': 'all_time'} | 518 stats_all_time = {'timeframe': 'all_time'} |
499 for d in [stats_7_days, stats_30_days, stats_all_time]: | 519 for d in [stats_7_days, stats_30_days, stats_all_time]: |
500 if d == stats_7_days: | 520 if d == stats_7_days: |
501 sql_insert = 'DATEDIFF(NOW(), git_commit.timestamp) <= 7' | 521 sql_insert = 'DATEDIFF(NOW(), git_commit.timestamp) <= 7' |
502 elif d == stats_30_days: | 522 elif d == stats_30_days: |
503 sql_insert = 'DATEDIFF(NOW(), git_commit.timestamp) <= 30' | 523 sql_insert = 'DATEDIFF(NOW(), git_commit.timestamp) <= 30' |
504 elif d == stats_all_time: | 524 elif d == stats_all_time: |
505 sql_insert = 'DATEDIFF(git_commit.timestamp, NOW()) < 0' | 525 sql_insert = 'DATEDIFF(git_commit.timestamp, NOW()) < 0' |
506 tot_commits = totaled_total_commits(cc, sql_insert) | 526 tot_commits = totaled_total_commits(cc, sql_insert) |
507 tot_suspicious = totaled_total_suspicious(cc, sql_insert) | 527 tot_suspicious = totaled_total_suspicious(cc, sql_insert) |
508 d['suspicious_to_total_ratio'] = totaled_ratio_calculator(tot_suspicious, | 528 d['suspicious_to_total_ratio'] = totaled_ratio_calculator(tot_suspicious, |
509 tot_commits) | 529 tot_commits) |
510 d['total_commits'] = tot_commits | 530 d['total_commits'] = tot_commits |
511 d['tbr_no_lgtm'] = totaled_tbr_no_lgtm(cc, sql_insert) | 531 count_tbr_no_lgtm, tbr_no_lgtm_commits = totaled_tbr_no_lgtm(cc, sql_insert) |
512 d['no_review_url'] = totaled_no_review_url(cc, sql_insert) | 532 d['tbr_no_lgtm'] = count_tbr_no_lgtm |
513 d['blank_tbr'] = totaled_blank_tbr(cc, sql_insert) | 533 d['tbr_no_lgtm_commits'] = tbr_no_lgtm_commits |
| 534 count_no_review_url, no_review_url_commits = totaled_no_review_url( |
| 535 cc, sql_insert) |
| 536 d['no_review_url'] = count_no_review_url |
| 537 d['no_review_url_commits'] = no_review_url_commits |
| 538 count_blank_tbr, blank_tbr_commits = totaled_blank_tbr(cc, sql_insert) |
| 539 d['blank_tbr'] = count_blank_tbr |
| 540 d['blank_tbr_commits'] = blank_tbr_commits |
514 output = [stats_7_days, stats_30_days, stats_all_time] | 541 output = [stats_7_days, stats_30_days, stats_all_time] |
515 return output | 542 return output |
516 | 543 |
517 | 544 |
518 def all_monthly_stats(cc, filename): # pragma: no cover | 545 def all_monthly_stats(cc, filename): # pragma: no cover |
519 """Write stats calculated over different time segments as json to file | 546 """Write stats calculated over different time segments as json to file |
520 | 547 |
521 Args: | 548 Args: |
522 cc(cursor) | 549 cc(cursor) |
523 filename(str): the json file to write to | 550 filename(str): the json file to write to |
(...skipping 25 matching lines...) Expand all Loading... |
549 """Writes stats for the past month Antibody leaderboard to a json file | 576 """Writes stats for the past month Antibody leaderboard to a json file |
550 | 577 |
551 Args: | 578 Args: |
552 cc(cursor) | 579 cc(cursor) |
553 filename(str): the json file to write to | 580 filename(str): the json file to write to |
554 """ | 581 """ |
555 sql_time_specification = 'AND DATEDIFF(NOW(), git_commit.timestamp) <= 30' | 582 sql_time_specification = 'AND DATEDIFF(NOW(), git_commit.timestamp) <= 30' |
556 output = tbr_to_total_people_ratio(cc, sql_time_specification) | 583 output = tbr_to_total_people_ratio(cc, sql_time_specification) |
557 with open(filename, 'w') as f: | 584 with open(filename, 'w') as f: |
558 json.dump(output, f) | 585 json.dump(output, f) |
OLD | NEW |