14
14
15
15
import argparse
16
16
import github
17
+ import sys
18
+ import time
17
19
18
20
19
21
def main (token , filter_gha_runners ):
@@ -44,6 +46,8 @@ def main(token, filter_gha_runners):
44
46
45
47
print (f"\n Found { in_progress_jobs } running jobs." )
46
48
49
+ return in_progress_jobs
50
+
47
51
48
52
if __name__ == "__main__" :
49
53
parser = argparse .ArgumentParser (
@@ -56,6 +60,20 @@ def main(token, filter_gha_runners):
56
60
default = None ,
57
61
nargs = "?" ,
58
62
)
63
+ parser .add_argument (
64
+ "--output-file" ,
65
+ type = str ,
66
+ help = "The output file to write time-series data to" ,
67
+ default = None ,
68
+ nargs = "?" ,
69
+ )
70
+ parser .add_argument (
71
+ "--data-collection-interval" ,
72
+ type = int ,
73
+ help = "The number of seconds between data collection intervals" ,
74
+ default = None ,
75
+ nargs = "?" ,
76
+ )
59
77
parser .add_argument (
60
78
"--filter-gha-runners" ,
61
79
help = "Only consider jobs running on hosted Github actions runners" ,
@@ -68,6 +86,31 @@ def main(token, filter_gha_runners):
68
86
help = "Consider all running jobs" ,
69
87
)
70
88
parser .set_defaults (filter_gha_runners = False )
71
-
72
89
args = parser .parse_args ()
73
- main (args .token , args .filter_gha_runners )
90
+
91
+ # Perform some basic argument validation
92
+
93
+ # If an output file is specified, the user must also specify the data
94
+ # collection interval.
95
+ if bool (args .output_file ) and not bool (args .data_collection_interval ):
96
+ print ("A data collection interval must be specified when --output_file is used" )
97
+ sys .exit (1 )
98
+
99
+ if args .data_collection_interval :
100
+ while True :
101
+ current_time = time .localtime ()
102
+ current_time_string = time .strftime ("%Y/%m/%d %H:%M:%S" , current_time )
103
+
104
+ print (f"Collecting data at { current_time_string } " )
105
+
106
+ current_job_count = main (args .token , args .filter_gha_runners )
107
+
108
+ if args .output_file :
109
+ with open (args .output_file , "a" ) as output_file_handle :
110
+ output_file_handle .write (
111
+ f"{ current_time_string } ,{ current_job_count } \n "
112
+ )
113
+
114
+ time .sleep (args .data_collection_interval )
115
+ else :
116
+ main (args .token , args .filter_gha_runners )
0 commit comments