Skip to content

Support '%Y' format #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions lib/tasks/statistics.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,38 @@ require_relative '../statistics.rb'

namespace :statistics do
desc '月次のイベント履歴を集計します'
task :aggregation, [:from_yyyymm, :to_yyyymm] => :environment do |tasks, args|
task :aggregation, [:from, :to] => :environment do |tasks, args|
date_from_str = -> (str) {
d = %w(%Y%m %Y/%m %Y-%m).map { |fmt|
formats = %w(%Y%m %Y/%m %Y-%m)
d = formats.map { |fmt|
begin
Time.zone.strptime(str, fmt)
rescue ArgumentError
Time.zone.local(str) if str.length == 4
end
}.compact.first
raise ArgumentError, "Invalid format: `#{str}`" if d.nil?
raise ArgumentError, "Invalid format: `#{str}`, allow format is #{formats.push('%Y').join(' or ')}" if d.nil?
d
}

from = (args[:from_yyyymm] ? date_from_str.call(args[:from_yyyymm]) : Time.current.prev_month).beginning_of_month
to = (args[:to_yyyymm] ? date_from_str.call(args[:to_yyyymm]) : Time.current.prev_month).end_of_month
from = if args[:from]
if args[:from].length == 4
date_from_str.call(args[:from]).beginning_of_year
else
date_from_str.call(args[:from]).beginning_of_month
end
else
Time.current.prev_month.beginning_of_month
end
to = if args[:to]
if args[:to].length == 4
date_from_str.call(args[:to]).end_of_year
else
date_from_str.call(args[:to]).end_of_month
end
else
Time.current.prev_month.end_of_month
end

EventHistory.where(evented_at: from..to).delete_all

Expand Down