Class: QuickSearch::AppstatsController

Inherits:
ApplicationController show all
Includes:
Auth
Defined in:
app/controllers/quick_search/appstats_controller.rb

Constant Summary

Constants included from Auth

QuickSearch::Auth::REALM, QuickSearch::Auth::SALT01, QuickSearch::Auth::SALT02, QuickSearch::Auth::SALTY, QuickSearch::Auth::USERS

Instance Method Summary collapse

Methods included from Auth

#auth

Instance Method Details

#clicks_overviewObject



280
281
282
# File 'app/controllers/quick_search/appstats_controller.rb', line 280

def clicks_overview
  @page_title = 'Clicks Overview'
end

#convert_to_time(date_input) ⇒ Object



300
301
302
# File 'app/controllers/quick_search/appstats_controller.rb', line 300

def convert_to_time(date_input)
  Time.parse(date_input)
end

#data_best_betsObject



90
91
92
93
94
95
96
97
98
# File 'app/controllers/quick_search/appstats_controller.rb', line 90

def data_best_bets
  num_results = params[:num_results] ? params[:num_results].to_i : 20
  serves = Event.where(@range).where(:category => "best-bets-regular", :action => 'serve').group(:item).order("count_category DESC").count(:category)
  clicks = Event.where(@range).where(:category => "best-bets-regular", :action => 'click').group(:item).count(:category)

  @result = process_spelling_best_bets_query(serves, clicks, "best_bet", 0, num_results)

  render_data
end

#data_best_bets_detailsObject



100
101
102
103
104
105
106
107
108
# File 'app/controllers/quick_search/appstats_controller.rb', line 100

def data_best_bets_details
  item = params[:item]
  serves = Event.where(@range).where(:category => "best-bets-regular", :action => 'serve', :item => item).group(:query).order("count_query DESC").count(:query)
  clicks = Event.where(@range).where(:category => "best-bets-regular", :action => 'click', :item => item).group(:query).count(:query)

  @result = process_spelling_best_bets_query(serves, clicks, "best_bet_details", item, 15)

  render_data
end

#data_general_statisticsObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/quick_search/appstats_controller.rb', line 7

def data_general_statistics
  @result = []

  clicks = Event.where(@range).where(:action => 'click').group(:created_at_string).order("created_at_string ASC").count(:created_at_string)
  @result << process_time_query(clicks)

  sessions = Session.where(@range).group(:created_at_string).order("created_at_string ASC").count(:created_at_string)
  @result << process_time_query(sessions)

  searches = Search.where(@range).group(:created_at_string).order("created_at_string ASC").count(:created_at_string)
  @result << process_time_query(searches)

  render_data
end

#data_general_tableObject



22
23
24
25
26
27
28
29
30
# File 'app/controllers/quick_search/appstats_controller.rb', line 22

def data_general_table
  @result = []

  @result << { "clicks" => Event.where(@range).where(:action => 'click').count }
  @result << { "searches" => Search.where(@range).count }
  @result << { "sessions" => Session.where(@range).count }

  render_data
end

#data_module_clicksObject



32
33
34
35
36
37
38
39
# File 'app/controllers/quick_search/appstats_controller.rb', line 32

def data_module_clicks
  clicks = Event.where(@range).where(excluded_categories).where(:action => 'click').group(:category).order("count_category DESC").count(:category)
  total_clicks = clicks.values.sum

  @result = process_module_result_query(clicks, "module", 0, 100, total_clicks)

  render_data
end

#data_module_detailsObject



50
51
52
53
54
55
56
57
58
# File 'app/controllers/quick_search/appstats_controller.rb', line 50

def data_module_details
  category = params[:category]
  clicks = Event.where(:category => category).where(:action => 'click').where(@range).group(:item).order('count_category DESC').count(:category)
  total_clicks = clicks.values.sum

  @result = process_module_result_query(clicks, "module_details", category, 15, total_clicks)

  render_data
end

#data_result_clicksObject



41
42
43
44
45
46
47
48
# File 'app/controllers/quick_search/appstats_controller.rb', line 41

