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

fixed reports for postgres #32

Open
wants to merge 1 commit into
base: 3-1-stable
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
4 changes: 2 additions & 2 deletions app/reports/spree/best_selling_products_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ def generate
join(:spree_products___products, products__id: :variants__product_id).
where(orders__state: 'complete').
where(orders__completed_at: @start_date..@end_date). #filter by params
group(:variant_id).
group(:variant_id, :variants__sku, :products__name).
order(sortable_sequel_expression)
end

def select_columns(dataset)
dataset.select{[
products__name.as(product_name),
Sequel.as(IF(STRCMP(variants__sku, ''), variants__sku, products__name), :sku),
Sequel.as(Sequel.lit("CASE variants.sku WHEN '' THEN products.name ELSE variants.sku END"), :sku),
sum(quantity).as(sold_count)
]}
end
Expand Down
18 changes: 9 additions & 9 deletions app/reports/spree/cart_additions_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ def initialize(options)

def generate
SpreeAdminInsights::ReportDb[:spree_cart_events___cart_events].
join(:spree_variants___variants, id: :variant_id).
join(:spree_products___products, id: :product_id).
where(cart_events__activity: 'add').
where(cart_events__created_at: @start_date..@end_date).
group(:variant_id).
order(sortable_sequel_expression)
join(:spree_variants___variants, id: :variant_id).
join(:spree_products___products, id: :product_id).
where(cart_events__activity: 'add').
where(cart_events__created_at: @start_date..@end_date).
group(:variant_id, :variants__sku, :products__name).
order(sortable_sequel_expression)
end

def select_columns(dataset)
dataset.select{[
dataset.select { [
products__name.as(product_name),
Sequel.as(IF(STRCMP(variants__sku, ''), variants__sku, products__name), :sku),
Sequel.as(Sequel.lit("CASE variants.sku WHEN '' THEN products.name ELSE variants.sku END"), :sku),
Sequel.as(count(:products__name), :additions),
Sequel.as(sum(cart_events__quantity), :quantity_change)
]}
] }
end
end
end
18 changes: 9 additions & 9 deletions app/reports/spree/cart_removals_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ def initialize(options)

def generate
SpreeAdminInsights::ReportDb[:spree_cart_events___cart_events].
join(:spree_variants___variants, id: :variant_id).
join(:spree_products___products, id: :product_id).
where(cart_events__activity: 'remove').
where(cart_events__created_at: @start_date..@end_date). #filter by params
group(:variant_id).
order(sortable_sequel_expression)
join(:spree_variants___variants, id: :variant_id).
join(:spree_products___products, id: :product_id).
where(cart_events__activity: 'remove').
where(cart_events__created_at: @start_date..@end_date).#filter by params
group(:variant_id, :variants__sku, :products__name).
order(sortable_sequel_expression)
end

def select_columns(dataset)
dataset.select{[
dataset.select { [
products__name.as(product_name),
Sequel.as(IF(STRCMP(variants__sku, ''), variants__sku, products__name), :sku),
Sequel.as(Sequel.lit("CASE variants.sku WHEN '' THEN products.name ELSE variants.sku END"), :sku),
Sequel.as(count(:products__name), :removals),
Sequel.as(sum(cart_events__quantity), :quantity_change)
]}
] }
end
end
end
22 changes: 11 additions & 11 deletions app/reports/spree/cart_updations_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ def initialize(options)

def generate
SpreeAdminInsights::ReportDb[:spree_cart_events___cart_events].
join(:spree_variants___variants, id: :variant_id).
join(:spree_products___products, id: :product_id).
where(activity: 'update').
where(cart_events__created_at: @start_date..@end_date). #filter by params
group(:variant_id).
order(sortable_sequel_expression)
join(:spree_variants___variants, id: :variant_id).
join(:spree_products___products, id: :product_id).
where(activity: 'update').
where(cart_events__created_at: @start_date..@end_date).#filter by params
group(:variant_id, :variants__sku, :products__name).
order(sortable_sequel_expression)
end

