Class: DNS::Monitor::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/dns/monitor/database.rb

Constant Summary collapse

TABLE =

This is the general DB structure

'domains'

Instance Method Summary collapse

Constructor Details

#initialize(db_path) ⇒ Database

Returns a new instance of Database.



7
8
9
10
# File 'lib/dns/monitor/database.rb', line 7

def initialize(db_path)
  @db_path = db_path
  initialize_db
end

Instance Method Details

#check(domain, rdap) ⇒ Object

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



13
14
15
16
17
18
19
20
21
22
23
24
# 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

#clearObject

This is mostly for testing



27
28
29
# File 'lib/dns/monitor/database.rb', line 27

def clear
  query { |db| db.execute "DELETE FROM #{TABLE} WHERE 1" }
end

#diff(previous_rdap, rdap) ⇒ Object

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.



35
36
37
38
39
# 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) ⇒ Object

Return all entries for a given domain as a Domain struct



42
43
44
45
# 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) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# 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) ⇒ Object

Just the latest entry plz



61
62
63
# File 'lib/dns/monitor/database.rb', line 61

def most_recent(domain)
  entries(domain).first || Domain.new
end