8000 feat(project-tree): activity log by mariusandra · Pull Request #33303 · PostHog/posthog · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(project-tree): activity log #33303

New issue 8000

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
8000
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10000
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
AND "posthog_filesystem"."type" = 'session_recording_playlist'
AND NOT ("posthog_filesystem"."shortcut"
AND "posthog_filesystem"."shortcut" IS NOT NULL))
ORDER BY "posthog_filesystem"."id" ASC
LIMIT 1
'''
# ---
# name: TestRecordingsThatMatchPlaylistFilters.test_sorts_nulls_first_and_then_least_recently_counted.1
Expand All @@ -43,7 +45,7 @@
AND "posthog_filesystem"."shortcut" IS NOT NULL))
'''
# ---
# name: TestRecordingsThatMatchPlaylistFilters.test_sorts_nulls_first_and_then_least_recently_counted.2
# name: TestRecordingsThatMatchPlaylistFilters.test_sorts_nulls_first_and_then_least_recently_counted.10
'''
SELECT "posthog_filesystem"."team_id",
"posthog_filesystem"."id",
Expand All @@ -65,7 +67,7 @@
AND "posthog_filesystem"."shortcut" IS NOT NULL))
'''
# ---
# name: TestRecordingsThatMatchPlaylistFilters.test_sorts_nulls_first_and_then_least_recently_counted.3
# name: TestRecordingsThatMatchPlaylistFilters.test_sorts_nulls_first_and_then_least_recently_counted.11
'''
SELECT "posthog_filesystem"."team_id",
"posthog_filesystem"."id",
Expand All @@ -85,9 +87,11 @@
AND "posthog_filesystem"."type" = 'session_recording_playlist'
AND NOT ("posthog_filesystem"."shortcut"
AND "posthog_filesystem"."shortcut" IS NOT NULL))
ORDER BY "posthog_filesystem"."id" ASC
LIMIT 1
'''
# ---
# name: TestRecordingsThatMatchPlaylistFilters.test_sorts_nulls_first_and_then_least_recently_counted.4
# name: TestRecordingsThatMatchPlaylistFilters.test_sorts_nulls_first_and_then_least_recently_counted.12
'''
SELECT COUNT(*)
FROM
Expand All @@ -109,7 +113,7 @@
HAVING COUNT("posthog_sessionrecordingplaylistitem"."id") = 99999) subquery
'''
# ---
# name: TestRecordingsThatMatchPlaylistFilters.test_sorts_nulls_first_and_then_least_recently_counted.5
# name: TestRecordingsThatMatchPlaylistFilters.test_sorts_nulls_first_and_then_least_recently_counted.13
'''
SELECT "posthog_sessionrecordingplaylist"."id"
FROM "posthog_sessionrecordingplaylist"
Expand All @@ -131,3 +135,191 @@
LIMIT 2500
'''
# ---
# name: TestRecordingsThatMatchPlaylistFilters.test_sorts_nulls_first_and_then_least_recently_counted.2
'''
SELECT "posthog_filesystem"."team_id",
"posthog_filesystem"."id",
"posthog_filesystem"."path",
"posthog_filesystem"."depth",
"posthog_filesystem"."type",
"posthog_filesystem"."ref",
"posthog_filesystem"."href",
"posthog_filesystem"."shortcut",
"posthog_filesystem"."meta",
"posthog_filesystem"."created_at",
"posthog_filesystem"."created_by_id",
"posthog_filesystem"."project_id"
FROM "posthog_filesystem"
WHERE ("posthog_filesystem"."ref" = '0001'
AND "posthog_filesystem"."team_id" = 99999
AND "posthog_filesystem"."type" = 'session_recording_playlist'
AND NOT ("posthog_filesystem"."shortcut"
AND "posthog_filesystem"."shortcut" IS NOT NULL))
ORDER BY "posthog_filesystem"."id" ASC
LIMIT 1
'''
# ---
# name: TestRecordingsThatMatchPlaylistFilters.test_sorts_nulls_first_and_then_least_recently_counted.3
'''
SELECT "posthog_filesystem"."team_id",
"posthog_filesystem"."id",
"posthog_filesystem"."path",
"posthog_filesystem"."depth",
"posthog_filesystem"."type",
"posthog_filesystem"."ref",
"posthog_filesystem"."href",
"posthog_filesystem"."shortcut",
"posthog_filesystem"."meta",
"posthog_filesystem"."created_at",
"posthog_filesystem"."created_by_id",
"posthog_filesystem"."project_id"
FROM "posthog_filesystem"
WHERE ("posthog_filesystem"."ref" = '0001'
AND "posthog_filesystem"."team_id" = 99999
AND "posthog_filesystem"."type" = 'session_recording_playlist'
AND NOT ("posthog_filesystem"."shortcut"
AND "posthog_filesystem"."shortcut" IS NOT NULL))
ORDER BY "posthog_filesystem"."id" ASC
LIMIT 1
'''
# ---
# name: TestRecordingsThatMatchPlaylistFilters.test_sorts_nulls_first_and_then_least_recently_counted.4
'''
SELECT "posthog_filesystem"."team_id",
"posthog_filesystem"."id",
"posthog_filesystem"."path",
"posthog_filesystem"."depth",
"posthog_filesystem"."type",
"posthog_filesystem"."ref",
"posthog_filesystem"."href",
"posthog_filesystem"."shortcut",
"posthog_filesystem"."meta",
"posthog_filesystem"."created_at",
"posthog_filesystem"."created_by_id",
"posthog_filesystem"."project_id"
FROM "posthog_filesystem"
WHERE ("posthog_filesystem"."ref" = '0001'
AND "posthog_filesystem"."team_id" = 99999
AND "posthog_filesystem"."type" = 'session_recording_playlist'
AND NOT ("posthog_filesystem"."shortcut"
AND "posthog_filesystem"."shortcut" IS NOT NULL))
'''
# ---
# name: TestRecordingsThatMatchPlaylistFilters.test_sorts_nulls_first_and_then_least_recently_counted.5
'''
SELECT "posthog_filesystem"."team_id",
"posthog_filesystem"."id",
"posthog_filesystem"."path",
"posthog_filesystem"."depth",
"posthog_filesystem"."type",
"posthog_filesystem"."ref",
"posthog_filesystem"."href",
"posthog_filesystem"."shortcut",
"posthog_filesystem"."meta",
"posthog_filesystem"."created_at",
"posthog_filesystem"."created_by_id",
"posthog_filesystem"."project_id"
FROM "posthog_filesystem"
WHERE ("posthog_filesystem"."ref" = '0001'
AND "posthog_filesystem"."team_id" = 99999
AND "posthog_filesystem"."type" = 'session_recording_playlist'
AND NOT ("posthog_filesystem"."shortcut"
AND "posthog_filesystem"."shortcut" IS NOT NULL))
ORDER BY "posthog_filesystem"."id" ASC
LIMIT 1
'''
# ---
# name: TestRecordingsThatMatchPlaylistFilters.test_sorts_nulls_first_and_then_least_recently_counted.6
'''
SELECT "posthog_filesystem"."team_id",
"posthog_filesystem"."id",
"posthog_filesystem"."path",
"posthog_filesystem"."depth",
"posthog_filesystem"."type",
"posthog_filesystem"."ref",
"posthog_filesystem"."href",
"posthog_filesystem"."shortcut",
"posthog_filesystem"."meta",
"posthog_filesystem"."created_at",
"posthog_filesystem"."created_by_id",
"posthog_filesystem"."project_id"
FROM "posthog_filesystem"
WHERE ("posthog_filesystem"."ref" = '0001'
AND "posthog_filesystem"."team_id" = 99999
AND "posthog_filesystem"."type" = 'session_recording_playlist'
AND NOT ("posthog_filesystem"."shortcut"
AND "posthog_filesystem"."shortcut" IS NOT NULL))
ORDER BY "posthog_filesystem"."id" ASC
LIMIT 1
'''
# ---
# name: TestRecordingsThatMatchPlaylistFilters.test_sorts_nulls_first_and_then_least_recently_counted.7
'''
SELECT "posthog_filesystem"."team_id",
"posthog_filesystem"."id",
"posthog_filesystem"."path",
"posthog_filesystem"."depth",
"posthog_filesystem"."type",
"posthog_filesystem"."ref",
"posthog_filesystem"."href",
"posthog_filesystem"."shortcut",
"posthog_filesystem"."meta",
"posthog_filesystem"."created_at",
"posthog_filesystem"."created_by_id",
"posthog_filesystem"."project_id"
FROM "posthog_filesystem"
WHERE ("posthog_filesystem"."ref" = '0001'
AND "posthog_filesystem"."team_id" = 99999
AND "posthog_filesystem"."type" = 'session_recording_playlist'
AND NOT ("posthog_filesystem"."shortcut"
AND "posthog_filesystem"."shortcut" IS NOT NULL))
'''
# ---
# name: TestRecordingsThatMatchPlaylistFilters.test_sorts_nulls_first_and_then_least_recently_counted.8
'''
SELECT "posthog_filesystem"."team_id",
"posthog_filesystem"."id",
"posthog_filesystem"."path",
"posthog_filesystem"."depth",
"posthog_filesystem"."type",
"posthog_filesystem"."ref",
"posthog_filesystem"."href",
"posthog_filesystem"."shortcut",
"posthog_filesystem"."meta",
"posthog_filesystem"."created_at",
"posthog_filesystem"."created_by_id",
"posthog_filesystem"."project_id"
FROM "posthog_filesystem"
WHERE ("posthog_filesystem"."ref" = '0001'
AND &qu 6DB6 ot;posthog_filesystem"."team_id" = 99999
AND "posthog_filesystem"."type" = 'session_recording_playlist'
AND NOT ("posthog_filesystem"."shortcut"
AND "posthog_filesystem"."shortcut" IS NOT NULL))
ORDER BY "posthog_filesystem"."id" ASC
LIMIT 1
'''
# ---
# name: TestRecordingsThatMatchPlaylistFilters.test_sorts_nulls_first_and_then_least_recently_counted.9
'''
SELECT "posthog_filesystem"."team_id",
"posthog_filesystem"."id",
"posthog_filesystem"."path",
"posthog_filesystem"."depth",
"posthog_filesystem"."type",
"posthog_filesystem"."ref",
"posthog_filesystem"."href",
"posthog_filesystem"."shortcut",
"posthog_filesystem"."meta",
"posthog_filesystem"."created_at",
"posthog_filesystem"."created_by_id",
"posthog_filesystem"."project_id"
FROM "posthog_filesystem"
WHERE ("posthog_filesystem"."ref" = '0001'
AND "posthog_filesystem"."team_id" = 99999
AND "posthog_filesystem"."type" = 'session_recording_playlist'
AND NOT ("posthog_filesystem"."shortcut"
AND "posthog_filesystem"."shortcut" IS NOT NULL))
ORDER BY "posthog_filesystem"."id" ASC
LIMIT 1
'''
# ---
1 change: 1 addition & 0 deletions frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4379,6 +4379,7 @@ export enum ActivityScope {
EXPERIMENT = 'Experiment',
SURVEY = 'Survey',
EARLY_ACCESS_FEATURE = 'EarlyAccessFeature',
FILE_SYSTEM = 'FileSystem',
COMMENT = 'Comment',
COHORT = 'Cohort',
TEAM = 'Team',
Expand Down
92 changes: 91 additions & 1 deletion posthog/api/file_system/file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
from rest_framework import filters, serializers, viewsets, pagination, status
from rest_framework.request import Request
from rest_framework.response import Response
import structlog
from loginas.utils import is_impersonated_session
from posthog.models.activity_logging.activity_log import (
Change,
Detail,
changes_between,
log_activity,
)

from posthog.api.utils import action
from posthog.api.routing import TeamAndOrgViewSetMixin
Expand All @@ -19,6 +27,28 @@

HOG_FUNCTION_TYPES = ["broadcast", "campaign", "destination", "site_app", "source", "transformation"]

logger = structlog.get_logger(__name__)


def log_file_system_activity(
*,
activity: str,
file_system: FileSystem,
user: User,
was_impersonated: bool,
changes: Optional[list[Change]] = None,
) -> None:
log_activity(
organization_id=file_system.team.organization_id,
team_id=file_system.team_id,
user=user,
was_impersonated=was_impersonated,
item_id=str(file_system.id),
scope="FileSystem",
activity=activity,
detail=Detail(name=file_system.path, changes=changes, type=file_system.type),
)


class FileSystemSerializer(serializers.ModelSerializer):
class Meta:
Expand All @@ -44,7 +74,21 @@ class Meta:
def update(self, instance: FileSystem, validated_data: dict[str, Any]) -> FileSystem:
if "path" in validated_data:
instance.depth = len(split_path(validated_data["path"]))
return super().update(instance, validated_data)
before_update = FileSystem.objects.get(pk=instance.pk)
updated_instance = super().update(instance, validated_data)
changes = changes_between(
"FileSystem",
previous=before_update,
current=updated_instance,
)
log_file_system_activity(
activity="updated",
file_system=updated_instance,
user=self.context["request"].user,
was_impersonated=is_impersonated_session(self.context["request"]),
changes=changes,
)
return updated_instance

def create(self, validated_data: dict[str, Any], *args: Any, **kwargs: Any) -> FileSystem:
request = self.context["request"]
Expand Down Expand Up @@ -76,6 +120,12 @@ def create(self, validated_data: dict[str, Any], *args: Any, **kwargs: Any) -> F
depth=depth,
**validated_data,
)
log_file_system_activity(
activity="created",
file_system=file_system,
user=request.user,
was_impersonated=is_impersonated_session(request),
)