def select_columns(dataset)
dataset.select{[
dataset.select { [
products__name.as(product_name),
Sequel.as(IF(STRCMP(variants__sku, ''), variants__sku, products__name), :sku),
Sequel.as(Sequel.lit("CASE variants.sku WHEN '' THEN products.name ELSE variants.sku END"), :sku),
Sequel.as(count(:products__name), :updations),
Sequel.as(sum(IF(cart_events__quantity >= 0, cart_events__quantity, 0)), :quantity_increase),
Sequel.as(sum(IF(cart_events__quantity <= 0, cart_events__quantity, 0)), :quantity_decrease)
]}
Sequel.as(sum(Sequel.lit("CASE WHEN cart_events.quantity >= 0 THEN cart_events.quantity ELSE 0 END")), :quantity_increase),
Sequel.as(sum(Sequel.lit("CASE WHEN cart_events.quantity <= 0 THEN cart_events.quantity ELSE 0 END")), :quantity_decrease)
] }
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ def no_pagination?

def generate
payment_methods = SpreeAdminInsights::ReportDb[:spree_payment_methods___payment_methods].
join(:spree_payments___payments, payment_method_id: :id).
where(payments__created_at: @start_date..@end_date). #filter by params
select{[
join(:spree_payments___payments, payment_method_id: :id).
where(payments__created_at: @start_date..@end_date).#filter by params
select { [
payment_method_id,
Sequel.as(name, :payment_method_name),
Sequel.as(IF(STRCMP(state, 'pending'), state, 'to be captured'), :payment_state),
Sequel.as(MONTHNAME(:payments__created_at), :month_name),
Sequel.as(MONTH(:payments__created_at), :number),
Sequel.as(YEAR(:payments__created_at), :year)
]}
Sequel.as(Sequel.lit("CASE state WHEN 'pending' THEN 'to be captured' ELSE state END"), :payment_state),
Sequel.as(DBUtils.month_name(:payments__created_at), :month_name),
Sequel.as(DBUtils.month_number(:payments__created_at), :number),
Sequel.as(DBUtils.year(:payments__created_at), :year)
] }

group_by_months = SpreeAdminInsights::ReportDb[payment_methods].
group(:months_name, :payment_method_name, :payment_state).
order(:year, :number).
select{[
group(:months_name, :payment_method_name, :payment_state, :t1__year, :t1__number).
order(:year, :number).
select { [
payment_method_name,
number,
payment_state,
year,
Sequel.as(concat(month_name, ' ', year), :months_name),
Sequel.as(COUNT(payment_method_id), :count),
]}
] }

grouped_by_payment_method_name = group_by_months.all.group_by { |record| record[:payment_method_name] }
data = []
Expand Down Expand Up @@ -74,10 +74,10 @@ def chart_json
},
tooltip: { valuePrefix: '#' },
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: collection.group_by { |r| r[:payment_state] }.map { |key, value| { name: key, data: value.map { |r| r[:count].to_i } } }
}
Expand Down
9 changes: 5 additions & 4 deletions app/reports/spree/payment_method_transactions_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ def generate
select{[
Sequel.as(payment_methods__name, :payment_method_name),
Sequel.as(payments__amount, :payment_amount),
Sequel.as(MONTHNAME(:payments__created_at), :month_name),
Sequel.as(MONTH(:payments__created_at), :number),
Sequel.as(YEAR(:payments__created_at), :year)
Sequel.as(DBUtils.month_name(:payments__created_at), :month_name),
Sequel.as(DBUtils.month_number(:payments__created_at), :number),
Sequel.as(DBUtils.year(:payments__created_at), :year)
]}

