Skip to content

Commit

Permalink
Include codespace id in primary key
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Jongmassey committed May 21, 2024
1 parent 1a89fbf commit 5e8fc42
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 1 deletion.
3 changes: 2 additions & 1 deletion metrics/github/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
1 change: 1 addition & 0 deletions metrics/github/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions metrics/github/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
1 change: 1 addition & 0 deletions metrics/timescaledb/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions tests/metrics/github/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 5e8fc42

Please sign in to comment.