Class: SaasPlatform::AnalyticsService

Inherits:
Object
  • Object
show all
Defined in:
app/services/saas_platform/analytics_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(account) ⇒ AnalyticsService

Returns a new instance of AnalyticsService.



3
4
5
# File 'app/services/saas_platform/analytics_service.rb', line 3

def initialize()
  @account = 
end

Instance Method Details

#revenue_over_time(period: :day, last: 30) ⇒ Object



7
8
9
10
11
12
# File 'app/services/saas_platform/analytics_service.rb', line 7

def revenue_over_time(period: :day, last: 30)
  @account.orders.paid
          .group_by_period(period, :created_at, last: last)
          .sum(:total_cents)
          .transform_values { |v| v / 100.0 }
end

#top_products(limit: 5) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'app/services/saas_platform/analytics_service.rb', line 14

def top_products(limit: 5)
  SaasPlatform::OrderItem.joins(:order)
                         .where(saas_platform_orders: { account_id: @account.id, status: 'paid' })
                         .joins(:product)
                         .group('saas_platform_products.name')
                         .sum(:quantity)
                         .sort_by { |_k, v| -v }
                         .first(limit)
                         .to_h
end

#user_growth(period: :week, last: 12) ⇒ Object



25
26
27
# File 'app/services/saas_platform/analytics_service.rb', line 25

def user_growth(period: :week, last: 12)
  @account.users.group_by_period(period, :created_at, last: last).count
end