def data_result_clicks
  clicks = Event.where(@range).where(:category => "result-types").where(:action => 'click').group(:item).order("count_item DESC").count(:item)
  total_clicks = clicks.values.sum

  @result = process_module_result_query(clicks, "result", 0, 100, total_clicks)

  render_data
end

#data_sessions_deviceObject



176
177
178
179
180
181
182
183
184
# File 'app/controllers/quick_search/appstats_controller.rb', line 176

def data_sessions_device
  use_perc = params[:use_perc]=="true" ? true : false
  sessions_on = Session.where(@range).where(:is_mobile => true).group(:created_at_string).order("created_at_string ASC").count(:created_at_string)
  sessions_off = Session.where(@range).where(:is_mobile => false).group(:created_at_string).order("created_at_string ASC").count(:created_at_string)

  @result = process_stacked_time_query(sessions_on, sessions_off, use_perc)
  
  render_data
end

#data_sessions_locationObject



166
167
168
169
170
171
172
173
174
# File 'app/controllers/quick_search/appstats_controller.rb', line 166

def data_sessions_location
  use_perc = params[:use_perc]=="true" ? true : false
  sessions_on = Session.where(@range).where(:on_campus => true).group(:created_at_string).order("created_at_string ASC").count(:created_at_string)
  sessions_off = Session.where(@range).where(:on_campus => false).group(:created_at_string).order("created_at_string ASC").count(:created_at_string)

  @result = process_stacked_time_query(sessions_on, sessions_off, use_perc)
  
  render_data
end

#data_sessions_overviewObject

In order to obtain all filter cases, an integer corresponding to the following truth table is formed: rowNumber onCampus offCampus isMobile notMobile | filters 0 0 0 0 0 | Neither filter applied (default) 1 0 0 0 1 | where(is_mobile=>false) 2 0 0 1 0 | where(is_mobile=>true) 3 0 0 1 1 | INVALID (isMobile & notMobile asserted) 4 0 1 0 0 | where(on_campus=>false) 5 0 1 0 1 | where(on_campus=>false, is_mobile=>false) 6 0 1 1 0 | where(on_campus=>false, is_mobile=>true) 7 0 1 1 1 | INVALID (isMobile & notMobile asserted) 8 1 0 0 0 | where(on_campus=>true) 9 1 0 0 1 | where(on_campus=>true, is_mobile=>false) 10 1 0 1 0 | where(on_campus=>true, is_mobile=>true) 11 1 0 1 1 | INVALID (isMobile & notMobile asserted) 12 1 1 0 0 | INVALID (onCampus & offCampus asserted) 13 1 1 0 1 | INVALID (onCampus & offCampus asserted) 14 1 1 1 0 | INVALID (onCampus & offCampus asserted) 15 1 1 1 1 | INVALID (onCampus & offCampus asserted) Thus, the integer filterCase, which corresponds to the rowNumber, can be formed by converting 4 bit binary term formed by the concatenation offCampus, isMobile, notMobile into an integer. Note: This filtering cannot be obtained by passing two boolean values (one for on_campus and one for is_mobile) as this would fail to account for cases where no filter is applied to one variable (ie. where we don’t care about either location or device)



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/controllers/quick_search/appstats_controller.rb', line 133

