-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feat/sla-21-business-hours
- Loading branch information
Showing
76 changed files
with
3,475 additions
and
428 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
app/builders/v2/reports/conversations/base_report_builder.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class V2::Reports::Conversations::BaseReportBuilder | ||
pattr_initialize :account, :params | ||
|
||
private | ||
|
||
AVG_METRICS = %w[avg_first_response_time avg_resolution_time reply_time].freeze | ||
COUNT_METRICS = %w[ | ||
conversations_count | ||
incoming_messages_count | ||
outgoing_messages_count | ||
resolutions_count | ||
bot_resolutions_count | ||
bot_handoffs_count | ||
].freeze | ||
|
||
def builder_class(metric) | ||
case metric | ||
when *AVG_METRICS | ||
V2::Reports::Timeseries::AverageReportBuilder | ||
when *COUNT_METRICS | ||
V2::Reports::Timeseries::CountReportBuilder | ||
end | ||
end | ||
|
||
def log_invalid_metric | ||
Rails.logger.error "ReportBuilder: Invalid metric - #{params[:metric]}" | ||
|
||
{} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class V2::Reports::Conversations::MetricBuilder < V2::Reports::Conversations::BaseReportBuilder | ||
def summary | ||
{ | ||
conversations_count: count('conversations_count'), | ||
incoming_messages_count: count('incoming_messages_count'), | ||
outgoing_messages_count: count('outgoing_messages_count'), | ||
avg_first_response_time: count('avg_first_response_time'), | ||
avg_resolution_time: count('avg_resolution_time'), | ||
resolutions_count: count('resolutions_count'), | ||
reply_time: count('reply_time') | ||
} | ||
end | ||
|
||
def bot_summary | ||
{ | ||
bot_resolutions_count: count('bot_resolutions_count'), | ||
bot_handoffs_count: count('bot_handoffs_count') | ||
} | ||
end | ||
|
||
private | ||
|
||
def count(metric) | ||
builder_class(metric).new(account, builder_params(metric)).aggregate_value | ||
end | ||
|
||
def builder_params(metric) | ||
params.merge({ metric: metric }) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class V2::Reports::Conversations::ReportBuilder < V2::Reports::Conversations::BaseReportBuilder | ||
def timeseries | ||
perform_action(:timeseries) | ||
end | ||
|
||
def aggregate_value | ||
perform_action(:aggregate_value) | ||
end | ||
|
||
private | ||
|
||
def perform_action(method_name) | ||
return builder.new(account, params).public_send(method_name) if builder.present? | ||
|
||
log_invalid_metric | ||
end | ||
|
||
def builder | ||
builder_class(params[:metric]) | ||
end | ||
end |
48 changes: 48 additions & 0 deletions
48
app/builders/v2/reports/timeseries/average_report_builder.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
class V2::Reports::Timeseries::AverageReportBuilder < V2::Reports::Timeseries::BaseTimeseriesBuilder | ||
def timeseries | ||
grouped_average_time = reporting_events.average(average_value_key) | ||
grouped_event_count = reporting_events.count | ||
grouped_average_time.each_with_object([]) do |element, arr| | ||
event_date, average_time = element | ||
arr << { | ||
value: average_time, | ||
timestamp: event_date.in_time_zone(timezone).to_i, | ||
count: grouped_event_count[event_date] | ||
} | ||
end | ||
end | ||
|
||
def aggregate_value | ||
object_scope.average(average_value_key) | ||
end | ||
|
||
private | ||
|
||
def event_name | ||
metric_to_event_name = { | ||
avg_first_response_time: :first_response, | ||
avg_resolution_time: :conversation_resolved, | ||
reply_time: :reply_time | ||
} | ||
metric_to_event_name[params[:metric].to_sym] | ||
end | ||
|
||
def object_scope | ||
scope.reporting_events.where(name: event_name, created_at: range) | ||
end | ||
|
||
def reporting_events | ||
@grouped_values = object_scope.group_by_period( | ||
group_by, | ||
:created_at, | ||
default_value: 0, | ||
range: range, | ||
permit: %w[day week month year hour], | ||
time_zone: timezone | ||
) | ||
end | ||
|
||
def average_value_key | ||
@average_value_key ||= params[:business_hours].present? ? :value_in_business_hours : :value | ||
end | ||
end |
46 changes: 46 additions & 0 deletions
46
app/builders/v2/reports/timeseries/base_timeseries_builder.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class V2::Reports::Timeseries::BaseTimeseriesBuilder | ||
include TimezoneHelper | ||
include DateRangeHelper | ||
DEFAULT_GROUP_BY = 'day'.freeze | ||
|
||
pattr_initialize :account, :params | ||
|
||
def scope | ||
case params[:type].to_sym | ||
when :account | ||
account | ||
when :inbox | ||
inbox | ||
when :agent | ||
user | ||
when :label | ||
label | ||
when :team | ||
team | ||
end | ||
end | ||
|
||
def inbox | ||
@inbox ||= account.inboxes.find(params[:id]) | ||
end | ||
|
||
def user | ||
@user ||= account.users.find(params[:id]) | ||
end | ||
|
||
def label | ||
@label ||= account.labels.find(params[:id]) | ||
end | ||
|
||
def team | ||
@team ||= account.teams.find(params[:id]) | ||
end | ||
|
||
def group_by | ||
@group_by ||= %w[day week month year hour].include?(params[:group_by]) ? params[:group_by] : DEFAULT_GROUP_BY | ||
end | ||
|
||
def timezone | ||
@timezone ||= timezone_name_from_offset(params[:timezone_offset]) | ||
end | ||
end |
Oops, something went wrong.