Class: RRD::Data

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rra, reader, header, data_start) ⇒ Data

Returns a new instance of Data.



6
7
8
9
10
11
12
13
# File 'lib/rrd/data.rb', line 6

def initialize(rra, reader, header, data_start)
  @rra = rra
  @reader = reader
  @header = header
  @data_start = data_start

  header.setup_reader(reader) # update endianess and alignment
end

Instance Attribute Details

#data_startObject (readonly)

Returns the value of attribute data_start.



4
5
6
# File 'lib/rrd/data.rb', line 4

def data_start
  @data_start
end

#headerObject (readonly)

Returns the value of attribute header.



4
5
6
# File 'lib/rrd/data.rb', line 4

def header
  @header
end

#readerObject (readonly)

Returns the value of attribute reader.



4
5
6
# File 'lib/rrd/data.rb', line 4

def reader
  @reader
end

#rraObject (readonly)

Returns the value of attribute rra.



4
5
6
# File 'lib/rrd/data.rb', line 4

def rra
  @rra
end

Instance Method Details

#fetch(options = {}, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rrd/data.rb', line 25

def fetch(options = {}, &block)
  row = options[:start_row] || 0
  rows = [options[:rows] || rra.rows, rra.rows].min

  head = 

  last_update = round_time(header.last_update.to_i)
  current_time = last_update - (rra.rows * step_time)

  columns = [:time] + header.datasources.map(&:name).map(&:to_sym)

  if options[:start_time]
    start_time = round_time(options[:start_time].to_i)

    if current_time < start_time
      row = ((start_time - current_time) / step_time).floor
      current_time = start_time
    end
  end

  if options[:end_time]
    end_time = round_time(options[:end_time].to_i)

    if end_time < last_update
      rows = ((end_time - current_time) / step_time).ceil
    end
  end

  if row < head
    reader.seek(base_position + ((rra.current_row + 1 + row) * row_size))
  end

  data = []
  while row < rows
    if head && row >= head
      reader.seek(base_position + (row - head) * row_size) # jump to beginning
      head = nil
    end

    current_time += step_time

    row_data = [current_time] + reader.read(row_size, unpack_formula)

    row_data = block.call(row_data, columns, options) if block

    data << row_data

    row += 1
  end

  data
end

#last_rowObject



19
20
21
22
23
# File 'lib/rrd/data.rb', line 19

def last_row
  reader.seek(base_position + rra.current_row * row_size)

  [last_update] + reader.read(row_size, unpack_formula)
end

#last_updateObject



15
16
17
# File 'lib/rrd/data.rb', line 15

def last_update
  @last_update ||= round_time(header.last_update.to_i)
end