2021-03-02 11:45:44 +00:00
|
|
|
from django.db.models.signals import m2m_changed
|
2021-06-03 03:44:20 +00:00
|
|
|
from django.db.models.signals import post_save, pre_delete, pre_save, post_delete
|
2021-03-02 11:45:44 +00:00
|
|
|
from django.dispatch import receiver
|
|
|
|
|
|
|
|
from orgs.models import Organization, OrganizationMember
|
|
|
|
from assets.models import Node
|
|
|
|
from perms.models import (AssetPermission, ApplicationPermission)
|
|
|
|
from users.models import UserGroup, User
|
|
|
|
from applications.models import Application
|
2021-05-10 02:54:48 +00:00
|
|
|
from terminal.models import Session
|
2021-03-02 11:45:44 +00:00
|
|
|
from assets.models import Asset, AdminUser, SystemUser, Domain, Gateway
|
|
|
|
from common.const.signals import POST_PREFIX
|
|
|
|
from orgs.caches import OrgResourceStatisticsCache
|
|
|
|
|
|
|
|
|
|
|
|
def refresh_user_amount_on_user_create_or_delete(user_id):
|
|
|
|
orgs = Organization.objects.filter(m2m_org_members__user_id=user_id).distinct()
|
|
|
|
for org in orgs:
|
|
|
|
org_cache = OrgResourceStatisticsCache(org)
|
|
|
|
org_cache.expire('users_amount')
|
2021-05-17 07:30:17 +00:00
|
|
|
OrgResourceStatisticsCache(Organization.root()).expire('users_amount')
|
2021-03-02 11:45:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=User)
|
|
|
|
def on_user_create_refresh_cache(sender, instance, created, **kwargs):
|
|
|
|
if created:
|
|
|
|
refresh_user_amount_on_user_create_or_delete(instance.id)
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(pre_delete, sender=User)
|
|
|
|
def on_user_delete_refresh_cache(sender, instance, **kwargs):
|
|
|
|
refresh_user_amount_on_user_create_or_delete(instance.id)
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(m2m_changed, sender=OrganizationMember)
|
|
|
|
def on_org_user_changed_refresh_cache(sender, action, instance, reverse, pk_set, **kwargs):
|
|
|
|
if not action.startswith(POST_PREFIX):
|
|
|
|
return
|
|
|
|
|
|
|
|
if reverse:
|
|
|
|
orgs = Organization.objects.filter(id__in=pk_set)
|
|
|
|
else:
|
|
|
|
orgs = [instance]
|
|
|
|
|
|
|
|
for org in orgs:
|
|
|
|
org_cache = OrgResourceStatisticsCache(org)
|
|
|
|
org_cache.expire('users_amount')
|
2021-05-17 07:30:17 +00:00
|
|
|
OrgResourceStatisticsCache(Organization.root()).expire('users_amount')
|
2021-03-02 11:45:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OrgResourceStatisticsRefreshUtil:
|
|
|
|
model_cache_field_mapper = {
|
2021-05-10 02:54:48 +00:00
|
|
|
ApplicationPermission: ['app_perms_amount'],
|
|
|
|
AssetPermission: ['asset_perms_amount'],
|
|
|
|
Application: ['applications_amount'],
|
|
|
|
Gateway: ['gateways_amount'],
|
|
|
|
Domain: ['domains_amount'],
|
2021-07-14 05:53:28 +00:00
|
|
|
SystemUser: ['system_users_amount', 'admin_users_amount'],
|
2021-05-10 02:54:48 +00:00
|
|
|
Node: ['nodes_amount'],
|
|
|
|
Asset: ['assets_amount'],
|
|
|
|
UserGroup: ['groups_amount'],
|
2021-03-02 11:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def refresh_if_need(cls, instance):
|
|
|
|
cache_field_name = cls.model_cache_field_mapper.get(type(instance))
|
|
|
|
if cache_field_name:
|
|
|
|
org_cache = OrgResourceStatisticsCache(instance.org)
|
2021-05-10 02:54:48 +00:00
|
|
|
org_cache.expire(*cache_field_name)
|
2021-05-17 07:30:17 +00:00
|
|
|
OrgResourceStatisticsCache(Organization.root()).expire(*cache_field_name)
|
2021-03-02 11:45:44 +00:00
|
|
|
|
|
|
|
|
2021-06-03 03:01:11 +00:00
|
|
|
@receiver(post_save)
|
|
|
|
def on_post_save_refresh_org_resource_statistics_cache(sender, instance, created, **kwargs):
|
|
|
|
if created:
|
|
|
|
OrgResourceStatisticsRefreshUtil.refresh_if_need(instance)
|
2021-03-02 11:45:44 +00:00
|
|
|
|
|
|
|
|
2021-06-03 03:44:20 +00:00
|
|
|
@receiver(post_delete)
|
|
|
|
def on_post_delete_refresh_org_resource_statistics_cache(sender, instance, **kwargs):
|
2021-03-02 11:45:44 +00:00
|
|
|
OrgResourceStatisticsRefreshUtil.refresh_if_need(instance)
|
2021-06-03 03:01:11 +00:00
|
|
|
|
|
|
|
|
2021-06-03 03:44:20 +00:00
|
|
|
def _refresh_session_org_resource_statistics_cache(instance: Session):
|
|
|
|
cache_field_name = ['total_count_online_users', 'total_count_online_sessions']
|
|
|
|
|
|
|
|
org_cache = OrgResourceStatisticsCache(instance.org)
|
|
|
|
org_cache.expire(*cache_field_name)
|
|
|
|
OrgResourceStatisticsCache(Organization.root()).expire(*cache_field_name)
|
|
|
|
|
|
|
|
|
2021-06-03 03:01:11 +00:00
|
|
|
@receiver(pre_save, sender=Session)
|
2021-06-03 03:44:20 +00:00
|
|
|
def on_session_pre_save(sender, instance: Session, **kwargs):
|
|
|
|
old = Session.objects.filter(id=instance.id).values_list('is_finished', flat=True)
|
|
|
|
if old:
|
|
|
|
instance._signal_old_is_finished = old[0]
|
|
|
|
else:
|
|
|
|
instance._signal_old_is_finished = None
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Session)
|
|
|
|
def on_session_changed_refresh_org_resource_statistics_cache(sender, instance, created, **kwargs):
|
|
|
|
if created or instance.is_finished != instance._signal_old_is_finished:
|
|
|
|
_refresh_session_org_resource_statistics_cache(instance)
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=Session)
|
|
|
|
def on_session_deleted_refresh_org_resource_statistics_cache(sender, instance, **kwargs):
|
|
|
|
_refresh_session_org_resource_statistics_cache(instance)
|