Skip to content

Commit

Permalink
Move counts to extras
Browse files Browse the repository at this point in the history
  • Loading branch information
pezholio committed Jun 6, 2014
1 parent 1c224b6 commit e22653a
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 86 deletions.
4 changes: 2 additions & 2 deletions app/controllers/main_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def status
@job_count = Delayed::Job.count

@counts = {
'certificates' => Certificate.counts,
'datasets' => ResponseSet.counts
'certificates' => Stats::Certificate.counts,
'datasets' => Stats::ResponseSet.counts
}

@head_commit = Rails.root.to_s.split("/").last
Expand Down
2 changes: 1 addition & 1 deletion app/models/certificate.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Certificate < ActiveRecord::Base
include Badges, Counts
include Badges

belongs_to :response_set

Expand Down
44 changes: 0 additions & 44 deletions app/models/concerns/counts.rb

This file was deleted.

16 changes: 0 additions & 16 deletions app/models/response_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,6 @@ class ResponseSet < ActiveRecord::Base
# there is already a protected method with this
# has_many :dependencies, :through => :survey

class << self

def counts
within_last_month = (Time.now - 1.month)..Time.now
{
:all => self.count,
:all_datasets => self.select("DISTINCT(dataset_id)").count,
:all_datasets_this_month => self.select("DISTINCT(dataset_id)").where(created_at: within_last_month).count,
:published_datasets => self.published.select("DISTINCT(dataset_id)").count,
:published_datasets_this_month => self.published.select("DISTINCT(dataset_id)").where(created_at: within_last_month).count
}
end

end


def self.has_blank_value?(hash)
return true if hash["answer_id"].kind_of?(Array) ? hash["answer_id"].all?{|id| id.blank?} : hash["answer_id"].blank?
return false if (q = Question.find_by_id(hash["question_id"])) and q.pick == "one"
Expand Down
69 changes: 69 additions & 0 deletions lib/extra/stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module Stats

module Certificate

def self.counts
Hash[[:all, :all_this_month, :published, :published_this_month, :levels].map do |count|
[count, self.send("#{count}")]
end]
end

def self.levels
Hash[[:basic, :pilot, :standard, :expert].map do |level|
[level, ActiveRecord::Base::Certificate.where(published: true, attained_level: level).count]
end]
end

def self.all
ActiveRecord::Base::Certificate.count
end

def self.all_this_month
ActiveRecord::Base::Certificate.where(created_at: Stats.within_last_month).count
end

def self.published
ActiveRecord::Base::Certificate.where(published: true).count
end

def self.published_this_month
ActiveRecord::Base::Certificate.where(published: true, created_at: Stats.within_last_month).count
end

end

class ResponseSet

def self.counts
Hash[[:all, :all_datasets, :all_datasets_this_month, :published_datasets, :published_datasets_this_month].map do |count|
[count, ResponseSet.send(count)]
end]
end

def self.all
ActiveRecord::Base::ResponseSet.count
end

def self.all_datasets
ActiveRecord::Base:: ResponseSet.select("DISTINCT(dataset_id)").count
end

def self.all_datasets_this_month
ActiveRecord::Base::ResponseSet.select("DISTINCT(dataset_id)").where(created_at: Stats.within_last_month).count
end

def self.published_datasets
ActiveRecord::Base::ResponseSet.published.select("DISTINCT(dataset_id)").count
end

def self.published_datasets_this_month
ActiveRecord::Base::ResponseSet.published.select("DISTINCT(dataset_id)").where(created_at: Stats.within_last_month).count
end

end

def self.within_last_month
(Time.now - 1.month)..Time.now
end

end
44 changes: 22 additions & 22 deletions lib/extra/stats_dump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module StatsDump
FILENAME = "statistics.csv"

def self.perform
file = Rackspace.dir.files.head FILENAME
if file.nil?
Expand All @@ -13,15 +13,15 @@ def self.perform
end
Delayed::Job.enqueue StatsDump, { :priority => 5, :run_at => 1.day.from_now }
end

def self.setup
csv = CSV.generate do |csv|
csv << [
"Date",
"All surveys started",
"All Certificates",
"All Datasets",
"Published Certificates",
"All Certificates",
"All Datasets",
"Published Certificates",
"Published Datasets",
"Raw level Certificates",
"Pilot level Certificates",
Expand All @@ -30,35 +30,35 @@ def self.setup
]
csv << stats_row
end

Rackspace.upload(FILENAME, csv)
end

def self.latest
file = Rackspace.dir.files.head FILENAME
csv = CSV.parse(file.body)
csv << stats_row

body = CSV.generate do |body|
csv.each { |row| body << row }
end

Rackspace.upload(FILENAME, body)
end

def self.stats_row
[
Date.today.to_s,
Certificate.counts[:all],
ResponseSet.counts[:all],
ResponseSet.counts[:all_datasets],
Certificate.counts[:published],
ResponseSet.counts[:published_datasets],
Certificate.counts[:levels][:basic],
Certificate.counts[:levels][:pilot],
Certificate.counts[:levels][:standard],
Certificate.counts[:levels][:expert]
Date.today.to_s,
Stats::Certificate.counts[:all],
Stats::ResponseSet.counts[:all],
Stats::ResponseSet.counts[:all_datasets],
Stats::Certificate.counts[:published],
Stats::ResponseSet.counts[:published_datasets],
Stats::Certificate.counts[:levels][:basic],
Stats::Certificate.counts[:levels][:pilot],
Stats::Certificate.counts[:levels][:standard],
Stats::Certificate.counts[:levels][:expert]
]
end
end

end
2 changes: 1 addition & 1 deletion test/unit/certificate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def setup
@certificate2.update_attributes(attained_level: "pilot")
@certificate3.update_attributes(attained_level: "standard")

counts = Certificate.counts
counts = Stats::Certificate.counts

assert_equal 3, counts[:all]
assert_equal 3, counts[:all_this_month]
Expand Down

0 comments on commit e22653a

Please sign in to comment.