Class: PigCI::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/pig_ci/report.rb

Direct Known Subclasses

DatabaseRequest, Memory, RequestTime

Defined Under Namespace

Classes: DatabaseRequest, Memory, RequestTime

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(historical_log_file: nil, i18n_key: nil, timestamp: nil) ⇒ Report

Returns a new instance of Report.



4
5
6
7
8
# File 'lib/pig_ci/report.rb', line 4

def initialize(historical_log_file: nil, i18n_key: nil, timestamp: nil)
  @i18n_key = i18n_key || self.class.name.underscore.split("/").last
  @historical_log_file = historical_log_file || PigCI.tmp_directory.join("#{@i18n_key}.json")
  @timestamp = timestamp || PigCI.run_timestamp
end

Instance Attribute Details

#historical_log_fileObject

Returns the value of attribute historical_log_file.



2
3
4
# File 'lib/pig_ci/report.rb', line 2

def historical_log_file
  @historical_log_file
end

#i18n_keyObject

Returns the value of attribute i18n_key.



2
3
4
# File 'lib/pig_ci/report.rb', line 2

def i18n_key
  @i18n_key
end

Class Method Details

.format_row(row) ⇒ Object



63
64
65
66
67
# File 'lib/pig_ci/report.rb', line 63

def self.format_row(row)
  row = row.dup
  row[:max_change_percentage] = "#{row[:max_change_percentage]}%"
  row
end

Instance Method Details

#column_keysObject



55
56
57
# File 'lib/pig_ci/report.rb', line 55

def column_keys
  %i[key max min mean number_of_requests max_change_percentage]
end

#headingsObject



10
11
12
# File 'lib/pig_ci/report.rb', line 10

def headings
  column_keys.collect { |key| I18n.t(".attributes.#{key}", scope: i18n_scope, locale: PigCI.locale) }
end

#historical_dataObject



47
48
49
# File 'lib/pig_ci/report.rb', line 47

def historical_data
  @historical_data ||= PigCI::Metric::Historical.new(historical_log_file: @historical_log_file).to_h
end

#i18n_nameObject



14
15
16
# File 'lib/pig_ci/report.rb', line 14

def i18n_name
  I18n.t(".name", scope: i18n_scope, locale: PigCI.locale)
end

#i18n_scopeObject



59
60
61
# File 'lib/pig_ci/report.rb', line 59

def i18n_scope
  @i18n_scope ||= "pig_ci.report.#{i18n_key}"
end

#max_for(timestamp) ⇒ Object



18
19
20
# File 'lib/pig_ci/report.rb', line 18

def max_for(timestamp)
  sorted_and_formatted_data_for(timestamp).collect { |row| row[:max] }.max
end

#over_threshold_for?(timestamp) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/pig_ci/report.rb', line 26

def over_threshold_for?(timestamp)
  return false unless threshold.present? && max_for(timestamp).present?

  max_for(timestamp) > threshold
end

#sorted_and_formatted_data_for(timestamp) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/pig_ci/report.rb', line 32

def sorted_and_formatted_data_for(timestamp)
  data_for(timestamp)[@i18n_key.to_sym].sort_by { |data|
    PigCI.report_row_sort_by(data)
  }.collect do |data|
    self.class.format_row(data)
  end
end

#thresholdObject



22
23
24
# File 'lib/pig_ci/report.rb', line 22

def threshold
  PigCI.thresholds.dig(@i18n_key.to_sym)
end

#timestampsObject



51
52
53
# File 'lib/pig_ci/report.rb', line 51

def timestamps
  historical_data.keys
end

#to_payload_for(timestamp) ⇒ Object



40
41
42
43
44
45
# File 'lib/pig_ci/report.rb', line 40

def to_payload_for(timestamp)
  {
    profiler: @i18n_key.to_sym,
    data: data_for(timestamp)[@i18n_key.to_sym]
  }
end