From 3b7b084419921803b7e0d59129dbeb2e457eedaf Mon Sep 17 00:00:00 2001 From: Jon Massey Date: Fri, 17 May 2024 11:54:53 +0100 Subject: [PATCH] Add task to fetch and store codespaces metrics As the codespaces data will change over time and deleted codespaces will disappear from the API, we do not drop and recreate the table each time the task is run as per the other tasks. Instead, calling the upsert() method ensures the table exists then merges the new data with the existing. Conversion method added to metrics.py is not a metric in the usual sense of the word, but instead renames some fields to match the database schema. This is neccesary due to different naming conventions in the domain dataclasses and database tables. --- metrics/github/metrics.py | 13 +++++++++++++ metrics/tasks/codespaces.py | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 metrics/tasks/codespaces.py diff --git a/metrics/github/metrics.py b/metrics/github/metrics.py index 569ab2e1..c8f5fd61 100644 --- a/metrics/github/metrics.py +++ b/metrics/github/metrics.py @@ -75,3 +75,16 @@ def convert_issue_counts_to_metrics(counts): } ) return metrics + + +def convert_codespaces_to_dicts(codespaces): + return [ + { + "organisation": c.org, + "repo": c.repo_name, + "user": c.user, + "created_at": c.created_at, + "last_used_at": c.last_used_at, + } + for c in codespaces + ] diff --git a/metrics/tasks/codespaces.py b/metrics/tasks/codespaces.py new file mode 100644 index 00000000..38159fb7 --- /dev/null +++ b/metrics/tasks/codespaces.py @@ -0,0 +1,24 @@ +import sys + +import structlog + +import metrics.github.github as github +from metrics.github.metrics import convert_codespaces_to_dicts +from metrics.timescaledb import db, tables + + +log = structlog.get_logger() + + +def main(): + log.info("Getting codespaces") + codespaces = github.codespaces(org="opensafely") + log.info(f"Got {len(codespaces)} codespaces") + + log.info("Writing data") + db.upsert(tables.GitHubCodespaces, convert_codespaces_to_dicts(codespaces)) + log.info("Written data") + + +if __name__ == "__main__": + sys.exit(main())