Module: AdminData::Analytics

Defined in:
lib/admin_data/analytics.rb

Defined Under Namespace

Classes: Dater

Class Method Summary collapse

Class Method Details

.daily_report(klass, end_date) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/admin_data/analytics.rb', line 124

def self.daily_report(klass, end_date)
  begin_date = end_date.ago(1.month)
  raise "begin_date should not be after end_date" if begin_date > end_date
  raise AdminData::NoCreatedAtColumnException unless klass.columns.find {|r| r.name == 'created_at'}

  begin_date = begin_date.beginning_of_day
  end_date = end_date.end_of_day

  dater = Dater.new(ActiveRecord::Base.connection.adapter_name)

  query = klass.unscoped
  query = query.where(["created_at >= ?", begin_date])
  query = query.where(["created_at <= ?", end_date])
  query = query.group(dater.group_by_key)
  query = query.select(dater.date_select_function)
  query = query.select(dater.count_function)
  debug "sql: " + query.to_sql
  result = query.all
  debug "sql result: #{result.inspect}"

  extract_data_from_result_set_daily(result, dater, begin_date, end_date).tap {|e| debug "formatted output: "+e.inspect}
end

.debug(msg) ⇒ Object



170
171
172
# File 'lib/admin_data/analytics.rb', line 170

def self.debug(msg)
  puts msg
end

.extract_data_from_result_set_daily(result, dater, begin_date, end_date) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/admin_data/analytics.rb', line 147

def self.extract_data_from_result_set_daily(result, dater, begin_date, end_date)
  count = result.map {|r| r[dater.count_select_key] }
  dates = result.map {|r| r[dater.date_select_key] }

  debug "count is: #{count.inspect}"
  debug "dates is: #{dates.inspect}"

  final_output= []

  while(begin_date) do
    s = begin_date.strftime('%Y-%m-%d')
    final_count = if index = dates.index(s)
      count[index].to_i
    else
      0
    end
    final_output << ["'#{s}'", final_count]
    begin_date = begin_date.tomorrow
    break if begin_date > end_date
  end
  final_output
end

.extract_data_from_result_set_monthly(result, dater, begin_date, end_date) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/admin_data/analytics.rb', line 86

def self.extract_data_from_result_set_monthly(result, dater, begin_date, end_date)
  debug "extracting from result set now"
  result_hash = {}
  result.each do |record|
    result_hash.merge!(record[dater.date_select_key].to_i => record[dater.count_select_key])
  end

  debug "result_hash.inspect is #{result_hash.inspect}"
  
  current_month = begin_date.strftime('%m').to_i
  months_order = (1..current_month).to_a.reverse + (current_month..12).to_a.reverse
  months_order.uniq!.reverse!

  debug "months order is #{months_order.inspect}"

  final_hash = ActiveSupport::OrderedHash.new
  months_order.each do |month|
    month = month.to_i #sqlite3 has months as 03 instead of 3
    m = Time.now.change(:month => month)
    m = m.ago(1.year) if month > current_month

    key = m.strftime('%b-%Y')
    key = "'#{key}'"
    if dater.adapter =~ /postgresql/i
      value = result_hash[month]
    elsif dater.adapter =~ /mysql/i
      value = result_hash[month]
    else
      value = result_hash[month]
    end
    value = value.to_i
    debug "month: #{month} key: #{key} value: #{value}"
    final_hash.merge!(key => value)
  end

  final_hash.to_a.tap {|e| debug e.inspect }
end

.monthly_report(klass, end_date) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/admin_data/analytics.rb', line 63

def self.monthly_report(klass, end_date)
  begin_date = end_date.ago(1.year)
  raise "begin_date should not be after end_date" if begin_date > end_date
  raise AdminData::NoCreatedAtColumnException unless klass.columns.find {|r| r.name == 'created_at'}

  begin_date = begin_date.beginning_of_day
  end_date = end_date.end_of_day

  dater = Dater.new(ActiveRecord::Base.connection.adapter_name, 'monthly')

  query = klass.unscoped
  query = query.where(["created_at >= ?", begin_date])
  query = query.where(["created_at <= ?", end_date])
  query = query.group(dater.group_by_key)
  query = query.select(dater.date_select_function)
  query = query.select(dater.count_function)
  debug "sql: " + query.to_sql
  result = query.all
  debug "sql result: #{result.inspect}"

  extract_data_from_result_set_monthly(result, dater, begin_date, end_date)
end