-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PTFE-168] ✨ Get job cost & unit testing (#33)
- Loading branch information
gaspardmoindrot
authored
May 15, 2023
1 parent
2f52045
commit caa4b4d
Showing
6 changed files
with
81 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from githubkit.webhooks import WorkflowJob | ||
|
||
from gh_actions_exporter.config import Settings | ||
|
||
|
||
class Cost(object): | ||
def __init__(self, settings: Settings): | ||
self.settings: Settings = settings | ||
|
||
def get_job_cost(self, workflow_job: WorkflowJob, flavor_label: str) -> float: | ||
cost_per_min: float = self.settings.job_costs.get( | ||
flavor_label, self.settings.default_cost | ||
) | ||
duration: int = int(workflow_job.completed_at.timestamp()) - int( | ||
workflow_job.started_at.timestamp() | ||
) | ||
|
||
return duration / 60 * cost_per_min |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from datetime import datetime, timedelta | ||
|
||
import pytest | ||
from githubkit.webhooks import WorkflowJob | ||
|
||
from gh_actions_exporter.config import Settings | ||
|
||
|
||
@pytest.fixture | ||
def workflow_job(): | ||
return WorkflowJob( | ||
id=123, | ||
run_id=456789, | ||
run_attempt=1, | ||
run_url="https://github.com/user/repo/actions/runs/123", | ||
head_sha="a1b2c3d4e5f6g7h8i9j0", | ||
node_id="MDg6Q29tbWl0MTIzNDU2Nzg5OnNhbXBsZQ==", | ||
name="Job 1", | ||
check_run_url="https://api.github.com/repos/user/repo/check-runs/123", | ||
html_url="https://github.com/user/repo/actions/runs/123", | ||
url="https://api.github.com/repos/user/repo/actions/jobs/123", | ||
status="queued", | ||
steps=[], | ||
conclusion=None, | ||
labels=["label1", "label2"], | ||
runner_id=None, | ||
runner_name=None, | ||
runner_group_id=None, | ||
runner_group_name=None, | ||
started_at=datetime(2021, 1, 1, 0, 0), | ||
completed_at=datetime(2021, 1, 1, 0, 0) + timedelta(minutes=10), | ||
workflow_name=None, | ||
head_branch=None, | ||
created_at=datetime.now(), | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def settings(): | ||
return Settings() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from gh_actions_exporter.cost import Cost | ||
|
||
|
||
def test_get_job_cost(workflow_job, settings): | ||
assert Cost(settings).get_job_cost(workflow_job, "large") == 0.16 | ||
|
||
|
||
def test_job_cost_unknown_label(workflow_job, settings): | ||
assert Cost(settings).get_job_cost(workflow_job, "xxx") == 0 |