admin: add metrics and charts
This commit is contained in:
		| @ -3,6 +3,8 @@ from typing import Union | ||||
|  | ||||
| from django.conf import settings | ||||
| from django.core.cache import cache | ||||
| from django.db.models import Count | ||||
| from django.db.models.fields.json import KeyTextTransform | ||||
| from django.shortcuts import redirect, reverse | ||||
| from django.views.generic import TemplateView | ||||
| from packaging.version import LegacyVersion, Version, parse | ||||
| @ -10,10 +12,9 @@ from packaging.version import LegacyVersion, Version, parse | ||||
| from passbook import __version__ | ||||
| from passbook.admin.mixins import AdminRequiredMixin | ||||
| from passbook.admin.tasks import VERSION_CACHE_KEY, update_latest_version | ||||
| from passbook.core.models import Application, Provider, Source, User | ||||
| from passbook.flows.models import Flow, Stage | ||||
| from passbook.audit.models import Event, EventAction | ||||
| from passbook.core.models import Provider, User | ||||
| from passbook.policies.models import Policy | ||||
| from passbook.stages.invitation.models import Invitation | ||||
|  | ||||
|  | ||||
| class AdministrationOverviewView(AdminRequiredMixin, TemplateView): | ||||
| @ -37,17 +38,27 @@ class AdministrationOverviewView(AdminRequiredMixin, TemplateView): | ||||
|             return parse(__version__) | ||||
|         return parse(version_in_cache) | ||||
|  | ||||
|     def get_most_used_applications(self): | ||||
|         """Get Most used applications, total login counts and unique users that have used them.""" | ||||
|         return ( | ||||
|             Event.objects.filter(action=EventAction.AUTHORIZE_APPLICATION) | ||||
|             .exclude(context__authorized_application=None) | ||||
|             .annotate(application=KeyTextTransform("authorized_application", "context")) | ||||
|             .annotate(user_pk=KeyTextTransform("pk", "user")) | ||||
|             .values("application") | ||||
|             .annotate(total_logins=Count("application")) | ||||
|             .annotate(unique_users=Count("user_pk", distinct=True)) | ||||
|             .values("unique_users", "application", "total_logins") | ||||
|             .order_by("-total_logins")[:15] | ||||
|         ) | ||||
|  | ||||
|     def get_context_data(self, **kwargs): | ||||
|         kwargs["application_count"] = len(Application.objects.all()) | ||||
|         kwargs["policy_count"] = len(Policy.objects.all()) | ||||
|         kwargs["user_count"] = len(User.objects.all()) - 1  # Remove anonymous user | ||||
|         kwargs["provider_count"] = len(Provider.objects.all()) | ||||
|         kwargs["source_count"] = len(Source.objects.all()) | ||||
|         kwargs["stage_count"] = len(Stage.objects.all()) | ||||
|         kwargs["flow_count"] = len(Flow.objects.all()) | ||||
|         kwargs["invitation_count"] = len(Invitation.objects.all()) | ||||
|         kwargs["version"] = parse(__version__) | ||||
|         kwargs["version_latest"] = self.get_latest_version() | ||||
|         kwargs["most_used_applications"] = self.get_most_used_applications() | ||||
|         kwargs["providers_without_application"] = Provider.objects.filter( | ||||
|             application=None | ||||
|         ) | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Jens Langhammer
					Jens Langhammer