Skip to content

Commit

Permalink
Handle null timestamps
Browse files Browse the repository at this point in the history
If either started_at or completed_at timestamp is null, use the current
timestamp as the event time and set duration to zero.

Signed-off-by: Michi Mutsuzaki <michi@isovalent.com>
  • Loading branch information
michi-covalent committed Sep 25, 2022
1 parent 14c1fa5 commit e871295
Show file tree
Hide file tree
Showing 5 changed files with 2,571 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ jobs:
# you must check out the repository
- name: Checkout
uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10.7'
- name: Run formatter
uses: psf/black@stable
- name: Run unit tests
run: |
pip install -r requirements.txt
python -m unittest test_push.py
- name: Step 1
run: echo step 1
- name: Step 2
Expand Down
30 changes: 22 additions & 8 deletions push.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ def workflow_run_to_stream(workflow_run, branch):
def job_to_stream(workflow_run, job, branch):
duration = 0
completed_at = datetime.datetime.now(datetime.timezone.utc)
if "started_at" in job and "completed_at" in job:
if (
"started_at" in job
and job["started_at"] is not None
and "completed_at" in job
and job["completed_at"] is not None
):
started_at = dateutil.parser.isoparse(job["started_at"])
completed_at = dateutil.parser.isoparse(job["completed_at"])
duration = int(datetime.timedelta.total_seconds(completed_at - started_at))
Expand All @@ -63,7 +68,12 @@ def job_to_stream(workflow_run, job, branch):
def step_to_stream(workflow_run, job, step, branch):
duration = 0
completed_at = datetime.datetime.now(datetime.timezone.utc)
if "started_at" in step and "completed_at" in step:
if (
"started_at" in step
and step["started_at"] is not None
and "completed_at" in step
and step["completed_at"] is not None
):
started_at = dateutil.parser.isoparse(step["started_at"])
completed_at = dateutil.parser.isoparse(step["completed_at"])
duration = int(datetime.timedelta.total_seconds(completed_at - started_at))
Expand All @@ -87,6 +97,15 @@ def step_to_stream(workflow_run, job, step, branch):
}


def jobs_to_stream(workflow_run, jobs, branch):
streams = []
for job in jobs["jobs"]:
streams.append(job_to_stream(workflow_run, job, branch))
for step in job["steps"]:
streams.append(step_to_stream(workflow_run, job, step, branch))
return streams


def main(loki_endpoint, loki_username, loki_password, workflow_run_url, github_token):
# Get workflow run.
headers = {
Expand All @@ -113,12 +132,7 @@ def main(loki_endpoint, loki_username, loki_password, workflow_run_url, github_t
if "pull_requests" in workflow_run and workflow_run["pull_requests"]:
branch = workflow_run["pull_requests"][0]["base"]["ref"]

streams = []
for job in jobs["jobs"]:
streams.append(job_to_stream(workflow_run, job, branch))
for step in job["steps"]:
streams.append(step_to_stream(workflow_run, job, step, branch))

streams = jobs_to_stream(workflow_run, jobs, branch)
streams.append(workflow_run_to_stream(workflow_run, branch))

body = {"streams": streams}
Expand Down
14 changes: 14 additions & 0 deletions test_push.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import json
from unittest import TestCase
from push import jobs_to_stream


class Test(TestCase):
def test_jobs_to_stream(self):
with open("testdata/run.json") as run_json, open(
"testdata/jobs.json"
) as jobs_json:
run = json.load(run_json)
jobs = json.load(jobs_json)
# Make sure it can handle null timestamps
jobs_to_stream(run, jobs, "main")

0 comments on commit e871295

Please sign in to comment.