class PRRD::Database

PRRD Database class

Attributes

archives[RW]
datasources[RW]
path[RW]

Accessors

start[RW]
step[RW]

Public Class Methods

new(values = nil) click to toggle source

Constructor

# File lib/prrd/database.rb, line 15
def initialize(values = nil)
  @datasources = []
  @archives = []
  @start = Time.now.to_i - 86_400
  @step = 300

  unless values.nil?
    values.each do |k, v|
      m = "#{k}=".to_sym
      next unless respond_to? m
      send m, v
    end
  end
end

Public Instance Methods

<<(object) click to toggle source

Add an object @param object [Object]

# File lib/prrd/database.rb, line 66
def <<(object)
  if object.is_a? PRRD::Database::Datasource
    add_datasource object
  elsif object.is_a? PRRD::Database::Archive
    add_archive object
  else
    fail 'Can not add this kind of object in PRRD::Database'
  end
end
add_archive(archive) click to toggle source

Add an archive object @param archive [PRRD::Database::Archive]

# File lib/prrd/database.rb, line 54
def add_archive(archive)
  @archives << archive
end
add_archives(archives) click to toggle source

Add archive objects @param archives [Array]

# File lib/prrd/database.rb, line 60
def add_archives(archives)
  @archives = archives
end
add_datasource(datasource) click to toggle source

Add a datasource object @param datasource [PRRD::Database::Datasource]

# File lib/prrd/database.rb, line 42
def add_datasource(datasource)
  @datasources << datasource
end
add_datasources(datasources) click to toggle source

Add datasource objects @param datasource [Array]

# File lib/prrd/database.rb, line 48
def add_datasources(datasources)
  @datasources = datasources
end
check_file() click to toggle source

Check database existence

# File lib/prrd/database.rb, line 36
def check_file
  fail 'Database path is missing' if  @path.nil? || !exists?
end
create() click to toggle source

Create a database @return [String]

# File lib/prrd/database.rb, line 78
def create
  File.write @path, ''

  cmd = []
  cmd << "#{PRRD.bin} create #{@path}"
  cmd << "--start #{@start} --step #{@step}"

  fail 'Datasources are missing' if @datasources.empty?
  @datasources.map { |e| cmd << e.to_s }

  fail 'Archives are missing' if @archives.empty?
  @archives.map { |e| cmd << e.to_s }

  # Execute
  PRRD.execute cmd.join ' '
end
exists?() click to toggle source

Does database file exist?

# File lib/prrd/database.rb, line 31
def exists?
  File.exist? @path
end
update(timestamp = nil, *values) click to toggle source

Update a database @param timestamp [Integer] @param values [Array] @return [String]

# File lib/prrd/database.rb, line 99
def update(timestamp = nil, *values)
  check_file
  timestamp ||= Time.now.to_i

  # Execute
  PRRD.execute "#{PRRD.bin} update #{@path} #{timestamp}:#{values.join ':'}"
end