def data_sessions_overview
  onCampus = params[:onCampus] ? params[:onCampus].to_i : 0
  offCampus = params[:offCampus] ? params[:offCampus].to_i : 0
  isMobile = params[:isMobile] ? params[:isMobile].to_i : 0
  notMobile = params[:notMobile] ? params[:notMobile].to_i : 0
  filterCase = (2**3)*onCampus + (2**2)*offCampus + (2**1)*isMobile + notMobile

  case filterCase
  when 1 #mobile=f
    sessions = Session.where(@range).where(:is_mobile => false).group(:created_at_string).order("created_at_string ASC").count(:created_at_string)
  when 2 #mobile=t
    sessions = Session.where(@range).where(:is_mobile => true).group(:created_at_string).order("created_at_string ASC").count(:created_at_string)
  when 4 #campus=f
    sessions = Session.where(@range).where(:on_campus => false).group(:created_at_string).order("created_at_string ASC").count(:created_at_string)
  when 5 #campus=f, mobile=f
    sessions = Session.where(@range).where(:on_campus => false, :is_mobile => false).group(:created_at_string).order("created_at_string ASC").count(:created_at_string)
  when 6 #campus=f, mobile=t
    sessions = Session.where(@range).where(:on_campus => false, :is_mobile => true).group(:created_at_string).order("created_at_string ASC").count(:created_at_string)
  when 8 #campus=t
    sessions = Session.where(@range).where(:on_campus => true).group(:created_at_string).order("created_at_string ASC").count(:created_at_string)
  when 9 #campus=t, mobile=f
    sessions = Session.where(@range).where(:on_campus => true, :is_mobile => false).group(:created_at_string).order("created_at_string ASC").count(:created_at_string)
  when 10 #campus=t, mobile=t
    sessions = Session.where(@range).where(:on_campus => true, :is_mobile => true).group(:created_at_string).order("created_at_string ASC").count(:created_at_string)
  else
    sessions = Session.where(@range).group(:created_at_string).order("created_at_string ASC").count(:created_at_string)
  end

  @result = process_time_query(sessions)

  render_data
end

#data_spelling_detailsObject



80
81
82
83
84
85
86
87
88
# File 'app/controllers/quick_search/appstats_controller.rb', line 80

def data_spelling_details
  item = params[:item]
  serves = Event.where(@range).where(:category => "spelling-suggestion", :action => 'serve', :item => item).group(:query).order("count_query DESC").count(:query)
  clicks = Event.where(@range).where(:category => "spelling-suggestion", :action => 'click', :item => item).group(:query).count(:query)

  @result = process_spelling_best_bets_query(serves, clicks, "spelling_details", item, 15)

  render_data
end

#data_spelling_suggestionsObject



70
71
72
73
74
75
76
77
78
# File 'app/controllers/quick_search/appstats_controller.rb', line 70

def data_spelling_suggestions
  num_results = params[:num_results] ? params[:num_results].to_i : 20
  serves = Event.where(@range).where(:category => "spelling-suggestion", :action => 'serve').group(:item).order("count_category DESC").count(:category)
  clicks = Event.where(@range).where(:category => "spelling-suggestion", :action => 'click').group(:item).count(:category)

  @result = process_spelling_best_bets_query(serves, clicks, "spelling_suggestion", 0, num_results)

  render_data
end

#data_top_searchesObject



60
61
62
63
64
65
66
67
68
# File 'app/controllers/quick_search/appstats_controller.rb', line 60

def data_top_searches
  num_results = params[:num_results] ? params[:num_results].to_i : 20
  searches = Search.where(:page => '/').where(@range).group(:query).order('count_query DESC').count(:query)
  total_searches = searches.sum {|k,v| v}

  @result = process_searches_query(searches, num_results, total_searches)

  render_data
end

#days_in_sampleObject



304
305
306
307
308
309
310
# File 'app/controllers/quick_search/appstats_controller.rb', line 304

def days_in_sample
  @days_in_sample = ((@end_date - @start_date) / (24*60*60)).round
  if @days_in_sample < 1
    @days_in_sample = 1
  end

end

#excluded_categoriesObject



328
329
330
# File 'app/controllers/quick_search/appstats_controller.rb', line 328

def excluded_categories
  "category <> \"common-searches\" AND category <> \"result-types\"AND category <> \"typeahead\""
end

#get_datesObject



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'app/controllers/quick_search/appstats_controller.rb', line 312

def get_dates
  start = params[:start_date]
  stop = params[:end_date]
  if (!start.blank?)
    @start_date = convert_to_time(start)
  else
    @start_date = Time.current - 180.days
  end
  if (!stop.blank?)
    @end_date = convert_to_time(stop)
  else
    @end_date = Time.current
  end
  @range = { :created_at => @start_date..@end_date }
end

#indexObject



276
277
278
# File 'app/controllers/quick_search/appstats_controller.rb', line 276

def index
  @page_title = 'Search Statistics'
end

