Class: AnalyticsService::ConversionRateService

Inherits:
Object
  • Object
show all
Includes:
ElasticAPM::SpanHelpers
Defined in:
app/services/analytics_service/conversion_rate_service.rb

Overview

ConversionRateService handles querying and formatting visitor and conversion rate data from ClickHouse for chart display.

Supports date range filtering with predefined options (matching allotment capacity chart):

  • today: Current day only

  • last_7_day: Last 7 full days INCLUDING today (e.g., July 15-21 if today is July 21)

  • last_30_day: Last 30 days INCLUDING today (e.g., June 20-July 19 if today is July 19)

  • this_month: From first day of current month up to today (e.g., July 1-19 if today is July 19)

  • last_month: Complete previous month (e.g., June 1-30 if today is July 19)

  • last_3_month: Last 3 months INCLUDING current month (e.g., Sep-Nov if today is Nov 21), grouped by month

Date Formatting:

  • Daily filters (today, last_7_day, last_30_day, this_month, last_month): Returns day of month only (01-31) with zero-padding (e.g., [“15”, “16”, “17”])

  • Monthly filter (last_3_month): Returns lowercase 3-letter month abbreviations (e.g., [“sep”, “oct”, “nov”])

Features:

  • Aggregates data by date (or month for last_3_month)

  • Removes duplicate dates by summing metrics

  • Implements caching with 1-hour expiration

Constant Summary collapse

CACHE_EXPIRATION =

1 hour in seconds

3600
MAX_RETRY_ATTEMPTS =
3
BASE_RETRY_DELAY =

1 second base delay

1.0
RETRYABLE_ERRORS =

ClickHouse transient errors that should trigger retry

[
  Timeout::Error,
  Net::ReadTimeout,
  Net::OpenTimeout,
  Errno::ECONNRESET,
  Errno::ECONNREFUSED,
  Errno::ETIMEDOUT,
  ActiveRecord::ConnectionNotEstablished,
  ActiveRecord::StatementInvalid, # Can include connection errors
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(restaurant_ids, date_filter = 'last_30_day') ⇒ ConversionRateService

Returns a new instance of ConversionRateService.



44
45
46
47
# File 'app/services/analytics_service/conversion_rate_service.rb', line 44

def initialize(restaurant_ids, date_filter = 'last_30_day')
  @restaurant_ids = Array(restaurant_ids)
  @date_filter = date_filter
end

Instance Method Details

#callHash

Returns formatted chart data with daily metrics Results are cached for 1 hour

Returns:

  • (Hash)

    Chart data with dates and metrics



52
53
54
55
56
57
58
# File 'app/services/analytics_service/conversion_rate_service.rb', line 52

def call
  cache_key = generate_cache_key
  Rails.cache.fetch(cache_key, expires_in: CACHE_EXPIRATION) do
    data = fetch_metrics
    format_chart_data(data)
  end
end