Skip to content

Commit d3ca47b

Browse files
committed
fix(admin): cleaned up app admin
1 parent 2071bbe commit d3ca47b

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

controller/sentry/admin.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Admin."""
2+
from datetime import timedelta
23
from typing import TYPE_CHECKING, Optional
34

45
from admin_action_tools import (
@@ -177,7 +178,7 @@ class AppAdmin(
177178
"reference",
178179
"get_event_status",
179180
"get_project",
180-
"last_seen",
181+
"get_active_status",
181182
"default_sample_rate",
182183
"active_sample_rate",
183184
"active_window_end",
@@ -258,6 +259,19 @@ def get_event_status(self, obj: App) -> str:
258259
return format_html(text, "red", "Yes")
259260
return format_html(text, "gray", "Pending")
260261

262+
@admin.display(description="Active", boolean=True)
263+
def get_active_status(self, obj: App) -> str:
264+
"""This method return the status of the app based on last_seen.
265+
266+
Args:
267+
obj (App): The app
268+
269+
Returns:
270+
bool: is active
271+
"""
272+
half_hour_mark = timezone.now() - timedelta(minutes=30)
273+
return obj.last_seen is not None and obj.last_seen > half_hour_mark
274+
261275
def get_changelist_actions(self, request: "HttpRequest") -> list[str]:
262276
"""This method return allowed changelist actions.
263277
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Generated by Django 4.1.6 on 2023-02-17 16:08
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("sentry", "0013_project_last_event"),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name="app",
15+
name="celery_collect_metrics",
16+
field=models.BooleanField(default=False, verbose_name="celery metrics"),
17+
),
18+
migrations.AlterField(
19+
model_name="app",
20+
name="wsgi_collect_metrics",
21+
field=models.BooleanField(default=False, verbose_name="wsgi metrics"),
22+
),
23+
]

controller/sentry/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Project(models.Model):
3939

4040
def __str__(self) -> str:
4141
"""Return Project string."""
42-
return f"Project({self.sentry_id} - {self.sentry_project_slug if self.sentry_project_slug else 'Pending'})"
42+
return f"Project({self.sentry_project_slug if self.sentry_project_slug else 'Pending'})"
4343

4444

4545
class Event(models.Model):
@@ -78,7 +78,7 @@ class App(models.Model):
7878
blank=True,
7979
default=partial(settings_default_value, "DEFAULT_WSGI_IGNORE_PATHS"),
8080
)
81-
wsgi_collect_metrics = models.BooleanField(default=False)
81+
wsgi_collect_metrics = models.BooleanField(default=False, verbose_name="wsgi metrics")
8282
wsgi_metrics = models.JSONField(null=True, blank=True)
8383

8484
# celery
@@ -87,7 +87,7 @@ class App(models.Model):
8787
blank=True,
8888
default=partial(settings_default_value, "DEFAULT_CELERY_IGNORE_TASKS"),
8989
)
90-
celery_collect_metrics = models.BooleanField(default=False)
90+
celery_collect_metrics = models.BooleanField(default=False, verbose_name="celery metrics")
9191
celery_metrics = models.JSONField(null=True, blank=True)
9292

9393
def __str__(self) -> str:

controller/sentry/tests/test_admin.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from datetime import timedelta
12
from unittest.mock import Mock, patch
23

34
import pytest
@@ -320,6 +321,21 @@ def test_app_get_event_status(admin_with_user):
320321
assert site.get_event_status(app) == '<b style="color:red;">Yes</b>'
321322

322323

324+
@pytest.mark.django_db
325+
@pytest.mark.parametrize("user_group", ["Developer"])
326+
@pytest.mark.admin_site(model_class=App)
327+
def test_app_get_app_status(admin_with_user):
328+
site, request = admin_with_user
329+
app = App(reference="abc", last_seen=timezone.now())
330+
assert site.get_active_status(app) == True
331+
332+
app = App(reference="abc", last_seen=None)
333+
assert site.get_active_status(app) == False
334+
335+
app = App(reference="abc", last_seen=timezone.now() - timedelta(minutes=60))
336+
assert site.get_active_status(app) == False
337+
338+
323339
@pytest.mark.parametrize("user_group", ["Developer"])
324340
@pytest.mark.admin_site(model_class=Project)
325341
def test_project_event_inlines(admin_with_user):

0 commit comments

Comments
 (0)