From 956316fbb1d16eac0ecea924062074f6438d74f6 Mon Sep 17 00:00:00 2001 From: Khai Tran Date: Thu, 28 Mar 2024 16:33:23 +0700 Subject: [PATCH] chore: Add statistic user by hourly --- locker_server/api/v1_0/users/views.py | 3 ++- .../api_orm/repositories/user_repository.py | 25 +++++++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/locker_server/api/v1_0/users/views.py b/locker_server/api/v1_0/users/views.py index 9296e90..5ec161e 100644 --- a/locker_server/api/v1_0/users/views.py +++ b/locker_server/api/v1_0/users/views.py @@ -960,7 +960,8 @@ def login_method_me(self, request, *args, **kwargs): def dashboard(self, request, *args, **kwargs): current_time = now() register_from_param = self.check_int_param( - self.request.query_params.get("register_from")) or current_time - 90 * 86400 + self.request.query_params.get("register_from") + ) or current_time - 90 * 86400 register_to_param = self.check_int_param(self.request.query_params.get("register_to")) or current_time duration_param = self.request.query_params.get("duration") or "monthly" device_type_param = self.request.query_params.get("device_type") or "" diff --git a/locker_server/api_orm/repositories/user_repository.py b/locker_server/api_orm/repositories/user_repository.py index 0f146aa..a4ed3c1 100644 --- a/locker_server/api_orm/repositories/user_repository.py +++ b/locker_server/api_orm/repositories/user_repository.py @@ -46,27 +46,42 @@ class UserORMRepository(UserRepository): @staticmethod def _generate_duration_init_data(start, end, duration="monthly"): durations_list = [] - for i in range((end - start).days + 1): - date = start + timedelta(days=i) + range_time = (end - start).total_seconds() // 3600 + 1 if duration == "hourly" else (end - start).days + for i in range(int(range_time) + 1): + if duration == "hourly": + date = start + timedelta(hours=i) + else: + date = start + timedelta(days=i) if duration == "daily": d = "{}-{:02}-{:02}".format(date.year, date.month, date.day) elif duration == "weekly": d = date.isocalendar()[:2] # e.g. (2022, 24) d = "{}-{:02}".format(*d) + elif duration == "hourly": + d = "{}-{:02}-{:02}T{:02}:00:00".format(date.year, date.month, date.day, date.hour) else: d = "{}-{:02}".format(date.year, date.month) durations_list.append(d) duration_init = dict() - for d in sorted(set(durations_list), reverse=True): - duration_init[d] = None + if duration == "hourly": + for d in reversed(durations_list): + duration_init[d] = None + else: + for d in sorted(set(durations_list), reverse=True): + duration_init[d] = None - # # Get annotation query + # Get annotation query if duration == "daily": query = "CONCAT(YEAR(FROM_UNIXTIME(creation_date)), '-', " \ "LPAD(MONTH(FROM_UNIXTIME(creation_date)), 2, '0'), '-', " \ "LPAD(DAY(FROM_UNIXTIME(creation_date)), 2, '0') )" elif duration == "weekly": query = "CONCAT(YEAR(FROM_UNIXTIME(creation_date)), '-', LPAD(WEEK(FROM_UNIXTIME(creation_date)), 2, '0'))" + elif duration == "hourly": + query = "CONCAT(YEAR(FROM_UNIXTIME(creation_date)), '-', " \ + "LPAD(MONTH(FROM_UNIXTIME(creation_date)), 2, '0'), '-', " \ + "LPAD(DAY(FROM_UNIXTIME(creation_date)), 2, '0'), 'T', " \ + "LPAD(HOUR(FROM_UNIXTIME(creation_date)), 2, '0'), ':00:00' )" else: query = "CONCAT(YEAR(FROM_UNIXTIME(creation_date)), '-', LPAD(MONTH(FROM_UNIXTIME(creation_date)), 2, '0'))"