return file_system

Expand Down Expand Up @@ -328,6 +378,13 @@ def destroy(self, request, *args, **kwargs):
else:
instance.delete()

log_file_system_activity(
activity="deleted",
file_system=instance,
user=request.user,
was_impersonated=is_impersonated_session(request),
)

return Response(status=status.HTTP_204_NO_CONTENT)

@action(methods=["GET"], detail=False)
Expand Down Expand Up @@ -399,6 +456,22 @@ def move(self, request: Request, *args: Any, **kwargs: Any) -> Response:
if first_leftover:
self._assure_parent_folders(first_leftover.path, instance.created_by, first_leftover.team)

log_file_system_activity(
activity="updated",
file_system=instance,
user=cast(User, request.user),
was_impersonated=is_impersonated_session(request),
changes=[
Change(
type="FileSystem",
field="path",
action="changed",
before=old_path,
after=instance.path,
)
],
)

return Response(
FileSystemSerializer(instance).data,
status=status.HTTP_200_OK,
Expand All @@ -407,6 +480,7 @@ def move(self, request: Request, *args: Any, **kwargs: Any) -> Response:
@action(methods=["POST"], detail=True)
def link(self, request: Request, *args: Any, **kwargs: Any) -> Response:
instance = self.get_object()
old_path = instance.path
new_path = request.data.get("new_path")
if not new_path:
return Response({"detail": "new_path is required"}, status=status.HTTP_400_BAD_REQUEST)
Expand Down Expand Up @@ -450,6 +524,22 @@ def link(self, request: Request, *args: Any, **kwargs: Any) -> Response:
instance.shortcut = True
instance.save() # A new instance is created with a new id

log_file_system_activity(
activity="updated",
file_system=instance,
user=cast(User, request.user),
was_impersonated=is_impersonated_session(request),
changes=[
Change(
type="FileSystem",
field="path",
action="changed",
before=old_path,
after=instance.path,
)
],
)

return Response(
FileSystemSerializer(instance).data,
status=status.HTTP_200_OK,
Expand Down
Loading
0