diff --git a/posthog/tasks/status_report.py b/posthog/tasks/status_report.py index 64e6cbb86fea1..45b6c071c55a9 100644 --- a/posthog/tasks/status_report.py +++ b/posthog/tasks/status_report.py @@ -13,7 +13,7 @@ logger = logging.getLogger(__name__) -def status_report() -> None: +def status_report(*, dry_run: bool = False) -> Dict[str, Any]: period_start, period_end = get_previous_week() report: Dict[str, Any] = { "posthog_version": VERSION, @@ -46,35 +46,37 @@ def status_report() -> None: sql.SQL( """ SELECT COUNT(DISTINCT person_id) as persons_count - FROM posthog_event JOIN posthog_persondistinctid ON (posthog_event.distinct_id = posthog_persondistinctid.distinct_id) WHERE posthog_event.team_id = %s AND posthog_event.created_at >= %s AND posthog_event.created_at < %s + FROM posthog_event JOIN posthog_persondistinctid ON (posthog_event.distinct_id = posthog_persondistinctid.distinct_id) WHERE posthog_event.team_id = %s AND posthog_event.created_at >= %s AND posthog_event.created_at <= %s """ ), - (team.id, report["period"]["start_inclusive"], report["period"]["end_exclusive"]), + (team.id, report["period"]["start_inclusive"], report["period"]["end_inclusive"]), ) team_report["persons_count_active_in_period"] = cursor.fetchone()[0] cursor.execute( sql.SQL( """ SELECT properties->>'$lib' as lib, COUNT(*) as count - FROM posthog_event WHERE team_id = %s AND created_at >= %s AND created_at < %s GROUP BY lib + FROM posthog_event WHERE team_id = %s AND created_at >= %s AND created_at <= %s GROUP BY lib """ ), - (team.id, report["period"]["start_inclusive"], report["period"]["end_exclusive"]), + (team.id, report["period"]["start_inclusive"], report["period"]["end_inclusive"]), ) team_report["events_count_by_lib"] = {result.lib: result.count for result in namedtuplefetchall(cursor)} cursor.execute( sql.SQL( """ SELECT event as name, COUNT(*) as count - FROM posthog_event WHERE team_id = %s AND created_at >= %s AND created_at < %s GROUP BY name + FROM posthog_event WHERE team_id = %s AND created_at >= %s AND created_at <= %s GROUP BY name """ ), - (team.id, report["period"]["start_inclusive"], report["period"]["end_exclusive"]), + (team.id, report["period"]["start_inclusive"], report["period"]["end_inclusive"]), ) team_report["events_count_by_name"] = {result.name: result.count for result in namedtuplefetchall(cursor)} report["teams"][team.id] = team_report - posthoganalytics.api_key = "sTMFPsFhdP1Ssg" - disabled = posthoganalytics.disabled - posthoganalytics.disabled = False - posthoganalytics.capture(get_machine_id(), "instance status report", report) - posthoganalytics.disabled = disabled + if not dry_run: + posthoganalytics.api_key = "sTMFPsFhdP1Ssg" + disabled = posthoganalytics.disabled + posthoganalytics.disabled = False + posthoganalytics.capture(get_machine_id(), "instance status report", report) + posthoganalytics.disabled = disabled + return report diff --git a/posthog/tasks/test/test_process_event.py b/posthog/tasks/test/test_process_event.py index 42b19274d9e84..17b2c6956ebc6 100644 --- a/posthog/tasks/test/test_process_event.py +++ b/posthog/tasks/test/test_process_event.py @@ -21,7 +21,7 @@ from posthog.tasks.process_event import process_event -class ProcessEvent(BaseTest): +class TestProcessEvent(BaseTest): def test_capture_new_person(self) -> None: user = self._create_user("tim") action1 = Action.objects.create(team=self.team) diff --git a/posthog/tasks/test/test_status_report.py b/posthog/tasks/test/test_status_report.py new file mode 100644 index 0000000000000..fd3f7d9adb2bc --- /dev/null +++ b/posthog/tasks/test/test_status_report.py @@ -0,0 +1,13 @@ +from freezegun import freeze_time + +from posthog.api.test.base import BaseTest +from posthog.models import Dashboard +from posthog.tasks.status_report import status_report + + +class TestStatusReport(BaseTest): + TESTS_API = True + + def test_sttus_report(self) -> None: + with freeze_time("2020-01-04T13:01:01Z"): + status_report(dry_run=True) diff --git a/posthog/tasks/test/test_webhook_message_formatting.py b/posthog/tasks/test/test_webhook_message_formatting.py index 6077fd8172802..768c2ee716065 100644 --- a/posthog/tasks/test/test_webhook_message_formatting.py +++ b/posthog/tasks/test/test_webhook_message_formatting.py @@ -10,7 +10,7 @@ ) -class WebhookMessage(BaseTest): +class TestWebhookMessage(BaseTest): def test_determine_webhook(self) -> None: self.team.slack_incoming_webhook = "https://hooks.slack.com/services/" webhook_type = determine_webhook_type(self.team) diff --git a/posthog/utils.py b/posthog/utils.py index b017d04ae544d..9168476a30584 100644 --- a/posthog/utils.py +++ b/posthog/utils.py @@ -8,6 +8,7 @@ import subprocess import time import uuid +from datetime import date from typing import Any, Dict, List, Optional, Tuple, Union from urllib.parse import urljoin, urlparse @@ -33,17 +34,16 @@ def absolute_uri(url: Optional[str] = None) -> str: return urljoin(settings.SITE_URL.rstrip("/") + "/", url.lstrip("/")) -def get_previous_week(at_date: Optional[datetime.datetime] = None) -> Tuple[datetime.datetime, datetime.datetime]: +def get_previous_week(at: Optional[datetime.datetime] = None) -> Tuple[datetime.datetime, datetime.datetime]: """ - Returns a tuple of datetime objects representing the start and end of the immediate - previous week to the passed date. + Returns a pair of datetimes, representing the start and end of the week preceding to the passed date's week. """ - if not at_date: - at_date = timezone.now() + if not at: + at = timezone.now() period_end: datetime.datetime = datetime.datetime.combine( - at_date - datetime.timedelta(timezone.now().weekday() + 1), datetime.time.max, tzinfo=pytz.UTC, + at - datetime.timedelta(timezone.now().weekday() + 1), datetime.time.max, tzinfo=pytz.UTC, ) # very end of the previous Sunday period_start: datetime.datetime = datetime.datetime.combine(