Class: MetricDb::FluidDb_Metric
- Defined in:
- lib/MetricDb/FluidDb/Metric.rb
Instance Method Summary collapse
- #after(t) ⇒ Object
- #firstAfter(t) ⇒ Object
- #get ⇒ Object
-
#initialize(db, id, name) ⇒ FluidDb_Metric
constructor
A new instance of FluidDb_Metric.
- #massage(input) ⇒ Object
- #massageArray(input) ⇒ Object
- #push(value) ⇒ Object
- #q(sql, params) ⇒ Object
- #range(dateRange) ⇒ Object
Constructor Details
#initialize(db, id, name) ⇒ FluidDb_Metric
Returns a new instance of FluidDb_Metric.
6 7 8 9 10 |
# File 'lib/MetricDb/FluidDb/Metric.rb', line 6 def initialize( db, id, name ) @db = db @id = id.to_i @name = name end |
Instance Method Details
#after(t) ⇒ Object
46 47 48 |
# File 'lib/MetricDb/FluidDb/Metric.rb', line 46 def after( t ) return q( "SELECT value, timestamp FROM value_tbl WHERE metric_id = ? AND timestamp > ? ORDER BY timestamp DESC", [@id, t ] ) end |
#firstAfter(t) ⇒ Object
50 51 52 53 54 |
# File 'lib/MetricDb/FluidDb/Metric.rb', line 50 def firstAfter( t ) return massage(@db.queryForArray( "SELECT value, timestamp FROM value_tbl WHERE metric_id = ? AND timestamp > ? ORDER BY timestamp ASC LIMIT 1", [@id, t ] )) rescue FluidDb::NoDataFoundError=>e return nil end |
#get ⇒ Object
35 36 37 38 39 40 |
# File 'lib/MetricDb/FluidDb/Metric.rb', line 35 def get() r = @db.queryForArray( "SELECT value, timestamp FROM value_tbl WHERE metric_id = ? ORDER BY timestamp DESC LIMIT 1", [@id] ) return massage(r) rescue FluidDb::NoDataFoundError=>e return nil end |
#massage(input) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/MetricDb/FluidDb/Metric.rb', line 15 def massage( input ) return massageArray( input ) if input.is_a? Hash l = Array.new input.each do |r| l << massageArray( r ) end return l end |
#massageArray(input) ⇒ Object
12 13 14 |
# File 'lib/MetricDb/FluidDb/Metric.rb', line 12 def massageArray( input ) return Hash["value", input["value"].to_i, "timestamp", Time.parse( input["timestamp"] )] end |
#push(value) ⇒ Object
30 31 32 33 |
# File 'lib/MetricDb/FluidDb/Metric.rb', line 30 def push( value ) @db.execute( "INSERT INTO value_tbl( metric_id, value ) VALUES ( ?, ? )", [@id, value] ) return true end |
#q(sql, params) ⇒ Object
26 27 28 |
# File 'lib/MetricDb/FluidDb/Metric.rb', line 26 def q( sql, params ) return massage( @db.queryForResultset(sql, params )) end |
#range(dateRange) ⇒ Object
42 43 44 |
# File 'lib/MetricDb/FluidDb/Metric.rb', line 42 def range( dateRange ) return q( "SELECT value, timestamp FROM value_tbl WHERE metric_id = ? AND timestamp >= ? AND timestamp <= ? ORDER BY timestamp DESC", [@id, dateRange.begin, dateRange.end ] ) end |