Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬
Ruby actioncablechat-widgetconversationcustomer-supportdashboarddesigndockerdocker-imageherokuintercomjavascriptlivechatopensourcerailsrubysassvuejswhatsappzendesk
class Api::V1::Accounts::Captain::AssistantStatsController < Api::V1::Accounts::BaseController
OVERVIEW_SUMMARY_CACHE_VERSION = 'v1'.freeze
OVERVIEW_SUMMARY_CACHE_TTL = 1.hour
before_action -> { authorize(Captain::Assistant, :metrics?) }, except: :drilldown
before_action -> { authorize(Captain::Assistant, :drilldown?) }, only: :drilldown
before_action :set_assistant
before_action :validate_timezone_offset, only: [:overview_summary, :drilldown]
def drilldown
return head :unprocessable_entity unless Captain::AssistantOverviewDrilldownBuilder.supported_metric?(params[:metric])
if params[:metric] == 'handoff_reason' && Captain::AssistantOverviewDrilldownBuilder::HANDOFF_REASONS.exclude?(params[:reason])
return head :unprocessable_entity
end
render json: Captain::AssistantOverviewDrilldownBuilder.new(
@assistant, params.permit(:metric, :reason, :range, :timezone_offset, :page, :per_page)
).build
end
def overview
metrics = Captain::AssistantOverviewStatsBuilder.new(@assistant, params[:range], params[:timezone_offset]).metrics
render json: metrics.merge(tracking_started_at: Captain::OutcomeTrackingHistory.started_at&.iso8601(6))
end
def overview_summary
cached_result = Redis::Alfred.get(overview_summary_cache_key)
result = cached_result ? JSON.parse(cached_result, symbolize_names: true) : generate_overview_summary
if result[:error]
render json: { error: result[:error] }, status: :unprocessable_content
else
render json: result
end
end
def resolution_flow
render json: Captain::AssistantResolutionFlowBuilder.new(@assistant, params[:range], params[:timezone_offset]).build
end
def resolution_trend
render json: Captain::AssistantResolutionTrendStatsBuilder.new(@assistant, params[:range], params[:timezone_offset]).metrics
end
private
def generate_overview_summary
result = Captain::AssistantOverviewSummaryService.new(
account: Current.account,
assistant: @assistant,
range: params[:range],
timezone_offset: params[:timezone_offset]
).perform
Redis::Alfred.set(overview_summary_cache_key, result.to_json, ex: OVERVIEW_SUMMARY_CACHE_TTL.to_i) unless result[:error]
result
end
def overview_summary_cache_key
[
'captain_assistant_overview_summary',
OVERVIEW_SUMMARY_CACHE_VERSION,
Current.account.id,
@assistant.cache_key_with_version,
stats_window.range,
stats_window.timezone,
Captain::OutcomeTrackingHistory.started_at&.iso8601(6),
Current.account.locale
].join(':')
end
def stats_window
@stats_window ||= Captain::AssistantStatsWindow.new(params[:range], params[:timezone_offset])
end
def validate_timezone_offset
head :unprocessable_entity unless stats_window.timezone_offset_valid?
end
def set_assistant
@assistant = Current.account.captain_assistants.find(params[:assistant_id])
end
end