2022-08-29 17:34:09 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# module grouping all statistics concerns
|
|
|
|
module Statistics::Concerns; end
|
|
|
|
|
|
|
|
# Provides various helpers for services dealing with statistics generation
|
|
|
|
module Statistics::Concerns::HelpersConcern
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
def default_options
|
|
|
|
yesterday = 1.day.ago
|
|
|
|
{
|
|
|
|
start_date: yesterday.beginning_of_day,
|
|
|
|
end_date: yesterday.end_of_day
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_date(date)
|
2022-10-10 12:20:39 +02:00
|
|
|
to_date(date).strftime('%Y-%m-%d')
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_date(date)
|
2022-08-29 17:34:09 +02:00
|
|
|
if date.is_a?(String)
|
2022-10-10 12:20:39 +02:00
|
|
|
Date.strptime(date, '%Y%m%d')
|
2022-08-29 17:34:09 +02:00
|
|
|
else
|
2022-10-10 12:20:39 +02:00
|
|
|
date
|
2022-08-29 17:34:09 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_info_stat(stat)
|
|
|
|
{
|
|
|
|
userId: stat[:user_id],
|
|
|
|
gender: stat[:gender],
|
|
|
|
age: stat[:age],
|
|
|
|
group: stat[:group]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def difference_in_hours(start_at, end_at)
|
|
|
|
if start_at.to_date == end_at.to_date
|
2023-01-31 17:10:07 +01:00
|
|
|
((end_at - start_at) / 3600.0).to_i
|
2022-08-29 17:34:09 +02:00
|
|
|
else
|
|
|
|
end_at_to_start_date = end_at.change(year: start_at.year, month: start_at.month, day: start_at.day)
|
|
|
|
hours = ((end_at_to_start_date - start_at) / 60 / 60).to_i
|
|
|
|
hours = ((end_at.to_date - start_at.to_date).to_i + 1) * hours if end_at.to_date > start_at.to_date
|
|
|
|
hours
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|