class EGauge::Data

Attributes

config_serial[R]

Attributes

registers[R]

Attributes

timestamp[R]

Attributes

Public Class Methods

new(xml) click to toggle source
# File lib/egauge/data.rb, line 18
def initialize(xml)
  # Store off Nokogiri xml tree
  @xml = xml
  @config_serial = @xml.group['serial']
  
  # Get first data segment, which sets the register info for all data segments
  data = @xml.group.data
  data = data.first if data.is_a?(Nokogiri::XML::NodeSet)
  
  # Save off our starting timestamp for this whole group
  @timestamp = EGauge::parse_time(data['time_stamp'])
  
  # Build our registers
  index = 0
  @registers = data.cname.collect do |col|
    reg = EGauge::Register.new(self, index, col)
    index += 1
    reg
  end
end
parse(xml) click to toggle source

Parse XML and return an EGauge::Data object containing the data. This will be the primary entry point for most uses of this gem.

# File lib/egauge/data.rb, line 10
def self.parse(xml)
  doc = Nokogiri::XML(xml)
  doc.slop!
  
  data = new(doc)
  data
end

Public Instance Methods

each_row() { |ts, vals| ... } click to toggle source

Run each row in the dataset, yielding |timestamp, [register vals…]|

# File lib/egauge/data.rb, line 55
def each_row
  @xml.group.elements.each do |chunk|
    # Set up for running this data chunk - prep timestamp and increment step from source xml
    ts = EGauge::parse_time(chunk['time_stamp'])
    step = chunk['time_delta'].to_i
    
    # Run each row in the chunk, and yield our results
    (chunk / './r').each do |row|
      vals = (row / './c').collect {|c| c.text.to_i}
      yield ts, vals
      ts += step
    end
  end
end
num_registers() click to toggle source
# File lib/egauge/data.rb, line 43
def num_registers
  @registers.count
end
num_rows() click to toggle source
# File lib/egauge/data.rb, line 47
def num_rows
  # Sum the count of rows across each <data> node
  @xml.group.elements.collect do |chunk|
    (chunk / './r').count
  end.inject(&:+)
end
to_a() click to toggle source

Return results as a 2D array, like so: [ [timestamp1, [val1, val2…]], [timestamp2, [val1, val2,…]], … ]

# File lib/egauge/data.rb, line 71
def to_a
  res = []
  each_row do |ts, vals|
    res << [ts, vals]
  end
  res
end
xml() click to toggle source
# File lib/egauge/data.rb, line 39
def xml
  @xml
end