Skip to content

Commit

Permalink
Add task to fetch and store codespaces metrics
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Jongmassey committed May 31, 2024
1 parent b7051a4 commit 3b7b084
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
13 changes: 13 additions & 0 deletions metrics/github/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
24 changes: 24 additions & 0 deletions metrics/tasks/codespaces.py
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit 3b7b084

Please sign in to comment.