Class: EGauge::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/egauge/data.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Data

Returns a new instance of Data.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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

Instance Attribute Details

#config_serialObject (readonly)

Attributes



6
7
8
# File 'lib/egauge/data.rb', line 6

def config_serial
  @config_serial
end

#registersObject (readonly)

Attributes



6
7
8
# File 'lib/egauge/data.rb', line 6

def registers
  @registers
end

#timestampObject (readonly)

Attributes



6
7
8
# File 'lib/egauge/data.rb', line 6

def timestamp
  @timestamp
end

Class Method Details

.parse(xml) ⇒ Object

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



10
11
12
13
14
15
16
# File 'lib/egauge/data.rb', line 10

def self.parse(xml)
  doc = Nokogiri::XML(xml)
  doc.slop!
  
  data = new(doc)
  data
end

Instance Method Details

#each_rowObject

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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_registersObject



43
44
45
# File 'lib/egauge/data.rb', line 43

def num_registers
  @registers.count
end

#num_rowsObject



47
48
49
50
51
52
# 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_aObject

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



71
72
73
74
75
76
77
# File 'lib/egauge/data.rb', line 71

def to_a
  res = []
  each_row do |ts, vals|
    res << [ts, vals]
  end
  res
end

#xmlObject



39
40
41
# File 'lib/egauge/data.rb', line 39

def xml
  @xml
end