Module: Kstats::Node::Database

Defined in:
lib/kstats/node/database.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dbObject (readonly)

Returns the value of attribute db.



7
8
9
# File 'lib/kstats/node/database.rb', line 7

def db
  @db
end

Class Method Details

.execute(query) ⇒ Object



20
21
22
# File 'lib/kstats/node/database.rb', line 20

def execute query
  @db.execute(query)
end

.initObject



9
10
11
12
13
14
15
16
17
18
# File 'lib/kstats/node/database.rb', line 9

def init
  @db = SQLite3::Database.new( Kstats::Node::Config['db_dir'] )
  @db.execute [
    "CREATE TABLE IF NOT EXISTS probe_data (id INTEGER PRIMARY KEY ASC, date DATETIME, period_type STRING, probe_id STRING, probe_key STRING, probe_value NUMBER)",
    "CREATE INDEX IF NOT EXISTS probe_data_period_type ON probe_data(period_type)",
    "CREATE INDEX IF NOT EXISTS probe_data_date ON probe_data(date)",
    "CREATE INDEX IF NOT EXISTS probe_data_probe_id ON probe_data(probe_id)",
    "CREATE INDEX IF NOT EXISTS probe_data_probe_key ON probe_data(probe_key)"
    ].join(";")
end

.save_probes_data(probes, type) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kstats/node/database.rb', line 24

def save_probes_data probes, type
  time = Time.now

  probes.each do |name, values|
    @db.close
    @db = SQLite3::Database.new( Kstats::Node::Config['db_dir'] )

    sql = "INSERT INTO probe_data (date, period_type, probe_id, probe_key, probe_value)\nVALUES (?, ?, ?, ?, ?)\n"
    values.each do |probe_key, probe_value|
      @db.execute(sql, time.to_s, type.to_s, name.to_s, probe_key.to_s, probe_value)
    end
  end
end