2014-11-05 21:25:18 +01:00
|
|
|
# Generate statistics, around events, by date or place
|
|
|
|
class StatsController < ApplicationController
|
2017-07-02 13:28:10 +02:00
|
|
|
has_scope :region, :locality, :tag, :daylimit, :year
|
|
|
|
has_scope :near, type: :hash, using: %i[location distance]
|
|
|
|
|
|
|
|
before_action :set_events, :counts, :temporal, :local, only: [:index]
|
2014-11-05 21:25:18 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-07-02 13:28:10 +02:00
|
|
|
def counts
|
|
|
|
@events_count = @events.count
|
|
|
|
@events_um_count = apply_scopes(Event.unmoderated).count
|
|
|
|
@orgas_count = apply_scopes(Orga).moderated.count
|
|
|
|
@orgas_um_count = apply_scopes(Orga.unmoderated).count
|
|
|
|
end
|
|
|
|
|
|
|
|
def temporal
|
|
|
|
@years = @events.group(year_grouping).count
|
|
|
|
@months = @events.group(year_grouping, month_grouping).count
|
|
|
|
end
|
|
|
|
|
|
|
|
def local
|
2017-07-02 14:13:09 +02:00
|
|
|
@region_events = @events.group(:region_id, year_grouping).count
|
|
|
|
@regions = Region.all.find_all do |region|
|
|
|
|
@years.sum { |year| @region_events[[region.id, year[0]]] || 0 } > 0
|
|
|
|
end
|
2017-07-02 13:28:10 +02:00
|
|
|
@city_events = @events.group(:city).having('count(city) > 3')
|
|
|
|
.order('count(city) desc').count
|
2014-11-05 21:25:18 +01:00
|
|
|
end
|
|
|
|
|
2017-07-02 13:28:10 +02:00
|
|
|
def set_events
|
|
|
|
@events = apply_scopes Event.moderated
|
2014-11-05 21:25:18 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def year_grouping
|
2017-04-22 20:01:47 +02:00
|
|
|
if %w[Mysql2 MySQL PostgreSQL].include? Event.connection.adapter_name
|
2014-11-05 21:25:18 +01:00
|
|
|
'extract(year from start_time)'
|
|
|
|
elsif Event.connection.adapter_name == 'SQLite'
|
|
|
|
'strftime("%Y", start_time)'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def month_grouping
|
2017-04-22 20:01:47 +02:00
|
|
|
if %w[Mysql2 MySQL PostgreSQL].include? Event.connection.adapter_name
|
2014-11-05 21:25:18 +01:00
|
|
|
'extract(month from start_time)'
|
|
|
|
elsif Event.connection.adapter_name == 'SQLite'
|
|
|
|
'strftime("%m", start_time)'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|