Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data Archival Changes #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/models/spree/product_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Spree::Product.class_eval do
has_many :page_view_events, -> { viewed }, class_name: 'Spree::PageEvent', foreign_key: :target_id
has_many :archived_page_view_events, -> { viewed }, class_name: 'Spree::ArchivedPageEvent', foreign_key: :target_id
end
13 changes: 11 additions & 2 deletions app/reports/spree/cart_additions_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def sku
end

def report_query
Spree::CartEvent
report_source
.added
.joins(variant: :product)
.where(created_at: reporting_period)
Expand All @@ -28,8 +28,17 @@ def report_query
'spree_products.slug as product_slug',
'spree_variants.sku as sku',
'count(spree_products.name) as additions',
'sum(spree_cart_events.quantity) as quantity_change'
"sum(#{report_source_table}.quantity) as quantity_change"
)
end

private def report_source
Spree::Config.events_tracker_archive_data ? Spree::ArchivedCartEvent : Spree::CartEvent
end

private def report_source_table
Spree::Config.events_tracker_archive_data ? 'spree_archived_cart_events' : 'spree_cart_events'
end

end
end
13 changes: 11 additions & 2 deletions app/reports/spree/cart_removals_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def sku
end

def report_query
Spree::CartEvent
report_source
.removed
.joins(variant: :product)
.where(created_at: reporting_period)
Expand All @@ -28,8 +28,17 @@ def report_query
'spree_products.slug as product_slug',
'spree_variants.sku as sku',
'count(spree_products.name) as removals',
'sum(spree_cart_events.quantity) as quantity_change'
"sum(#{report_source_table}.quantity) as quantity_change"
)
end

private def report_source
Spree::Config.events_tracker_archive_data ? Spree::ArchivedCartEvent : Spree::CartEvent
end

private def report_source_table
Spree::Config.events_tracker_archive_data ? 'spree_archived_cart_events' : 'spree_cart_events'
end

end
end
15 changes: 12 additions & 3 deletions app/reports/spree/cart_updations_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ def sku
end

def report_query
quantity_increase_sql = "CASE WHEN quantity > 0 then spree_cart_events.quantity ELSE 0 END"
quantity_decrease_sql = "CASE WHEN quantity < 0 then spree_cart_events.quantity ELSE 0 END"
quantity_increase_sql = "CASE WHEN quantity > 0 then #{report_source_table}.quantity ELSE 0 END"
quantity_decrease_sql = "CASE WHEN quantity < 0 then #{report_source_table}.quantity ELSE 0 END"

Spree::CartEvent
report_source
.updated
.joins(variant: :product)
.where(created_at: reporting_period)
Expand All @@ -35,5 +35,14 @@ def report_query
"SUM(#{ quantity_decrease_sql }) as quantity_decrease"
)
end

private def report_source
Spree::Config.events_tracker_archive_data ? Spree::ArchivedCartEvent : Spree::CartEvent
end

private def report_source_table
Spree::Config.events_tracker_archive_data ? 'spree_archived_cart_events' : 'spree_cart_events'
end

end
end
19 changes: 14 additions & 5 deletions app/reports/spree/product_views_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ def report_query
viewed_events =
Spree::Product
.where(Spree::Product.arel_table[:name].matches(search_name))
.joins(:page_view_events)
.where(spree_page_events: { created_at: reporting_period })
.group('product_name', 'product_slug', 'spree_page_events.actor_id', 'spree_page_events.session_id')
.joins(event_scope)
.where(report_source_table.to_sym => { created_at: reporting_period })
.group('product_name', 'product_slug', "#{report_source_table}.actor_id", "#{report_source_table}.session_id")
.select(
'spree_products.name as product_name',
'spree_products.slug as product_slug',
'COUNT(*) as total_views_per_session',
'spree_page_events.session_id as session_id',
'spree_page_events.actor_id as actor_id'
"#{report_source_table}.session_id as session_id",
"#{report_source_table}.actor_id as actor_id"
)
Spree::Report::QueryFragments
.from_subquery(viewed_events)
Expand All @@ -42,5 +42,14 @@ def report_query
private def search_name
search[:name].present? ? "%#{ search[:name] }%" : '%'
end

