11# The starting point
22import os
3+ from datetime import datetime
34
45from prettytable import PrettyTable
56
6- from python . customlogger import getlogger
7- from python . ghaworkflows import getrepoworkflows
8- from python . ghorg import getreposfromorganisation
7+ from customlogger import getlogger
8+ from ghaworkflows import getrepoworkflows
9+ from ghorg import getreposfromorganisation
910
1011
1112class RepoData :
@@ -24,6 +25,7 @@ def __init__(self, name, usage, actions):
2425logger = getlogger ()
2526
2627repo_name_column_header = "Repo Name"
28+ datetime_format = "%Y-%m-%d %H:%M"
2729
2830
2931def main ():
@@ -32,23 +34,23 @@ def main():
3234 logger .info (f'*************** Getting repos for { org } ***************' )
3335 # Get all the repo names for the org, will page results too
3436 # repo names are returned sorted
35- repoNames = getreposfromorganisation (org )
37+ repo_names = getreposfromorganisation (org )
3638
37- reposUsage = []
38- totalCosts = dict .fromkeys (['UBUNTU' , 'MACOS' , 'WINDOWS' ], 0 )
39+ repos_usage = []
40+ total_costs = dict .fromkeys (['UBUNTU' , 'MACOS' , 'WINDOWS' ], 0 )
3941 # Collect the data from each repo
40- for repoName in repoNames :
42+ for repo_name in repo_names :
4143 actions = []
42- repoData = RepoData (repoName , dict .fromkeys (['UBUNTU' , 'MACOS' , 'WINDOWS' ], 0 ), actions )
43- logger .info (f"*************** Repo Name { repoData .name } ***************" )
44- getrepoworkflows (org , repoData )
45- reposUsage .append (repoData )
46- logger .info (f"*************** Repo Usage Summary { repoData .usage } ***************" )
47- totalCosts ["UBUNTU" ] += repoData .usage ["UBUNTU" ]
48- totalCosts ["MACOS" ] += repoData .usage ["MACOS" ]
49- totalCosts ["WINDOWS" ] += repoData .usage ["WINDOWS" ]
50-
51- logger .info (f"***************Total Costs: { totalCosts } *******************" )
44+ repo_data = RepoData (repo_name , dict .fromkeys (['UBUNTU' , 'MACOS' , 'WINDOWS' ], 0 ), actions )
45+ logger .info (f"*************** Repo Name { repo_data .name } ***************" )
46+ getrepoworkflows (org , repo_data )
47+ repos_usage .append (repo_data )
48+ logger .info (f"*************** Repo Usage Summary { repo_data .usage } ***************" )
49+ total_costs ["UBUNTU" ] += repo_data .usage ["UBUNTU" ]
50+ total_costs ["MACOS" ] += repo_data .usage ["MACOS" ]
51+ total_costs ["WINDOWS" ] += repo_data .usage ["WINDOWS" ]
52+
53+ logger .info (f"***************Total Costs: { total_costs } *******************" )
5254 # table tp print out per repo/workflow
5355 # Repo names are already sorted and we don't want to sort on tables
5456 # as order would mess up with totals
@@ -59,8 +61,8 @@ def main():
5961 summary_table : PrettyTable = PrettyTable ()
6062 summary_table .field_names = [repo_name_column_header , "Ubuntu" , "MacOS" , "Windows" ]
6163 summary_table .align [repo_name_column_header ] = "l"
62-
63- for repo in reposUsage :
64+ validate_total_costs = dict . fromkeys ([ 'UBUNTU' , 'MACOS' , 'WINDOWS' ], 0 )
65+ for repo in repos_usage :
6466 summary_table .add_row ([repo .name , repo .usage ["UBUNTU" ], repo .usage ["MACOS" ], repo .usage ["WINDOWS" ]])
6567 first_row : bool = True
6668 if not repo .actions :
@@ -70,14 +72,28 @@ def main():
7072 workflow_table .add_row ([repo .name , action .name , action .workflow ['UBUNTU' ], action .workflow ['MACOS' ],
7173 action .workflow ['WINDOWS' ]])
7274 first_row = False
75+ validate_total_costs ["UBUNTU" ] += action .workflow ["UBUNTU" ]
76+ validate_total_costs ["MACOS" ] += action .workflow ["MACOS" ]
77+ validate_total_costs ["WINDOWS" ] += action .workflow ["WINDOWS" ]
7378 else :
7479 workflow_table .add_row (["" , action .name , action .workflow ['UBUNTU' ], action .workflow ['MACOS' ],
7580 action .workflow ['WINDOWS' ]])
81+ validate_total_costs ["UBUNTU" ] += action .workflow ["UBUNTU" ]
82+ validate_total_costs ["MACOS" ] += action .workflow ["MACOS" ]
83+ validate_total_costs ["WINDOWS" ] += action .workflow ["WINDOWS" ]
84+
7685 workflow_table .add_row (["--------" , "--------" , "-----" , "-----" , "-----" ])
7786
7887 summary_table .add_row (["---------" , "----" , "----" , "----" ])
79- summary_table .add_row (["Total Costs" , totalCosts ["UBUNTU" ], totalCosts ["MACOS" ], totalCosts ["WINDOWS" ]])
88+ summary_table .add_row (
89+ ["Billable Minutes " + datetime .now ().strftime (datetime_format ), total_costs ["UBUNTU" ],
90+ total_costs ["MACOS" ],
91+ total_costs ["WINDOWS" ]])
8092 summary_table .add_row (["---------" , "----" , "----" , "----" ])
93+ workflow_table .add_row (["Billable Minutes " + datetime .now ().strftime (datetime_format ), "" ,
94+ validate_total_costs ["UBUNTU" ], validate_total_costs ["MACOS" ],
95+ validate_total_costs ["WINDOWS" ]])
96+
8197 print (summary_table )
8298 print (workflow_table )
8399
0 commit comments