Module: PredictabilityEngine::Report::ImageGenerator

Defined in:
lib/predictability_engine/report/image_generator.rb

Class Method Summary collapse

Class Method Details

.capture_chart(page, chart_id, images_path) ⇒ Object



29
30
31
32
33
# File 'lib/predictability_engine/report/image_generator.rb', line 29

def self.capture_chart(page, chart_id, images_path)
  title = Constants::CHART_CONFIG[chart_id][:title]
  page.locator(".chart-panel:has(h2:text-is(\"#{title}\"))")
      .screenshot(path: File.join(images_path, "#{chart_id}.png"))
end

.generate(report, base_dir, width: 800, height: 600, scale: 1) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/predictability_engine/report/image_generator.rb', line 8

def self.generate(report, base_dir, width: 800, height: 600, scale: 1)
  images_path = File.join(base_dir, 'images')
  FileUtils.mkdir_p(images_path)
  temp_html = "tmp/images_#{report.object_id}.html"
  File.write(temp_html, report.render_html(layout: :standard))

  require 'playwright'
  Playwright.create(playwright_cli_executable_path: report.playwright_bin) do |playwright|
    playwright.chromium.launch(**report.playwright_chromium_launch_opts) do |browser|
      page = browser.new_page(viewport: { width: width, height: height },
                              deviceScaleFactor: scale)
      page.goto("file://#{File.expand_path(temp_html)}")
      sleep 2
      Constants::CHART_CONFIG.each_key { |id| capture_chart(page, id, images_path) }
    end
  end
  images_path
ensure
  FileUtils.rm_f(temp_html) if temp_html
end