1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-04 15:24:23 +01:00
fab-manager/app/workers/period_statistics_worker.rb

34 lines
1.0 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# Asynchronously generate the statistics for the given period
# This worker is triggered when enabling the statistics and with `rails fablab:es:generate_stats`
class PeriodStatisticsWorker
include Sidekiq::Worker
# @param period {String|Integer} date string or number of days until current date
def perform(period)
days = date_to_days(period)
Rails.logger.info "\n==> generating statistics for the last #{days} days <==\n"
if days.zero?
Statistics::BuilderService.generate_statistic(start_date: Time.current.beginning_of_day, end_date: Time.current.end_of_day)
else
days.times.each do |i|
2022-08-29 17:34:09 +02:00
Statistics::BuilderService.generate_statistic(start_date: i.day.ago.beginning_of_day, end_date: i.day.ago.end_of_day)
end
end
end
def date_to_days(value)
return value.to_i if number?(value)
date = Date.parse(value.to_s)
(Time.current.to_date - date).to_i
end
def number?(string)
true if Float(string)
rescue StandardError
false
end
end