ahoy_captain 0.91 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (137) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +8 -4
  3. data/Rakefile +23 -2
  4. data/app/assets/javascript/ahoy_captain/controllers/application_controller.js +20 -0
  5. data/app/assets/javascript/ahoy_captain/controllers/combobox_controller.js +371 -0
  6. data/app/assets/javascript/ahoy_captain/controllers/filter_modal_controller.js +45 -0
  7. data/app/assets/javascript/ahoy_captain/controllers/frame_link_controller.js +20 -0
  8. data/app/assets/javascript/ahoy_captain/controllers/funnel_chart_controller.js +58 -16
  9. data/app/assets/javascript/ahoy_captain/controllers/interval_controller.js +5 -0
  10. data/app/assets/javascript/ahoy_captain/controllers/line_chart_controller.js +235 -21
  11. data/app/assets/javascript/ahoy_captain/controllers/map_controller.js +47 -0
  12. data/app/assets/javascript/ahoy_captain/controllers/predicate_select_controller.js +1 -1
  13. data/app/assets/javascript/ahoy_captain/controllers/properties_controller.js +8 -0
  14. data/app/assets/javascript/ahoy_captain/controllers/property_filter_controller.js +45 -0
  15. data/app/assets/javascript/ahoy_captain/controllers/realtime_controller.js +4 -2
  16. data/app/assets/javascript/ahoy_captain/controllers/tile_controller.js +33 -0
  17. data/app/assets/javascript/ahoy_captain/helpers/chart_utils.js +156 -0
  18. data/app/assets/javascript/ahoy_captain/helpers/countries.js +2261 -0
  19. data/app/assets/javascript/ahoy_captain/helpers/number_formatters.js +55 -0
  20. data/app/components/ahoy_captain/combobox_component.html.erb +33 -0
  21. data/app/components/ahoy_captain/combobox_component.rb +13 -0
  22. data/app/components/ahoy_captain/comparison_link_component.html.erb +17 -0
  23. data/app/components/ahoy_captain/comparison_link_component.rb +44 -0
  24. data/app/components/ahoy_captain/dropdown_link_component.html.erb +2 -4
  25. data/app/components/ahoy_captain/dropdown_link_component.rb +4 -0
  26. data/app/components/ahoy_captain/filter/dropdown_component.html.erb +8 -6
  27. data/app/components/ahoy_captain/filter/modal_component.html.erb +7 -5
  28. data/app/components/ahoy_captain/filter/select_component.html.erb +23 -21
  29. data/app/components/ahoy_captain/filter/select_component.rb +2 -1
  30. data/app/components/ahoy_captain/filter/tag_component.html.erb +1 -1
  31. data/app/components/ahoy_captain/previous_next_component.html.erb +8 -0
  32. data/app/components/ahoy_captain/previous_next_component.rb +11 -0
  33. data/app/components/ahoy_captain/stats/comparable_container_component.html.erb +25 -0
  34. data/app/components/ahoy_captain/stats/comparable_container_component.rb +86 -0
  35. data/app/components/ahoy_captain/stats/container_component.html.erb +13 -6
  36. data/app/components/ahoy_captain/stats/container_component.rb +15 -1
  37. data/app/components/ahoy_captain/sticky_nav_component.html.erb +27 -20
  38. data/app/components/ahoy_captain/sticky_nav_component.rb +11 -0
  39. data/app/components/ahoy_captain/table_component.rb +13 -4
  40. data/app/components/ahoy_captain/tables/devices_table_component.rb +11 -0
  41. data/app/components/ahoy_captain/tables/dynamic_table.rb +13 -0
  42. data/app/components/ahoy_captain/tables/dynamic_table_component.rb +204 -0
  43. data/app/components/ahoy_captain/tables/goals_table_component.rb +17 -0
  44. data/app/components/ahoy_captain/tables/header_component.html.erb +5 -0
  45. data/app/components/ahoy_captain/tables/header_component.rb +18 -0
  46. data/app/components/ahoy_captain/tables/headers/header_component.rb +4 -0
  47. data/app/components/ahoy_captain/tables/properties_table_component.rb +27 -0
  48. data/app/components/ahoy_captain/tables/row_component.html.erb +4 -0
  49. data/app/components/ahoy_captain/tables/rows/row_component.rb +0 -1
  50. data/app/components/ahoy_captain/tile_component.html.erb +19 -9
  51. data/app/components/ahoy_captain/tile_component.rb +9 -1
  52. data/app/controllers/ahoy_captain/application_controller.rb +7 -16
  53. data/app/controllers/ahoy_captain/exports_controller.rb +1 -2
  54. data/app/controllers/ahoy_captain/filters/base_controller.rb +1 -3
  55. data/app/controllers/ahoy_captain/filters/goals_controller.rb +9 -0
  56. data/app/controllers/ahoy_captain/filters/pages/actions_controller.rb +1 -1
  57. data/app/controllers/ahoy_captain/filters/pages/entry_pages_controller.rb +1 -1
  58. data/app/controllers/ahoy_captain/filters/pages/exit_pages_controller.rb +1 -1
  59. data/app/controllers/ahoy_captain/filters/properties/values_controller.rb +4 -4
  60. data/app/controllers/ahoy_captain/filters/utms_controller.rb +1 -1
  61. data/app/controllers/ahoy_captain/locations/cities_controller.rb +22 -0
  62. data/app/controllers/ahoy_captain/locations/countries_controller.rb +22 -0
  63. data/app/controllers/ahoy_captain/locations/maps_controller.rb +24 -0
  64. data/app/controllers/ahoy_captain/locations/regions_controller.rb +22 -0
  65. data/app/controllers/ahoy_captain/properties_controller.rb +41 -0
  66. data/app/controllers/ahoy_captain/stats/base_controller.rb +86 -5
  67. data/app/controllers/ahoy_captain/stats/bounce_rates_controller.rb +1 -1
  68. data/app/controllers/ahoy_captain/stats/total_pageviews_controller.rb +1 -1
  69. data/app/controllers/ahoy_captain/stats/total_visits_controller.rb +1 -1
  70. data/app/controllers/ahoy_captain/stats/unique_visitors_controller.rb +2 -1
  71. data/app/controllers/ahoy_captain/stats/views_per_visits_controller.rb +1 -10
  72. data/app/controllers/ahoy_captain/stats/visit_durations_controller.rb +1 -1
  73. data/app/helpers/ahoy_captain/application_helper.rb +33 -9
  74. data/app/models/ahoy_captain/comparison_mode.rb +72 -0
  75. data/app/models/ahoy_captain/filter_parser.rb +33 -18
  76. data/app/models/ahoy_captain/range_from_params.rb +78 -0
  77. data/app/models/ahoy_captain/rangeable.rb +0 -3
  78. data/app/models/concerns/ahoy_captain/compare_mode.rb +19 -0
  79. data/app/models/concerns/ahoy_captain/limitable.rb +17 -0
  80. data/app/models/concerns/ahoy_captain/range_options.rb +1 -14
  81. data/app/presenters/ahoy_captain/dashboard_presenter.rb +18 -54
  82. data/app/queries/ahoy_captain/application_query.rb +74 -10
  83. data/app/queries/ahoy_captain/event_query.rb +7 -2
  84. data/app/queries/ahoy_captain/stats/average_views_per_visit_query.rb +11 -4
  85. data/app/queries/ahoy_captain/stats/average_visit_duration_query.rb +14 -2
  86. data/app/queries/ahoy_captain/stats/base_query.rb +18 -0
  87. data/app/queries/ahoy_captain/stats/bounce_rates_query.rb +15 -1
  88. data/app/queries/ahoy_captain/stats/total_pageviews_query.rb +2 -2
  89. data/app/queries/ahoy_captain/stats/total_visitors_query.rb +1 -1
  90. data/app/queries/ahoy_captain/stats/unique_visitors_query.rb +1 -1
  91. data/app/queries/ahoy_captain/stats/views_per_visit_query.rb +1 -1
  92. data/app/queries/ahoy_captain/stats/visit_duration_query.rb +2 -2
  93. data/app/queries/ahoy_captain/visit_query.rb +1 -2
  94. data/app/queries/concerns/ahoy_captain/comparable_queries.rb +25 -0
  95. data/app/queries/concerns/ahoy_captain/comparable_query.rb +138 -0
  96. data/app/queries/concerns/ahoy_captain/lazy_comparable_query.rb +42 -0
  97. data/app/views/ahoy_captain/devices/_table.html.erb +1 -4
  98. data/app/views/ahoy_captain/goals/index.html.erb +1 -4
  99. data/app/views/ahoy_captain/layouts/application.html.erb +0 -1
  100. data/app/views/ahoy_captain/layouts/shared/_tile_loader.html.erb +12 -0
  101. data/app/views/ahoy_captain/locations/maps/show.html.erb +3 -0
  102. data/app/views/ahoy_captain/properties/_form.html.erb +6 -0
  103. data/app/views/ahoy_captain/properties/index.html.erb +3 -0
  104. data/app/views/ahoy_captain/properties/show.html.erb +6 -0
  105. data/app/views/ahoy_captain/roots/_filters.html.erb +47 -1
  106. data/app/views/ahoy_captain/roots/show.html.erb +109 -48
  107. data/app/views/ahoy_captain/stats/base/index.html.erb +37 -8
  108. data/app/views/ahoy_captain/stats/show.html.erb +14 -11
  109. data/config/routes.rb +9 -3
  110. data/lib/ahoy_captain/ahoy/event_methods.rb +12 -14
  111. data/lib/ahoy_captain/configuration.rb +3 -2
  112. data/lib/ahoy_captain/engine.rb +4 -0
  113. data/lib/ahoy_captain/filters_configuration.rb +10 -6
  114. data/lib/ahoy_captain/version.rb +1 -1
  115. data/lib/ahoy_captain.rb +7 -1
  116. data/lib/generators/ahoy_captain/templates/config.rb.tt +7 -0
  117. metadata +137 -22
  118. data/app/assets/javascript/ahoy_captain/controllers/active_links_controller.js +0 -18
  119. data/app/assets/javascript/ahoy_captain/controllers/filter_tag_controller.js +0 -20
  120. data/app/assets/javascript/ahoy_captain/controllers/search_select_controller.js +0 -65
  121. data/app/components/ahoy_captain/tables/headers/devices_header_component.html.erb +0 -3
  122. data/app/components/ahoy_captain/tables/headers/devices_header_component.rb +0 -9
  123. data/app/components/ahoy_captain/tables/headers/goals_header_component.html.erb +0 -6
  124. data/app/components/ahoy_captain/tables/headers/goals_header_component.rb +0 -9
  125. data/app/components/ahoy_captain/tables/rows/devices_row_component.html.erb +0 -5
  126. data/app/components/ahoy_captain/tables/rows/devices_row_component.rb +0 -12
  127. data/app/components/ahoy_captain/tables/rows/goals_row_component.html.erb +0 -11
  128. data/app/components/ahoy_captain/tables/rows/goals_row_component.rb +0 -20
  129. data/app/controllers/ahoy_captain/cities_controller.rb +0 -20
  130. data/app/controllers/ahoy_captain/countries_controller.rb +0 -20
  131. data/app/controllers/ahoy_captain/regions_controller.rb +0 -20
  132. /data/app/views/ahoy_captain/{cities → locations/cities}/index.html+details.erb +0 -0
  133. /data/app/views/ahoy_captain/{cities → locations/cities}/index.html.erb +0 -0
  134. /data/app/views/ahoy_captain/{countries → locations/countries}/index.html+details.erb +0 -0
  135. /data/app/views/ahoy_captain/{countries → locations/countries}/index.html.erb +0 -0
  136. /data/app/views/ahoy_captain/{regions → locations/regions}/index.html+details.erb +0 -0
  137. /data/app/views/ahoy_captain/{regions → locations/regions}/index.html.erb +0 -0
