Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add redis/db access monitoring feature. #106

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions authome/admin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ def _usergroups(self,obj):
return mark_safe("{} ({})".format(result,usergroupnames))
_usergroups.short_description = "User Groups"

def _session_timeout(self,obj):
if not obj :
return "-"
else:
usergroups = models.UserGroup.find_groups(obj.email,cacheable=False)[0]
return models.UserGroup.get_session_timeout(usergroups) or "-"
_session_timeout.short_description = "Session Timeout"



def _usergroupnames(self,obj):
if not obj :
return ""
Expand Down Expand Up @@ -283,12 +293,12 @@ def delete_queryset(self, request, queryset):
self.delete_model(request,o)

class UserAdmin(UserAuthorizationCheckMixin,UserGroupsMixin,DatetimeMixin,CatchModelExceptionMixin,auth.admin.UserAdmin):
list_display = ('username', 'email', 'first_name', 'last_name','is_active', 'is_staff','last_idp','_last_login')
list_display = ('username', 'email', 'first_name', 'last_name','is_active', 'is_staff','_session_timeout','last_idp','_last_login')
list_filter = ( 'is_superuser',)
readonly_fields = ("_last_login","_date_joined","username","first_name","last_name","is_staff","is_superuser","_email","_usergroups","last_idp","_modified")
readonly_fields = ("_last_login","_date_joined","username","first_name","last_name","is_staff","is_superuser","_email","_usergroups","_session_timeout","last_idp","_modified")
fieldsets = (
(None, {'fields': ('_email', )}),
('Personal info', {'fields': ('username','first_name', 'last_name')}),
('Personal info', {'fields': ('username','first_name', 'last_name',"_session_timeout")}),
('Permissions', {
'fields': ('is_active', 'is_staff', 'is_superuser',"_usergroups" ),
}),
Expand Down
127 changes: 121 additions & 6 deletions authome/admin/monitoradmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from . import admin
from .. import models
from ..cache import cache

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -48,6 +49,20 @@ def _avg_time(self,obj):
return round(obj.avg_time,2)
_avg_time.short_description = "Avg Time"

def _redis_avg_time(self,obj):
if not obj or not obj.redis_avg_time:
return ""
else:
return round(obj.redis_avg_time,2)
_redis_avg_time.short_description = "Redis Avg Time"

def _db_avg_time(self,obj):
if not obj or not obj.db_avg_time:
return ""
else:
return round(obj.db_avg_time,2)
_db_avg_time.short_description = "DB Avg Time"

def _domains(self,obj):
if not obj or not obj.domains:
return ""
Expand Down Expand Up @@ -85,14 +100,62 @@ class SSOMethodTrafficDataInline(TrafficDataPropertyMixin,djangoadmin.TabularInl
readonly_fields = ("sso_method","requests","_total_time","_min_time","_max_time","_avg_time","_status","_domains")
fields = readonly_fields

def _domains(self,obj):
if not obj or not obj.domains:
return ""
else:
datas = [(k,v) for k,v in obj.domains.items()]
datas.sort(key=lambda o:((o[1].get("requests") or 0) * -1,o[0]) if isinstance(o[1],dict) else (o[1] * -1,o[0]))
return mark_safe("<pre>{}</pre>".format("\r\n".join(" {} : {}".format(o[0],json.dumps(o[1],sort_keys=True,indent=4) if isinstance(o[1],dict) else o[1]) for o in datas)))
_domains.short_description = "Groups"

if settings.REDIS_TRAFFIC_MONITOR_LEVEL > 0 and settings.DB_TRAFFIC_MONITOR_LEVEL > 0:
list_display_4_cluster = ("_start_time","_cluster","_servers","requests","_min_time","_max_time","_avg_time","redis_requests","_redis_avg_time","db_requests","_db_avg_time","get_remote_sessions","delete_remote_sessions")
list_display = ("_start_time","_cluster","_servers","requests","_min_time","_max_time","_avg_time","redis_requests","_redis_avg_time","db_requests","_db_avg_time")
fields_4_cluster = ("_cluster","_start_time","_end_time","_serverlist","requests","_min_time","_max_time","_avg_time","redis_requests","_redis_avg_time","db_requests","_db_avg_time","get_remote_sessions","delete_remote_sessions","_status","_domains","_batchid")
fields = ("_cluster","_start_time","_end_time","_serverlist","requests","_min_time","_max_time","_avg_time","redis_requests","_redis_avg_time","db_requests","_db_avg_time","_status","_domains","_batchid")
elif settings.REDIS_TRAFFIC_MONITOR_LEVEL > 0:
list_display_4_cluster = ("_start_time","_cluster","_servers","requests","_min_time","_max_time","_avg_time","redis_requests","_redis_avg_time","get_remote_sessions","delete_remote_sessions")
list_display = ("_start_time","_cluster","_servers","requests","_min_time","_max_time","_avg_time","redis_requests","_redis_avg_time")
fields_4_cluster = ("_cluster","_start_time","_end_time","_serverlist","requests","_min_time","_max_time","_avg_time","redis_requests","_redis_avg_time","get_remote_sessions","delete_remote_sessions","_status","_domains","_batchid")
fields = ("_cluster","_start_time","_end_time","_serverlist","requests","_min_time","_max_time","_avg_time","redis_requests","_redis_avg_time","_status","_domains","_batchid")
elif settings.DB_TRAFFIC_MONITOR_LEVEL > 0:
list_display_4_cluster = ("_start_time","_cluster","_servers","requests","_min_time","_max_time","_avg_time","db_requests","_db_avg_time","get_remote_sessions","delete_remote_sessions")
list_display = ("_start_time","_cluster","_servers","requests","_min_time","_max_time","_avg_time","db_requests","_db_avg_time")
fields_4_cluster = ("_cluster","_start_time","_end_time","_serverlist","requests","_min_time","_max_time","_avg_time","db_requests","_db_avg_time","get_remote_sessions","delete_remote_sessions","_status","_domains","_batchid")
fields = ("_cluster","_start_time","_end_time","_serverlist","requests","_min_time","_max_time","_avg_time","db_requests","_db_avg_time","_status","_domains","_batchid")
else:
list_display_4_cluster = ("_start_time","_cluster","_servers","requests","_min_time","_max_time","_avg_time","get_remote_sessions","delete_remote_sessions")
list_display = ("_start_time","_cluster","_servers","requests","_min_time","_max_time","_avg_time")
fields_4_cluster = ("_cluster","_start_time","_end_time","_serverlist","requests","_min_time","_max_time","_avg_time","get_remote_sessions","delete_remote_sessions","_status","_domains","_batchid")
fields = ("_cluster","_start_time","_end_time","_serverlist","requests","_min_time","_max_time","_avg_time","_status","_domains","_batchid")

class TrafficDataAdmin(TrafficDataPropertyMixin,admin.DatetimeMixin,djangoadmin.ModelAdmin):
list_display = ("_start_time","_cluster","_servers","requests","_min_time","_max_time","_avg_time","get_remote_sessions","delete_remote_sessions")
readonly_fields = ("_cluster","_start_time","_end_time","_serverlist","requests","_min_time","_max_time","_avg_time","get_remote_sessions","delete_remote_sessions","_status","_domains","_batchid")
fields = readonly_fields
ordering = ("-start_time","clusterid")
list_filter = ['clusterid']
inlines = [SSOMethodTrafficDataInline]

@property
def list_display(self):
if settings.AUTH2_CLUSTER_ENABLED and cache.auth2_clusters:
return list_display_4_cluster
else:
return list_display

@property
def readonly_fields(self):
if settings.AUTH2_CLUSTER_ENABLED and cache.auth2_clusters:
return fields_4_cluster
else:
return fields

@property
def fields(self):
if settings.AUTH2_CLUSTER_ENABLED and cache.auth2_clusters:
return fields_4_cluster
else:
return fields

def _subreports(self,obj):
if not obj:
return ""
Expand Down Expand Up @@ -143,17 +206,69 @@ class SSOMethodTrafficReportInline(TrafficDataPropertyMixin,djangoadmin.TabularI
readonly_fields = ("sso_method","requests","_total_time","_min_time","_max_time","_avg_time","_status","_domains")
fields = readonly_fields

def _domains(self,obj):
if not obj or not obj.domains:
return ""
else:
datas = [(k,v) for k,v in obj.domains.items()]
datas.sort(key=lambda o:((o[1].get("requests") or 0) * -1,o[0]) if isinstance(o[1],dict) else (o[1] * -1,o[0]))
return mark_safe("<pre>{}</pre>".format("\r\n".join(" {} : {}".format(o[0],json.dumps(o[1],sort_keys=True,indent=4) if isinstance(o[1],dict) else o[1]) for o in datas)))
_domains.short_description = "Groups"

if settings.REDIS_TRAFFIC_MONITOR_LEVEL > 0 and settings.DB_TRAFFIC_MONITOR_LEVEL > 0:
report_list_display_4_cluster = ("_report_type","_start_time","_cluster","requests","_min_time","_max_time","_avg_time","redis_requests","_redis_avg_time","db_requests","_db_avg_time","get_remote_sessions","delete_remote_sessions","_subreports")
report_list_display = ("_report_type","_start_time","_cluster","requests","_min_time","_max_time","_avg_time","redis_requests","_redis_avg_time","db_requests","_db_avg_time","_subreports")

report_fields_4_cluster = ("_cluster","_report_type","_start_time","_end_time","requests","_min_time","_max_time","_avg_time","redis_requests","_redis_avg_time","db_requests","_db_avg_time","get_remote_sessions","delete_remote_sessions","_status","_domains")
report_fields = ("_cluster","_report_type","_start_time","_end_time","requests","_min_time","_max_time","_avg_time","redis_requests","_redis_avg_time","db_requests","_db_avg_time","_status","_domains")
elif settings.REDIS_TRAFFIC_MONITOR_LEVEL > 0:
report_list_display_4_cluster = ("_report_type","_start_time","_cluster","requests","_min_time","_max_time","_avg_time","redis_requests","_redis_avg_time","get_remote_sessions","delete_remote_sessions","_subreports")
report_list_display = ("_report_type","_start_time","_cluster","requests","_min_time","_max_time","_avg_time","redis_requests","_redis_avg_time","_subreports")

report_fields_4_cluster = ("_cluster","_report_type","_start_time","_end_time","requests","_min_time","_max_time","_avg_time","redis_requests","_redis_avg_time","get_remote_sessions","delete_remote_sessions","_status","_domains")
report_fields = ("_cluster","_report_type","_start_time","_end_time","requests","_min_time","_max_time","_avg_time","redis_requests","_redis_avg_time","_status","_domains")
elif settings.DB_TRAFFIC_MONITOR_LEVEL > 0:
report_list_display_4_cluster = ("_report_type","_start_time","_cluster","requests","_min_time","_max_time","_avg_time","db_requests","_db_avg_time","get_remote_sessions","delete_remote_sessions","_subreports")
report_list_display = ("_report_type","_start_time","_cluster","requests","_min_time","_max_time","_avg_time","db_requests","_db_avg_time","_subreports")

report_fields_4_cluster = ("_cluster","_report_type","_start_time","_end_time","requests","_min_time","_max_time","_avg_time","db_requests","_db_avg_time","get_remote_sessions","delete_remote_sessions","_status","_domains")
report_fields = ("_cluster","_report_type","_start_time","_end_time","requests","_min_time","_max_time","_avg_time","db_requests","_db_avg_time","_status","_domains")
else:
report_list_display_4_cluster = ("_report_type","_start_time","_cluster","requests","_min_time","_max_time","_avg_time","get_remote_sessions","delete_remote_sessions","_subreports")
report_list_display = ("_report_type","_start_time","_cluster","requests","_min_time","_max_time","_avg_time","_subreports")

report_fields_4_cluster = ("_cluster","_report_type","_start_time","_end_time","requests","_min_time","_max_time","_avg_time","get_remote_sessions","delete_remote_sessions","_status","_domains")
report_fields = ("_cluster","_report_type","_start_time","_end_time","requests","_min_time","_max_time","_avg_time","_status","_domains")

class TrafficReportAdmin(TrafficDataPropertyMixin,admin.DatetimeMixin,djangoadmin.ModelAdmin):
list_display = ("_report_type","_start_time","_cluster","requests","_min_time","_max_time","_avg_time","get_remote_sessions","delete_remote_sessions","_subreports")
readonly_fields = ("_cluster","_report_type","_start_time","_end_time","requests","_min_time","_max_time","_avg_time","get_remote_sessions","delete_remote_sessions","_status","_domains")
fields = readonly_fields
ordering = ("report_type","-start_time",'clusterid')
list_filter = ['clusterid',"report_type"]
inlines = [SSOMethodTrafficReportInline]

traffic_data_list_url_name = 'admin:{}_{}_changelist'.format(models.TrafficData._meta.app_label,models.TrafficData._meta.model_name)
traffic_report_list_url_name = 'admin:{}_{}_changelist'.format(models.TrafficReport._meta.app_label,models.TrafficReport._meta.model_name)

@property
def list_display(self):
if settings.AUTH2_CLUSTER_ENABLED and cache.auth2_clusters:
return report_list_display_4_cluster
else:
return report_list_display

@property
def readonly_fields(self):
if settings.AUTH2_CLUSTER_ENABLED and cache.auth2_clusters:
return report_fields_4_cluster
else:
return report_fields

@property
def fields(self):
if settings.AUTH2_CLUSTER_ENABLED and cache.auth2_clusters:
return report_fields_4_cluster
else:
return report_fields

def _subreports(self,obj):
if not obj:
return ""
Expand Down
Loading
Loading