class DataStore::Table
Attributes
identifier[R]
original_value[R]
table_index[R]
Public Class Methods
new(identifier, table_index = 0)
click to toggle source
Initialize the table by passsing an identifier
# File lib/data_store/table.rb, line 8 def initialize(identifier, table_index = 0) @identifier = identifier @table_index = table_index end
Public Instance Methods
add(value, options = {})
click to toggle source
Add a new datapoint to the table In case of a counter type, store the difference between current and last value And calculates average values on the fly according to compression schema
Options (hash):
* created: timestamp * type: gauge or counter * table_index: in which compressed table * factor: multiply value with the given factor
# File lib/data_store/table.rb, line 32 def add(value, options = {}) created = options[:created] || Time.now.utc.to_f type = options[:type] || parent.type factor = options[:factor] || 1 original_idx = @table_index @table_index = options[:table_index] if options[:table_index] push(value, type, created, factor) @table_index = original_idx end
count()
click to toggle source
Return the total number of datapoints in the table
# File lib/data_store/table.rb, line 52 def count dataset.count end
dataset()
click to toggle source
Return the corresponding dataset with the datapoitns
# File lib/data_store/table.rb, line 57 def dataset database[table_name] end
fetch(options)
click to toggle source
Fetch the corresponding datapoints
Options:
* :from * :till
# File lib/data_store/table.rb, line 67 def fetch(options) datapoints = [] query = parent.db[timeslot(options)].where{created >= options[:from]}.where{created <= options[:till]}.order(:created) query.all.map{|record| datapoints <<[record[:created], record[:value]]} datapoints end
import(datapoints)
click to toggle source
Import original datapoints, mostly to recreate compression tables
# File lib/data_store/table.rb, line 75 def import(datapoints) datapoints.each do |data| add(data[0], table_index: 0, created: data[1]) end end
last()
click to toggle source
Return the most recent datapoint added
# File lib/data_store/table.rb, line 47 def last model.last end
model()
click to toggle source
# File lib/data_store/table.rb, line 42 def model @model ||= Class.new(Sequel::Model(dataset)) end
parent()
click to toggle source
Return a the corresponding parent class, i.e the settings from the data_stores table
# File lib/data_store/table.rb, line 14 def parent DataStore::Base.find(identifier: identifier) end
Private Instance Methods
calculate_average_values()
click to toggle source
# File lib/data_store/table.rb, line 103 def calculate_average_values calculator = AverageCalculator.new(self) calculator.perform end
database()
click to toggle source
# File lib/data_store/table.rb, line 122 def database @database ||= DataStore::Base.db end
difference_with_previous(value)
click to toggle source
# File lib/data_store/table.rb, line 108 def difference_with_previous(value) @original_value = value unless last.nil? begin value = value - last[:original_value] last.delete if last[:value] == last[:original_value] rescue TypeError #It is possible a value is not stored properly the last time so we get a 'TypeError: nil can't be coerced into Float' last.delete value = 0 end end value end
push(value, type, created, factor)
click to toggle source
# File lib/data_store/table.rb, line 93 def push(value, type, created, factor) value = difference_with_previous(value) if type.to_s == 'counter' datapoint = { value: value * factor, created: created } datapoint[:original_value] = original_value if original_value database.transaction do dataset << datapoint end calculate_average_values end
table_name()
click to toggle source
# File lib/data_store/table.rb, line 126 def table_name parent.table_names[table_index] end
timeslot(options)
click to toggle source
# File lib/data_store/table.rb, line 83 def timeslot(options) distance = options[:till] - options[:from] index = 0 parent.time_borders.each_with_index do |value, idx| index = idx break if value >= distance end parent.table_names[index] end