From 16aca879ce9ef79942da97891913e4f5ebfc45dc Mon Sep 17 00:00:00 2001 From: nalabjp Date: Mon, 26 Feb 2018 04:56:18 +0900 Subject: [PATCH] Fix static events aggregation --- app/models/dojo_event_service.rb | 5 ++++- lib/statistics/aggregation.rb | 25 ++++++++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app/models/dojo_event_service.rb b/app/models/dojo_event_service.rb index e1569debd..bcde8ceab 100644 --- a/app/models/dojo_event_service.rb +++ b/app/models/dojo_event_service.rb @@ -1,6 +1,9 @@ class DojoEventService < ApplicationRecord + EXTERNAL_SERVICES = %i( connpass doorkeeper facebook ) + INTERNAL_SERVICES = %i( static_yaml ) + belongs_to :dojo - enum name: %i( connpass doorkeeper facebook static_yaml ) + enum name: EXTERNAL_SERVICES + INTERNAL_SERVICES validates :name, presence: true diff --git a/lib/statistics/aggregation.rb b/lib/statistics/aggregation.rb index 8b14f09c8..ccdacc0e8 100644 --- a/lib/statistics/aggregation.rb +++ b/lib/statistics/aggregation.rb @@ -69,14 +69,22 @@ def date_from(str) end def fetch_dojos - DojoEventService.names.keys.each.with_object({}) do |name, hash| + { + externals: find_dojos_by(DojoEventService::EXTERNAL_SERVICES), + internals: find_dojos_by(DojoEventService::INTERNAL_SERVICES) + } + end + + def find_dojos_by(services) + services.each.with_object({}) do |name, hash| hash[name] = Dojo.eager_load(:dojo_event_services).where(dojo_event_services: { name: name }).to_a end end class Base def initialize(dojos, from, to) - @dojos = dojos + @externals = dojos[:externals] + @internals = dojos[:internals] @list = build_list(from, to) @from = from @to = to @@ -86,6 +94,7 @@ def run with_notifying do delete_event_histories execute + execute_once end end @@ -99,7 +108,7 @@ def with_notifying end def delete_event_histories - @dojos.keys.each do |kind| + (@externals.keys + @internals.keys).each do |kind| "Statistics::Tasks::#{kind.to_s.camelize}".constantize.delete_event_histories(@from..@to) end end @@ -108,6 +117,12 @@ def execute raise NotImplementedError.new("You must implement #{self.class}##{__method__}") end + def execute_once + @internals.each do |kind, list| + "Statistics::Tasks::#{kind.to_s.camelize}".constantize.new(list, nil, nil).run + end + end + def build_list(_from, _to) raise NotImplementedError.new("You must implement #{self.class}##{__method__}") end @@ -124,7 +139,7 @@ def execute @list.each do |date| puts "Aggregate for #{date_format(date)}~#{date_format(date.end_of_week)}" - @dojos.each do |kind, list| + @externals.each do |kind, list| "Statistics::Tasks::#{kind.to_s.camelize}".constantize.new(list, date, true).run end end @@ -150,7 +165,7 @@ def execute @list.each do |date| puts "Aggregate for #{date_format(date)}" - @dojos.each do |kind, list| + @externals.each do |kind, list| "Statistics::Tasks::#{kind.to_s.camelize}".constantize.new(list, date, false).run end end