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

#after_create, 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?, properties, request, #request, #resource_name, resource_name, #save, #to_hash, #update, #update_params, #update_request, #validate, web_request, zabbix_attr, zabbix_define

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
20
# File 'lib/rubix/models/time_series.rb', line 12

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

Instance Attribute Details

#fromObject



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

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

#raw_dataObject



50
51
52
# File 'lib/rubix/models/time_series.rb', line 50

def raw_data
  @raw_data ||= []
end

#uptoObject



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

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

Class Method Details

.default_fromObject



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

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

.default_history(item) ⇒ Object



46
47
48
# File 'lib/rubix/models/time_series.rb', line 46

def self.default_history item
  item.respond_to?(:value_type) ? Item::VALUE_CODES[item.value_type] : 3
end

.default_uptoObject



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

def self.default_upto
  Time.now.utc
end

.find(options = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/rubix/models/time_series.rb', line 77

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



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

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,
                  :history   => (options[:history] || default_history(options[:item])).to_s
                })
end

.get_paramsObject

Requests ==



64
65
66
# File 'lib/rubix/models/time_series.rb', line 64

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

.zabbix_nameObject



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

def self.zabbix_name
  'history'
end

Instance Method Details

#historyObject



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

def history
  @history ||= self.class.default_history(item)
end

#parsed_dataObject

Transformations ==



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rubix/models/time_series.rb', line 91

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