stats: display contact rate

This commit is contained in:
Pierre de La Morinerie 2018-12-18 09:35:23 +00:00
parent dcb452d7e6
commit d614ea6bd5
7 changed files with 282 additions and 0 deletions

View file

@ -59,6 +59,22 @@ class Helpscout::API
end
end
def conversations_report(year, month)
Rails.logger.info("[HelpScout API] Retrieving conversations report for #{month}-#{year}")
params = {
start: Time.utc(year, month).iso8601,
end: Time.utc(year, month).next_month.iso8601
}
response = call_api(:get, 'reports/conversations?' + params.to_query)
if !response.success?
raise StandardError, "Error while fetching conversation report: #{response.status} '#{response.body}'"
end
parse_response_body(response)
end
private
def attachments(file)

View file

@ -0,0 +1,49 @@
# Fetch and compute monthly reports about the users conversations on Helpscout
class Helpscout::UserConversationsAdapter
EXCLUDED_TAGS = ['openlab', 'bizdev', 'admin', 'webinaire']
def initialize(from, to)
@from = from
@to = to
end
# Return an array of monthly reports
def reports
@reports ||= (@from..@to)
.group_by { |date| [date.year, date.month] }
.keys
.map { |key| { year: key[0], month: key[1] } }
.map { |interval| report(interval[:year], interval[:month]) }
end
private
def report(year, month)
report = fetch_conversations_report(year, month)
total_conversations = report.dig(:current, :totalConversations)
excluded_conversations = report
.dig(:tags, :top)
.select { |tag| tag[:name]&.in?(EXCLUDED_TAGS) }
.map { |tag| tag[:count] }
.sum
{
start_date: report.dig(:current, :startDate).to_datetime,
end_date: report.dig(:current, :endDate).to_datetime,
conversations_count: total_conversations - excluded_conversations
}
end
def fetch_conversations_report(year, month)
if year == Date.today.year && month == Date.today.month
raise ArgumentError, 'The report for the current month will change in the future, and cannot be cached.'
end
@helpscout_api ||= Helpscout::API.new
Rails.cache.fetch("helpscout-conversation-report-#{year}-#{month}") do
@helpscout_api.conversations_report(year, month)
end
end
end