Sha256: 502f635cb3c9ae293b5138408ea8edea68b05a9fe03c76ed17f5074cc5775aef

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module SiteHealth
  class PageSpeedSummarizer
    def initialize(data)
      @data = data[:checked_urls]
    end

    def to_csv
      to_matrix.map { |row| row.join(',') }.join("\n")
    end

    def to_matrix
      header = %w[
        url
        total_speed_score
        css_kb
        html_kb
        image_kb
        javascript_kb
        other_kb
        total_kbytes
        number_hosts
        number_js_resources
        number_css_resources
        number_resources
        number_static_resources
        started_at
        finished_at
        runtime_in_seconds
      ]
      rows = @data.map do |_, data|
        pagespeed_data = data.dig(:checks, :google_page_speed).data
        next unless pagespeed_data

        url = data[:url]
        started_at = data[:started_at]
        finished_at = data[:finished_at]
        runtime = data[:runtime_in_seconds]

        build_row(url, runtime, started_at, finished_at, pagespeed_data)
      end.reject(&:nil?)

      [header] + rows
    end

    def build_row(url, runtime_in_seconds, started_at, finished_at, pagespeed_data)
      stats = pagespeed_data[:page_stats]

      kbytes_columns = [
        bytes_to_kb(stats[:css_response_bytes]),
        bytes_to_kb(stats[:html_response_bytes]),
        bytes_to_kb(stats[:image_response_bytes]),
        bytes_to_kb(stats[:javascript_response_bytes]),
        bytes_to_kb(stats[:other_response_bytes]),
      ]
      kbytes_columns << kbytes_columns.sum.round(1)

      host_columns = [
        stats[:number_hosts],
        stats[:number_js_resources],
        stats[:number_css_resources],
        stats[:number_resources],
        stats[:number_static_resources],
      ]

      total_speed_score = pagespeed_data.dig(:rule_groups, :SPEED, :score)

      [url, total_speed_score] + kbytes_columns + host_columns +
        [started_at, finished_at, runtime_in_seconds]
    end

    def bytes_to_kb(bytes, round: 1)
      (bytes / 1024.0).round(round)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
site_health-0.2.0 lib/site_health/summarizers/page_size_summarizer.rb