[Bugfix] 修复因数据不支持timezone引起的仪表盘数据为空的bug

pull/3816/head
ibuler 2020-03-23 17:55:28 +08:00
parent b19c49da41
commit 0fd43f48f0
1 changed files with 17 additions and 4 deletions

View File

@ -24,7 +24,8 @@ class MonthLoginMetricMixin:
@lazyproperty @lazyproperty
def session_month_dates(self): def session_month_dates(self):
return self.session_month.dates('date_start', 'day') dates = self.session_month.dates('date_start', 'day')
return dates
def get_month_day_metrics(self): def get_month_day_metrics(self):
month_str = [ month_str = [
@ -57,12 +58,22 @@ class MonthLoginMetricMixin:
def asset_disabled_total(self): def asset_disabled_total(self):
return Asset.objects.filter(is_active=False).count() return Asset.objects.filter(is_active=False).count()
@staticmethod
def get_date_start_2_end(d):
time_min = timezone.datetime.min.time()
time_max = timezone.datetime.max.time()
tz = timezone.get_current_timezone()
ds = timezone.datetime.combine(d, time_min).replace(tzinfo=tz)
de = timezone.datetime.combine(d, time_max).replace(tzinfo=tz)
return ds, de
def get_date_login_count(self, date): def get_date_login_count(self, date):
tp = "LOGIN" tp = "LOGIN"
count = self.__get_data_from_cache(date, tp) count = self.__get_data_from_cache(date, tp)
if count is not None: if count is not None:
return count return count
count = Session.objects.filter(date_start__date=date).count() ds, de = self.get_date_start_2_end(date)
count = Session.objects.filter(date_start__range=(ds, de)).count()
self.__set_data_to_cache(date, tp, count) self.__set_data_to_cache(date, tp, count)
return count return count
@ -80,7 +91,8 @@ class MonthLoginMetricMixin:
count = self.__get_data_from_cache(date, tp) count = self.__get_data_from_cache(date, tp)
if count is not None: if count is not None:
return count return count
count = Session.objects.filter(date_start__date=date)\ ds, de = self.get_date_start_2_end(date)
count = Session.objects.filter(date_start__range=(ds, de))\
.values('user').distinct().count() .values('user').distinct().count()
self.__set_data_to_cache(date, tp, count) self.__set_data_to_cache(date, tp, count)
return count return count
@ -97,7 +109,8 @@ class MonthLoginMetricMixin:
count = self.__get_data_from_cache(date, tp) count = self.__get_data_from_cache(date, tp)
if count is not None: if count is not None:
return count return count
count = Session.objects.filter(date_start__date=date) \ ds, de = self.get_date_start_2_end(date)
count = Session.objects.filter(date_start__range=(ds, de)) \
.values('asset').distinct().count() .values('asset').distinct().count()
self.__set_data_to_cache(date, tp, count) self.__set_data_to_cache(date, tp, count)
return count return count