55from django .shortcuts import get_object_or_404
66
77from django_filters .rest_framework import DjangoFilterBackend
8+ from loginas .utils import is_impersonated_session
89from pydantic import BaseModel
910from rest_framework import status , viewsets
1011from rest_framework .exceptions import Throttled , ValidationError
4041from posthog .hogql_queries .hogql_query_runner import HogQLQueryRunner
4142from posthog .hogql_queries .query_runner import BLOCKING_EXECUTION_MODES
4243from posthog .models import User
44+ from posthog .models .activity_logging .activity_log import Detail , changes_between , log_activity
4345from posthog .rate_limit import APIQueriesBurstThrottle , APIQueriesSustainedThrottle
4446from posthog .schema_migrations .upgrade import upgrade
4547from posthog .types import InsightQueryNode
@@ -156,6 +158,18 @@ def create(self, request: Request, *args, **kwargs) -> Response:
156158 is_active = data .is_active if data .is_active is not None else True ,
157159 )
158160
161+ # Activity log: created
162+ log_activity (
163+ organization_id = self .organization .id ,
164+ team_id = self .team .id ,
165+ user = cast (User , request .user ),
166+ was_impersonated = is_impersonated_session (request ),
167+ item_id = str (endpoint .id ),
168+ scope = "Endpoint" ,
169+ activity = "created" ,
170+ detail = Detail (name = endpoint .name ),
171+ )
172+
159173 return Response (
160174 {
161175 "id" : str (endpoint .id ),
@@ -183,6 +197,11 @@ def create(self, request: Request, *args, **kwargs) -> Response:
183197 def update (self , request : Request , name = None , * args , ** kwargs ) -> Response :
184198 """Update an existing endpoint."""
185199 endpoint = get_object_or_404 (Endpoint , team = self .team , name = name )
200+ # Capture a snapshot for diffing
201+ try :
202+ before_update = Endpoint .objects .get (pk = endpoint .id )
203+ except Endpoint .DoesNotExist :
204+ before_update = None
186205
187206 upgraded_query = upgrade (request .data )
188207 data = self .get_model (upgraded_query , EndpointRequest )
@@ -200,6 +219,19 @@ def update(self, request: Request, name=None, *args, **kwargs) -> Response:
200219
201220 endpoint .save ()
202221
222+ # Activity log: updated with field diffs
223+ changes = changes_between ("Endpoint" , previous = before_update , current = endpoint )
224+ log_activity (
225+ organization_id = self .organization .id ,
226+ team_id = self .team .id ,
227+ user = cast (User , request .user ),
228+ was_impersonated = is_impersonated_session (request ),
229+ item_id = str (endpoint .id ),
230+ scope = "Endpoint" ,
231+ activity = "updated" ,
232+ detail = Detail (name = endpoint .name , changes = changes ),
233+ )
234+
203235 return Response (
204236 {
205237 "id" : str (endpoint .id ),
0 commit comments