Class: PRRD::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/prrd/database.rb,
lib/prrd/database/archive.rb,
lib/prrd/database/datasource.rb

Overview

PRRD Database class

Defined Under Namespace

Classes: Archive, Datasource

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = nil) ⇒ Database

Constructor



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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

Instance Attribute Details

#archivesObject

Returns the value of attribute archives.



12
13
14
# File 'lib/prrd/database.rb', line 12

def archives
  @archives
end

#datasourcesObject

Returns the value of attribute datasources.



12
13
14
# File 'lib/prrd/database.rb', line 12

def datasources
  @datasources
end

#pathObject

Accessors



10
11
12
# File 'lib/prrd/database.rb', line 10

def path
  @path
end

#startObject

Returns the value of attribute start.



11
12
13
# File 'lib/prrd/database.rb', line 11

def start
  @start
end

#stepObject

Returns the value of attribute step.



11
12
13
# File 'lib/prrd/database.rb', line 11

def step
  @step
end

Instance Method Details

#<<(object) ⇒ Object

Add an object

Parameters:

  • object (Object)


66
67
68
69
70
71
72
73
74
# 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) ⇒ Object

Add an archive object

Parameters:



54
55
56
# File 'lib/prrd/database.rb', line 54

def add_archive(archive)
  @archives << archive
end

#add_archives(archives) ⇒ Object

Add archive objects

Parameters:

  • archives (Array)


60
61
62
# File 'lib/prrd/database.rb', line 60

def add_archives(archives)
  @archives = archives
end

#add_datasource(datasource) ⇒ Object

Add a datasource object

Parameters:



42
43
44
# File 'lib/prrd/database.rb', line 42

def add_datasource(datasource)
  @datasources << datasource
end

#add_datasources(datasources) ⇒ Object

Add datasource objects

Parameters:

  • datasource (Array)


48
49
50
# File 'lib/prrd/database.rb', line 48

def add_datasources(datasources)
  @datasources = datasources
end

#check_fileObject

Check database existence



36
37
38
# File 'lib/prrd/database.rb', line 36

def check_file
  fail 'Database path is missing' if  @path.nil? || !exists?
end

#createString

Create a database

Returns:

  • (String)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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?Boolean

Does database file exist?

Returns:

  • (Boolean)


31
32
33
# File 'lib/prrd/database.rb', line 31

def exists?
  File.exist? @path
end

#update(timestamp = nil, *values) ⇒ String

Update a database

Parameters:

  • timestamp (Integer) (defaults to: nil)
  • values (Array)

Returns:

  • (String)


99
100
101
102
103
104
105
# 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