Class: Helium::DataPoint

Inherits:
Resource show all
Defined in:
lib/helium/data_point.rb

Instance Attribute Summary collapse

Attributes inherited from Resource

#id, #params, #type

Instance Method Summary collapse

Methods inherited from Resource

#==, all, all_path, create, #created_at, #destroy, #eql?, find, #hash, initialize_from_path, #metadata, resource_name, #resource_name, #resource_path, singleton, #to_json, #update, #updated_at

Methods included from Utils

#datetime_to_iso, #kebab_case

Constructor Details

#initialize(opts = {}) ⇒ DataPoint

Returns a new instance of DataPoint



5
6
7
8
9
10
11
# File 'lib/helium/data_point.rb', line 5

def initialize(opts = {})
  super(opts)

  @timestamp  = @params.dig("attributes", "timestamp")
  @value      = @params.dig("attributes", "value")
  @port       = @params.dig("attributes", "port")
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port



3
4
5
# File 'lib/helium/data_point.rb', line 3

def port
  @port
end

#timestampObject (readonly)

Returns the value of attribute timestamp



3
4
5
# File 'lib/helium/data_point.rb', line 3

def timestamp
  @timestamp
end

#valueObject (readonly)

Returns the value of attribute value



3
4
5
# File 'lib/helium/data_point.rb', line 3

def value
  @value
end

Instance Method Details

#aggregate?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/helium/data_point.rb', line 32

def aggregate?
  [max, min, avg].none? { |agg_value| agg_value.nil? }
end

#as_jsonObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/helium/data_point.rb', line 36

def as_json
  j = super.merge({
    timestamp: timestamp,
    port: port
  })

  if aggregate?
    j[:max] = max
    j[:min] = min
    j[:avg] = avg
  else
    j[:value] = value
  end

  j
end

#avgObject



27
28
29
30
# File 'lib/helium/data_point.rb', line 27

def avg
  return nil unless @value.is_a?(Hash)
  @value["avg"]
end

#maxObject



17
18
19
20
# File 'lib/helium/data_point.rb', line 17

def max
  return nil unless @value.is_a?(Hash)
  @value["max"]
end

#minObject



22
23
24
25
# File 'lib/helium/data_point.rb', line 22

def min
  return nil unless @value.is_a?(Hash)
  @value["min"]
end