Class: DataStore::Table
- Inherits:
-
Object
- Object
- DataStore::Table
- Defined in:
- lib/data_store/table.rb
Instance Attribute Summary collapse
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
-
#original_value ⇒ Object
readonly
Returns the value of attribute original_value.
-
#table_index ⇒ Object
readonly
Returns the value of attribute table_index.
Instance Method Summary collapse
-
#add(value, options = {}) ⇒ Object
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.
-
#count ⇒ Object
Return the total number of datapoints in the table.
-
#dataset ⇒ Object
Return the corresponding dataset with the datapoitns.
-
#fetch(options) ⇒ Object
Fetch the corresponding datapoints.
-
#import(datapoints) ⇒ Object
Import original datapoints, mostly to recreate compression tables.
-
#initialize(identifier, table_index = 0) ⇒ Table
constructor
Initialize the table by passsing an identifier.
-
#last ⇒ Object
Return the most recent datapoint added.
- #model ⇒ Object
-
#parent ⇒ Object
Return a the corresponding parent class, i.e the settings from the data_stores table.
Constructor Details
#initialize(identifier, table_index = 0) ⇒ Table
Initialize the table by passsing an identifier
8 9 10 11 |
# File 'lib/data_store/table.rb', line 8 def initialize(identifier, table_index = 0) @identifier = identifier @table_index = table_index end |
Instance Attribute Details
#identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
5 6 7 |
# File 'lib/data_store/table.rb', line 5 def identifier @identifier end |
#original_value ⇒ Object (readonly)
Returns the value of attribute original_value.
5 6 7 |
# File 'lib/data_store/table.rb', line 5 def original_value @original_value end |
#table_index ⇒ Object (readonly)
Returns the value of attribute table_index.
5 6 7 |
# File 'lib/data_store/table.rb', line 5 def table_index @table_index end |
Instance Method Details
#add(value, options = {}) ⇒ Object
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
32 33 34 35 36 37 38 39 40 |
# File 'lib/data_store/table.rb', line 32 def add(value, = {}) created = [:created] || Time.now.utc.to_f type = [:type] || parent.type factor = [:factor] || 1 original_idx = @table_index @table_index = [:table_index] if [:table_index] push(value, type, created, factor) @table_index = original_idx end |
#count ⇒ Object
Return the total number of datapoints in the table
52 53 54 |
# File 'lib/data_store/table.rb', line 52 def count dataset.count end |
#dataset ⇒ Object
Return the corresponding dataset with the datapoitns
57 58 59 |
# File 'lib/data_store/table.rb', line 57 def dataset database[table_name] end |
#fetch(options) ⇒ Object
Fetch the corresponding datapoints
Options:
* :from
* :till
67 68 69 70 71 72 |
# File 'lib/data_store/table.rb', line 67 def fetch() datapoints = [] query = parent.db[timeslot()].where{created >= [:from]}.where{created <= [:till]}.order(:created) query.all.map{|record| datapoints <<[record[:created], record[:value]]} datapoints end |
#import(datapoints) ⇒ Object
Import original datapoints, mostly to recreate compression tables
75 76 77 78 79 |
# 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 ⇒ Object
Return the most recent datapoint added
47 48 49 |
# File 'lib/data_store/table.rb', line 47 def last model.last end |
#model ⇒ Object
42 43 44 |
# File 'lib/data_store/table.rb', line 42 def model @model ||= Class.new(Sequel::Model(dataset)) end |