@@ -1,8 +1,9 @@
1
+ require_relative '../concerns/ahoy_captain/comparable_queries'
2
+
1
3
  module AhoyCaptain
2
4
  class ApplicationQuery
3
5
  # if you want to enforce returning a relation
4
6
  class_attribute :strict, default: true
5
- include Rangeable
6
7
 
7
8
  delegate_missing_to :@query
8
9
 
@@ -63,7 +64,7 @@ module AhoyCaptain
63
64
  event: (AhoyCaptain.event.ransackable_attributes + AhoyCaptain.event.ransackable_scopes).map(&:to_s),
64
65
  }
65
66
 
66
- pattern = /(?:_not_eq|_eq|_in|_not_in|_cont|_not_cont|_i_cont)$/
67
+ pattern = /(?:#{Ransack.predicates.sorted_names_with_underscores.to_h.values.join("|")})$/
67
68
  params[:q].each do |key, value|
68
69
  attribute_name = key.gsub(pattern, '')
69
70
  if type == :event && (ransackable_attributes[:visit].include?(attribute_name) || ransackable_attributes[:visit].include?(key))
@@ -76,35 +77,57 @@ module AhoyCaptain
76
77
  end
77
78
  end
78
79
 
80
+ merge_params = {}
81
+ ransackable_params.each do |k,v|
82
+ transform = false
83
+ if v == AhoyCaptain.none.value
84
+ transform = true
85
+ elsif v.is_a?(Array) && v[0] == AhoyCaptain.none.value
86
+ transform = true
87
+ end
88
+
89
+ if transform
90
+ key = k.dup
91
+ ransackable_params.delete(key)
92
+ predicate = Ransack::Predicate.detect_and_strip_from_string!(key)
93
+ if predicate.include?("not")
94
+ merge_params["#{key}_not_null"] = '1'
95
+ else
96
+ merge_params["#{key}_null"] = '1'
97
+ end
98
+ end
99
+ end
100
+
101
+ ransackable_params.merge!(merge_params)
79
102
  # send the right format
80
103
  # ::Ahoy::Visit.ransack(events_time_lt: Time.now).result.to_sql
81
104
  # is not
82
105
  # ::Ahoy::Visit.ransack(events_time_lt: Time.now.to_i).result.to_sql
83
106
  if range
84
107
  if type == :event
85
- if range.size == 2
108
+ if range.realtime?
86
109
  ransackable_params['time_gt'] = range[0]
87
- ransackable_params['time_lt'] = range[1]
88
110
  ransackable_params["visit_started_at_gt"] = range[0]
89
- ransackable_params["visit_started_at_lt"] = range[1]
90
111
  else
91
112
  ransackable_params['time_gt'] = range[0]
113
+ ransackable_params['time_lt'] = range[1]
92
114
  ransackable_params["visit_started_at_gt"] = range[0]
115
+ ransackable_params["visit_started_at_lt"] = range[1]
93
116
  end
94
117
  elsif type == :visit
95
- if range.size == 2
118
+ if range.realtime?
119
+ ransackable_params["started_at_gt"] = range[0]
120
+ ransackable_params["events_time_gt"] = range[0]
121
+ else
96
122
  ransackable_params["started_at_gt"] = range[0]
97
123
  ransackable_params["started_at_lt"] = range[1]
98
124
  ransackable_params["events_time_gteq"] = range[0]
99
125
  ransackable_params["events_time_lteq"] = range[1]
100
- else
101
- ransackable_params["started_at_gt"] = range[0]
102
- ransackable_params["events_time_gt"] = range[0]
103
126
  end
104
127
  end
105
128
  end
106
129
 
107
- ransackable_params
130
+ ransackify(ransackable_params, type)
108
131
  end
109
132
 
110
133
  # merge both sets of ransackable params and ensure that they're being set on the correct association
@@ -118,5 +141,46 @@ module AhoyCaptain
118
141
  raise ArgumentError, "use ransack_params_for(type)"
119
142
  end
120
143
  end
144
+
145
+ def ransackify(query, type)
146
+ return unless query
147
+
148
+ query = query.try(:permit!).try(:to_h) unless query.is_a?(Hash)
149
+ obj = query.each_with_object({}) do |(k, v), obj|
150
+ if k.starts_with?('properties.')
151
+ field = k.split('properties.').last
152
+ operation = Ransack::Predicate.detect_and_strip_from_string!(field)
153
+
154
+ raise ArgumentError, "No valid predicate for #{field}" unless operation
155
+
156
+ prefix = type == :event ? "" : "events_"
157
+ obj[:c] ||= []
158
+
159
+ obj[:c] << {
160
+ a: {
161
+ '0' => {
162
+ name: "#{prefix}properties",
163
+ ransacker_args: field
164
+ }
165
+ },
166
+ p: operation,
167
+ v: [v]
168
+ }
169
+
170
+ else
171
+ obj[k] = v
172
+ end
173
+ end
174
+
175
+ if type == :event
176
+ return obj
177
+ else
178
+ return obj
179
+ end
180
+ end
181
+
182
+ def range
183
+ RangeFromParams.from_params(params)
184
+ end
121
185
  end
122
186
  end
@@ -1,6 +1,5 @@
1
1
  module AhoyCaptain
2
2
  class EventQuery < ApplicationQuery
3
- include Rangeable
4
3
 
5
4
  def build
6
5
  entry_pages = ransack_params_for(:event).select { |k,v| k.start_with?("entry_page") }
@@ -13,7 +12,7 @@ module AhoyCaptain
13
12
  ransack_params_for(:event).reject { |k,v| k.start_with?("visit_") }, context: shared_context
14
13
  )
