|
@@ -1,10 +1,12 @@
|
|
|
from __future__ import annotations
|
|
|
|
|
|
-from django.db import IntegrityError
|
|
|
+import datetime
|
|
|
|
|
|
-from sentry.models import AuditLogEntry, OutboxCategory, OutboxScope, RegionOutbox, UserIP
|
|
|
+from django.db import IntegrityError, router
|
|
|
+
|
|
|
+from sentry.models import AuditLogEntry, OutboxCategory, OutboxScope, RegionOutbox, User, UserIP
|
|
|
from sentry.services.hybrid_cloud.log import AuditLogEvent, LogService, UserIpEvent
|
|
|
-from sentry.utils import metrics
|
|
|
+from sentry.silo import unguarded_write
|
|
|
|
|
|
|
|
|
class DatabaseBackedLogService(LogService):
|
|
@@ -23,7 +25,7 @@ class DatabaseBackedLogService(LogService):
|
|
|
raise
|
|
|
|
|
|
def record_user_ip(self, *, event: UserIpEvent) -> None:
|
|
|
- updated, created = UserIP.objects.create_or_update(
|
|
|
+ UserIP.objects.create_or_update(
|
|
|
user_id=event.user_id,
|
|
|
ip_address=event.ip_address,
|
|
|
values=dict(
|
|
@@ -32,12 +34,13 @@ class DatabaseBackedLogService(LogService):
|
|
|
region_code=event.region_code,
|
|
|
),
|
|
|
)
|
|
|
- if not created and not updated:
|
|
|
- # This happens when there is an integrity error adding the UserIP -- such as when user is deleted,
|
|
|
- # or the ip address does not match the db validation. This is expected and not an error condition
|
|
|
- # in low quantities.
|
|
|
- # TODO: Break the foreign key and simply remove this code path.
|
|
|
- metrics.incr("hybrid_cloud.audit_log.user_ip_event.stale_event")
|
|
|
+ with unguarded_write(router.db_for_write(User)):
|
|
|
+ # It greatly simplifies testing not to be too aggressive on updating the last_active due to many
|
|
|
+ # comparisons with serializers.
|
|
|
+ User.objects.filter(
|
|
|
+ id=event.user_id,
|
|
|
+ last_active__lt=(event.last_seen - datetime.timedelta(minutes=1)),
|
|
|
+ ).update(last_active=event.last_seen)
|
|
|
|
|
|
def find_last_log(
|
|
|
self, *, organization_id: int | None, target_object_id: int | None, event: int | None
|