From e946fc89ddeab9739bd41f5e06bb313ad93829a5 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Wed, 21 Jan 2026 14:45:08 +0530 Subject: [PATCH] feat: allow heatmap download --- lib/tasks/download_report.rake | 59 +++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/lib/tasks/download_report.rake b/lib/tasks/download_report.rake index c68418432..59d8383b7 100644 --- a/lib/tasks/download_report.rake +++ b/lib/tasks/download_report.rake @@ -4,13 +4,15 @@ # POSTGRES_STATEMENT_TIMEOUT=600s NEW_RELIC_AGENT_ENABLED=false bundle exec rake download_report:agent # POSTGRES_STATEMENT_TIMEOUT=600s NEW_RELIC_AGENT_ENABLED=false bundle exec rake download_report:inbox # POSTGRES_STATEMENT_TIMEOUT=600s NEW_RELIC_AGENT_ENABLED=false bundle exec rake download_report:label +# POSTGRES_STATEMENT_TIMEOUT=600s NEW_RELIC_AGENT_ENABLED=false bundle exec rake download_report:conversation_heatmap +# POSTGRES_STATEMENT_TIMEOUT=600s NEW_RELIC_AGENT_ENABLED=false bundle exec rake download_report:resolution_heatmap # # The task will prompt for: # - Account ID # - Start Date (YYYY-MM-DD) # - End Date (YYYY-MM-DD) # - Timezone Offset (e.g., 0, 5.5, -5) -# - Business Hours (y/n) - whether to use business hours for time metrics +# - Business Hours (y/n) - whether to use business hours for time metrics (skipped for heatmap tasks) # # Output: ___.csv @@ -26,7 +28,7 @@ module DownloadReportTasks $stdin.gets.chomp end - def self.collect_params + def self.collect_params(include_business_hours: true) account_id = prompt('Enter Account ID') abort 'Error: Account ID is required' if account_id.blank? @@ -42,8 +44,11 @@ module DownloadReportTasks timezone_offset = prompt('Enter Timezone Offset (e.g., 0, 5.5, -5)') timezone_offset = timezone_offset.blank? ? 0 : timezone_offset.to_f - business_hours = prompt('Use Business Hours? (y/n)') - business_hours = business_hours.downcase == 'y' + business_hours = false + if include_business_hours + business_hours = prompt('Use Business Hours? (y/n)') + business_hours = business_hours.downcase == 'y' + end begin tz = ActiveSupport::TimeZone[timezone_offset] @@ -159,6 +164,34 @@ module DownloadReportTasks filename = "#{account.id}_label_#{data[:start_date]}_#{data[:end_date]}.csv" save_csv(filename, headers, rows) end + + def self.download_heatmap_report(metric:, filename_prefix:, header_label:) + data = collect_params(include_business_hours: false) + account = data[:account] + + puts "\nGenerating #{filename_prefix.tr('_', ' ')} report..." + report_params = data[:params].merge({ + metric: metric, + group_by: 'hour', + type: :account + }) + report = V2::Reports::Conversations::ReportBuilder.new(account, report_params).timeseries + timezone = ActiveSupport::TimeZone[data[:params][:timezone_offset]] + + headers = ['Date', 'Hour', header_label] + rows = report.map do |row| + time = timezone.at(row[:timestamp]) + hour = time.hour + [ + time.to_date.to_s, + "#{hour}:00 - #{hour + 1}:00", + row[:value] + ] + end + + filename = "#{account.id}_#{filename_prefix}_#{data[:start_date]}_#{data[:end_date]}.csv" + save_csv(filename, headers, rows) + end end # rubocop:enable Metrics/CyclomaticComplexity # rubocop:enable Metrics/AbcSize @@ -180,4 +213,22 @@ namespace :download_report do task label: :environment do DownloadReportTasks.download_label_report end + + desc 'Download conversation heatmap report as CSV' + task conversation_heatmap: :environment do + DownloadReportTasks.download_heatmap_report( + metric: 'conversations_count', + filename_prefix: 'conversation_heatmap', + header_label: 'Conversations' + ) + end + + desc 'Download resolution heatmap report as CSV' + task resolution_heatmap: :environment do + DownloadReportTasks.download_heatmap_report( + metric: 'resolutions_count', + filename_prefix: 'resolution_heatmap', + header_label: 'Resolutions' + ) + end end