예제 #1
0
def test_smoothing_intervals_copes_with_null_values(client: Client):
    """
    The Smoothing feature should allow specifying a number of intervals over
    which we will provide smoothing of the aggregated trend data.
    """
    organization = create_organization(name="test org")
    team = create_team(organization=organization)
    user = create_user("user", "pass", organization)

    client.force_login(user)
    cache.clear()

    with freeze_time("2021-09-20T16:00:00"):
        journeys_for(
            events_by_person={
                "abc": [
                    {
                        "event": "$pageview",
                        "timestamp": "2021-09-01"
                    },
                    {
                        "event": "$pageview",
                        "timestamp": "2021-09-01"
                    },
                    {
                        "event": "$pageview",
                        "timestamp": "2021-09-01"
                    },
                    # No events on 2 Sept
                    {
                        "event": "$pageview",
                        "timestamp": "2021-09-03"
                    },
                    {
                        "event": "$pageview",
                        "timestamp": "2021-09-03"
                    },
                    {
                        "event": "$pageview",
                        "timestamp": "2021-09-03"
                    },
                ]
            },
            team=team,
        )

        interval_3_trend = get_trends_ok(
            client,
            team=team,
            request=TrendsRequest(
                date_from="2021-09-01",
                date_to="2021-09-03",
                interval="day",
                insight="TRENDS",
                display="ActionsLineGraph",
                smoothing_intervals=3,
                events=[{
                    "id": "$pageview",
                    "name": "$pageview",
                    "custom_name": None,
                    "type": "events",
                    "order": 0,
                    "properties": [],
                }],
            ),
        )

        assert interval_3_trend == {
            "is_cached":
            False,
            "last_refresh":
            "2021-09-20T16:00:00Z",
            "next":
            None,
            "result": [{
                "action": ANY,
                "label": "$pageview",
                "count": 6.0,
                "data": [3.0, 1.0, 2.0],
                "labels": ["1-Sep-2021", "2-Sep-2021", "3-Sep-2021"],
                "days": ["2021-09-01", "2021-09-02", "2021-09-03"],
                "persons_urls": ANY,
                "filter": ANY,
            }],
        }

        interval_1_trend = get_trends_ok(
            client,
            team=team,
            request=TrendsRequest(
                date_from="2021-09-01",
                date_to="2021-09-03",
                interval="day",
                insight="TRENDS",
                display="ActionsLineGraph",
                smoothing_intervals=1,
                events=[{
                    "id": "$pageview",
                    "name": "$pageview",
                    "custom_name": None,
                    "type": "events",
                    "order": 0,
                    "properties": [],
                }],
            ),
        )

        assert interval_1_trend == {
            "is_cached":
            False,
            "last_refresh":
            "2021-09-20T16:00:00Z",
            "next":
            None,
            "result": [{
                "action": ANY,
                "label": "$pageview",
                "count": 6.0,
                "data": [3.0, 0.0, 3.0],
                "labels": ["1-Sep-2021", "2-Sep-2021", "3-Sep-2021"],
                "days": ["2021-09-01", "2021-09-02", "2021-09-03"],
                "persons_urls": ANY,
                "filter": ANY,
            }],
        }