#process_module_result_query(query, keyHeading, parent, num_results, total_clicks) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'app/controllers/quick_search/appstats_controller.rb', line 208

def process_module_result_query(query, keyHeading, parent, num_results, total_clicks)
  sub = []
  query.to_a[0..num_results-1].each_with_index do |d, i|
    label = d[0]
    count = d[1]
    row = {"rank" => i+1,
           "label" => (label.blank? ? "(blank)"  : label),
           "clickcount" => count,
           "percentage" => ((100.0*count)/total_clicks).round(2),
           "parent" => parent,
           "expanded" => 0,
           "key" => keyHeading + (label.blank? ? "(blank)"  : label) + parent.to_s}
    sub << row
  end
  return sub
end

#process_searches_query(searches, num_results, total_searches) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'app/controllers/quick_search/appstats_controller.rb', line 244

def process_searches_query(searches, num_results, total_searches)
  sub = []
  last_row = {}
  searches.to_a[0..num_results-1].each_with_index do |d, i|
    query = d[0]
    count = d[1]
    if (last_row=={}) 
      last_cum_percentage = 0
    else 
      last_cum_percentage = last_row["cum_perc"]
    end
    row = {"rank" => i+1,
           "label" => query,
           "count" => count,
           "percentage" => ((100.0*count)/total_searches).round(2),
           "cum_perc" => (last_cum_percentage + ((100.0*count)/total_searches)),
           "cum_percentage" => (last_cum_percentage + ((100.0*count)/total_searches)).round(2),
           "key" => "top_search" + query}
    sub << row
    last_row = row
  end
  return sub
end

#process_spelling_best_bets_query(serves, clicks, keyHeading, parent, num_results) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'app/controllers/quick_search/appstats_controller.rb', line 225

def process_spelling_best_bets_query(serves, clicks, keyHeading, parent, num_results)
  sub = []
  serves.to_a[0..num_results-1].each_with_index do |d , i|
    label = d[0]
    serve_count = d[1]
    click_count = clicks[label] ? clicks[label] : 0
    row = {"rank" => i+1,
           "label" => label,
           "serves" => serve_count,
           "clicks" =>  click_count,
           "ratio" => (100.0*click_count/serve_count).round(2),
           "parent" => parent,
           "expanded" => 0,
           "key" => keyHeading + label + parent.to_s}
    sub << row
  end
  return sub
end

#process_stacked_time_query(query1, query2, use_perc) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
# File 'app/controllers/quick_search/appstats_controller.rb', line 196

def process_stacked_time_query(query1, query2, use_perc)
  sub = []
  query1.each do |date , count1|
    count2 = query2[date] ? query2[date] : 0
    row = { "date" => date ,
            "on" => use_perc ? count1.to_f/(count1+count2) : count1,
            "off" => use_perc ? count2.to_f/(count1+count2) : count2}
    sub << row
  end
  return sub
end

#process_time_query(query) ⇒ Object



186
187
188
189
190
191
192
193
194
# File 'app/controllers/quick_search/appstats_controller.rb', line 186

def process_time_query(query)
  sub = []
  query.each do |date , count|
    row = { "date" => date ,
            "count" => count}
    sub << row
  end
  return sub
end

#render_dataObject



268
269
270
271
272
273
274
# File 'app/controllers/quick_search/appstats_controller.rb', line 268

def render_data
  respond_to do |format|
    format.json {
      render :json => @result
    }
  end
end

#sessions_detailsObject



296
297
298
# File 'app/controllers/quick_search/appstats_controller.rb', line 296

def sessions_details
  @page_title = 'Sessions Details'
end

#sessions_overviewObject



292
293
294
# File 'app/controllers/quick_search/appstats_controller.rb', line 292

def sessions_overview
  @page_title = 'Sessions Overview'
end

#top_searchesObject



284
285
286
# File 'app/controllers/quick_search/appstats_controller.rb', line 284

def top_searches
  @page_title = 'Top Searches'
end

#top_spotObject



288
289
290
# File 'app/controllers/quick_search/appstats_controller.rb', line 288

def top_spot
  @page_title = params[:ga_top_spot_module]
end