15
14
 
16
- visit_params = ransack_params_for(:visit).reject { |k,v| k.start_with?("event_") || k.start_with?("events_") }.transform_keys { |key| "visit_#{key}" }
15
+ visit_params = ransack_params_for(:visit).reject { |k,v| k.start_with?("event_") || k.start_with?("events_") || k == :c }.transform_keys { |key| "visit_#{key}" }
17
16
  search_children = AhoyCaptain.visit.ransack(
18
17
  visit_params, context: shared_context
19
18
  )
@@ -33,5 +32,11 @@ module AhoyCaptain
33
32
 
34
33
  joined.where(shared_conditions.reduce(&:and))
35
34
  end
35
+
36
+ def page_view
37
+ @query = @query.page_view
38
+
39
+ self
40
+ end
36
41
  end
37
42
  end
@@ -1,12 +1,19 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
3
  # pls fix
4
- class AverageViewsPerVisitQuery < ApplicationQuery
4
+ class AverageViewsPerVisitQuery < BaseQuery
5
5
  def build
6
- event_query.joins(:visit)
7
- .where(name: "$view")
8
- .group("#{AhoyCaptain.visit.table_name}.id")
6
+ subquery = event_query.select("count(ahoy_events.visit_id) as count").where(name: "$view").group(:visit_id)
9
7
 