예제 #2
0
def test_insight_retention_missing_persons_gh_5443(client: Client):
    """
    This is a regression test for GH-5443.

    The scenario here is that, an api request is being made for person retention, specifically for:

      1. a "Week" period is being requested
      2. events just over a week from the first event for a user

    """

    organization = create_organization(name="test org")
    team = create_team(organization=organization)
    user = create_user("user", "pass", organization)

    identify(distinct_id="abc", team_id=team.id)

    #  This event will be the first event for the Person wrt the retention
    #  period
    capture_event(
        event=EventData(
            event="event_name", team_id=team.id, distinct_id="abc", timestamp=datetime(2021, 3, 29), properties={},
        )
    )

    # Create an event for just over a week from the initial identify event
    capture_event(
        event=EventData(
            event="event_name", team_id=team.id, distinct_id="abc", timestamp=datetime(2021, 4, 5), properties={},
        )
    )

    client.force_login(user)

    # These params are taken from
    # https://sentry.io/organizations/posthog/issues/2516393859/events/df790b8837a54051a140aa1fee51adfc/?project=1899813
    response = get_retention(
        client=client,
        events=[
            {
                "id": "$pageview",
                "math": None,
                "name": "$pageview",
                "type": "events",
                "order": 0,
                "properties": [],
                "math_property": None,
            }
        ],
        date_from="-90d",
        date_to="2021-03-31T18:22:50.579Z",
        display="ActionsTable",
        selected_interval=10,
        total_intervals=11,
        insight="RETENTION",
        period="Week",
        retention_type="retention_first_time",
        target_entity={"id": "event_name", "name": "event_name", "type": "events", "order": 0},
        returning_entity={
            "id": "event_name",
            "math": None,
            "name": "event_name",
            "type": "events",
            "order": None,
            "properties": [],
            "math_property": None,
        },
    )

    assert response.status_code == 200, response.content
    data = response.json()

    # NOTE: prior to the fix for GH-5443, this test would fail by returning an
    # empty list. To "fix" I have make the generation of "appearances" more
    # forgiving of getting too much data from the clickhouse query.
    assert data["result"] == [
        {
            "appearances": [1],
            "person": {
                "created_at": "2021-08-03T00:00:00Z",
                "distinct_ids": ["abc"],
                "id": ANY,
                "is_identified": False,
                "name": "abc",
                "properties": {},
                "uuid": ANY,
            },
        },
    ]
예제 #3
0
def test_includes_only_intervals_within_range(client: Client):
    """
    This is the case highlighted by https://github.com/PostHog/posthog/issues/2675

    Here the issue is that we request, for instance, 14 days as the
    date_from, display at weekly intervals but previously we
    were displaying 4 ticks on the date axis. If we were exactly on the
    beginning of the week for two weeks then we'd want 2 ticks.
    Otherwise we would have 3 ticks as the range would be intersecting
    with three weeks. We should never need to display 4 ticks.
    """
    organization = create_organization(name="test org")
    team = create_team(organization=organization)
    user = create_user("user", "pass", organization)

    client.force_login(user)
    cache.clear()

    #  I'm creating a cohort here so that I can use as a breakdown, just because
    #  this is what was used demonstrated in
    #  https://github.com/PostHog/posthog/issues/2675 but it might not be the
    #  simplest way to reproduce

    # "2021-09-19" is a sunday, i.e. beginning of week
    with freeze_time("2021-09-20T16:00:00"):
        #  First identify as a member of the cohort
        distinct_id = "abc"
        update_or_create_person(distinct_ids=[distinct_id],
                                team_id=team.id,
                                properties={"cohort_identifier": 1})
        cohort = create_cohort_ok(client=client,
                                  team_id=team.id,
                                  name="test cohort",
                                  groups=[{
                                      "properties": {
                                          "cohort_identifier": 1
                                      }
                                  }])

        journeys_for(
            events_by_person={
                distinct_id: [
                    {
                        "event": "$pageview",
                        "timestamp": "2021-09-04"
                    },
                    {
                        "event": "$pageview",
                        "timestamp": "2021-09-05"
                    },
                    {
                        "event": "$pageview",
                        "timestamp": "2021-09-12"
                    },
                    {
                        "event": "$pageview",
                        "timestamp": "2021-09-19"
                    },
                ]
            },
            team=team,
        )

        trends = get_trends_ok(
            client,
            team=team,
            request=TrendsRequestBreakdown(
                date_from="-14days",
                date_to="2021-09-21",
                interval="week",
                insight="TRENDS",
                breakdown=json.dumps([cohort["id"]]),
                breakdown_type="cohort",
                display="ActionsLineGraph",
                events=[{
                    "id": "$pageview",
                    "math": "dau",
                    "name": "$pageview",
                    "custom_name": None,
                    "type": "events",
                    "order": 0,
                    "properties": [],
                    "math_property": None,
                }],
            ),
        )
        assert trends == {
            "is_cached":
            False,
            "last_refresh":
            "2021-09-20T16:00:00Z",
            "next":
            None,
            "result": [{
                "action": ANY,
                "breakdown_value": cohort["id"],
                "label": "$pageview - test cohort",
                "count": 3.0,
                "data": [1.0, 1.0, 1.0],
                # Prior to the fix this would also include '29-Aug-2021'
                "labels": ["5-Sep-2021", "12-Sep-2021", "19-Sep-2021"],
                "days": ["2021-09-05", "2021-09-12", "2021-09-19"],
                "persons_urls": ANY,
                "filter": ANY,
            }],
        }