1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-03-15 12:29:16 +01:00

(wip) building stats to ES

This commit is contained in:
Sylvain 2022-10-10 17:42:25 +02:00
parent ef502070be
commit b4f3f7f737
4 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
class Stats::StoreOrder
include Elasticsearch::Persistence::Model
include StatConcern
attribute :orderId, Integer
attribute :state, String
attribute :products, Array
attribute :categories, Array
attribute :ca, Float
end

View File

@ -0,0 +1,22 @@
# frozen_string_literal: true
# Generate statistics indicators about store's orders
class Statistics::Builders::StoreOrdersBuilderService
include Statistics::Concerns::HelpersConcern
include Statistics::Concerns::StoreOrdersConcern
class << self
def build(options = default_options)
# project list
Statistics::FetcherService.store_orders_list(options).each do |o|
Stats::StoreOrder.create({ date: format_date(o[:date]),
type: 'order',
subType: 'store',
ca: o[:ca],
products: o[:order_products],
categories: o[:order_categories],
stat: 1 }.merge(user_info_stat(o)))
end
end
end
end

View File

@ -0,0 +1,32 @@
# frozen_string_literal: true
# Provides methods to consolidate data from Store Orders to use in statistics
module Statistics::Concerns::StoreOrdersConcern
extend ActiveSupport::Concern
class_methods do
def get_order_products(order)
order.order_items.where(orderable_type: 'Product').map do |item|
{ id: item.orderable_id, name: item.orderable.name }
end
end
def get_order_categories(order)
order.order_items
.where(orderable_type: 'Product')
.map(&:orderable)
.map(&:product_category)
.map { |cat| { id: cat.id, name: cat.name } }
.uniq
end
def store_order_info(order)
{
order_id: order.id,
order_state: order.state,
order_products: get_order_products(order),
order_categories: get_order_categories(order)
}
end
end
end

View File

@ -182,6 +182,19 @@ class Statistics::FetcherService
result
end
def store_orders_list(options = default_options)
result = []
Order.includes(order_items: [:orderable])
.where('order_items.orderable_type == "Product"')
.where('orders.created_at >= :start_date AND orders.created_at <= :end_date', options)
.each do |o|
result.push({ date: o.created_at.to_date, ca: calcul_ca(o.invoice) }
.merge(user_info(o.statistic_profile))
.merge(store_order_info(o)))
end
result
end
private
def add_ca(profile, new_ca, users_list)