Class: RailsPerformance::Events::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_performance/events/record.rb

Constant Summary collapse

DEFAULT_COLOR =
"#FF00FF"
DEFAULT_LABEL_COLOR =
"#FF00FF"
DEFAULT_LABEL_ORIENTATION =
"horizontal"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, datetimei:, options: {}) ⇒ Record

Returns a new instance of Record.



26
27
28
29
30
# File 'lib/rails_performance/events/record.rb', line 26

def initialize(name:, datetimei:, options: {})
  @name = name
  @datetimei = datetimei
  @options = options
end

Instance Attribute Details

#datetimeiObject (readonly)

Returns the value of attribute datetimei.



4
5
6
# File 'lib/rails_performance/events/record.rb', line 4

def datetimei
  @datetimei
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/rails_performance/events/record.rb', line 4

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/rails_performance/events/record.rb', line 4

def options
  @options
end

Class Method Details

.allObject



17
18
19
20
21
22
23
# File 'lib/rails_performance/events/record.rb', line 17

def all
  _, values = RailsPerformance::Utils.fetch_from_redis("rails_performance:records:events:*")
  Array(values).map do |value|
    json = JSON.parse(value)
    new(name: json["name"], datetimei: json["datetimei"], options: Hash(json["options"]))
  end
end

.create(name:, datetimei: Time.now.to_i, options: {}) ⇒ Object



11
12
13
14
15
# File 'lib/rails_performance/events/record.rb', line 11

def create(name:, datetimei: Time.now.to_i, options: {})
  instance = new(name: name, datetimei: datetimei, options: options)
  instance.save
  instance
end

Instance Method Details

#rails_performance_keyObject



36
37
38
# File 'lib/rails_performance/events/record.rb', line 36

def rails_performance_key
  "rails_performance:records:events:#{datetimei}|#{RailsPerformance::EVENTS_SCHEMA}"
end

#saveObject



32
33
34
# File 'lib/rails_performance/events/record.rb', line 32

def save
  RailsPerformance::Utils.save_to_redis(rails_performance_key, value)
end

#to_annotationObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rails_performance/events/record.rb', line 49

def to_annotation
  {
    x: datetimei * 1000,
    borderColor: options.dig("borderColor") || DEFAULT_COLOR,
    label: {
      borderColor: options.dig("label", "borderColor") || DEFAULT_LABEL_COLOR,
      orientation: options.dig("label", "orientation") || DEFAULT_LABEL_ORIENTATION,
      text: options.dig("label", "text") || name
    }
  }
end

#valueObject



40
41
42
43
44
45
46
47
# File 'lib/rails_performance/events/record.rb', line 40

def value
  {
    name: name,
    datetime: RailsPerformance::Utils.from_datetimei(datetimei.to_i),
    datetimei: datetimei,
    options: options
  }
end