class Ring::SQA::Database

Public Class Methods

new() click to toggle source
# File lib/ring/sqa/database.rb, line 49
def initialize
  sequel_opts = { max_connections: 1, pool_timout: 60 }
  if CFG.ram_database?
    @db = Sequel.sqlite sequel_opts
  else
    file = '%s.db' % CFG.afi
    file = File.join CFG.directory, file
    File.unlink file rescue nil # delete old database
    @db = Sequel.sqlite file, sequel_opts
  end
  create_db
  require_relative 'database/model.rb'
end

Public Instance Methods

add(record) click to toggle source
# File lib/ring/sqa/database.rb, line 8
def add record
  record[:time]    = Time.now.utc.to_i
  record[:latency] = nil
  record[:result]  = 'no response'
  ping = Ping.new(record).save
  Log.debug "added '#{record}' to database with id #{ping.id}" if CFG.debug?
  return ping
end
id_range(first, last) click to toggle source
# File lib/ring/sqa/database.rb, line 39
def id_range first, last
  Ping.distinct.where(:id=>first..last)
end
nodes_down(first_id) click to toggle source
# File lib/ring/sqa/database.rb, line 26
def nodes_down first_id
  max_id = (Ping.max(:id) or first_id)
  [max_id, id_range(first_id, max_id).exclude(:result => 'ok')]
end
purge(older_than=3600) click to toggle source
# File lib/ring/sqa/database.rb, line 35
def purge older_than=3600
  Ping.where{time < (Time.now.utc-older_than).to_i}.delete
end
table_exists?() click to toggle source
# File lib/ring/sqa/database.rb, line 43
def table_exists?
  @db.table_exists?(:pings)
end
up_since?(id, peer) click to toggle source
# File lib/ring/sqa/database.rb, line 31
def up_since? id, peer
  Ping.where{id > id}.where(:peer=>peer).count > 0
end
update(record_id, result, latency=nil) click to toggle source
# File lib/ring/sqa/database.rb, line 17
def update record_id, result, latency=nil
  if record = Ping[record_id]
    Log.debug "updating record_id '#{record_id}' with result '#{result}' and latency '#{latency}'" if CFG.debug?
    record.update(:result=>result, :latency=>latency)
  else
    Log.error "wanted to update record_id #{record_id}, but it does not exist"
  end
end

Private Instance Methods

create_db() click to toggle source
# File lib/ring/sqa/database.rb, line 63
def create_db
  @db.create_table?(:pings) do
    primary_key :id
    Fixnum      :time
    String      :peer
    Fixnum      :latency
    String      :result
  end
end