2021-06-18 00:06:02 +02:00
|
|
|
from typing import Any, Dict, List, Tuple
|
|
|
|
|
|
|
|
from django.conf import settings
|
2023-03-04 01:52:14 +01:00
|
|
|
from django.db.models import QuerySet
|
2021-06-18 00:06:02 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.shortcuts import render
|
|
|
|
|
|
|
|
from analytics.views.activity_common import (
|
|
|
|
format_date_for_activity_reports,
|
|
|
|
get_user_activity_summary,
|
|
|
|
make_table,
|
|
|
|
)
|
|
|
|
from zerver.decorator import require_server_admin
|
2023-12-15 01:16:00 +01:00
|
|
|
from zerver.models import UserActivity, UserProfile
|
|
|
|
from zerver.models.users import get_user_profile_by_id
|
2021-06-18 00:06:02 +02:00
|
|
|
|
|
|
|
if settings.BILLING_ENABLED:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-06-23 20:44:31 +02:00
|
|
|
def get_user_activity_records(
|
|
|
|
user_profile: UserProfile,
|
|
|
|
) -> QuerySet[UserActivity]:
|
2021-06-18 00:06:02 +02:00
|
|
|
fields = [
|
|
|
|
"user_profile__full_name",
|
|
|
|
"query",
|
|
|
|
"client__name",
|
|
|
|
"count",
|
|
|
|
"last_visit",
|
|
|
|
]
|
|
|
|
|
|
|
|
records = UserActivity.objects.filter(
|
2021-10-13 21:16:34 +02:00
|
|
|
user_profile=user_profile,
|
2021-06-18 00:06:02 +02:00
|
|
|
)
|
|
|
|
records = records.order_by("-last_visit")
|
|
|
|
records = records.select_related("user_profile", "client").only(*fields)
|
|
|
|
return records
|
|
|
|
|
|
|
|
|
2022-06-23 20:44:31 +02:00
|
|
|
def raw_user_activity_table(records: QuerySet[UserActivity]) -> str:
|
2021-06-18 00:06:02 +02:00
|
|
|
cols = [
|
|
|
|
"query",
|
|
|
|
"client",
|
|
|
|
"count",
|
|
|
|
"last_visit",
|
|
|
|
]
|
|
|
|
|
2022-06-23 20:44:31 +02:00
|
|
|
def row(record: UserActivity) -> List[Any]:
|
2021-06-18 00:06:02 +02:00
|
|
|
return [
|
|
|
|
record.query,
|
|
|
|
record.client.name,
|
|
|
|
record.count,
|
|
|
|
format_date_for_activity_reports(record.last_visit),
|
|
|
|
]
|
|
|
|
|
|
|
|
rows = list(map(row, records))
|
|
|
|
title = "Raw data"
|
|
|
|
return make_table(title, cols, rows)
|
|
|
|
|
|
|
|
|
|
|
|
def user_activity_summary_table(user_summary: Dict[str, Dict[str, Any]]) -> str:
|
|
|
|
rows = []
|
|
|
|
for k, v in user_summary.items():
|
2023-07-22 01:15:10 +02:00
|
|
|
if k in ("name", "user_profile_id"):
|
2021-06-18 00:06:02 +02:00
|
|
|
continue
|
|
|
|
client = k
|
|
|
|
count = v["count"]
|
|
|
|
last_visit = v["last_visit"]
|
|
|
|
row = [
|
|
|
|
format_date_for_activity_reports(last_visit),
|
|
|
|
client,
|
|
|
|
count,
|
|
|
|
]
|
|
|
|
rows.append(row)
|
|
|
|
|
|
|
|
rows = sorted(rows, key=lambda r: r[0], reverse=True)
|
|
|
|
|
|
|
|
cols = [
|
|
|
|
"last_visit",
|
|
|
|
"client",
|
|
|
|
"count",
|
|
|
|
]
|
|
|
|
|
|
|
|
title = "User activity"
|
|
|
|
return make_table(title, cols, rows)
|
|
|
|
|
|
|
|
|
|
|
|
@require_server_admin
|
2021-10-13 21:16:34 +02:00
|
|
|
def get_user_activity(request: HttpRequest, user_profile_id: int) -> HttpResponse:
|
|
|
|
user_profile = get_user_profile_by_id(user_profile_id)
|
|
|
|
records = get_user_activity_records(user_profile)
|
2021-06-18 00:06:02 +02:00
|
|
|
|
|
|
|
data: List[Tuple[str, str]] = []
|
|
|
|
user_summary = get_user_activity_summary(records)
|
|
|
|
content = user_activity_summary_table(user_summary)
|
|
|
|
|
|
|
|
data += [("Summary", content)]
|
|
|
|
|
|
|
|
content = raw_user_activity_table(records)
|
|
|
|
data += [("Info", content)]
|
|
|
|
|
2021-10-13 21:16:34 +02:00
|
|
|
title = user_profile.delivery_email
|
2021-06-18 00:06:02 +02:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"analytics/activity.html",
|
|
|
|
context=dict(data=data, title=title),
|
|
|
|
)
|