Class: PartnerService::Benchmark::CalculateService
- Inherits:
-
ApplicationService
- Object
- ApplicationService
- PartnerService::Benchmark::CalculateService
- Includes:
- ElasticAPM::SpanHelpers
- Defined in:
- app/services/partner_service/benchmark/calculate_service.rb
Overview
Service to calculate benchmark data for partner portal Uses pre-aggregated MongoDB collections for improved performance
Data Sources:
-
Monthly view: restaurant_benchmark_monthly, restaurant_benchmark_tags_monthly
-
Weekly view: restaurant_benchmark_weekly, restaurant_benchmark_tags_weekly
The pre-aggregated collections contain calculated metrics (gmv, total_covers, avg_spend_per_person) eliminating the need for runtime aggregation.
Constant Summary collapse
- MIN_PERIODS =
Minimum periods required for meaningful benchmark comparison
2- MAX_PERIODS =
Maximum periods to prevent performance issues and maintain readability Based on: Partner dashboard UX research and MongoDB query performance testing
12
Instance Attribute Summary collapse
-
#data_type ⇒ Object
readonly
Returns the value of attribute data_type.
-
#data_view_type ⇒ Object
readonly
Returns the value of attribute data_view_type.
-
#end_date ⇒ Object
readonly
Returns the value of attribute end_date.
-
#end_month ⇒ Object
readonly
Returns the value of attribute end_month.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#restaurant ⇒ Object
readonly
Returns the value of attribute restaurant.
-
#restaurants ⇒ Object
readonly
Returns the value of attribute restaurants.
-
#start_date ⇒ Object
readonly
Returns the value of attribute start_date.
-
#start_month ⇒ Object
readonly
Returns the value of attribute start_month.
Attributes inherited from ApplicationService
Instance Method Summary collapse
-
#call ⇒ Hash?
Execute the service.
-
#error_messages ⇒ String
Get error messages.
-
#initialize(restaurant:, data_view_type:, data_type:, restaurants: nil, start_date: nil, end_date: nil, start_month: nil, end_month: nil) ⇒ CalculateService
constructor
Initialize the service.
-
#valid? ⇒ Boolean
Check if service is valid.
Methods inherited from ApplicationService
Constructor Details
#initialize(restaurant:, data_view_type:, data_type:, restaurants: nil, start_date: nil, end_date: nil, start_month: nil, end_month: nil) ⇒ CalculateService
Initialize the service
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/services/partner_service/benchmark/calculate_service.rb', line 58 def initialize(restaurant:, data_view_type:, data_type:, restaurants: nil, start_date: nil, end_date: nil, start_month: nil, end_month: nil) raise ArgumentError, 'Restaurant cannot be nil' if restaurant.nil? @restaurant = restaurant @restaurants = restaurants || [restaurant] @data_view_type = data_view_type @data_type = data_type @start_date = start_date&.to_date if start_date.present? @end_date = end_date&.to_date if end_date.present? @start_month = parse_month(start_month) if start_month.present? @end_month = parse_month(end_month) if end_month.present? @errors = [] @mongo_client = MongoDbService::Connection.new @competitor_ids_ordered = nil end |
Instance Attribute Details
#data_type ⇒ Object (readonly)
Returns the value of attribute data_type.
45 46 47 |
# File 'app/services/partner_service/benchmark/calculate_service.rb', line 45 def data_type @data_type end |
#data_view_type ⇒ Object (readonly)
Returns the value of attribute data_view_type.
45 46 47 |
# File 'app/services/partner_service/benchmark/calculate_service.rb', line 45 def data_view_type @data_view_type end |
#end_date ⇒ Object (readonly)
Returns the value of attribute end_date.
45 46 47 |
# File 'app/services/partner_service/benchmark/calculate_service.rb', line 45 def end_date @end_date end |
#end_month ⇒ Object (readonly)
Returns the value of attribute end_month.
45 46 47 |
# File 'app/services/partner_service/benchmark/calculate_service.rb', line 45 def end_month @end_month end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
45 46 47 |
# File 'app/services/partner_service/benchmark/calculate_service.rb', line 45 def errors @errors end |
#restaurant ⇒ Object (readonly)
Returns the value of attribute restaurant.
45 46 47 |
# File 'app/services/partner_service/benchmark/calculate_service.rb', line 45 def restaurant @restaurant end |
#restaurants ⇒ Object (readonly)
Returns the value of attribute restaurants.
45 46 47 |
# File 'app/services/partner_service/benchmark/calculate_service.rb', line 45 def restaurants @restaurants end |
#start_date ⇒ Object (readonly)
Returns the value of attribute start_date.
45 46 47 |
# File 'app/services/partner_service/benchmark/calculate_service.rb', line 45 def start_date @start_date end |
#start_month ⇒ Object (readonly)
Returns the value of attribute start_month.
45 46 47 |
# File 'app/services/partner_service/benchmark/calculate_service.rb', line 45 def start_month @start_month end |
Instance Method Details
#call ⇒ Hash?
Execute the service
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/services/partner_service/benchmark/calculate_service.rb', line 77 def call return nil unless valid? case data_view_type when 'monthly' calculate_monthly_benchmark when 'weekly' calculate_weekly_benchmark else @errors << 'Invalid data_view_type' nil end end |
#error_messages ⇒ String
Get error messages
103 104 105 |
# File 'app/services/partner_service/benchmark/calculate_service.rb', line 103 def errors.join(', ') end |
#valid? ⇒ Boolean
Check if service is valid
95 96 97 98 |
# File 'app/services/partner_service/benchmark/calculate_service.rb', line 95 def valid? validate_parameters errors.empty? end |