Skip to content

Commit

Permalink
Merge pull request #24 from midokura/get-data-for-job
Browse files Browse the repository at this point in the history
Add job time attributes
  • Loading branch information
gerardsegarra authored Apr 29, 2024
2 parents b3bd49c + d27ef87 commit afbb9a6
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 17 deletions.
4 changes: 4 additions & 0 deletions src/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def job_id(self):
def run_id(self):
return self.data["workflow_job"]["run_id"]

@property
def node_id(self):
return self.data["workflow_job"]["node_id"]

@property
def name(self):
return self.data["workflow_job"]["name"].replace("\n", " ")
Expand Down
32 changes: 29 additions & 3 deletions src/jobs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
from datetime import datetime
from github import GithubJob


class Job:
def __init__(self, github_job: GithubJob) -> None:
self.github_job = github_job
self.github_job: GithubJob = None
self.stauts: str = None

self.queued_at: datetime = None
self.in_progress_at: datetime = None
self.completed_at: datetime = None

self._update_attributes(github_job)

self.node_id = self.github_job.node_id

def _update_attributes(self, github_job: GithubJob):
self.github_job: GithubJob = github_job
self.status = github_job.action

if self.github_job.action == "queued":
self.queued_at = self.github_job.time_start

if (
self.github_job.action == "in_progress"
or self.github_job.action == "completed"
):
self.in_progress_at = self.github_job.time_start
self.completed_at = self.github_job.time_completed

def update(self, github_job: GithubJob):
self._update_attributes(github_job)


class JobEventsHandler:
Expand Down Expand Up @@ -43,8 +70,7 @@ def _process_in_progress_event(self, event: dict):
if not job:
job = self._create_job(GithubJob(event))
else:
# Update github job event from job
job.github_job = GithubJob(event)
job.update(GithubJob(event))

self.in_progress[job_id] = job

Expand Down
5 changes: 3 additions & 2 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

def parse_datetime(date: str) -> datetime:
"""Parse GitHub date to object."""
exp = "%Y-%m-%dT%H:%M:%SZ"
return datetime.strptime(date, exp)
if date:
exp = "%Y-%m-%dT%H:%M:%SZ"
return datetime.strptime(date, exp)


def dict_to_logfmt(data: dict) -> str:
Expand Down
89 changes: 77 additions & 12 deletions tests/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,61 @@

from unittest.mock import Mock

from jobs import JobEventsHandler
from datetime import datetime
from github import GithubJob
from jobs import Job, JobEventsHandler

GITHUB_TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"


@pytest.fixture
def new_job_event():
return {"workflow_job": {"id": "workflow_id"}, "action": "queued"}
return {
"workflow_job": {
"id": "workflow_id",
"started_at": "2024-04-29T12:43:16Z",
"completed_at": None,
"node_id": "CR_kwDOHC6jj88AAAAFqGXrPQ",
},
"action": "queued",
}


@pytest.fixture
def in_progress_job_event():
return {"workflow_job": {"id": "workflow_id"}, "action": "in_progress"}
return {
"workflow_job": {
"id": "workflow_id",
"started_at": "2024-04-29T12:43:32Z",
"completed_at": None,
"node_id": "CR_kwDOHC6jj88AAAAFqGXrPQ",
},
"action": "in_progress",
}


@pytest.fixture
def completed_job_event():
return {"workflow_job": {"id": "workflow_id"}, "action": "completed"}


def test_new_job(new_job_event):
return {
"workflow_job": {
"id": "workflow_id",
"started_at": "2024-04-29T12:43:32Z",
"completed_at": "2024-04-29T12:45:09Z",
"node_id": "CR_kwDOHC6jj88AAAAFqGXrPQ",
},
"action": "completed",
}


def test_new_job_event(new_job_event):
handler = JobEventsHandler()

handler.process_event(new_job_event)

assert handler.queued.get("workflow_id")


def test_in_progress_job(in_progress_job_event):
def test_in_progress_job_event(in_progress_job_event):
handler = JobEventsHandler()
job = Mock()
handler.queued["workflow_id"] = job
Expand All @@ -39,18 +67,55 @@ def test_in_progress_job(in_progress_job_event):
assert handler.in_progress.get("workflow_id") == job


def test_unprocessed_in_progress_job(in_progress_job_event):
def test_unprocessed_in_progress_job_event(in_progress_job_event):
handler = JobEventsHandler()
handler.process_event(in_progress_job_event)

assert handler.in_progress.get("workflow_id")


def test_completed_job(completed_job_event):
def test_completed_job_event(completed_job_event):
handler = JobEventsHandler()
handler.in_progress["workflow_id"] = Mock()

handler.process_event(completed_job_event)

assert not handler.queued.get("workflow_id")
assert not handler.in_progress.get("workflow_id")
assert handler.queued.get("workflow_id") is None
assert handler.in_progress.get("workflow_id") is None


def test_new_job(new_job_event):
job = Job(GithubJob(new_job_event))

assert job.status == "queued"
assert job.queued_at == datetime.strptime(
"2024-04-29T12:43:16Z", GITHUB_TIME_FORMAT
)
assert job.in_progress_at is None


def test_update_in_progress_job(new_job_event, in_progress_job_event):
job = Job(GithubJob(new_job_event))
job.update(GithubJob(in_progress_job_event))

assert job.status == "in_progress"
assert job.queued_at == datetime.strptime(
"2024-04-29T12:43:16Z", GITHUB_TIME_FORMAT
)
assert job.in_progress_at == datetime.strptime(
"2024-04-29T12:43:32Z", GITHUB_TIME_FORMAT
)
assert job.completed_at is None


def test_update_completed_job(in_progress_job_event, completed_job_event):
job = Job(GithubJob(in_progress_job_event))
job.update(GithubJob(completed_job_event))

assert job.status == "completed"
assert job.in_progress_at == datetime.strptime(
"2024-04-29T12:43:32Z", GITHUB_TIME_FORMAT
)
assert job.completed_at == datetime.strptime(
"2024-04-29T12:45:09Z", GITHUB_TIME_FORMAT
)

0 comments on commit afbb9a6

Please sign in to comment.