8
+ AhoyCaptain.event.select("count").from("(#{subquery.to_sql}) as events")
9
+ end
10
+
11
+ def self.cast_type(_column)
12
+ nil
13
+ end
14
+
15
+ def self.cast_value(_, value)
16
+ value.to_i
10
17
  end
11
18
  end
12
19
  end
@@ -1,11 +1,23 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
- class AverageVisitDurationQuery < ApplicationQuery
3
+ class AverageVisitDurationQuery < BaseQuery
4
4
  def build
5
5
  max_events = event_query.select("#{AhoyCaptain.event.table_name}.visit_id, max(#{AhoyCaptain.event.table_name}.time) as created_at").group("visit_id")
6
- visit_query.select("avg((max_events.created_at - #{AhoyCaptain.visit.table_name}.started_at)) as average_visit_duration")
6
+ visit_query.select("avg((max_events.created_at - #{AhoyCaptain.visit.table_name}.started_at))::interval as average_visit_duration")
7
7
  .joins("LEFT JOIN (#{max_events.to_sql}) as max_events ON #{AhoyCaptain.visit.table_name}.id = max_events.visit_id")
8
8
  end
9
+
10
+ def self.cast_type(value)
11
+ ActiveRecord::Type.lookup(:string)
12
+ end
13
+
14
+ def self.cast_value(_type, value)
15
+ if value.present?
16
+ ActiveSupport::Duration.parse(value)
17
+ else
18
+ ActiveSupport::Duration.parse("P0MT0S")
19
+ end
20
+ end
9
21
  end
