From d6d0851e3f2cf834696280fd7e1e3e07f70e86d4 Mon Sep 17 00:00:00 2001 From: Shivam Jain Date: Mon, 11 Jun 2018 16:26:21 +0530 Subject: [PATCH 1/2] update report source to archived records --- app/models/spree/product_decorator.rb | 2 +- app/reports/spree/cart_additions_report.rb | 4 ++-- app/reports/spree/cart_removals_report.rb | 4 ++-- app/reports/spree/cart_updations_report.rb | 6 +++--- app/reports/spree/product_views_report.rb | 8 ++++---- .../spree/product_views_to_cart_additions_report.rb | 4 ++-- app/reports/spree/product_views_to_purchases_report.rb | 2 +- app/reports/spree/trending_search_report.rb | 4 ++-- app/reports/spree/user_pool_report.rb | 4 ++-- 9 files changed, 19 insertions(+), 19 deletions(-) diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index f497de1..f0e0142 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -1,3 +1,3 @@ Spree::Product.class_eval do - has_many :page_view_events, -> { viewed }, class_name: 'Spree::PageEvent', foreign_key: :target_id + has_many :page_view_events, -> { viewed }, class_name: 'Spree::ArchivedPageEvent', foreign_key: :target_id end diff --git a/app/reports/spree/cart_additions_report.rb b/app/reports/spree/cart_additions_report.rb index 3c3eddc..861620f 100644 --- a/app/reports/spree/cart_additions_report.rb +++ b/app/reports/spree/cart_additions_report.rb @@ -18,7 +18,7 @@ def sku end def report_query - Spree::CartEvent + Spree::ArchivedCartEvent .added .joins(variant: :product) .where(created_at: reporting_period) @@ -28,7 +28,7 @@ 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(spree_archived_cart_events.quantity) as quantity_change' ) end end diff --git a/app/reports/spree/cart_removals_report.rb b/app/reports/spree/cart_removals_report.rb index 292d8a1..703ec56 100644 --- a/app/reports/spree/cart_removals_report.rb +++ b/app/reports/spree/cart_removals_report.rb @@ -18,7 +18,7 @@ def sku end def report_query - Spree::CartEvent + Spree::ArchivedCartEvent .removed .joins(variant: :product) .where(created_at: reporting_period) @@ -28,7 +28,7 @@ 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(spree_archived_cart_events.quantity) as quantity_change' ) end end diff --git a/app/reports/spree/cart_updations_report.rb b/app/reports/spree/cart_updations_report.rb index ee4498e..1798285 100644 --- a/app/reports/spree/cart_updations_report.rb +++ b/app/reports/spree/cart_updations_report.rb @@ -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 spree_archived_cart_events.quantity ELSE 0 END" + quantity_decrease_sql = "CASE WHEN quantity < 0 then spree_archived_cart_events.quantity ELSE 0 END" - Spree::CartEvent + Spree::ArchivedCartEvent .updated .joins(variant: :product) .where(created_at: reporting_period) diff --git a/app/reports/spree/product_views_report.rb b/app/reports/spree/product_views_report.rb index dee1348..ee43d64 100644 --- a/app/reports/spree/product_views_report.rb +++ b/app/reports/spree/product_views_report.rb @@ -18,14 +18,14 @@ def report_query 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') + .where(spree_archived_page_events: { created_at: reporting_period }) + .group('product_name', 'product_slug', 'spree_archived_page_events.actor_id', 'spree_archived_page_events.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' + 'spree_archived_page_events.session_id as session_id', + 'spree_archived_page_events.actor_id as actor_id' ) Spree::Report::QueryFragments .from_subquery(viewed_events) diff --git a/app/reports/spree/product_views_to_cart_additions_report.rb b/app/reports/spree/product_views_to_cart_additions_report.rb index ae7fcff..8b426b3 100644 --- a/app/reports/spree/product_views_to_cart_additions_report.rb +++ b/app/reports/spree/product_views_to_cart_additions_report.rb @@ -19,7 +19,7 @@ def cart_to_view_ratio def report_query cart_additions = - Spree::CartEvent + Spree::ArchivedCartEvent .added .joins(variant: :product) .where(created_at: reporting_period) @@ -27,7 +27,7 @@ def report_query .select( 'spree_products.name as product_name', 'spree_products.slug as product_slug', - 'SUM(spree_cart_events.quantity) as cart_additions' + 'SUM(spree_archived_cart_events.quantity) as cart_additions' ) total_views = Spree::Product diff --git a/app/reports/spree/product_views_to_purchases_report.rb b/app/reports/spree/product_views_to_purchases_report.rb index c3a65f9..1f9ca52 100644 --- a/app/reports/spree/product_views_to_purchases_report.rb +++ b/app/reports/spree/product_views_to_purchases_report.rb @@ -18,7 +18,7 @@ def purchase_to_view_ratio # This is inconsistent across postgres and mysql deeplink product_name: { template: %Q{{%# o.product_name %}} } def report_query - page_events_ar = Arel::Table.new(:spree_page_events) + page_events_ar = Arel::Table.new(:spree_archived_page_events) purchase_line_items_ar = Arel::Table.new(:purchase_line_items) Spree::Report::QueryFragments.from_subquery(purchase_line_items, as: :purchase_line_items) diff --git a/app/reports/spree/trending_search_report.rb b/app/reports/spree/trending_search_report.rb index af25b8d..41bec2b 100644 --- a/app/reports/spree/trending_search_report.rb +++ b/app/reports/spree/trending_search_report.rb @@ -36,10 +36,10 @@ def report_query end private def searches - Spree::PageEvent + Spree::ArchivedPageEvent .where(activity: 'search') .where(created_at: reporting_period) - .where(Spree::PageEvent.arel_table[:search_keywords].matches(keyword_search)) + .where(Spree::ArchivedPageEvent.arel_table[:search_keywords].matches(keyword_search)) .select("search_keywords as searched_term") end diff --git a/app/reports/spree/user_pool_report.rb b/app/reports/spree/user_pool_report.rb index e4cb5c8..14e8c3d 100644 --- a/app/reports/spree/user_pool_report.rb +++ b/app/reports/spree/user_pool_report.rb @@ -52,7 +52,7 @@ def report_query end private def visitors - Spree::PageEvent + Spree::ArchivedPageEvent .where(created_at: reporting_period) .select( *time_scale_selects, @@ -73,7 +73,7 @@ def report_query end private def registered_users_visited - Spree::PageEvent + Spree::ArchivedPageEvent .where(created_at: reporting_period) .where.not(actor_id: nil) .group(:session_id, :actor_id, *time_scale_columns) From af123ceab447e1573556d46503d674cc8dca84ac Mon Sep 17 00:00:00 2001 From: Shivam Jain Date: Mon, 11 Jun 2018 19:20:38 +0530 Subject: [PATCH 2/2] modify report source based on admin archival choice --- app/models/spree/product_decorator.rb | 3 ++- app/reports/spree/cart_additions_report.rb | 13 +++++++++++-- app/reports/spree/cart_removals_report.rb | 13 +++++++++++-- app/reports/spree/cart_updations_report.rb | 15 ++++++++++++--- app/reports/spree/product_views_report.rb | 19 ++++++++++++++----- .../product_views_to_cart_additions_report.rb | 18 +++++++++++++++--- .../product_views_to_purchases_report.rb | 7 ++++++- app/reports/spree/trending_search_report.rb | 9 +++++++-- app/reports/spree/user_pool_report.rb | 8 ++++++-- 9 files changed, 84 insertions(+), 21 deletions(-) diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index f0e0142..4fccbb6 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -1,3 +1,4 @@ Spree::Product.class_eval do - has_many :page_view_events, -> { viewed }, class_name: 'Spree::ArchivedPageEvent', foreign_key: :target_id + 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 diff --git a/app/reports/spree/cart_additions_report.rb b/app/reports/spree/cart_additions_report.rb index 861620f..feacd02 100644 --- a/app/reports/spree/cart_additions_report.rb +++ b/app/reports/spree/cart_additions_report.rb @@ -18,7 +18,7 @@ def sku end def report_query - Spree::ArchivedCartEvent + report_source .added .joins(variant: :product) .where(created_at: reporting_period) @@ -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_archived_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 diff --git a/app/reports/spree/cart_removals_report.rb b/app/reports/spree/cart_removals_report.rb index 703ec56..59df979 100644 --- a/app/reports/spree/cart_removals_report.rb +++ b/app/reports/spree/cart_removals_report.rb @@ -18,7 +18,7 @@ def sku end def report_query - Spree::ArchivedCartEvent + report_source .removed .joins(variant: :product) .where(created_at: reporting_period) @@ -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_archived_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 diff --git a/app/reports/spree/cart_updations_report.rb b/app/reports/spree/cart_updations_report.rb index 1798285..93523fe 100644 --- a/app/reports/spree/cart_updations_report.rb +++ b/app/reports/spree/cart_updations_report.rb @@ -18,10 +18,10 @@ def sku end def report_query - quantity_increase_sql = "CASE WHEN quantity > 0 then spree_archived_cart_events.quantity ELSE 0 END" - quantity_decrease_sql = "CASE WHEN quantity < 0 then spree_archived_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::ArchivedCartEvent + report_source .updated .joins(variant: :product) .where(created_at: reporting_period) @@ -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 diff --git a/app/reports/spree/product_views_report.rb b/app/reports/spree/product_views_report.rb index ee43d64..92a4cd3 100644 --- a/app/reports/spree/product_views_report.rb +++ b/app/reports/spree/product_views_report.rb @@ -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_archived_page_events: { created_at: reporting_period }) - .group('product_name', 'product_slug', 'spree_archived_page_events.actor_id', 'spree_archived_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_archived_page_events.session_id as session_id', - 'spree_archived_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) @@ -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 diff --git a/app/reports/spree/product_views_to_cart_additions_report.rb b/app/reports/spree/product_views_to_cart_additions_report.rb index 8b426b3..433fdac 100644 --- a/app/reports/spree/product_views_to_cart_additions_report.rb +++ b/app/reports/spree/product_views_to_cart_additions_report.rb @@ -19,7 +19,7 @@ def cart_to_view_ratio def report_query cart_additions = - Spree::ArchivedCartEvent + report_source .added .joins(variant: :product) .where(created_at: reporting_period) @@ -27,11 +27,11 @@ def report_query .select( 'spree_products.name as product_name', 'spree_products.slug as product_slug', - 'SUM(spree_archived_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', @@ -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 diff --git a/app/reports/spree/product_views_to_purchases_report.rb b/app/reports/spree/product_views_to_purchases_report.rb index 1f9ca52..99f24d5 100644 --- a/app/reports/spree/product_views_to_purchases_report.rb +++ b/app/reports/spree/product_views_to_purchases_report.rb @@ -18,7 +18,7 @@ def purchase_to_view_ratio # This is inconsistent across postgres and mysql deeplink product_name: { template: %Q{{%# o.product_name %}} } def report_query - page_events_ar = Arel::Table.new(:spree_archived_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) @@ -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 diff --git a/app/reports/spree/trending_search_report.rb b/app/reports/spree/trending_search_report.rb index 41bec2b..40327a6 100644 --- a/app/reports/spree/trending_search_report.rb +++ b/app/reports/spree/trending_search_report.rb @@ -36,15 +36,20 @@ def report_query end private def searches - Spree::ArchivedPageEvent + report_source .where(activity: 'search') .where(created_at: reporting_period) - .where(Spree::ArchivedPageEvent.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 diff --git a/app/reports/spree/user_pool_report.rb b/app/reports/spree/user_pool_report.rb index 14e8c3d..6a16074 100644 --- a/app/reports/spree/user_pool_report.rb +++ b/app/reports/spree/user_pool_report.rb @@ -52,7 +52,7 @@ def report_query end private def visitors - Spree::ArchivedPageEvent + report_source .where(created_at: reporting_period) .select( *time_scale_selects, @@ -73,7 +73,7 @@ def report_query end private def registered_users_visited - Spree::ArchivedPageEvent + report_source .where(created_at: reporting_period) .where.not(actor_id: nil) .group(:session_id, :actor_id, *time_scale_columns) @@ -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