Skip to content

Commit

Permalink
chore: Add statistic user by hourly
Browse files Browse the repository at this point in the history
  • Loading branch information
khaitranquang committed Mar 28, 2024
1 parent 3896a35 commit 956316f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
3 changes: 2 additions & 1 deletion locker_server/api/v1_0/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand Down
25 changes: 20 additions & 5 deletions locker_server/api_orm/repositories/user_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))"

Expand Down

0 comments on commit 956316f

Please sign in to comment.