10
22
  end
11
23
  end
@@ -0,0 +1,18 @@
1
+ module AhoyCaptain
2
+ module Stats
3
+ class BaseQuery < ApplicationQuery
4
+ include ComparableQuery
5
+ include LazyComparableQuery
6
+
7
+ protected
8
+
9
+ def self.cast_type(column)
10
+ ActiveRecord::Type.lookup(::Ahoy::Visit.columns_hash[column.to_s].type)
11
+ end
12
+
13
+ def self.cast_value(type, value)
14
+ type.cast(value)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,6 +1,6 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
- class BounceRatesQuery < ApplicationQuery
3
+ class BounceRatesQuery < BaseQuery
4
4
  def build
5
5
  total_visits = visit_query.select("date(#{AhoyCaptain.visit.table_name}.started_at) as date, count(*) as count").group("date(#{AhoyCaptain.visit.table_name}.started_at)")
6
6
  subquery = visit_query.select(:id, :started_at).joins(:events).group("#{AhoyCaptain.visit.table_name}.id, #{AhoyCaptain.visit.table_name}.started_at").having("count(#{AhoyCaptain.event.table_name}.id) = 1")
@@ -12,6 +12,20 @@ module AhoyCaptain
12
12
  ::Ahoy::Visit.with(total_visits: total_visits, single_page_visits: single_page_visits, daily_bounce_rate: daily_bounce_rate).select("bounce_rate, date").from("daily_bounce_rate")
13
13
  end
14
14
 
15
+ protected
16
+
17
+ def self.cast_type(column)
18
+ ActiveRecord::Type.lookup(:decimal)
19
+ end
20
+
21
+ def self.cast_value(type, value)
22
+ if value.blank?
23
+ return 0.to_d
24
+ end
25
+
26
+ super.round(2)
27
+ end
28
+
15
29
  end
16
30
  end
17
31
  end
@@ -1,8 +1,8 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
- class TotalPageviewsQuery < ApplicationQuery
3
+ class TotalPageviewsQuery < BaseQuery
4
4
  def build
5
- event_query.where(name: AhoyCaptain.config.event[:view_name])
5
+ event_query.page_view
6
6
  end
7
7
  end
8
8
  end
@@ -1,6 +1,6 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
- class TotalVisitorsQuery < ApplicationQuery
3
+ class TotalVisitorsQuery < BaseQuery
4
4
  def build
5
5
  visit_query.distinct.select(:id)
6
6
  end
@@ -1,6 +1,6 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
- class UniqueVisitorsQuery < ApplicationQuery
3
+ class UniqueVisitorsQuery < BaseQuery
4
4
  def build
5
5
  visit_query.distinct.select(:visitor_token)
6
6
  end
@@ -1,6 +1,6 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
- class ViewsPerVisitQuery < ApplicationQuery
3
+ class ViewsPerVisitQuery < BaseQuery
4
4
  def build
5
5
  events = event_query
6
6
  .joins(:visit)
@@ -1,13 +1,13 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
- class VisitDurationQuery < ApplicationQuery
3
+ class VisitDurationQuery < BaseQuery
4
4
  def build
5
5
  events = event_query
6
6
  .reselect("max(#{AhoyCaptain.event.table_name}.time) - min(#{AhoyCaptain.event.table_name}.time) as duration, #{AhoyCaptain.event.table_name}.visit_id")
7
7
  .group("#{AhoyCaptain.event.table_name}.visit_id")
8
8
 
9
9
  ::Ahoy::Visit
10
- .select("duration as duration, started_at")
10
+ .select("duration::duration as duration, started_at")
11
11
  .from(events, :views_per_visit_table)
12
12
  .joins("inner join #{AhoyCaptain.visit.table_name} on #{AhoyCaptain.visit.table_name}.id = views_per_visit_table.visit_id")
13
13
  end
@@ -1,6 +1,5 @@
1
1
  module AhoyCaptain
2
2
  class VisitQuery < ApplicationQuery
3
- include Rangeable
4
3
 
5
4
  def build
6
5
  shared_context = Ransack::Context.for(AhoyCaptain.visit)
