Patrick Williams | d0269de | 2022-01-06 21:04:25 -0600 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | import argparse |
| 4 | import json |
| 5 | import os |
| 6 | import re |
Patrick Williams | d0269de | 2022-01-06 21:04:25 -0600 | [diff] [blame] | 7 | from collections import defaultdict |
Patrick Williams | d0269de | 2022-01-06 21:04:25 -0600 | [diff] [blame] | 8 | from typing import Dict |
| 9 | |
Patrick Williams | a3db66b | 2022-12-04 16:27:08 -0600 | [diff] [blame] | 10 | import libvoters.acceptable as acceptable |
| 11 | from libvoters.time import TimeOfDay, timestamp |
| 12 | |
Patrick Williams | d0269de | 2022-01-06 21:04:25 -0600 | [diff] [blame] | 13 | |
| 14 | class subcmd: |
| 15 | def __init__(self, parser: argparse._SubParsersAction) -> None: |
| 16 | p = parser.add_parser( |
| 17 | "analyze-reviews", help="Determine points for reviews" |
| 18 | ) |
| 19 | |
| 20 | p.add_argument( |
| 21 | "--before", |
| 22 | "-b", |
| 23 | help="Before timestamp (YYYY-MM-DD)", |
| 24 | required=True, |
| 25 | ) |
| 26 | p.add_argument( |
| 27 | "--after", |
| 28 | "-a", |
| 29 | help="After timestamp (YYYY-MM-DD)", |
| 30 | required=True, |
| 31 | ) |
| 32 | |
| 33 | p.set_defaults(cmd=self) |
| 34 | |
| 35 | def run(self, args: argparse.Namespace) -> int: |
| 36 | before = timestamp(args.before, TimeOfDay.AM) |
| 37 | after = timestamp(args.after, TimeOfDay.PM) |
| 38 | |
| 39 | changes_per_user: Dict[str, list[int]] = defaultdict(list) |
| 40 | |
| 41 | for f in sorted(os.listdir(args.dir)): |
| 42 | path = os.path.join(args.dir, f) |
| 43 | if not os.path.isfile(path): |
| 44 | continue |
| 45 | |
Patrick Williams | a3db66b | 2022-12-04 16:27:08 -0600 | [diff] [blame] | 46 | if not re.match(r"[0-9]*\.json", f): |
Patrick Williams | d0269de | 2022-01-06 21:04:25 -0600 | [diff] [blame] | 47 | continue |
| 48 | |
| 49 | with open(path, "r") as file: |
| 50 | data = json.load(file) |
| 51 | |
| 52 | project = data["project"] |
| 53 | id_number = data["number"] |
| 54 | author = data["owner"]["username"] |
| 55 | |
| 56 | if not acceptable.project(project): |
| 57 | print("Rejected project:", project, id_number) |
| 58 | |
| 59 | comments_per_user: Dict[str, int] = defaultdict(int) |
| 60 | |
| 61 | for patch_set in data["patchSets"]: |
| 62 | created_on = data["createdOn"] |
| 63 | |
| 64 | if created_on > before or created_on < after: |
| 65 | continue |
| 66 | |
| 67 | if "comments" not in patch_set: |
| 68 | continue |
| 69 | |
| 70 | for comment in patch_set["comments"]: |
| 71 | reviewer = comment["reviewer"]["username"] |
| 72 | |
| 73 | if reviewer == author: |
| 74 | continue |
| 75 | if not acceptable.file(project, comment["file"]): |
| 76 | continue |
| 77 | |
| 78 | comments_per_user[reviewer] += 1 |
| 79 | |
| 80 | print(project, id_number) |
Patrick Williams | e310dd9 | 2022-12-07 06:55:38 -0600 | [diff] [blame^] | 81 | for user, count in comments_per_user.items(): |
Patrick Williams | d0269de | 2022-01-06 21:04:25 -0600 | [diff] [blame] | 82 | if count < 3: |
| 83 | continue |
| 84 | print(" ", user, count) |
| 85 | changes_per_user[user].append(id_number) |
| 86 | |
| 87 | with open(os.path.join(args.dir, "reviews.json"), "w") as outfile: |
| 88 | outfile.write(json.dumps(changes_per_user, indent=4)) |
| 89 | |
| 90 | return 0 |