Class: EGauge::Data
- Inherits:
-
Object
- Object
- EGauge::Data
- Defined in:
- lib/egauge/data.rb
Instance Attribute Summary collapse
-
#config_serial ⇒ Object
readonly
Attributes.
-
#registers ⇒ Object
readonly
Attributes.
-
#timestamp ⇒ Object
readonly
Attributes.
Class Method Summary collapse
-
.parse(xml) ⇒ Object
Parse XML and return an EGauge::Data object containing the data.
Instance Method Summary collapse
-
#each_row ⇒ Object
Run each row in the dataset, yielding |timestamp, [register vals…]|.
-
#initialize(xml) ⇒ Data
constructor
A new instance of Data.
- #num_registers ⇒ Object
- #num_rows ⇒ Object
-
#to_a ⇒ Object
Return results as a 2D array, like so: [ [timestamp1, [val1, val2…]], [timestamp2, [val1, val2,…]], … ].
- #xml ⇒ Object
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_serial ⇒ Object (readonly)
Attributes
6 7 8 |
# File 'lib/egauge/data.rb', line 6 def config_serial @config_serial end |
#registers ⇒ Object (readonly)
Attributes
6 7 8 |
# File 'lib/egauge/data.rb', line 6 def registers @registers end |
#timestamp ⇒ Object (readonly)
Attributes
6 7 8 |
# File 'lib/egauge/data.rb', line 6 def @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_row ⇒ Object
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_registers ⇒ Object
43 44 45 |
# File 'lib/egauge/data.rb', line 43 def num_registers @registers.count end |
#num_rows ⇒ Object
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_a ⇒ Object
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 |
#xml ⇒ Object
39 40 41 |
# File 'lib/egauge/data.rb', line 39 def xml @xml end |