Class: SiteHealth::PageSpeedSummarizer

Inherits:
Object
  • Object
show all
Defined in:
lib/site_health/summarizers/page_size_summarizer.rb

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ PageSpeedSummarizer

Returns a new instance of PageSpeedSummarizer.



5
6
7
# File 'lib/site_health/summarizers/page_size_summarizer.rb', line 5

def initialize(data)
  @data = data[:checked_urls]
end

Instance Method Details

#build_row(url, runtime_in_seconds, started_at, finished_at, pagespeed_data) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/site_health/summarizers/page_size_summarizer.rb', line 47

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

#bytes_to_kb(bytes, round: 1) ⇒ Object



73
74
75
# File 'lib/site_health/summarizers/page_size_summarizer.rb', line 73

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

#to_csvObject



9
10
11
# File 'lib/site_health/summarizers/page_size_summarizer.rb', line 9

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

#to_matrixObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/site_health/summarizers/page_size_summarizer.rb', line 13

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