Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix timestamp parsing #47

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions gantry/models/job.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
from datetime import datetime

from gantry.util.time import webhook_timestamp


class Job:
Expand All @@ -17,9 +18,10 @@ def __init__(
self.gl_id = gl_id
# handle jobs that haven't started or finished
if start:
self.start = datetime.fromisoformat(start).timestamp()
self.start = webhook_timestamp(start)
if end:
self.end = datetime.fromisoformat(end).timestamp()
self.end = webhook_timestamp(end)

self.ref = ref

@property
Expand Down
4 changes: 2 additions & 2 deletions gantry/tests/defs/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"build_status": "success",
"build_name": "[email protected] /jcchwaj %[email protected] arch=linux-ubuntu20.04-x86_64_v3 E4S",
"build_id": 9892514, # not used in testing unless it already exists in the db
"build_started_at": "2024-01-24T17:24:06.000Z",
"build_finished_at": "2024-01-24T17:47:00.000Z",
"build_started_at": "2024-01-24 17:24:06 UTC",
"build_finished_at": "2024-01-24 17:47:00 UTC",
"ref": "pr42264_bugfix/mathomp4/hdf5-appleclang15",
"runner": {"description": "aws"},
}
Expand Down
15 changes: 15 additions & 0 deletions gantry/util/time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import datetime


def webhook_timestamp(dt: str) -> float:
"""Converts a gitlab webhook datetime to a unix timestamp."""
# gitlab sends dates in 2021-02-23 02:41:37 UTC format
# documentation says they use iso 8601, but they don't consistently apply it
# https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html#job-events
GITLAB_DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S %Z"
# strptime doesn't tag with timezone by default
return (
datetime.datetime.strptime(dt, GITLAB_DATETIME_FORMAT)
.replace(tzinfo=datetime.timezone.utc)
.timestamp()
)