private def report_source_table
Spree::Config.events_tracker_archive_data ? 'spree_archived_page_events' : 'spree_page_events'
end

private def event_scope
Spree::Config.events_tracker_archive_data ? :archived_page_view_events : :page_view_events
end

end
end
18 changes: 15 additions & 3 deletions app/reports/spree/product_views_to_cart_additions_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ def cart_to_view_ratio

def report_query
cart_additions =
Spree::CartEvent
report_source
.added
.joins(variant: :product)
.where(created_at: reporting_period)
.group('spree_products.name', 'spree_products.slug')
.select(
'spree_products.name as product_name',
'spree_products.slug as product_slug',
'SUM(spree_cart_events.quantity) as cart_additions'
"SUM(#{report_source_table}.quantity) as cart_additions"
)
total_views =
Spree::Product
.joins(:page_view_events)
.joins(event_scope)
.group(:name)
.select(
'spree_products.name as product_name',
Expand All @@ -48,5 +48,17 @@ def report_query
)
end

private def report_source
Spree::Config.events_tracker_archive_data ? Spree::ArchivedCartEvent : Spree::CartEvent
end

private def report_source_table
Spree::Config.events_tracker_archive_data ? 'spree_archived_cart_events' : 'spree_cart_events'
end

private def event_scope
Spree::Config.events_tracker_archive_data ? :archived_page_view_events : :page_view_events
end

end
end
7 changes: 6 additions & 1 deletion app/reports/spree/product_views_to_purchases_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def purchase_to_view_ratio # This is inconsistent across postgres and mysql
deeplink product_name: { template: %Q{<a href="/admin/products/{%# o.product_slug %}" target="_blank">{%# o.product_name %}</a>} }

def report_query
page_events_ar = Arel::Table.new(:spree_page_events)
page_events_ar = Arel::Table.new(report_source_table)
purchase_line_items_ar = Arel::Table.new(:purchase_line_items)

Spree::Report::QueryFragments.from_subquery(purchase_line_items, as: :purchase_line_items)
Expand Down Expand Up @@ -50,5 +50,10 @@ def report_query
'spree_products.id as product_id'
)
end

private def report_source_table
Spree::Config.events_tracker_archive_data ? :spree_archived_page_events : :spree_page_events
end

end
end
9 changes: 7 additions & 2 deletions app/reports/spree/trending_search_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,20 @@ def report_query
end

private def searches
Spree::PageEvent
report_source
.where(activity: 'search')
.where(created_at: reporting_period)
.where(Spree::PageEvent.arel_table[:search_keywords].matches(keyword_search))
.where(report_source.arel_table[:search_keywords].matches(keyword_search))
.select("search_keywords as searched_term")
end

private def keyword_search
search[:keywords_cont].present? ? "%#{ search[:keywords_cont] }%" : '%'
end

private def report_source
Spree::Config.events_tracker_archive_data ? Spree::ArchivedPageEvent : Spree::PageEvent
end

end
end
8 changes: 6 additions & 2 deletions app/reports/spree/user_pool_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def report_query
end

private def visitors
Spree::PageEvent
report_source
.where(created_at: reporting_period)
.select(
*time_scale_selects,
Expand All @@ -73,7 +73,7 @@ def report_query
end

private def registered_users_visited
Spree::PageEvent
report_source
.where(created_at: reporting_period)
.where.not(actor_id: nil)
.group(:session_id, :actor_id, *time_scale_columns)
Expand All @@ -83,5 +83,9 @@ def report_query
)
end

private def report_source
Spree::Config.events_tracker_archive_data ? Spree::ArchivedPageEvent : Spree::PageEvent
end

end
end