group_by_months = SpreeAdminInsights::ReportDb[payments].
group(:months_name, :payment_method_name).
group(:months_name, :payment_method_name, :t1__year, :t1__number).
order(:year, :number).
select{[
number,
Expand All @@ -37,6 +37,7 @@ def generate
grouped_by_payment_method_name.each_pair do |name, collection|
data << fill_missing_values({ payment_method_name: name, payment_amount: 0 }, collection)
end

@data = data.flatten
end

Expand Down
100 changes: 76 additions & 24 deletions app/reports/spree/product_views_to_purchases_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,85 @@ def initialize(options)
end

def generate(options = {})
line_items = ::SpreeAdminInsights::ReportDb[:spree_line_items___line_items].
join(:spree_orders___orders, id: :order_id).
join(:spree_variants___variants, variants__id: :line_items__variant_id).
join(:spree_products___products, products__id: :variants__product_id).
where(orders__state: 'complete').
where(orders__created_at: @start_date..@end_date). #filter by params
select{[line_items__quantity,
line_items__id,
line_items__variant_id,
sum(quantity).as(purchases),
products__name.as(product_name),
products__id.as(product_id)]}.
group(:products__name).as(:line_items)

::SpreeAdminInsights::ReportDb[line_items].join(:spree_page_events___page_events, page_events__target_id: :product_id).
where(page_events__target_type: 'Spree::Product', page_events__activity: 'view').
group(:product_name).
order(sortable_sequel_expression)
purchases_by_product = ::SpreeAdminInsights::ReportDb[:spree_line_items___line_items]
.join(:spree_variants___variants, id: :variant_id)
.join(:spree_products___products, id: :variants__product_id)
.join(:spree_orders___orders, id: :line_items__order_id)
.where(orders__state: 'complete')
.where(orders__created_at: @start_date..@end_date)
.group(:products__id)
.select { [
Sequel.as(Sequel.function(:SUM, :line_items__quantity), :purchases),
Sequel.as(:products__name, :product_name),
Sequel.as(:products__id, :product_id)
] }.as(:purchases_by_product)


case DBUtils.adapter
when :postgresql
generate_postgresql(purchases_by_product)
else
generate_mysql(purchases_by_product)
end
end

def select_columns(dataset)
dataset.select{[
product_name,
count('*').as(views),
purchases,
Sequel.as(ROUND(purchases / count('*'), 2), :purchase_to_view_ratio)
]}
base_columns = [:product_name, :purchases]

case DBUtils.adapter
when :postgresql
dataset.select { base_columns | [
:views,
Sequel.as(Sequel.lit("ROUND((purchases / views :: NUMERIC), 2)"), :purchase_to_view_ratio)
] }
else
dataset.select { base_columns | [
COUNT('*').as(:views),
Sequel.as(Sequel.lit("ROUND(purchases / COUNT('*'), 2)"), :purchase_to_view_ratio)
] }
end
end

private

def generate_postgresql(purchases_by_product)
view_events = ::SpreeAdminInsights::ReportDb[:spree_page_events___page_events]
.where(page_events__target_type: 'Spree::Product', page_events__activity: 'view')
.group(:target_id)
.select { [
Sequel.as(Sequel.function(:COUNT, '*'), :views),
:page_events__target_id
] }.as(:view_events)

not_ordered_result = ::SpreeAdminInsights::ReportDb[line_items(purchases_by_product)]
.join(::SpreeAdminInsights::ReportDb[view_events], target_id: :product_id)
.distinct(:product_id)

::SpreeAdminInsights::ReportDb[not_ordered_result].order(sortable_sequel_expression)
end

def generate_mysql(purchases_by_product)
::SpreeAdminInsights::ReportDb[line_items(purchases_by_product)]
.join(:spree_page_events___page_events, page_events__target_id: :product_id)
.where(page_events__target_type: 'Spree::Product', page_events__activity: 'view')
.group(:product_id)
.order(sortable_sequel_expression)
end

def line_items(purchases_by_product)
line_items = ::SpreeAdminInsights::ReportDb[purchases_by_product]
.join(:spree_variants___variants, product_id: :purchases_by_product__product_id)
.join(:spree_line_items___line_items, variant_id: :variants__id)

columns = [:line_items__quantity, :line_items__id, :line_items__variant_id,
:purchases_by_product__purchases, :purchases_by_product__product_name, :purchases_by_product__product_id]

case DBUtils.adapter
when :postgresql
line_items.distinct(:purchases_by_product__product_id).select(*columns).as(:line_items)
else
line_items.group(:purchases_by_product__product_id).select(*columns).as(:line_items)
end
end
end
end
Loading