Class: Railswatch::Events::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/railswatch/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(record) ⇒ Record

Returns a new instance of Record.



27
28
29
# File 'lib/railswatch/events/record.rb', line 27

def initialize(record)
  @record = record
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



6
7
8
# File 'lib/railswatch/events/record.rb', line 6

def record
  @record
end

Class Method Details

.allObject



22
23
24
# File 'lib/railswatch/events/record.rb', line 22

def all
  Railswatch::Models::EventRecord.order(:occurred_at).map { |record| new(record) }
end

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



13
14
15
16
17
18
19
20
# File 'lib/railswatch/events/record.rb', line 13

def create(name:, datetimei: Time.now.to_i, options: {})
  model = Railswatch::Models::EventRecord.create!(
    name: name,
    options: options,
    occurred_at: Railswatch::Utils.from_datetimei(datetimei.to_i)
  )
  new(model)
end

Instance Method Details

#datetimeiObject



33
34
35
# File 'lib/railswatch/events/record.rb', line 33

def datetimei
  record.occurred_at.to_i
end

#optionsObject



37
38
39
# File 'lib/railswatch/events/record.rb', line 37

def options
  (record.options || {}).deep_stringify_keys
end

#to_annotationObject



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

def to_annotation
  {
    x: datetimei * 1000,
    borderColor: options['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



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

def value
  {
    name: name,
    datetime: record.occurred_at,
    datetimei: datetimei,
    options: options
  }
end