@@ -9,7 +8,7 @@ module AhoyCaptain
9
8
  ransack_params_for(:visit).reject { |k,v| k.start_with?("events_") }, context: shared_context
10
9
  )
11
10
  search_children = AhoyCaptain.event.ransack(
12
- ransack_params_for(:event).reject { |k,v| k.start_with?("visit_") }.transform_keys { |key| "events_#{key}" }, context: shared_context
11
+ ransack_params_for(:event).reject { |k,v| k.start_with?("visit_") || k == :c }.transform_keys { |key| "events_#{key}" }, context: shared_context
13
12
  )
14
13
 
15
14
  shared_conditions = [search_parents, search_children].map { |search|
@@ -0,0 +1,25 @@
1
+ module AhoyCaptain
2
+ module ComparableQueries
3
+ def compare_range
4
+ @compare_range ||= begin
5
+ ComparisonMode.new(@params).compared_to_range
6
+ end
7
+ end
8
+
9
+ def range
10
+ @range ||= @query.send(:range)
11
+ end
12
+
13
+ def comparison_params
14
+ params = @params.deep_dup
15
+ params.delete("period")
16
+
17
+ params[:start_date] = compare_range[0]
18
+ params[:end_date] = compare_range[1]
19
+ params
20
+ end
21
+ end
22
+ end
23
+
24
+ require_relative './lazy_comparable_query'
25
+ require_relative './comparable_query'
@@ -0,0 +1,138 @@
1
+ module AhoyCaptain
2
+ module ComparableQuery
3
+ class Comparison
4
+ class ComparisonResult
5
+ attr_reader :current, :compared_to
6
+ def initialize(current, compared_to)
7
+ @current = current
8
+ @compared_to = compared_to
9
+ end
10
+ end
11
+
12
+ def initialize(query)
13
+ @query = query
14
+ @params = @query.params.deep_dup
15
+ @compare = @query.class.call(comparison_params)
16
+ @model = @query.all.klass
17
+ @query_class = @query.class
18
+ end
19
+
20
+ def count(column_name = :id)
21
+ @operation = :count
22
+ @column = column_name
23
+ perform_calculations(:count, column_name)
24
+
25
+ self
26
+ end
27
+
28
+ def average(column_name)
29
+ @operation = :average
30
+ @column = column_name
31
+
32
+ perform_calculations(:average, column_name)
33
+
34
+ self
35
+ end
36
+
37
+ def perform_calculations(operation, column_name)
38
+ @query = perform_calculation(@query, operation, column_name)
39
+ @compare = perform_calculation(@compare, operation, column_name)
40
+ end
41
+
42
+ def perform_calculation(q, operation, column_name)
43
+ operation = operation.to_s.downcase
44
+ # If #count is used with #distinct (i.e. `relation.distinct.count`) it is
45
+ # considered distinct.
46
+ distinct = q.send(:distinct_value)
47
+
48
+ if operation == "count"
49
+ column_name ||= q.send(:select_for_count)
50
+ if column_name == :all
51
+ if !distinct
52
+ distinct = q.send(:distinct_select?, :select_for_count) if q.group_values.empty?
53
+ elsif q.send(:group_values).any? || q.send(:select_values).empty? && q.order_values.empty?
54
+ column_name = q.primary_key
55
+ end
56
+ elsif q.all.send(:distinct_select?, column_name)
57
+ distinct = nil
58
+ end
59
+ end
60
+
61
+ if q.group_values.any?
62
+ raise "use a subquery"
63
+ else
64
+ execute_simple_calculation(q, operation, column_name, distinct)
65
+ end
66
+ end
67
+
68
+ def result
69
+ result = @model.with(
70
+ current: @query.to_sql,
71
+ compare: @compare.to_sql
72
+ ).select("current, compare").from("current, compare")[0]
73
+ type = @query_class.cast_type(@column)
74
+
75
+ if result
76
+ current = @query_class.cast_value(type, result.current[1...-1])
77
+ compare = @query_class.cast_value(type, result.compare[1...-1])
78
+ else
79
+ current = @query_class.cast_value(type, '0')
80
+ compare = @query_class.cast_value(type, '0')
81
+ end
82
+
83
+ @result = ComparisonResult.new((current), (compare))
84
+ end
85
+
86
+ def compare_range
87
+ @compare_range ||= begin
88
+ og_range = range
89
+ [og_range[0] - (og_range[1] - og_range[0]), og_range[0]]
90
+ end
91
+
92
+ end
93
+
94
+ def range
95
+ @range ||= @query.send(:range)
96
+ end
97
+
98
+ private
99
+
100
+ def execute_simple_calculation(q, operation, column_name, distinct) # :nodoc:
101
+ if operation == "count" && (column_name == :all && distinct || q.has_limit_or_offset?)
102
+ # Shortcut when limit is zero.
103
+ return 0 if q.limit_value == 0
104
+
105
+ q.send(:build_count_subquery, q.spawn, column_name, distinct)
106
+ else
107
+ # PostgreSQL doesn't like ORDER BY when there are no GROUP BY
108
+ relation = q.all.unscope(:order).distinct!(false)
109
+
110
+ column = q.all.send(:aggregate_column, column_name)
111
+ select_value = q.all.send(:operation_over_aggregate_column, column, operation, distinct)
112
+ select_value.distinct = true if operation == "sum" && distinct
113
+
114
+ relation.select_values = [select_value]
115
+
116
+ relation.arel
117
+ end
118
+ end
119
+
120
+ def comparison_params
121
+ params = @params.deep_dup
122
+ params.delete("period")
123
+
124
+ params[:start_date] = compare_range[0]
125
+ params[:end_date] = compare_range[1]
126
+ params
127
+ end
128
+ end
129
+
130
+ def with_comparison(enabled = false)
131
+ if enabled
132
+ Comparison.new(self)
133
+ else
134
+ self
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,42 @@
1
+ module AhoyCaptain
2
+ module LazyComparableQuery
3
+ class LazyComparison
4
+ class LazyComparisonResult
5
+ attr_accessor :current, :compared_to
6
+ end
7
+
8
+ include ::AhoyCaptain::ComparableQueries
9
+
10
+ def initialize(query)
11
+ @query = query
12
+ @params = @query.params.deep_dup
13
+ @compare = @query.class.call(comparison_params)
14
+ end
15
+
16
+ def method_missing(name, *args)
17
+ perform_operations(name, *args)
18
+ end
19
+
20
+ def perform_operations(name, *args)
21
+ @query = @query.public_send(name, *args)
22
+ @compare = @compare.public_send(name, *args)
23
+
24
+ self
25
+ end
26
+
27
+ def result
28
+ return @result if @result
29
+
30
+ @result = LazyComparisonResult.new.tap { |comparison| comparison.current = @query; comparison.compared_to = @compare }
31
+ end
32
+ end
33
+
34
+ def with_lazy_comparison(enabled = false)
35
+ if enabled
36
+ LazyComparison.new(self)
37
+ else
38
+ self
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,5 +1,2 @@
1
1
  <%= render AhoyCaptain::TableComponent.new(items: @devices,
2
- category_name: 'Devices',
3
- unit_name: 'Visitors',
4
- header: ::AhoyCaptain::Tables::Headers::DevicesHeaderComponent,
5
- row: ::AhoyCaptain::Tables::Rows::DevicesRowComponent) %>
2
+ table: ::AhoyCaptain::Tables::DevicesTableComponent) %>
@@ -1,7 +1,4 @@
1
1
  <%= turbo_frame_tag :goals do %>
