Skip to content

Commit

Permalink
Fix date range filter
Browse files Browse the repository at this point in the history
  • Loading branch information
vrigal committed Nov 13, 2023
1 parent 9a97dce commit a673d1d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
52 changes: 52 additions & 0 deletions tests/test_worker/test_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pytest
from unittest.mock import patch, MagicMock, call
from treeherder.workers.stats import publish_stats
from treeherder.model.models import Push, Job
from django.utils import timezone
from datetime import timedelta


@pytest.mark.django_db
@patch('treeherder.workers.stats.get_stats_client')
def test_publish_stats_nothing_to_do(get_worker_mock, django_assert_num_queries, caplog):
statsd_client = MagicMock()
get_worker_mock.return_value = statsd_client
assert Push.objects.count() == 0
assert Job.objects.count() == 0
with django_assert_num_queries(2):
publish_stats()
assert [(level, message) for _, level, message in caplog.record_tuples] == [
(20, 'Publishing runtime statistics to statsd'),
(20, 'Ingested 0 pushes'),
(20, 'Ingested 0 jobs in total'),
]
assert statsd_client.call_args_list == []


@pytest.mark.django_db
@patch('treeherder.workers.stats.get_stats_client')
def test_publish_stats(
get_worker_mock, eleven_jobs_stored_new_date, django_assert_num_queries, caplog, settings
):
"Test statsd statistics publication task"
settings.CELERY_STATS_PUBLICATION_DELAY = 10
statsd_client = MagicMock()
get_worker_mock.return_value = statsd_client
assert Push.objects.count() == 10
assert Job.objects.count() == 11
Push.objects.update(time=timezone.now() - timedelta(minutes=10))
Job.objects.update(end_time=timezone.now() - timedelta(minutes=10))

with django_assert_num_queries(2):
publish_stats()
assert [(level, message) for _, level, message in caplog.record_tuples] == [
(20, 'Publishing runtime statistics to statsd'),
(20, 'Ingested 10 pushes'),
(20, 'Ingested 11 jobs in total'),
]
assert statsd_client.incr.call_args_list == [
call('push', 10),
call('jobs', 11),
call('jobs_repo.mozilla-central', 11),
call('jobs_state.completed', 11),
]
2 changes: 1 addition & 1 deletion treeherder/workers/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def publish_stats():
# Round the date to the current date range
# This should not overlapse as the beat is set as a relative cron based delay in minutes
end_date = end_date - timedelta(
minutes=end_date.minute - end_date.minute % settings.CELERY_STATS_PUBLICATION_DELAY,
minutes=end_date.minute % settings.CELERY_STATS_PUBLICATION_DELAY,
seconds=end_date.second,
microseconds=end_date.microsecond,
)
Expand Down

0 comments on commit a673d1d

Please sign in to comment.