ahoy_captain 0.77 → 0.81

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascript/ahoy_captain/application.js +4 -4
  3. data/app/assets/javascript/ahoy_captain/controllers/application.js +5 -5
  4. data/app/assets/javascript/ahoy_captain/controllers/application_controller.js +9 -10
  5. data/app/assets/javascript/ahoy_captain/controllers/details_modal_controller.js +5 -5
  6. data/app/assets/javascript/ahoy_captain/controllers/dropdown_label_controller.js +2 -2
  7. data/app/assets/javascript/ahoy_captain/controllers/filter_controller.js +64 -58
  8. data/app/assets/javascript/ahoy_captain/controllers/filter_tag_controller.js +11 -8
  9. data/app/assets/javascript/ahoy_captain/controllers/funnel_chart_controller.js +77 -133
  10. data/app/assets/javascript/ahoy_captain/controllers/index.js +4 -3
  11. data/app/assets/javascript/ahoy_captain/controllers/interval_controller.js +10 -0
  12. data/app/assets/javascript/ahoy_captain/controllers/link_controller.js +22 -22
  13. data/app/assets/javascript/ahoy_captain/controllers/navigation_controller.js +10 -10
  14. data/app/assets/javascript/ahoy_captain/controllers/realtime_controller.js +7 -8
  15. data/app/controllers/ahoy_captain/application_controller.rb +17 -15
  16. data/app/controllers/ahoy_captain/campaigns_controller.rb +2 -10
  17. data/app/controllers/ahoy_captain/cities_controller.rb +2 -6
  18. data/app/controllers/ahoy_captain/countries_controller.rb +2 -6
  19. data/app/controllers/ahoy_captain/devices_controller.rb +3 -6
  20. data/app/controllers/ahoy_captain/entry_pages_controller.rb +2 -4
  21. data/app/controllers/ahoy_captain/exit_pages_controller.rb +3 -4
  22. data/app/controllers/ahoy_captain/exports_controller.rb +15 -0
  23. data/app/controllers/ahoy_captain/realtimes_controller.rb +1 -1
  24. data/app/controllers/ahoy_captain/regions_controller.rb +3 -7
  25. data/app/controllers/ahoy_captain/sources_controller.rb +2 -5
  26. data/app/controllers/ahoy_captain/stats/base_controller.rb +61 -0
  27. data/app/controllers/ahoy_captain/stats/bounce_rates_controller.rb +3 -2
  28. data/app/controllers/ahoy_captain/stats/total_pageviews_controller.rb +1 -1
  29. data/app/controllers/ahoy_captain/stats/total_visits_controller.rb +1 -1
  30. data/app/controllers/ahoy_captain/stats/unique_visitors_controller.rb +1 -1
  31. data/app/controllers/ahoy_captain/stats/views_per_visits_controller.rb +7 -6
  32. data/app/controllers/ahoy_captain/stats/visit_durations_controller.rb +1 -1
  33. data/app/controllers/ahoy_captain/top_pages_controller.rb +2 -8
  34. data/app/decorators/ahoy_captain/application_decorator.rb +27 -3
  35. data/app/decorators/ahoy_captain/campaign_decorator.rb +8 -0
  36. data/app/decorators/ahoy_captain/city_decorator.rb +12 -0
  37. data/app/decorators/ahoy_captain/country_decorator.rb +10 -0
  38. data/app/decorators/ahoy_captain/device_decorator.rb +13 -2
  39. data/app/decorators/ahoy_captain/page_decorator.rb +11 -0
  40. data/app/decorators/ahoy_captain/region_decorator.rb +16 -0
  41. data/app/decorators/ahoy_captain/source_decorator.rb +7 -0
  42. data/app/models/ahoy_captain/export.rb +48 -0
  43. data/app/presenters/ahoy_captain/dashboard_presenter.rb +24 -16
  44. data/app/presenters/ahoy_captain/funnel_presenter.rb +32 -29
  45. data/app/presenters/ahoy_captain/goals_presenter.rb +32 -23
  46. data/app/queries/ahoy_captain/application_query.rb +5 -2
  47. data/app/queries/ahoy_captain/campaign_query.rb +14 -0
  48. data/app/queries/ahoy_captain/city_query.rb +11 -0
  49. data/app/queries/ahoy_captain/country_query.rb +10 -0
  50. data/app/queries/ahoy_captain/device_query.rb +10 -0
  51. data/app/queries/ahoy_captain/entry_pages_query.rb +3 -2
  52. data/app/queries/ahoy_captain/event_query.rb +16 -1
  53. data/app/queries/ahoy_captain/exit_pages_query.rb +6 -4
  54. data/app/queries/ahoy_captain/region_query.rb +11 -0
  55. data/app/queries/ahoy_captain/source_query.rb +10 -0
  56. data/app/queries/ahoy_captain/stats/average_visit_duration_query.rb +3 -7
  57. data/app/queries/ahoy_captain/stats/bounce_rates_query.rb +9 -6
  58. data/app/queries/ahoy_captain/stats/total_visitors_query.rb +1 -1
  59. data/app/queries/ahoy_captain/stats/unique_visitors_query.rb +1 -1
  60. data/app/queries/ahoy_captain/stats/views_per_visit_query.rb +2 -2
  61. data/app/queries/ahoy_captain/stats/visit_duration_query.rb +4 -4
  62. data/app/queries/ahoy_captain/top_page_query.rb +13 -0
  63. data/app/queries/ahoy_captain/visit_query.rb +12 -12
  64. data/app/views/ahoy_captain/goals/index.html.erb +5 -5
  65. data/app/views/ahoy_captain/roots/show.html.erb +1 -1
  66. data/app/views/ahoy_captain/stats/base/index.html.erb +9 -1
  67. data/app/views/ahoy_captain/stats/show.html.erb +1 -1
  68. data/config/routes.rb +1 -0
  69. data/lib/ahoy_captain/ahoy/event_methods.rb +1 -1
  70. data/lib/ahoy_captain/configuration.rb +2 -2
  71. data/lib/ahoy_captain/engine.rb +1 -0
  72. data/lib/ahoy_captain/goals.rb +8 -4
  73. data/lib/ahoy_captain/period_collection.rb +1 -1
  74. data/lib/ahoy_captain/version.rb +1 -1
  75. data/lib/generators/ahoy_captain/migration_generator.rb +21 -0
  76. data/lib/generators/ahoy_captain/templates/config.rb.tt +18 -3
  77. data/lib/generators/ahoy_captain/templates/migration.rb.tt +7 -0
  78. metadata +42 -4
  79. data/app/models/ahoy_captain/current.rb +0 -9
  80. data/app/models/ahoy_captain/url_helpers.rb +0 -6
