mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-02 03:57:52 +00:00
refactor: better data access
This commit is contained in:
@@ -14,22 +14,16 @@ class V2::Reports::Conversations::ReportingEventsAggregator < V2::Reports::Conve
|
|||||||
private
|
private
|
||||||
|
|
||||||
def compute_metrics
|
def compute_metrics
|
||||||
row = base_relation.pluck(*select_fragments).first || default_row
|
result = base_relation.select(*select_fragments).first
|
||||||
|
return default_metrics unless result
|
||||||
|
|
||||||
counts_hash(row.first(COUNT_MAPPINGS.size)).merge(averages_hash(row.last(AVERAGE_MAPPINGS.size)))
|
COUNT_MAPPINGS.keys.index_with { |key| result.public_send(key) || 0 }
|
||||||
end
|
.merge(AVERAGE_MAPPINGS.keys.index_with { |key| result.public_send(key) })
|
||||||
|
|
||||||
def counts_hash(values)
|
|
||||||
COUNT_MAPPINGS.keys.zip(values).to_h.transform_values { |value| value || 0 }
|
|
||||||
end
|
|
||||||
|
|
||||||
def averages_hash(values)
|
|
||||||
AVERAGE_MAPPINGS.keys.zip(values).to_h
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def select_fragments
|
def select_fragments
|
||||||
COUNT_MAPPINGS.map { |_metric, event_name| count_fragment(event_name) } +
|
COUNT_MAPPINGS.map { |metric, event_name| "#{count_fragment(event_name)} AS #{metric}" } +
|
||||||
AVERAGE_MAPPINGS.map { |_metric, event_name| average_fragment(event_name) }
|
AVERAGE_MAPPINGS.map { |metric, event_name| "#{average_fragment(event_name)} AS #{metric}" }
|
||||||
end
|
end
|
||||||
|
|
||||||
def count_fragment(event_name)
|
def count_fragment(event_name)
|
||||||
@@ -55,7 +49,8 @@ class V2::Reports::Conversations::ReportingEventsAggregator < V2::Reports::Conve
|
|||||||
params[:business_hours].present? ? 'value_in_business_hours' : 'value'
|
params[:business_hours].present? ? 'value_in_business_hours' : 'value'
|
||||||
end
|
end
|
||||||
|
|
||||||
def default_row
|
def default_metrics
|
||||||
Array.new(COUNT_MAPPINGS.size, 0) + Array.new(AVERAGE_MAPPINGS.size)
|
COUNT_MAPPINGS.keys.index_with { 0 }
|
||||||
|
.merge(AVERAGE_MAPPINGS.keys.index_with { nil })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user