Skip to content

Fix static events aggregation #264

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
Feb 25, 2018
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion app/models/dojo_event_service.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down
25 changes: 20 additions & 5 deletions lib/statistics/aggregation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -86,6 +94,7 @@ def run
with_notifying do
delete_event_histories
execute
execute_once
end
end

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down