@@ -1,25 +1,25 @@
1
- import {Controller} from "@hotwired/stimulus"
1
+ import { Controller } from '@hotwired/stimulus';
2
2
 
3
3
  export default class extends Controller {
4
4
  connect() {
5
5
  this.queryString = new URLSearchParams(window.location.search);
6
- this.baseURL = window.location.pathname.replace(/\/$/, "");
6
+ this.baseURL = window.location.pathname.replace(/\/$/, '');
7
7
  }
8
-
8
+
9
9
  navigate() {
10
10
  const url = `${this.baseURL}?${this.queryString.toString()}`;
11
- Turbo.visit(url, { action: "replace"})
11
+ Turbo.visit(url, { action: 'replace' });
12
12
  }
13
13
 
14
14
  addQueryParam({ detail: { paramKey, paramValue } }) {
15
- this.queryString.append(paramKey, paramValue)
15
+ this.queryString.append(paramKey, paramValue);
16
16
  }
17
-
17
+
18
18
  removeQueryParam({ detail: { paramKey, paramValue } }) {
19
19
  if (!this.queryString.has(paramKey)) {
20
- paramKey += "[]"
20
+ paramKey += '[]';
21
21
  }
22
- this.queryString.delete(paramKey,paramValue)
23
- this.navigate()
22
+ this.queryString.delete(paramKey, paramValue);
23
+ this.navigate();
24
24
  }
25
- }
25
+ }
@@ -1,13 +1,12 @@
1
- import {Controller} from "@hotwired/stimulus"
1
+ import { Controller } from '@hotwired/stimulus';
2
2
 
