Skip to content

Commit

Permalink
Add statistics route.
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanzdosilovic committed Jan 14, 2020
1 parent 521cc2e commit a24db63
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
52 changes: 52 additions & 0 deletions app/controllers/info_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,56 @@ def isolate
render plain: @@isolate
end

def statistics
@@language_name ||= Hash[Language.unscoped.pluck(:id, :name)]

count_by_language = []
Submission.unscoped.group(:language_id).count.each do |language_id, count|
count_by_language << {
language: {
id: language_id,
name: @@language_name[language_id]
},
count: count
}
end
count_by_language = count_by_language.sort_by { |x| x[:count] }.reverse

count_by_status = []
Submission.unscoped.group(:status_id).count.each do |status_id, count|
count_by_status << {
status: {
id: status_id,
name: Status.find_by(id: status_id).name # Not a SQL query!
},
count: count
}
end
count_by_status = count_by_status.sort_by{ |x| x[:count] }.reverse

now = DateTime.now
today = DateTime.now.beginning_of_day.to_date
last_30_days = Submission.unscoped.group("created_at::DATE").where("created_at::DATE >= ?", today - 30).count
last_30_days_result = {}
(today-30...today).each do |day|
last_30_days_result[day.to_date] = last_30_days[day] || 0
end
last_30_days_result = last_30_days_result.sort.reverse.to_h

database_size = ActiveRecord::Base.connection.execute("SELECT pg_size_pretty(pg_database_size('#{ENV['POSTGRES_DB']}')) AS size").to_a[0]["size"]

render json: {
now: now,
submissions: {
total: Submission.count,
today: last_30_days[today],
last_30_days: last_30_days_result
},
languages: count_by_language,
statuses: count_by_status,
database: {
size: database_size
}
}
end
end
3 changes: 2 additions & 1 deletion config/initializers/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ENV['INTERVAL'] = ENV['INTERVAL'].presence || '0.1'
ENV['COUNT'] = ENV['COUNT'].presence || '1'
ENV['QUEUE'] = ENV['QUEUE'].presence || '*'
ENV['RAILS_ENV'] = ENV['RAILS_ENV'].presence || 'production'
ENV['RAILS_ENV'] = ENV['RAILS_ENV'].presence || 'production'
ENV['POSTGRES_DB'] = ENV['POSTGRES_DB'].presence || 'postgres'
3 changes: 1 addition & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@

get 'system_info', to: 'info#system_info'
get 'config_info', to: 'info#config_info'

get 'isolate', to: 'info#isolate'

get 'about', to: 'info#about'
get 'version', to: 'info#version'
get 'license', to: 'info#license'
get 'statistics', to: 'info#statistics'

post 'authenticate', to: 'sessions#authenticate'
post 'authorize', to: 'sessions#authorize'
Expand Down

0 comments on commit a24db63

Please sign in to comment.