Skip to content

Commit

Permalink
make visitor click info admin views faster
Browse files Browse the repository at this point in the history
  • Loading branch information
devxpy committed Oct 17, 2023
1 parent 3dfc05f commit 9a40d85
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
10 changes: 8 additions & 2 deletions gooeysite/custom_filters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import typing

from django.db import DataError
from django.db.models import F, Func, QuerySet, Count
Expand All @@ -7,7 +8,10 @@


def json_field_nested_lookup_keys(
qs: QuerySet, field: str, max_depth: int = 3
qs: QuerySet,
field: str,
max_depth: int = 3,
exclude_keys: typing.Iterable[str] = (),
) -> list[str]:
nested_keys = [field]
for _ in range(max_depth):
Expand All @@ -23,6 +27,7 @@ def json_field_nested_lookup_keys(
.distinct()
.values_list("keys", flat=True)
)
if not child in exclude_keys
)
except DataError:
next_keys.append(parent)
Expand All @@ -36,6 +41,7 @@ def related_json_field_summary(
qs: QuerySet = None,
query_param: str = None,
instance_id: int = None,
exclude_keys: typing.Iterable[str] = (),
):
if query_param is None:
try:
Expand All @@ -56,7 +62,7 @@ def related_json_field_summary(
if qs is None:
qs = manager.all()

nested_keys = json_field_nested_lookup_keys(qs, field)
nested_keys = json_field_nested_lookup_keys(qs, field, exclude_keys=exclude_keys)

results = {
key.split(field + "__")[-1]: [
Expand Down
33 changes: 24 additions & 9 deletions url_shortener/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
)
from url_shortener import models

JSON_FIELDS = ["browser", "device", "os", "ip_data"]

EXCLUDE_KEYS = {
"version",
"version_string",
"ip",
"asn_start",
"asn_end",
"asn_code",
}


@admin.register(models.ShortenedURL)
class ShortenedURLAdmin(admin.ModelAdmin):
Expand Down Expand Up @@ -66,8 +77,10 @@ def view_visitors(self, obj: models.ShortenedURL):
@admin.display(description="Visitor Summary")
def view_visitor_summary(self, surl: models.ShortenedURL):
html = ""
for field in ["browser", "device", "os", "ip_data"]:
results = related_json_field_summary(surl.visitors, field)
for field in JSON_FIELDS:
results = related_json_field_summary(
surl.visitors, field, exclude_keys=EXCLUDE_KEYS
)
html += "<h2>" + field.replace("_", " ").capitalize() + "</h2>"
html += loader.render_to_string(
"anaylsis_result.html", context=dict(results=results)
Expand All @@ -76,14 +89,19 @@ def view_visitor_summary(self, surl: models.ShortenedURL):
return html


def jsonfieldlistfilter(field: str):
def jsonfieldlistfilter(
field: str,
exclude_keys=EXCLUDE_KEYS,
):
class JSONFieldListFilter(admin.SimpleListFilter):
title = field.replace("_", " ").capitalize()
parameter_name = field

def lookups(self, request, model_admin):
qs = model_admin.model.objects.all()
lookups = json_field_nested_lookup_keys(qs, field)
lookups = json_field_nested_lookup_keys(
qs, field, exclude_keys=exclude_keys
)
return [
(
json.dumps([k, v]),
Expand All @@ -106,12 +124,8 @@ def queryset(self, request, queryset):
@admin.register(models.VisitorClickInfo)
class VisitorClickInfoAdmin(admin.ModelAdmin):
list_filter = [
jsonfieldlistfilter("browser"),
jsonfieldlistfilter("device"),
jsonfieldlistfilter("os"),
jsonfieldlistfilter("ip_data"),
"created_at",
]
] + [jsonfieldlistfilter(name) for name in JSON_FIELDS]
search_fields = ["ip_address", "user_agent", "ip_data"] + [
f"shortened_url__{field}" for field in ShortenedURLAdmin.search_fields
]
Expand All @@ -122,4 +136,5 @@ class VisitorClickInfoAdmin(admin.ModelAdmin):
"created_at",
]
ordering = ["created_at"]
autocomplete_fields = ["shortened_url"]
actions = [export_to_csv, export_to_excel]

0 comments on commit 9a40d85

Please sign in to comment.