Class: Rubix::TimeSeries

Inherits:
Model
  • Object
show all
Includes:
Associations::BelongsToItem
Defined in:
lib/rubix/models/time_series.rb

Instance Attribute Summary collapse

Attributes inherited from Model

#id, #properties

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Associations::BelongsToItem

#item, #item=, #item_id, #item_id=

Methods inherited from Model

all, all_params, all_request, #before_destroy, #before_update, #create, #create_params, #create_request, #destroy, #destroy_params, #destroy_request, each, find_or_create, find_request, #id_field, id_field, list, #new_record?, request, #request, #resource_name, resource_name, #save, #update, #update_params, #update_request, #validate

Methods included from Logs

#debug, #error, #fatal, #info, #warn

Constructor Details

#initialize(properties = {}) ⇒ TimeSeries

Returns a new instance of TimeSeries.



12
13
14
15
16
17
18
19
# File 'lib/rubix/models/time_series.rb', line 12

def initialize properties={}
  super(properties)
  @from       = properties[:from]
  @upto       = properties[:upto]
  
  self.item    = properties[:item]
  self.item_id = properties[:item_id]
end

Instance Attribute Details

#fromObject



29
30
31
# File 'lib/rubix/models/time_series.rb', line 29

def from
  @from ||= self.class.default_from
end

#raw_dataObject



41
42
43
# File 'lib/rubix/models/time_series.rb', line 41

def raw_data
  @raw_data ||= []
end

#uptoObject



37
38
39
# File 'lib/rubix/models/time_series.rb', line 37

def upto
  @upto ||= self.class.default_upto
end

Class Method Details

.default_fromObject



25
26
27
# File 'lib/rubix/models/time_series.rb', line 25

def self.default_from
  (Time.now - 3600).utc
end

.default_uptoObject



33
34
35
# File 'lib/rubix/models/time_series.rb', line 33

def self.default_upto
  Time.now.utc
end

.find(options = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/rubix/models/time_series.rb', line 67

def self.find options={}
  response = find_request(options)
  case
  when response.success?
    new(options.merge(:raw_data => response.result))
  else
    error("Error finding #{resource_name} using #{options.inspect}: #{response.error_message}")
  end
end

.find_params(options = {}) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/rubix/models/time_series.rb', line 59

def self.find_params options={}
  super().merge({
                  :itemids => [options[:item_id].to_s],
                  :time_from => (options[:from] || default_from).to_i.to_s,
                  :time_till => (options[:upto] || default_upto).to_i.to_s
                })
end

.get_paramsObject

Requests ==



55
56
57
# File 'lib/rubix/models/time_series.rb', line 55

def self.get_params
  super().merge(:output => :extend)
end

.zabbix_nameObject



21
22
23
# File 'lib/rubix/models/time_series.rb', line 21

def self.zabbix_name
  'history'
end

Instance Method Details

#parsed_dataObject

Transformations ==



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rubix/models/time_series.rb', line 81

def parsed_data
  return @parsed_data if @parsed_data
  caster = case
           when item.nil?                        then nil
           when item.value_type == :float        then :to_f
           when item.value_type == :unsigned_int then :to_i
           end
  @parsed_data = raw_data.map do |point|
    next unless point.is_a?(Hash) && point['clock'] && point['value']
    timestamp = point['clock'].to_i
    next if timestamp == 0
    {
      'time'  => Time.at(timestamp),
      'value' => (caster ? point['value'].send(caster) : point['value'])
    }
  end.compact
end