Class: DataStore::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/data_store/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#identifierObject (readonly)

Returns the value of attribute identifier.



5
6
7
# File 'lib/data_store/table.rb', line 5

def identifier
  @identifier
end

#original_valueObject (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_indexObject (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, 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

#countObject

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

#datasetObject

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(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) ⇒ 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

#lastObject

Return the most recent datapoint added



47
48
49
# File 'lib/data_store/table.rb', line 47

def last
  model.last
end

#modelObject



42
43
44
# File 'lib/data_store/table.rb', line 42

def model
  @model ||= Class.new(Sequel::Model(dataset))
end

#parentObject

Return a the corresponding parent class, i.e the settings from the data_stores table



14
15
16
# File 'lib/data_store/table.rb', line 14

def parent
  DataStore::Base.find(identifier: identifier)
end