style: extract methods from upsert_rollup to reduce method length
This commit is contained in:
@@ -118,27 +118,30 @@ module ReportingEvents::RollupService
|
|||||||
def upsert_rollup(dimension_type, dimension_id, metric, metric_data)
|
def upsert_rollup(dimension_type, dimension_id, metric, metric_data)
|
||||||
# rubocop:disable Rails/SkipsModelValidations
|
# rubocop:disable Rails/SkipsModelValidations
|
||||||
ReportingEventsRollup.upsert(
|
ReportingEventsRollup.upsert(
|
||||||
{
|
rollup_attributes(dimension_type, dimension_id, metric, metric_data),
|
||||||
account_id: @account.id,
|
|
||||||
date: event_date,
|
|
||||||
dimension_type: dimension_type,
|
|
||||||
dimension_id: dimension_id,
|
|
||||||
metric: metric,
|
|
||||||
count: metric_data[:count],
|
|
||||||
sum_value: metric_data[:sum_value],
|
|
||||||
sum_value_business_hours: metric_data[:sum_value_business_hours],
|
|
||||||
created_at: Time.current,
|
|
||||||
updated_at: Time.current
|
|
||||||
},
|
|
||||||
unique_by: [:account_id, :date, :dimension_type, :dimension_id, :metric],
|
unique_by: [:account_id, :date, :dimension_type, :dimension_id, :metric],
|
||||||
on_duplicate: Arel.sql(
|
on_duplicate: upsert_on_duplicate_sql
|
||||||
'count = reporting_events_rollups.count + EXCLUDED.count, ' \
|
|
||||||
'sum_value = reporting_events_rollups.sum_value + EXCLUDED.sum_value, ' \
|
|
||||||
'sum_value_business_hours = reporting_events_rollups.sum_value_business_hours + EXCLUDED.sum_value_business_hours, ' \
|
|
||||||
'updated_at = EXCLUDED.updated_at'
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
# rubocop:enable Rails/SkipsModelValidations
|
# rubocop:enable Rails/SkipsModelValidations
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def rollup_attributes(dimension_type, dimension_id, metric, metric_data)
|
||||||
|
{
|
||||||
|
account_id: @account.id, date: event_date,
|
||||||
|
dimension_type: dimension_type, dimension_id: dimension_id, metric: metric,
|
||||||
|
count: metric_data[:count], sum_value: metric_data[:sum_value],
|
||||||
|
sum_value_business_hours: metric_data[:sum_value_business_hours],
|
||||||
|
created_at: Time.current, updated_at: Time.current
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def upsert_on_duplicate_sql
|
||||||
|
Arel.sql(
|
||||||
|
'count = reporting_events_rollups.count + EXCLUDED.count, ' \
|
||||||
|
'sum_value = reporting_events_rollups.sum_value + EXCLUDED.sum_value, ' \
|
||||||
|
'sum_value_business_hours = reporting_events_rollups.sum_value_business_hours + EXCLUDED.sum_value_business_hours, ' \
|
||||||
|
'updated_at = EXCLUDED.updated_at'
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user