2
2
  <%= render AhoyCaptain::TableComponent.new(items: @presenter.goals,
3
- header: ::AhoyCaptain::Tables::Headers::GoalsHeaderComponent,
4
- row: ::AhoyCaptain::Tables::Rows::GoalsRowComponent) %>
5
-
6
-
3
+ table: ::AhoyCaptain::Tables::GoalsTableComponent) %>
7
4
  <% end %>
@@ -21,7 +21,6 @@
21
21
  <meta name="theme-color" content="#ffffff">
22
22
  <link href="https://cdn.jsdelivr.net/npm/daisyui@3.5.0/dist/full.css" rel="stylesheet" type="text/css"/>
23
23
  <script src="https://cdn.tailwindcss.com"></script>
24
- <link href="https://unpkg.com/slim-select@latest/dist/slimselect.css" rel="stylesheet" />
25
24
  <%= ahoy_captain_importmap_tags %>
26
25
  <style>
27
26
  /* Application styles */
@@ -0,0 +1,12 @@
1
+ <div class="mt-5" >
2
+ <div role="status" class="animate-pulse">
3
+ <div class="flex justify-between w-full mb-2">
4
+ <div class="h-2.5 bg-gray-200 rounded-full dark:bg-gray-700 w-32"></div>
5
+ <div class="h-2.5 bg-gray-300 rounded-full dark:bg-gray-600 w-24"></div>
6
+ </div>
7
+ <% 10.times do |i| %>
8
+ <div class="h-6 bg-gray-200 dark:bg-gray-700 max-w-[<%= 100 - (i * 10) %>%] mb-2.5"></div>
9
+ <% end %>
10
+ <span class="sr-only">Loading...</span>
11
+ </div>
12
+ </div>
@@ -0,0 +1,3 @@
1
+ <%= turbo_frame_tag :geography do %>
2
+ <canvas style="position: relative; width: 500px; height: 300px;" data-controller="map" data-map-data-value="<%= @countries.to_json %>"></canvas>
3
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <select class="select text-accent select-sm w-full max-w-sm" data-controller="properties" data-action="change->properties#handleChange">
2
+ <option></option>
3
+ <% @options.each do |key, value| %>
4
+ <option value="<%= property_path(id: key) %>" <%= 'selected' if local_assigns[:selected] == value %>><%= value %></option>
5
+ <% end %>
6
+ </select>
@@ -0,0 +1,3 @@
1
+ <%= turbo_frame_tag :goals do %>
2
+ <%= render 'ahoy_captain/properties/form' %>
3
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <%= turbo_frame_tag :goals do %>
2
+ <%= render 'ahoy_captain/properties/form', selected: Base64.strict_decode64(params[:id]) %>
3
+
4
+ <%= render AhoyCaptain::TableComponent.new(items: @properties,
5
+ table: ::AhoyCaptain::Tables::PropertiesTableComponent) %>
6
+ <% end %>
@@ -6,12 +6,39 @@
6
6
  <%= render AhoyCaptain::Filter::ModalComponent.new(title: "Filter by #{name}", id: "#{filters.modal_name}") do |modal| %>