3
3
  export default class extends Controller {
4
- static targets = ["label"]
4
+ static targets = ['label'];
5
5
 
6
6
  connect() {
7
- this.reload = this.reload.bind(this)
8
- this.setLabel = this.setLabel.bind(this)
7
+ this.reload = this.reload.bind(this);
8
+ this.setLabel = this.setLabel.bind(this);
9
9
  this.labelCount = 0;
10
-
11
10
  }
12
11
 
13
12
  reload() {
@@ -17,11 +16,11 @@ export default class extends Controller {
17
16
 
18
17
  setLabel() {
19
18
  this.labelTarget.title = `Last updated ${this.labelCount} seconds ago`;
20
- this.labelCount += 1
19
+ this.labelCount += 1;
21
20
  }
22
21
 
23
22
  disconnect() {
24
- clearInterval(this.labelInterval)
25
- clearInterval(this.reloadInterval)
23
+ clearInterval(this.labelInterval);
24
+ clearInterval(this.reloadInterval);
26
25
  }
27
26
  }
@@ -1,5 +1,3 @@
1
-
2
-
3
1
  module AhoyCaptain
4
2
  module Limitable
5
3
  private
@@ -22,19 +20,25 @@ module AhoyCaptain
22
20
 
23
21
  layout 'ahoy_captain/layouts/application'
24
22
 
25
- before_action do
26
- Current.request = self
23
+ # show the details frame
24
+ before_action :use_details_frame
25
+
26
+ # act like an spa without being an spa
27
+ before_action :act_like_an_spa
28
+
29
+ rescue_from Widget::WidgetDisabled do |e|
30
+ render template: 'ahoy_captain/shared/widget_disabled', locals: { frame: e.frame }
27
31
  end
28
32
 
29
- # show the details frame
30
- before_action do
33
+ private
34
+
35
+ def use_details_frame
31
36
  if request.headers['Turbo-Frame'] == 'details'
32
37
  request.variant = :details
33
38
  end
34
39
  end
35
40
 
36
- # act like an spa without being an spa
37
- before_action do
41
+ def act_like_an_spa
38
42
  if request.format.html? && request.headers['Turbo-Frame'].blank?
39
43
  if request.path != root_path
40
44
  requested_params = Rails.application.routes.recognize_path(request.path).except(:controller, :action)
@@ -46,12 +50,6 @@ module AhoyCaptain
46
50
  end
47
51
  end
48
52
 
49
- rescue_from Widget::WidgetDisabled do |e|
50
- render template: 'ahoy_captain/shared/widget_disabled', locals: { frame: e.frame }
51
- end
52
-
53
- private
54
-
55
53
  def visit_query
56
54
  VisitQuery.call(params)
57
55
  end
@@ -60,6 +58,7 @@ module AhoyCaptain
60
58
  EventQuery.call(params)
61
59
  end
62
60
 
61
+ # Only paginate details requests requests
63
62
  def paginate(collection)
64
63
  if paginate?
65
64
  pagy, results = pagy(collection, page: params[:page])
@@ -75,7 +74,10 @@ module AhoyCaptain
75
74
  end
76
75
 
77
76
  def cached(*names)
78
- AhoyCaptain.cache.fetch("ahoy_captain:#{names.join(":")}:#{request.query_parameters.sort.map { |k,v| "#{k}-#{v}" }.join(":")}", expire_in: AhoyCaptain.config.cache.ttl) do
77
+ if AhoyCaptain.cache.class == ActiveSupport::Cache::NullStore
78
+ return yield
79
+ end
80
+ AhoyCaptain.cache.fetch("ahoy_captain:#{names.join(":")}:#{request.query_parameters.sort.map { |k,v| "#{k}-#{v}" }.join(":")}", expire_in: AhoyCaptain.config.cache[:ttl]) do
79
81
  yield
80
82
  end
81
83
  end
@@ -10,17 +10,9 @@ module AhoyCaptain
10
10
 
11
11
  def index
12
12
  results = cached(:campaigns, params[:campaigns_type]) do
13
- visit_query
14
- .select(
15
- "COALESCE(#{params[:campaigns_type]}, 'Direct/None') as label",
16
- "count(COALESCE(#{params[:campaigns_type]}, 'Direct/None')) as count",
17
- "sum(count(COALESCE(#{params[:campaigns_type]}, 'Direct/None'))) OVER() as total_count"
18
- )
19
- .group("COALESCE(#{params[:campaigns_type]}, 'Direct/None')")
20
- .order(Arel.sql("count(COALESCE(#{params[:campaigns_type]}, 'Direct/None')) desc"))
21
- .limit(limit)
13
+ CampaignQuery.call(params).limit(limit)
22
14
  end
23
- @campaigns = paginate(results).map { |campaign| CampaignDecorator.new(campaign) }
15
+ @campaigns = paginate(results).map { |campaign| CampaignDecorator.new(campaign, self) }
24
16
  @campaign_type = params[:campaigns_type]&.titleize&.gsub("Utm", "UTM")
25
17
  end
26
18
  end
@@ -10,15 +10,11 @@ module AhoyCaptain
10
10
 
11
11
  def index
12
12
  results = cached(:cities) do
13
- visit_query
14
- .select("city, country, count(concat(city, region, country)) as count, sum(count(concat(city, region, country))) over() as total_count")
15
- .where.not(city: nil)
16
- .group("city, region, country")
17
- .order(Arel.sql "count(concat(city, region, country)) desc")
13
+ CityQuery.call(params)
18
14
  .limit(limit)
19
15
  end
20
16
 
21
- @cities = paginate(results).map { |city| CityDecorator.new(city) }
17
+ @cities = paginate(results).map { |city| CityDecorator.new(city, self) }
22
18
  end
23
19
  end
24
20
  end
@@ -1,7 +1,6 @@
1
1
  module AhoyCaptain
2
2
  class CountriesController < ApplicationController
3
3
  include Limitable
4
- include Rangeable
5
4
 
6
5
  before_action do
7
6
  if Widget.disabled?(:locations, :countries)
@@ -11,14 +10,11 @@ module AhoyCaptain
11
10
 
12
11
  def index
13
12
  results = cached(:countries) do
14
- visit_query
15
- .reselect("country as label, count(country) as count, sum(count(country)) OVER() as total_count")
16
- .group("country")
17
- .order("count(country) desc")
13
+ CountryQuery.call(params)
18
14
  .limit(limit)
19
15
  end
20
16
 
21
- @countries = paginate(results).map { |country| CountryDecorator.new(country) }
17
+ @countries = paginate(results).map { |country| CountryDecorator.new(country, self) }
22
18
  end
23
19
  end
24
20
  end
@@ -10,14 +10,11 @@ module AhoyCaptain
10
10
 
11
11
  def index
12
12
  results = cached(:devices, params[:devices_type]) do
13
- visit_query
14
- .select("#{params[:devices_type]} as label", "count(#{params[:devices_type]}) as count", "sum(count(#{params[:devices_type]})) over() as total_count")
15
- .group(params[:devices_type])
16
- .order("count(#{params[:devices_type]}) desc")
17
- .limit(limit)
13
+ DeviceQuery.call(params)
14
+ .limit(limit)
18
15
  end
19
16
 
20
- @devices = results.map { |device| DeviceDecorator.new(device) }
17
+ @devices = results.map { |device| DeviceDecorator.new(device, self) }
21
18
  end
22
19
  end
23
20
  end
@@ -10,12 +10,10 @@ module AhoyCaptain
10
10
 
11
11
  def index
12
12
  results = cached(:entry_pages) do
13
- EntryPagesQuery.call(params, event_query)
14
- .order(Arel.sql "count(#{AhoyCaptain.config.event[:url_column]}) desc")
15
- .limit(limit)
13
+ EntryPagesQuery.call(params).limit(limit)
16
14
  end
17
15
 
18
- @pages = paginate(results).map { |page| EntryPageDecorator.new(page) }
16
+ @pages = paginate(results).map { |page| EntryPageDecorator.new(page, self) }
19
17
  end
20
18
  end
21
19
  end
@@ -10,11 +10,10 @@ module AhoyCaptain
10
10
 
11
11
  def index
12
12
  results = cached(:exit_pages) do
13
- ExitPagesQuery.call(params, event_query)
14
- .order(Arel.sql "count(#{AhoyCaptain.config.event[:url_column]}) desc")
15
- .limit(limit)
13
+ ExitPagesQuery.call(params)
14
+ .limit(limit)
16
15
  end
17
- @pages = paginate(results).map { |page| ExitPageDecorator.new(page) }
16
+ @pages = paginate(results).map { |page| ExitPageDecorator.new(page, self) }
18
17
  end
19
18
  end
20
19
  end
@@ -0,0 +1,15 @@
1
+ module AhoyCaptain
2
+ class ExportsController < ApplicationController
3
+ skip_before_action :act_like_an_spa
4
+ include Rangeable
5
+
6
+ def show
7
+ export = Export.new(params, self).build
8
+ file = export.to_zip
9
+ send_data file.read,
10
+ type: 'application/zip',
11
+ disposition: 'attachment',
12
+ filename: "AhoyCaptain export #{request.host} #{range[0].to_date} to #{range[1].to_date}.zip"
13
+ end
14
+ end
15
+ end
@@ -1,7 +1,7 @@
1
1
  module AhoyCaptain
2
2
  class RealtimesController < ApplicationController
3
3
  def show
4
- @total = event_query.where('time > ?', 1.minute.ago).distinct(:visit_id).count(:visit_id)
4
+ @total = event_query.where("#{AhoyCaptain.event.table_name}.time > ?", 1.minute.ago).distinct(:visit_id).count(:visit_id)
5
5
  end
6
6
  end
7
7
  end
@@ -10,15 +10,11 @@ module AhoyCaptain
10
10
 
11
11
  def index
12
12
  results = cached(:regions) do
13
- visit_query
14
- .reselect("region, country, count(concat(region, country)) as count, sum(count(region)) over() as total_count")
15
- .where.not(region: nil)
16
- .group("region, country")
17
- .order(Arel.sql "count(concat(region, country)) desc")
18
- .limit(limit)
13
+ RegionQuery.call(params)
14
+ .limit(limit)
19
15
  end
20
16
 
21
- @regions = paginate(results).map { |region| RegionDecorator.new(region) }
17
+ @regions = paginate(results).map { |region| RegionDecorator.new(region, self) }
22
18
  end
23
19
  end
24
20
  end
@@ -10,14 +10,11 @@ module AhoyCaptain
10
10
 
11
11
  def index
12
12
  results = cached(:sources) do
13
- visit_query.select("substring(referring_domain from '(?:.*://)?(?:www\.)?([^/?]*)') as referring_domain, count(substring(referring_domain from '(?:.*://)?(?:www\.)?([^/?]*)')) as count, sum(count(substring(referring_domain from '(?:.*://)?(?:www\.)?([^/?]*)'))) OVER() as total_count")
14
- .where.not(referring_domain: nil)
15
- .group("substring(referring_domain from '(?:.*://)?(?:www\.)?([^/?]*)')")
16
- .order(Arel.sql "count(substring(referring_domain from '(?:.*://)?(?:www\.)?([^/?]*)')) desc")
13
+ SourceQuery.call(params)
17
14
  .limit(limit)
18
15
  end
19
16
 
20
- @sources = paginate(results).map { |source| AhoyCaptain::SourceDecorator.new(source) }
17
+ @sources = paginate(results).map { |source| AhoyCaptain::SourceDecorator.new(source, self) }
21
18
  end
22
19
  end
23
20
 
@@ -1,6 +1,67 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
3
  class BaseController < ApplicationController
4
+ include Rangeable
5
+
6
+ INTERVAL_PERIOD = {
7
+ "realtime" => ["minute"],
8
+ "day" => ["minute", "hour"],
9
+ "7d" => ["hour", "day"],
10
+ "month" => ["day", "week"],
11
+ "all" => ["day", "week", "month"]
12
+ }
13
+
14
+ INTERVALS = ["minute", "hour", "day", "week", "month"]
15
+ private
16
+
17
+ helper_method :selected_interval
18
+ def selected_interval
19
+ if params[:interval].in?(INTERVALS)
20
+ params[:interval]
21
+ else
22
+ default_interval_for_period
23
+ end
24
+ end
25
+
26
+ def default_interval_for_period
27
+ default_interval_for_date_range(range)
28
+ end
29
+
30
+ def default_interval_for_date_range(range)
31
+ if range[1].nil?
32
+ # assume we're in a realtime
33
+ return INTERVAL_PERIOD["realtime"][0]
34
+ end
35
+ diff = (range[1] - range[0]).seconds
36
+ if diff.in_months > 1
37
+ "month"
38
+ elsif diff.in_days > 0
39
+ "day"
40
+ else
41
+ "hour"
42
+ end
43
+ end
44
+
45
+ helper_method :available_intervals
46
+ def available_intervals
47
+ if range
48
+ return INTERVAL_PERIOD["realtime"] if range[1].nil?
49
+
50
+ diff = (range[1] - range[0]).seconds.in_days
51
+
52
+ if diff == 0
53
+ INTERVAL_PERIOD["day"]
54
+ elsif diff <= 7
55
+ INTERVAL_PERIOD["7d"]
56
+ elsif diff <= 31
57
+ INTERVAL_PERIOD["month"]
58
+ else
59
+ INTERVAL_PERIOD["all"]
60
+ end
61
+ else
62
+ INTERVAL_PERIOD["month"]
63
+ end
64
+ end
4
65
  end
5
66
  end
6
67
  end
@@ -1,9 +1,10 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
3
  class BounceRatesController < BaseController
4
+ # @todo: this is lazy
4
5
  def index
5
- @stats = AhoyCaptain::Stats::BounceRatesQuery.call(params).group_by_day("ahoy_visits.started_at").average("num_events * 100")
6
- #.group_by_day("ahoy_visits.started_at").average("total_events")
6
+ @stats = AhoyCaptain::Stats::BounceRatesQuery.call(params)
7
+ @stats = @stats.group_by_period(selected_interval, "daily_bounce_rate.date").average("bounce_rate")
7
8
  end
8
9
  end
9
10
  end
@@ -2,7 +2,7 @@ module AhoyCaptain
2
2
  module Stats
3
3
  class TotalPageviewsController < BaseController
4
4
  def index
5
- @stats = AhoyCaptain::Stats::TotalPageviewsQuery.call(params).group_by_day(:time).count
5
+ @stats = AhoyCaptain::Stats::TotalPageviewsQuery.call(params).group_by_period(selected_interval, :time).count
6
6
  end
7
7
  end
8
8
  end
@@ -2,7 +2,7 @@ module AhoyCaptain
2
2
  module Stats
3
3
  class TotalVisitsController < BaseController
4
4
  def index
5
- @stats = AhoyCaptain::Stats::TotalVisitorsQuery.call(params).group_by_day(:started_at).count
5
+ @stats = AhoyCaptain::Stats::TotalVisitorsQuery.call(params).group_by_period(selected_interval, :started_at).count
6
6
  end
7
7
  end
8
8
  end
@@ -2,7 +2,7 @@ module AhoyCaptain
2
2
  module Stats
3
3
  class UniqueVisitorsController < BaseController
4
4
  def index
5
- @stats = AhoyCaptain::Stats::UniqueVisitorsQuery.call(params).group_by_day(:started_at).count
5
+ @stats = AhoyCaptain::Stats::UniqueVisitorsQuery.call(params).group_by_period(selected_interval, :started_at).count
6
6
  end
7
7
  end
8
8
  end
@@ -1,13 +1,14 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
3
  class ViewsPerVisitsController < BaseController
4
- include Rangeable
5
-
4
+ # @todo: make me a window func
6
5
  def index
7
- @stats = AhoyCaptain::Stats::ViewsPerVisitQuery.call(params).group_by_day('views_per_visit_table.started_at').average(:views_per_visit)
8
- (range[0]..range[1]).to_a.each do |date|
9
- unless @stats.key?(date.to_date)
10
- @stats[date.to_date] = 2
6
+ @stats = AhoyCaptain::Stats::ViewsPerVisitQuery.call(params).group_by_period(selected_interval, 'views_per_visit_table.started_at').average(:views_per_visit)
7
+ if range[1]
8
+ (range[0].to_date..range[1].to_date).to_a.each do |date|
9
+ unless @stats.key?(date.to_date)
10
+ @stats[date.to_date] = 2
11
+ end
11
12
  end
12
13
  end
13
14
  end
@@ -2,7 +2,7 @@ module AhoyCaptain
2
2
  module Stats
3
3
  class VisitDurationsController < BaseController
4
4
  def index
5
- @stats = AhoyCaptain::Stats::VisitDurationQuery.call(params).group('started_at').average(:duration)
5
+ @stats = AhoyCaptain::Stats::VisitDurationQuery.call(params).group_by_period(selected_interval, 'started_at').average(:duration)
6
6
  end
7
7
  end
8
8
  end
@@ -10,17 +10,11 @@ module AhoyCaptain
10
10
 
11
11
  def index
12
12
  results = cached(:top_pages) do
13
- event_query.with_routes.select(
14
- "#{AhoyCaptain.config.event[:url_column]} as url",
15
- "count(*) as count",
16
- "sum(count(*)) over() as total_count"
17
- )
18
- .group(Arel.sql ("(#{AhoyCaptain.config.event[:url_column]})"))
19
- .order(Arel.sql("count(#{AhoyCaptain.config.event[:url_column]}) desc"))
13
+ TopPageQuery.call(params)
20
14
  .limit(limit)
21
15
  end
22
16
 
23
- @pages = paginate(results).map { |page| TopPageDecorator.new(page) }
17
+ @pages = paginate(results).map { |page| TopPageDecorator.new(page, self) }
24
18
  end
25
19
  end
26
20
  end
@@ -1,15 +1,39 @@
1
+ require 'csv'
2
+
1
3
  module AhoyCaptain
2
4
  class ApplicationDecorator
3
5
  attr_reader :object
4
6
 
5
- def initialize(object)
7
+ def self.to_csv(collection, context)
8
+ rows = collection.map { |row| new(row, context) }
9
+ CSV.generate do |csv|
10
+ csv << csv_map(context.params).keys
11
+
12
+ rows.each do |row|
13
+ items = []
14
+ csv_map.values.each do |attr|
15
+ items << row.send(attr)
16
+ end
17
+
18
+ csv << items
19
+ end
20
+ end
21
+ end
22
+
23
+ def self.csv_map(params = {})
24
+ raise NotImplementedError
25
+ end
26
+
27
+ delegate_missing_to :object
28
+ def initialize(object, context)
6
29
  @object = object
30
+ @context = context
7
31
  end
8
32
 
9
33
  private
10
34
 
11
35
  def h
12
- @h ||= Current.request.view_context
36
+ @h ||= @context.view_context
13
37
  end
14
38
 
15
39
  def params
@@ -28,7 +52,7 @@ module AhoyCaptain
28
52
  end
29
53
 
30
54
  def request
31
- Current.request.request
55
+ @context.request
32
56
  end
33
57
  end
34
58
  end
@@ -1,5 +1,13 @@
1
1
  module AhoyCaptain
2
2
  class CampaignDecorator < ApplicationDecorator
3
+
4
+ def self.csv_map(params = {})
5
+ {
6
+ params[:campaigns_type] => :label,
7
+ "Total" => :unit_amount
8
+ }
9
+ end
10
+
3
11
  def display_name
4
12
  if object.label == "Direct/None"
5
13
  value = ""
@@ -1,10 +1,22 @@
1
1
  module AhoyCaptain
2
2
  class CityDecorator < CountryDecorator
3
+ def self.csv_map(params = {})
4
+ {
5
+ "Country" => :country,
6
+ "City" => :city,
7
+ "Total" => :unit_amount
8
+ }
9
+ end
10
+
3
11
  def display_name
4
12
  search = search_query(country_eq: object.country, city_eq: object.city)
5
13
  frame_link("#{country_emoji(object.country)} #{object.city}", search)
6
14
  end
7
15
 
16
+ def country
17
+ "#{country_emoji(object.country)} #{object.country}"
18
+ end
19
+
8
20
  def unit_amount
9
21
  object.count
10
22
  end
@@ -1,6 +1,16 @@
1
1
  module AhoyCaptain
2
2
  class CountryDecorator < ApplicationDecorator
3
3
  EMOJI_MAP = { "AD": "🇦🇩", "AE": "🇦🇪", "AF": "🇦🇫", "AG": "🇦🇬", "AI": "🇦🇮", "AL": "🇦🇱", "AM": "🇦🇲", "AO": "🇦🇴", "AQ": "🇦🇶", "AR": "🇦🇷", "AS": "🇦🇸", "AT": "🇦🇹", "AU": "🇦🇺", "AW": "🇦🇼", "AX": "🇦🇽", "AZ": "🇦🇿", "BA": "🇧🇦", "BB": "🇧🇧", "BD": "🇧🇩", "BE": "🇧🇪", "BF": "🇧🇫", "BG": "🇧🇬", "BH": "🇧🇭", "BI": "🇧🇮", "BJ": "🇧🇯", "BL": "🇧🇱", "BM": "🇧🇲", "BN": "🇧🇳", "BO": "🇧🇴", "BQ": "🇧🇶", "BR": "🇧🇷", "BS": "🇧🇸", "BT": "🇧🇹", "BV": "🇧🇻", "BW": "🇧🇼", "BY": "🇧🇾", "BZ": "🇧🇿", "CA": "🇨🇦", "CC": "🇨🇨", "CD": "🇨🇩", "CF": "🇨🇫", "CG": "🇨🇬", "CH": "🇨🇭", "CI": "🇨🇮", "CK": "🇨🇰", "CL": "🇨🇱", "CM": "🇨🇲", "CN": "🇨🇳", "CO": "🇨🇴", "CR": "🇨🇷", "CU": "🇨🇺", "CV": "🇨🇻", "CW": "🇨🇼", "CX": "🇨🇽", "CY": "🇨🇾", "CZ": "🇨🇿", "DE": "🇩🇪", "DJ": "🇩🇯", "DK": "🇩🇰", "DM": "🇩🇲", "DO": "🇩🇴", "DZ": "🇩🇿", "EC": "🇪🇨", "EE": "🇪🇪", "EG": "🇪🇬", "EH": "🇪🇭", "ER": "🇪🇷", "ES": "🇪🇸", "ET": "🇪🇹", "FI": "🇫🇮", "FJ": "🇫🇯", "FK": "🇫🇰", "FM": "🇫🇲", "FO": "🇫🇴", "FR": "🇫🇷", "GA": "🇬🇦", "GB": "🇬🇧", "GD": "🇬🇩", "GE": "🇬🇪", "GF": "🇬🇫", "GG": "🇬🇬", "GH": "🇬🇭", "GI": "🇬🇮", "GL": "🇬🇱", "GM": "🇬🇲", "GN": "🇬🇳", "GP": "🇬🇵", "GQ": "🇬🇶", "GR": "🇬🇷", "GS": "🇬🇸", "GT": "🇬🇹", "GU": "🇬🇺", "GW": "🇬🇼", "GY": "🇬🇾", "HK": "🇭🇰", "HM": "🇭🇲", "HN": "🇭🇳", "HR": "🇭🇷", "HT": "🇭🇹", "HU": "🇭🇺", "ID": "🇮🇩", "IE": "🇮🇪", "IL": "🇮🇱", "IM": "🇮🇲", "IN": "🇮🇳", "IO": "🇮🇴", "IQ": "🇮🇶", "IR": "🇮🇷", "IS": "🇮🇸", "IT": "🇮🇹", "JE": "🇯🇪", "JM": "🇯🇲", "JO": "🇯🇴", "JP": "🇯🇵", "KE": "🇰🇪", "KG": "🇰🇬", "KH": "🇰🇭", "KI": "🇰🇮", "KM": "🇰🇲", "KN": "🇰🇳", "KP": "🇰🇵", "KR": "🇰🇷", "KW": "🇰🇼", "KY": "🇰🇾", "KZ": "🇰🇿", "LA": "🇱🇦", "LB": "🇱🇧", "LC": "🇱🇨", "LI": "🇱🇮", "LK": "🇱🇰", "LR": "🇱🇷", "LS": "🇱🇸", "LT": "🇱🇹", "LU": "🇱🇺", "LV": "🇱🇻", "LY": "🇱🇾", "MA": "🇲🇦", "MC": "🇲🇨", "MD": "🇲🇩", "ME": "🇲🇪", "MF": "🇲🇫", "MG": "🇲🇬", "MH": "🇲🇭", "MK": "🇲🇰", "ML": "🇲🇱", "MM": "🇲🇲", "MN": "🇲🇳", "MO": "🇲🇴", "MP": "🇲🇵", "MQ": "🇲🇶", "MR": "🇲🇷", "MS": "🇲🇸", "MT": "🇲🇹", "MU": "🇲🇺", "MV": "🇲🇻", "MW": "🇲🇼", "MX": "🇲🇽", "MY": "🇲🇾", "MZ": "🇲🇿", "NA": "🇳🇦", "NC": "🇳🇨", "NE": "🇳🇪", "NF": "🇳🇫", "NG": "🇳🇬", "NI": "🇳🇮", "NL": "🇳🇱", "NO": "🇳🇴", "NP": "🇳🇵", "NR": "🇳🇷", "NU": "🇳🇺", "NZ": "🇳🇿", "OM": "🇴🇲", "PA": "🇵🇦", "PE": "🇵🇪", "PF": "🇵🇫", "PG": "🇵🇬", "PH": "🇵🇭", "PK": "🇵🇰", "PL": "🇵🇱", "PM": "🇵🇲", "PN": "🇵🇳", "PR": "🇵🇷", "PS": "🇵🇸", "PT": "🇵🇹", "PW": "🇵🇼", "PY": "🇵🇾", "QA": "🇶🇦", "RE": "🇷🇪", "RO": "🇷🇴", "RS": "🇷🇸", "RU": "🇷🇺", "RW": "🇷🇼", "SA": "🇸🇦", "SB": "🇸🇧", "SC": "🇸🇨", "SD": "🇸🇩", "SE": "🇸🇪", "SG": "🇸🇬", "SH": "🇸🇭", "SI": "🇸🇮", "SJ": "🇸🇯", "SK": "🇸🇰", "SL": "🇸🇱", "SM": "🇸🇲", "SN": "🇸🇳", "SO": "🇸🇴", "SR": "🇸🇷", "SS": "🇸🇸", "ST": "🇸🇹", "SV": "🇸🇻", "SX": "🇸🇽", "SY": "🇸🇾", "SZ": "🇸🇿", "TC": "🇹🇨", "TD": "🇹🇩", "TF": "🇹🇫", "TG": "🇹🇬", "TH": "🇹🇭", "TJ": "🇹🇯", "TK": "🇹🇰", "TL": "🇹🇱", "TM": "🇹🇲", "TN": "🇹🇳", "TO": "🇹🇴", "TR": "🇹🇷", "TT": "🇹🇹", "TV": "🇹🇻", "TW": "🇹🇼", "TZ": "🇹🇿", "UA": "🇺🇦", "UG": "🇺🇬", "UM": "🇺🇲", "US": "🇺🇸", "UY": "🇺🇾", "UZ": "🇺🇿", "VA": "🇻🇦", "VC": "🇻🇨", "VE": "🇻🇪", "VG": "🇻🇬", "VI": "🇻🇮", "VN": "🇻🇳", "VU": "🇻🇺", "WF": "🇼🇫", "WS": "🇼🇸", "YE": "🇾🇪", "YT": "🇾🇹", "ZA": "🇿🇦", "ZM": "🇿🇲", "ZW": "🇿🇼" }
4
+ def self.csv_map(params = {})
5
+ {
6
+ "Country" => :label,
7
+ "Total" => :unit_amount
8
+ }
9
+ end
10
+
11
+ def label
12
+ "#{country_emoji(country)} #{country}"
13
+ end
4
14
 
5
15
  def display_name
6
16
  search = search_query(country_eq: object.label)
@@ -1,8 +1,19 @@
1
1
  module AhoyCaptain
2
2
  class DeviceDecorator < ApplicationDecorator
3
+ def self.csv_map(params = {})
4
+ {
5
+ "#{params[:devices_type]}" => :label,
6
+ "Total" => :unit_amount
7
+ }
8
+ end
9
+
3
10
  def display_name
4
- search = search_query("#{params[:devices_type]}_eq" => object.label)
5
- frame_link(object.label, search)
11
+ search = search_query("#{params[:devices_type]}_eq" => label)
12
+ frame_link(label, search)
13
+ end
14
+
15
+ def label
16
+ object.label
6
17
  end
7
18
 
8
19
  def unit_amount
@@ -1,5 +1,16 @@
1
1
  module AhoyCaptain
2
2
  class PageDecorator < ApplicationDecorator
3
+ def self.csv_map(params = {})
4
+ {
5
+ "URL" => :label,
6
+ "Total" => :unit_amount
7
+ }
8
+ end
9
+
10
+ def label
11
+ object.url
12
+ end
13
+
3
14
  def display_name
4
15
  search = search_query(type => object.url)
5
16
  frame_link(object.url, search)