@@ -195,23 +195,56 @@ def query_for_reviews(
195
195
return list (new_commits .values ())
196
196
197
197
198
+ def get_past_contributors (bq_client : bigquery .Client ) -> set [str ]:
199
+ """Get past contributors to LLVM from BigQuery dataset.
200
+
201
+ Args:
202
+ bq_client: The BigQuery client to use.
203
+
204
+ Returns:
205
+ Set of unique past contributors to LLVM.
206
+ """
207
+ results = bq_client .query ("""
208
+ SELECT
209
+ DISTINCT commit_author
210
+ FROM %s.%s
211
+ WHERE commit_author IS NOT NULL
212
+ """ % (OPERATIONAL_METRICS_DATASET , LLVM_COMMITS_TABLE )).result ()
213
+ return set (row .commit_author for row in results )
214
+
215
+
198
216
def upload_daily_metrics_to_grafana (
199
217
grafana_api_key : str ,
200
218
grafana_metrics_userid : str ,
201
219
new_commits : list [LLVMCommitInfo ],
220
+ past_contributors : set [str ],
202
221
) -> None :
203
222
"""Upload daily commit metrics to Grafana.
204
223
205
224
Args:
206
225
grafana_api_key: The key to make API requests with.
207
226
grafana_metrics_userid: The user to make API requests with.
208
227
new_commits: List of commits to process & upload to Grafana.
228
+ past_contributors: Set of unique past contributors to LLVM.
209
229
"""
230
+
231
+ def post_data (data : str ) -> None :
232
+ """Helper function to post data to Grafana."""
233
+ response = requests .post (
234
+ GRAFANA_URL ,
235
+ headers = {"Content-Type" : "text/plain" },
236
+ data = data ,
237
+ auth = (grafana_metrics_userid , grafana_api_key ),
238
+ )
239
+ if response .status_code < 200 or response .status_code >= 300 :
240
+ logging .error ("Failed to submit data to Grafana: %s" , response .text )
241
+
210
242
# Count each type of commit made
211
243
approval_count = 0
212
244
review_count = 0
213
245
pull_request_count = 0
214
246
push_count = 0
247
+ contributors = set ()
215
248
for commit in new_commits :
216
249
if commit .is_approved :
217
250
approval_count += 1
@@ -221,44 +254,49 @@ def upload_daily_metrics_to_grafana(
221
254
pull_request_count += 1
222
255
else :
223
256
push_count += 1
257
+ contributors .add (commit .commit_author )
224
258
225
259
# Post data via InfluxDB API call
260
+ # Commit data
226
261
request_data = (
227
262
"llvm_project_main_daily_commits"
228
263
" approval_count={},review_count={},pull_request_count={},push_count={}"
229
264
).format (approval_count , review_count , pull_request_count , push_count )
230
- response = requests .post (
231
- GRAFANA_URL , # Set timestamp precision to seconds
232
- headers = {"Content-Type" : "text/plain" },
233
- data = request_data ,
234
- auth = (grafana_metrics_userid , grafana_api_key ),
235
- )
265
+ post_data (request_data )
236
266
237
- if response .status_code < 200 or response .status_code >= 300 :
238
- logging .error ("Failed to submit data to Grafana: %s" , response .text )
267
+ # Contributor data
268
+ request_data = (
269
+ "llvm_project_main"
270
+ " daily_unique_contributor_count={},all_time_unique_contributor_count={}"
271
+ .format (len (contributors ), len (contributors | past_contributors ))
272
+ )
273
+ post_data (request_data )
239
274
240
275
241
- def upload_daily_metrics_to_bigquery (new_commits : list [LLVMCommitInfo ]) -> None :
276
+ def upload_daily_metrics_to_bigquery (
277
+ bq_client : bigquery .Client , new_commits : list [LLVMCommitInfo ]
278
+ ) -> None :
242
279
"""Upload processed commit metrics to a BigQuery dataset.
243
280
244
281
Args:
282
+ bq_client: The BigQuery client to use.
245
283
new_commits: List of commits to process & upload to BigQuery.
246
284
"""
247
- bq_client = bigquery .Client ()
248
285
table_ref = bq_client .dataset (OPERATIONAL_METRICS_DATASET ).table (
249
286
LLVM_COMMITS_TABLE
250
287
)
251
288
table = bq_client .get_table (table_ref )
252
289
commit_records = [dataclasses .asdict (commit ) for commit in new_commits ]
253
290
bq_client .insert_rows (table , commit_records )
254
- bq_client .close ()
255
291
256
292
257
293
def main () -> None :
258
294
github_token = os .environ ["GITHUB_TOKEN" ]
259
295
grafana_api_key = os .environ ["GRAFANA_API_KEY" ]
260
296
grafana_metrics_userid = os .environ ["GRAFANA_METRICS_USERID" ]
261
297
298
+ bq_client = bigquery .Client ()
299
+
262
300
# Scrape new commits
263
301
date_to_scrape = datetime .datetime .now (
264
302
datetime .timezone .utc
@@ -275,13 +313,21 @@ def main() -> None:
275
313
logging .info ("Querying for reviews of new commits." )
276
314
new_commit_info = query_for_reviews (new_commits , github_token )
277
315
316
+ logging .info ("Getting set of past LLVM contributors." )
317
+ past_contributors = get_past_contributors (bq_client )
318
+
278
319
logging .info ("Uploading metrics to Grafana." )
279
320
upload_daily_metrics_to_grafana (
280
- grafana_api_key , grafana_metrics_userid , new_commit_info
321
+ grafana_api_key ,
322
+ grafana_metrics_userid ,
323
+ new_commit_info ,
324
+ past_contributors ,
281
325
)
282
326
283
327
logging .info ("Uploading metrics to BigQuery." )
284
- upload_daily_metrics_to_bigquery (new_commit_info )
328
+ upload_daily_metrics_to_bigquery (bq_client , new_commit_info )
329
+
330
+ bq_client .close ()
285
331
286
332
287
333
if __name__ == "__main__" :
0 commit comments