From 5e8fc42629362cad8270caf81b3dd1ab3dad9519 Mon Sep 17 00:00:00 2001 From: Jon Massey Date: Tue, 21 May 2024 21:50:55 +0100 Subject: [PATCH] Include codespace id in primary key codespace created_at as returned from github API is only second-precision and so no reliable as PK alone. Documentation is unclear as to the scope of uniqueness of the id field returned by the API, so compounding with created_at is a way of hedging our bets. --- metrics/github/github.py | 3 ++- metrics/github/metrics.py | 1 + metrics/github/query.py | 1 + metrics/timescaledb/tables.py | 1 + tests/metrics/github/test_github.py | 2 ++ 5 files changed, 7 insertions(+), 1 deletion(-) diff --git a/metrics/github/github.py b/metrics/github/github.py index 9651206..6e3872f 100644 --- a/metrics/github/github.py +++ b/metrics/github/github.py @@ -110,6 +110,7 @@ def from_dict(cls, data, repo): @dataclass(frozen=True) class Codespace: + id: int repo: Repo user: str created_at: datetime.datetime @@ -119,7 +120,7 @@ class Codespace: def from_dict(cls, repo, **kwargs): if "repo_name" in kwargs: del kwargs["repo_name"] - return cls(repo, **kwargs) + return cls(repo=repo, **kwargs) def codespaces(org): diff --git a/metrics/github/metrics.py b/metrics/github/metrics.py index cfcfa66..7204911 100644 --- a/metrics/github/metrics.py +++ b/metrics/github/metrics.py @@ -80,6 +80,7 @@ def convert_issue_counts_to_metrics(counts): def get_codespaces_metrics(codespaces): return [ { + "id": c.id, "created_at": c.created_at, "organisation": c.repo.org, "repo": c.repo.name, diff --git a/metrics/github/query.py b/metrics/github/query.py index 3f13a11..e03cd71 100644 --- a/metrics/github/query.py +++ b/metrics/github/query.py @@ -166,6 +166,7 @@ def codespaces(org): codespaces = _client().rest_query("/orgs/{org}/codespaces", org=org) for codespace in codespaces: yield { + "id": codespace["id"], "user": codespace["owner"]["login"], "repo_name": codespace["repository"]["name"], "created_at": codespace["created_at"], diff --git a/metrics/timescaledb/tables.py b/metrics/timescaledb/tables.py index 1b639a1..dad0008 100644 --- a/metrics/timescaledb/tables.py +++ b/metrics/timescaledb/tables.py @@ -7,6 +7,7 @@ GitHubCodespaces = Table( "github_codespaces", metadata, + Column("id", Integer, primary_key=True), Column("created_at", TIMESTAMP(timezone=True), primary_key=True), Column("organisation", Text), Column("repo", Text), diff --git a/tests/metrics/github/test_github.py b/tests/metrics/github/test_github.py index e4ed5c6..f4b54e2 100644 --- a/tests/metrics/github/test_github.py +++ b/tests/metrics/github/test_github.py @@ -41,12 +41,14 @@ def test_codespaces(patch): { "opensafely": [ { + "id": 1, "user": "testuser", "repo_name": "testrepo", "created_at": datetime.datetime.now().isoformat(), "last_used_at": datetime.datetime.now().isoformat(), }, { + "id": 2, "user": "testuser", "repo_name": "testrepo2", "created_at": datetime.datetime.now().isoformat(),