-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cohorts): add activity logs (#21805)
- Loading branch information
1 parent
5f119af
commit 0ea5d6a
Showing
9 changed files
with
286 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import '../../lib/components/Cards/InsightCard/InsightCard.scss' | ||
|
||
import { | ||
ActivityLogItem, | ||
defaultDescriber, | ||
HumanizedChange, | ||
userNameForLogItem, | ||
} from 'lib/components/ActivityLog/humanizeActivity' | ||
import { Link } from 'lib/lemon-ui/Link' | ||
import { urls } from 'scenes/urls' | ||
|
||
const nameOrLinkToCohort = (id?: string | null, name?: string | null): string | JSX.Element => { | ||
const displayName = name || '(empty string)' | ||
return id ? <Link to={urls.cohort(id)}>{displayName}</Link> : displayName | ||
} | ||
|
||
export function cohortActivityDescriber(logItem: ActivityLogItem, asNotification?: boolean): HumanizedChange { | ||
if (logItem.scope != 'Cohort') { | ||
console.error('cohort describer received a non-cohort activity') | ||
return { description: null } | ||
} | ||
|
||
if (logItem.activity == 'created') { | ||
return { | ||
description: ( | ||
<> | ||
<strong>{userNameForLogItem(logItem)}</strong> created the cohort:{' '} | ||
{nameOrLinkToCohort(logItem?.item_id, logItem?.detail.name)} | ||
</> | ||
), | ||
} | ||
} | ||
|
||
if (logItem.activity == 'deleted') { | ||
return { | ||
description: ( | ||
<> | ||
<strong>{userNameForLogItem(logItem)}</strong> deleted the cohort: {logItem.detail.name} | ||
</> | ||
), | ||
} | ||
} | ||
|
||
if (logItem.activity == 'updated') { | ||
return { | ||
description: ( | ||
<> | ||
<strong>{userNameForLogItem(logItem)}</strong> updated the cohort:{' '} | ||
{nameOrLinkToCohort(logItem?.item_id, logItem?.detail.name)} | ||
</> | ||
), | ||
} | ||
} | ||
return defaultDescriber(logItem, asNotification, nameOrLinkToCohort(logItem?.detail.short_id)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import json | ||
from datetime import datetime, timedelta | ||
from typing import Any | ||
from typing import Optional, Any | ||
from unittest import mock | ||
from unittest.mock import patch | ||
|
||
from django.core.files.uploadedfile import SimpleUploadedFile | ||
|
@@ -34,6 +35,31 @@ class TestCohort(TestExportMixin, ClickhouseTestMixin, APIBaseTest, QueryMatchin | |
def capture_select_queries(self): | ||
return self.capture_queries(("INSERT INTO cohortpeople", "SELECT", "ALTER", "select", "DELETE")) | ||
|
||
def _get_cohort_activity( | ||
self, | ||
flag_id: Optional[int] = None, | ||
team_id: Optional[int] = None, | ||
expected_status: int = status.HTTP_200_OK, | ||
): | ||
if team_id is None: | ||
team_id = self.team.id | ||
|
||
if flag_id: | ||
url = f"/api/projects/{team_id}/cohorts/{flag_id}/activity" | ||
else: | ||
url = f"/api/projects/{team_id}/cohorts/activity" | ||
|
||
activity = self.client.get(url) | ||
self.assertEqual(activity.status_code, expected_status) | ||
return activity.json() | ||
|
||
def assert_cohort_activity(self, cohort_id: Optional[int], expected: list[dict]): | ||
activity_response = self._get_cohort_activity(cohort_id) | ||
|
||
activity: list[dict] = activity_response["results"] | ||
self.maxDiff = None | ||
assert activity == expected | ||
|
||
@patch("posthog.api.cohort.report_user_action") | ||
@patch("posthog.tasks.calculate_cohort.calculate_cohort_ch.delay", side_effect=calculate_cohort_ch) | ||
@patch("posthog.models.cohort.util.sync_execute", side_effect=sync_execute) | ||
|
@@ -281,6 +307,96 @@ def test_cohort_list(self): | |
self.assertEqual(response["results"][0]["name"], "whatever") | ||
self.assertEqual(response["results"][0]["created_by"]["id"], self.user.id) | ||
|
||
def test_cohort_activity_log(self): | ||
self.team.app_urls = ["http://somewebsite.com"] | ||
self.team.save() | ||
Person.objects.create(team=self.team, properties={"prop": 5}) | ||
Person.objects.create(team=self.team, properties={"prop": 6}) | ||
|
||
self.client.post( | ||
f"/api/projects/{self.team.id}/cohorts", | ||
data={"name": "whatever", "groups": [{"properties": {"prop": 5}}]}, | ||
) | ||
|
||
cohort = Cohort.objects.filter(team=self.team).last() | ||
assert cohort is not None | ||
|
||
self.assert_cohort_activity( | ||
cohort_id=cohort.pk, | ||
expected=[ | ||
{ | ||
"user": {"first_name": "", "email": "[email protected]"}, | ||
"activity": "created", | ||
"scope": "Cohort", | ||
"item_id": str(cohort.pk), | ||
"detail": {"changes": None, "trigger": None, "name": "whatever", "short_id": None, "type": None}, | ||
"created_at": mock.ANY, | ||
} | ||
], | ||
) | ||
|
||
self.client.patch( | ||
f"/api/projects/{self.team.id}/cohorts/{cohort.pk}", | ||
data={"name": "woohoo", "groups": [{"properties": {"prop": 6}}]}, | ||
) | ||
cohort.refresh_from_db() | ||
assert cohort.name == "woohoo" | ||
|
||
self.assert_cohort_activity( | ||
cohort_id=cohort.pk, | ||
expected=[ | ||
{ | ||
"user": {"first_name": "", "email": "[email protected]"}, | ||
"activity": "updated", | ||
"scope": "Cohort", | ||
"item_id": str(cohort.pk), | ||
"detail": { | ||
"changes": [ | ||
{ | ||
"type": "Cohort", | ||
"action": "changed", | ||
"field": "name", | ||
"before": "whatever", | ||
"after": "woohoo", | ||
}, | ||
{ | ||
"type": "Cohort", | ||
"action": "changed", | ||
"field": "groups", | ||
"before": [ | ||
{ | ||
"days": None, | ||
"count": None, | ||
"label": None, | ||
"end_date": None, | ||
"event_id": None, | ||
"action_id": None, | ||
"properties": [{"key": "prop", "type": "person", "value": 5}], | ||
"start_date": None, | ||
"count_operator": None, | ||
} | ||
], | ||
"after": [{"properties": [{"key": "prop", "type": "person", "value": 6}]}], | ||
}, | ||
], | ||
"trigger": None, | ||
"name": "woohoo", | ||
"short_id": None, | ||
"type": None, | ||
}, | ||
"created_at": mock.ANY, | ||
}, | ||
{ | ||
"user": {"first_name": "", "email": "[email protected]"}, | ||
"activity": "created", | ||
"scope": "Cohort", | ||
"item_id": str(cohort.pk), | ||
"detail": {"changes": None, "trigger": None, "name": "whatever", "short_id": None, "type": None}, | ||
"created_at": mock.ANY, | ||
}, | ||
], | ||
) | ||
|
||
def test_csv_export_new(self): | ||
# Test 100s of distinct_ids, we only want ~10 | ||
Person.objects.create( | ||
|
Oops, something went wrong.