summaryrefslogtreecommitdiff
path: root/contrib/auto-naming/update-named-status.rb
blob: 8ff60ca6854d2b0c4a5e1e8d5e723c3256b68a9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/ruby

require "yaml"

require 'db'
require 'db-config'

def do_update verbose
	now = "TIMESTAMP '" + $db.query_row("SELECT max(last_seen) AS max FROM router_claims_nickname")['max'].to_s + "'"

	denamed = $db.do("
			UPDATE router_claims_nickname
			SET named=false
			WHERE named
			  AND last_seen < #{now} - INTERVAL '6 months'")
	puts "de-named: #{denamed}" if verbose

	named = $db.do("
			UPDATE router_claims_nickname
			SET named=true
			WHERE NOT named
			  AND first_seen < #{now} - INTERVAL '2 weeks'
			  AND last_seen  > #{now} - INTERVAL '2 days'
			  AND NOT EXISTS (SELECT *
			       FROM router_claims_nickname AS innertable
			       WHERE named
			         AND router_claims_nickname.nickname_id=innertable.nickname_id) "+ # if that nickname is already named, we lose.
			" AND NOT EXISTS (SELECT *
			       FROM router_claims_nickname AS innertable
			       WHERE router_claims_nickname.nickname_id=innertable.nickname_id
			         AND router_claims_nickname.router_id <> innertable.router_id
			         AND last_seen > #{now} - INTERVAL '1 month') ") # if nobody else wanted that nickname in the last month we are set
	puts "named: #{named}" if verbose
end

if __FILE__ == $0
	$db = Db.new($CONFIG['database']['dbname'], $CONFIG['database']['user'], $CONFIG['database']['password'])
	verbose = ARGV.first == "-v"

	$db.transaction_begin
	do_update verbose
	$db.transaction_commit
end