Class: HeatLoss::ChartExporter

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/heat_loss/chart_exporter.rb

Overview

Service to export Highcharts configurations to PNG images
Uses the Highcharts Export Server API to convert chart configs to images

Defined Under Namespace

Classes: Result

Constant Summary collapse

EXPORT_URL =
'https://export.highcharts.com/'
REFERER =
'https://www.warmlyyours.com/'
USER_AGENT =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.month_chart_config(heat_loss) ⇒ Hash

Build a column chart configuration for heat loss vs heat supply by month

Parameters:

  • heat_loss (Hash)

    Heat loss calculation data

Returns:

  • (Hash)

    Highcharts column chart configuration



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/services/heat_loss/chart_exporter.rb', line 88

def self.month_chart_config(heat_loss)
  months = %w[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec]
  hl_averages = (heat_loss['hlc_averages'] || []).map { |o| o['heat_loss'].round }
  heat_supplied = (heat_loss['heat_supplied'] || []).map { |o| o['heat_supplied'].round }

  {
    chart: { type: 'column', width: 500, height: 300, backgroundColor: '#ffffff' },
    title: { text: 'Heat Loss vs. Heat Supply' },
    xAxis: { categories: months, crosshair: true },
    yAxis: { title: { text: 'BTU/hour' } },
    series: [
      { name: 'Heat Loss', data: hl_averages, color: '#FF6B6B' },
      { name: 'Heat Supplied', data: heat_supplied, color: '#4ECDC4' }
    ],
    credits: { enabled: false }
  }
end

.pie_chart_config(heat_loss) ⇒ Hash

Build a pie chart configuration for heat loss by type

Parameters:

  • heat_loss (Hash)

    Heat loss calculation data

Returns:

  • (Hash)

    Highcharts pie chart configuration



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/services/heat_loss/chart_exporter.rb', line 62

def self.pie_chart_config(heat_loss)
  pie_data = (heat_loss['hlc_by_object'] || []).map do |o|
    { name: o['name'], y: o['heat_loss'].round, color: o['color'] }
  end

  {
    chart: { type: 'pie', width: 400, height: 300, backgroundColor: '#ffffff' },
    title: { text: 'Heat Loss by Type' },
    plotOptions: {
      pie: {
        allowPointSelect: true,
        cursor: 'pointer',
        dataLabels: {
          enabled: true,
          format: '<b>{point.name}</b>: {point.y} BTU/hr'
        }
      }
    },
    series: [{ name: 'Heat Loss', colorByPoint: true, data: pie_data }],
    credits: { enabled: false }
  }
end

.temp_chart_config(room, heat_loss) ⇒ Hash

Build a line chart configuration for room vs outside temperature

Parameters:

  • room (RoomConfiguration)

    The room configuration

  • heat_loss (Hash)

    Heat loss calculation data

Returns:

  • (Hash)

    Highcharts line chart configuration



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/services/heat_loss/chart_exporter.rb', line 110

def self.temp_chart_config(room, heat_loss)
  months = %w[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec]
  desired_temps = (heat_loss['avg_temperatures'] || []).map { |_o| room.desired_temperature.round }
  ambient_temps = (heat_loss['ambient_temperatures'] || []).map { |o| o['temperature'].round }
  avg_temps = (heat_loss['avg_temperatures'] || []).map { |o| o['temperature'].round }

  {
    chart: { type: 'line', width: 500, height: 300, backgroundColor: '#ffffff' },
    title: { text: 'Room vs. Outside Temperature' },
    xAxis: { categories: months },
    yAxis: { title: { text: 'Temperature (°F)' } },
    series: [
      { name: 'Desired Temperature', data: desired_temps, color: '#FF6B6B', dashStyle: 'Dash' },
      { name: 'Average Room Temperature', data: ambient_temps, color: '#4ECDC4' },
      { name: 'Outside Temperature', data: avg_temps, color: '#95A5A6' }
    ],
    credits: { enabled: false }
  }
end

Instance Method Details

#process(chart_config:, width: 400, height: 300, type: 'image/png') ⇒ Result

Export a chart configuration to PNG image data

Parameters:

  • chart_config (Hash)

    Highcharts chart configuration

  • width (Integer) (defaults to: 400)

    Chart width in pixels

  • height (Integer) (defaults to: 300)

    Chart height in pixels

  • type (String) (defaults to: 'image/png')

    Output type ('image/png' or 'image/svg+xml')

Returns:

  • (Result)

    with image_data (binary string) or error



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/services/heat_loss/chart_exporter.rb', line 25

def process(chart_config:, width: 400, height: 300, type: 'image/png')
  response = connection.post do |req|
    req.body = {
      'options' => chart_config.to_json,
      'type' => type,
      'width' => width.to_s,
      'height' => height.to_s
    }
  end

  if response.success?
    Result.new(
      success: true,
      image_data: response.body,
      content_type: response.headers['content-type'],
      error: nil
    )
  else
    Result.new(
      success: false,
      image_data: nil,
      content_type: nil,
      error: "HTTP #{response.status}: #{response.body&.truncate(200)}"
    )
  end
rescue Faraday::Error => e
  Result.new(
    success: false,
    image_data: nil,
    content_type: nil,
    error: "#{e.class}: #{e.message}"
  )
end