Improvements

This commit is contained in:
Edward Betts 2023-12-21 18:59:28 +00:00
parent b6ce1cbe64
commit 239bfaa79b

View file

@ -7,6 +7,7 @@ import json
import os import os
import sys import sys
import typing import typing
from collections import Counter
from datetime import date, datetime, timedelta from datetime import date, datetime, timedelta
import pytz import pytz
@ -26,18 +27,18 @@ url = f"https://{hostname}/api/v1/repos/issues/search"
def download_bugs(state: str) -> list[Bug]: def download_bugs(state: str) -> list[Bug]:
"""Get all bugs from forgejo.""" """Get all bugs from forgejo."""
# params: dict[str, str | int] = {}
all_bugs: list[Bug] = [] all_bugs: list[Bug] = []
page = 0 page = 0
headers = {"Authorization": "token " + token}
while bugs := requests.get( while bugs := requests.get(
url, url,
headers={"Authorization": "token " + token}, headers=headers,
params=typing.cast(CallParams, {"state": state, "page": page}), params=typing.cast(CallParams, {"state": state, "page": page}),
).json(): ).json():
if isinstance(bugs, dict): if isinstance(bugs, dict):
print(bugs) print(bugs)
sys.exit(1) sys.exit(1)
assert isinstance(bugs, list) assert isinstance(bugs, list)
all_bugs += bugs all_bugs += bugs
page += 1 page += 1
@ -70,8 +71,8 @@ def parse_date(date_str: str | None) -> date | None:
def count_open_bugs(bug_reports: list[Bug]) -> list[tuple[str, int]]: def count_open_bugs(bug_reports: list[Bug]) -> list[tuple[str, int]]:
"""Count the number of open bugs for each date based on a list of bug reports.""" """Count the number of open bugs for each date based on a list of bug reports."""
open_dates: dict[date, int] = {} open_dates: Counter[date] = Counter()
close_dates: dict[date, int] = {} close_dates: Counter[date] = Counter()
# Process each bug report # Process each bug report
seen: set[int] = set() seen: set[int] = set()
@ -80,12 +81,12 @@ def count_open_bugs(bug_reports: list[Bug]) -> list[tuple[str, int]]:
continue continue
seen.add(report["id"]) seen.add(report["id"])
open_date = parse_date(report["created_at"]) open_date = parse_date(report["created_at"])
if open_date: assert open_date
open_dates[open_date] = open_dates.get(open_date, 0) + 1 open_dates[open_date] += 1
close_date = parse_date(report["closed_at"]) close_date = parse_date(report["closed_at"])
if close_date: if close_date:
close_dates[close_date] = close_dates.get(close_date, 0) + 1 close_dates[close_date] += 1
# Create a date range from the earliest open date to the latest close date # Create a date range from the earliest open date to the latest close date
start_date = min(open_dates.keys()) start_date = min(open_dates.keys())
@ -112,7 +113,8 @@ def main() -> None:
json_data = json.dumps(open_bugs_over_time) json_data = json.dumps(open_bugs_over_time)
template_html = open("template.html").read() template_html = open("template.html").read()
with open("chart.html", "w") as out: dest = config.get("output", "dest")
with open(dest, "w") as out:
out.write(template_html.replace("jsonData = []", "jsonData = " + json_data)) out.write(template_html.replace("jsonData = []", "jsonData = " + json_data))