7
7
  <% modal.with_modal_display do %>
8
8
  <% filters.each do |filter| %>
9
- <%= render AhoyCaptain::Filter::SelectComponent.new(label: filter.label, column: filter.column, url: public_send(filter.url), predicates: filter.predicates, multiple: filter.multiple, form: form) %>
9
+ <%= render AhoyCaptain::Filter::SelectComponent.new(label: filter.label, column: filter.column, url: public_send(filter.url), predicates: filter.predicates, multiple: filter.multiple, form: form) %>
10
10
  <% end %>
11
11
  <% end %>
12
12
  <% end %>
13
13
  <% end %>
14
14
 
15
+ <%= render AhoyCaptain::Filter::ModalComponent.new(title: "Filter by Custom Property", id: "customPropertyFilterModal") do |modal| %>
16
+ <% modal.with_modal_display do %>
17
+ <div data-controller="property-filter">
18
+ <fieldset class="flex space-x-4 items-end">
19
+ <% if current_property_filter %>
20
+ <%= render ::AhoyCaptain::ComboboxComponent.new(name: "", multiple: false, column: "", url: filters_properties_names_path, value: [current_property_filter[:key].delete_prefix("properties.")], select_html: { "data-property-filter-target" => 'name', id: "property-name" }) %>
21
+ <% else %>
22
+ <%= render ::AhoyCaptain::ComboboxComponent.new(name: "", multiple: false, column: "", url: filters_properties_names_path, select_html: { "data-property-filter-target" => 'name', id: "property-name" } ) %>
23
+ <% end %>
24
+
25
+ <div class="flex flex-col w-[20%]">
26
+ <select class='select select-primary bg-gray-900 ring-0 focus-within:ring-0 focus-within:ring-0 focus:ring-0 focus:outline-none inline-flex justify-between items-center w-full rounded-md border px-4 py-2 text-sm'>
27
+ <option value="in">in</option>
28
+ </select>
29
+ </div>
30
+
31
+ <% if current_property_filter %>
32
+ <%= render ::AhoyCaptain::ComboboxComponent.new(name: "q[#{current_property_filter[:key]}_in]", multiple: false, column: "", url: filters_properties_values_path, value: [current_property_filter[:value]], select_html: { "data-property-filter-target" => 'value', id: "property-value" } ) %>
33
+ <% else %>
34
+ <%= render ::AhoyCaptain::ComboboxComponent.new(name: "", multiple: false, column: "", url: filters_properties_values_path, select_html: { "data-property-filter-target" => 'value', id: "property-value" }, disabled: true) %>
35
+ <% end %>
36
+
37
+ </fieldset>
38
+
39
+ </div>
40
+ <% end %>
41
+ <% end %>
15
42
 
16
43
  <%= render AhoyCaptain::Filter::ModalComponent.new(title: "Custom Range", id: "customRangeModal") do |modal| %>
17
44
  <% modal.with_modal_display do %>
@@ -31,4 +58,23 @@
31
58
  </div>
32
59
  <% end %>
33
60
  <% end %>
61
+
62
+ <%= render AhoyCaptain::Filter::ModalComponent.new(title: "Custom Comparison", id: "customComparisonModal") do |modal| %>
63
+ <% modal.with_modal_display do %>
64
+ <div class="flex gap-2 w-full">
65
+ <div class="form-control w-full max-w-xs">
66
+ <label class="label" for="start_date">
67
+ <span class="label-text">Start Date</span>
68
+ </label>
69
+ <input type="datetime-local" id="start_date" name="compare_to_start_date" class="input input-bordered w-full" value="<%= params[:compare_to_start_date] %>" />
70
+ </div>
71
+ <div class="form-control w-full max-w-xs">
72
+ <label class="label" for="end_date">
73
+ <span class="label-text">End Date</span>
74
+ </label>
75
+ <input type="datetime-local" id="end_date" name="compare_to_end_date" class="input input-bordered w-full" value="<%= params[:compare_to_end_date] %>" />
76
+ </div>
77
+ </div>
78
+ <% end %>
79
+ <% end %>
34
80
  <% end %>