class DNS::Monitor::Database

Constants

TABLE

This is the general DB structure

Public Class Methods

new(db_path) click to toggle source
# File lib/dns/monitor/database.rb, line 7
def initialize(db_path)
  @db_path = db_path
  initialize_db
end

Public Instance Methods

check(domain, rdap) click to toggle source

Returns a Check object, or nothing if the params were empty

# File lib/dns/monitor/database.rb, line 13
def check(domain, rdap)
  return if domain.nil? || rdap.nil?

  changes = diff((most_recent(domain).rdap || '{}'), rdap)

  if changes.empty?
    Check.new domain, :ok
  else
    insert(domain, rdap)
    Check.new domain, :changed, changes
  end
end
clear() click to toggle source

This is mostly for testing

# File lib/dns/monitor/database.rb, line 27
def clear
  query { |db| db.execute "DELETE FROM #{TABLE} WHERE 1" }
end
diff(previous_rdap, rdap) click to toggle source

Compare two different RDAP values NOTE: We have to do a JSON conversion to compare instead of String, because the values come back from the server in arbitrary JSON key order.

# File lib/dns/monitor/database.rb, line 35
def diff(previous_rdap, rdap)
  # easy_diff returns [added, removed] hashes, we want "removed"
  changes = JSON.parse(previous_rdap).easy_diff(JSON.parse(rdap)).last
  filter_noisy_keys(changes)
end
entries(domain) click to toggle source

Return all entries for a given domain as a Domain struct

# File lib/dns/monitor/database.rb, line 42
def entries(domain)
  sql = "SELECT * FROM #{TABLE} WHERE domain=? ORDER BY created_at DESC"
  query {|db| db.execute(sql, [domain]) }.map{ |row| Domain.new(*row) }
end
filter_noisy_keys(changes) click to toggle source
# File lib/dns/monitor/database.rb, line 47
def filter_noisy_keys(changes)
  # We get a lot of "last update of RDAP" events which aren't something
  # we need notifications about. Remove those.
  # WARNING: mutation follows
  if changes.fetch('events', false)
    changes['events'] = changes['events'].reject do |event|
      event.fetch('eventAction', '').match?(/last update of RDAP/i)
    end
    changes.delete('events') if changes['events'].empty?
  end
  changes
end
most_recent(domain) click to toggle source

Just the latest entry plz

# File lib/dns/monitor/database.rb, line 61
def most_recent(domain)
  entries(domain).first || Domain.new
end

Private Instance Methods

initialize_db() click to toggle source
# File lib/dns/monitor/database.rb, line 67
        def initialize_db
          query do |db| db.execute <<~SQL
            CREATE TABLE IF NOT EXISTS #{TABLE} (
              domain VARCHAR(255),
              rdap TEXT,
              created_at TIMESTAMP DEFAULT (DATETIME('now','localtime'))
            );
          SQL
          end
        end
insert(domain, rdap) click to toggle source
# File lib/dns/monitor/database.rb, line 78
def insert(domain, rdap)
  query do |db|
    db.execute "INSERT INTO #{TABLE} (domain, rdap) VALUES (?,?)", [ domain, rdap ]
  end
end
query() { |db| ... } click to toggle source
# File lib/dns/monitor/database.rb, line 84
def query
  db = SQLite3::Database.new @db_path
  result = yield db
  db.close if db
  result
end