Skip to content

Commit

Permalink
Fix status report period (#1810)
Browse files Browse the repository at this point in the history
* Fix status report period

* Update tests and add a basic status_report one
  • Loading branch information
Twixes authored Oct 7, 2020
1 parent 0ced04c commit 34ad57c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
26 changes: 14 additions & 12 deletions posthog/tasks/status_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion posthog/tasks/test/test_process_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions posthog/tasks/test/test_status_report.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion posthog/tasks/test/test_webhook_message_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions posthog/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(
Expand Down

0 comments on commit 34ad57c

Please sign in to comment.