-
Notifications
You must be signed in to change notification settings - Fork 13
/
repo_health_dashboard.py
106 lines (96 loc) · 3.51 KB
/
repo_health_dashboard.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
"""
repo-health-dashboard CLI
"""
import argparse
import codecs
import datetime
import glob
import os
import yaml
from .utils import utils
def main():
"""
Create basic dashboard
"""
parser = argparse.ArgumentParser(description="Create basic dashboard")
parser.add_argument(
"--dashboard-name",
help="name of dashboard to trigger e.g. repo_health, dependencies_health",
dest="dashboard_name",
default="repo_health",
)
parser.add_argument(
"--data-dir",
help="location of where data yaml files are located",
required=True,
dest="data_dir",
)
parser.add_argument(
"--output-csv",
help="path to csv output",
dest="output_csv",
default="dashboard",
)
parser.add_argument(
"--output-sqlite",
help="path to sqlite output",
dest="output_sqlite",
default="dashboard",
)
parser.add_argument(
"--configuration",
help="path to yaml file with configurations for key orders and aliases",
default=None,
)
parser.add_argument(
"--data-life-time",
help="days: how many days before individual data yaml files are outdated",
default=1,
)
parser.add_argument(
"--append",
help="whether to append existing dashboard file",
action="store_true",
)
args = parser.parse_args()
# collect configurations if they were input
configuration_name = "py_dependency_health" if args.dashboard_name == 'py_dependency_health' else "main"
configurations = {
configuration_name: {"check_order": [], "repo_name_order": [], "key_aliases": {}}
}
if args.configuration:
with codecs.open(args.configuration, "r", "utf-8") as f:
file_data = f.read()
parsed_file_data = yaml.safe_load(file_data)
sheets = parsed_file_data.keys()
for sheet in sheets:
configurations[sheet] = utils.get_sheets(parsed_file_data, sheet)
data_dir = os.path.abspath(args.data_dir)
data_files_pattern = "*/*.yaml" if args.dashboard_name == "repo_health" else "*.yaml"
files = glob.glob(os.path.join(data_dir, data_files_pattern), recursive=False)
data = {}
for file_path in files:
file_name = file_path[file_path.rfind("/") + 1:]
repo_name = file_name.replace("_repo_health.yaml", "")
# TODO(jinder): maybe add a try block here
with codecs.open(file_path, "r", "utf-8") as f:
file_data = f.read()
parsed_file_data = yaml.safe_load(file_data)
date_of_collection = parsed_file_data["TIMESTAMP"]
today_date = datetime.datetime.now().date()
days_since_collection = abs((today_date - date_of_collection).days)
if days_since_collection > int(args.data_life_time):
continue
org_name = f'{parsed_file_data["org_name"]}/' if "org_name" in parsed_file_data else ''
org_repo_name = f'{org_name}{repo_name}'
data[org_repo_name] = parsed_file_data
output = utils.squash_and_standardize_metadata_by_repo(data)
for key, configuration in configurations.items():
utils.write_squashed_metadata_to_csv(
output, args.output_csv + "_" + key, configuration, args.append
)
if args.dashboard_name == "repo_health":
utils.write_squashed_metadata_to_sqlite(
output, f"dashboard_{key}", configuration, args.output_sqlite)
if __name__ == "__main__":
main()