-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_unasigned.rb
More file actions
127 lines (101 loc) · 4.83 KB
/
fix_unasigned.rb
File metadata and controls
127 lines (101 loc) · 4.83 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/ruby -w
require 'rubygems'
require 'mysql2'
require 'rest-client'
require 'json'
require 'colorize'
require 'elasticsearch'
def get_source_data(h,cdbusr,cdbpwd,cdb)
log = File.new('log/fix_unassigned.log', 'w')
unassigned_npids = []
assigned_npids = []
tested_assigned = []
unassigned_not_found = []
puts "Create view for querying couchdb"
system("curl -X PUT http://#{cdbusr}:#{cdbpwd}@#{h}:5984/#{cdb}/_design/identifiers --data-binary @identifiers.json")
puts 'Loading couchdb data ....'
begin
doc = RestClient.get("http://#{h}:5984/#{cdb}/_design/identifiers/_view/get_all_identifiers")
rescue RestClient::ExceptionWithResponse
end
puts 'Parsing couchdb data ...'
d = JSON.parse(doc)
puts 'Filtering couchdb data ...'
puts 'Filtering all Primary NPIDs'
primary_npids = d['rows'].map {|y|y['value']['id']}
primary_npids = primary_npids.select{|r|r.size == 6}
puts 'Filtering all legacy NPIDs'
legacy_npids = d['rows'].map {|s|s['value']['identifiers']}
legacy_npids = legacy_npids.flatten
legacy_npids = legacy_npids.select{|z|z.size == 6}
all_npids = primary_npids
puts 'Check for unassigned primary_npids with person data'
client = Elasticsearch::Client.new url: "http://#{h}:9200"
#puts 'Checking in couchdb'
#couchdb_npid = RestClient.get("http://#{h}:5984/#{cdb.gsub('_person','')}/_all_docs?include_docs=true&limit=2")
#couchdb_npid = JSON.parse(couchdb_npid)
#raise couchdb_npid.inspect
all_npids.uniq! #Convert to uniq
all_npids.each do |npid|
puts " Checking #{npid}"
tested_assigned << npid
es_client = client.search index:'dde',type:'npids', body:{query:{match:{ national_id: npid}}}
if es_client['hits']['total'] == 0 #If NPID not found in NPID DB
#No record found in database
unassigned_not_found << npid
else
npid_decimal_value = es_client['hits']['hits'][0]['_id']
#puts 'Checked if it is marked as assigned'
if es_client['hits']['hits'][0]['_source'].has_key?('assigned')
if es_client['hits']['hits'][0]['_source']['assigned'] == true
#puts 'OK!'
assigned_npids << "NPID: #{npid} : Decimal value: #{npid_decimal_value}"
else
#puts 'Something is wrong'
unassigned_npids << npid_decimal_value
end
else
#puts 'Something is wrong'
unassigned_npids << npid_decimal_value
end
printf("\rPercentage complete: %.1f record %.d of %.d",(tested_assigned.length/all_npids.length.to_f*100.0),tested_assigned.length,all_npids.length)
end
end
unassigned_npids.uniq!
puts 'Fixing affected IDs'
count = []
puts unassigned_npids.inspect
unassigned_npids.each do |fix_npid|
count << fix_npid
puts "#{count.length} of #{unassigned_npids.length} : #{(count.length/unassigned_npids.length.to_f) * 100}%"
npid_to_fix = RestClient.get("http://#{h}:5984/#{cdb.gsub('_person','')}/#{fix_npid}")
url = "http://#{h}:5984/#{cdb.gsub('_person','')}/#{fix_npid}"
npid_to_fix = JSON.parse(npid_to_fix)
npid_to_fix['assigned'] = true
npid_to_fix['updated_at'] = Time.now.strftime("%Y-%m-%d %H:%M:%S")
params = npid_to_fix.to_json
RestClient.put url,params,:content_type => 'application/json'
end
puts "Writing results to log"
log.syswrite("Summary: \n\n
NPIDs with Demographics but with no NPID record in NPID database: #{unassigned_not_found.length} \n\n
NPIDs that has Demographics but NPID is not flagged as assigned: #{unassigned_npids.length} \n\n
NPIDS that are okey: #{assigned_npids.length} \n\n
NPIDS tested: #{tested_assigned.length} \n\n
NPIDs with Demographics but with no NPID record in NPID database: \n\n #{unassigned_not_found} \n\n\n\n\n
NPIDs that has Demographics but NPID is not flagged as assigned: #{unassigned_npids} \n\n\n\n\n
NPIDS that are okey: #{assigned_npids} \n\n\n\n\n
NPIDS tested: #{tested_assigned}")
puts "Assigned NPIDs: #{assigned_npids.length} : Unassigned #{unassigned_npids.length} : Unassigned not found: #{unassigned_not_found.length} : NPIDs tested: #{tested_assigned.length}"
end
#Start program
#Get parameters from terminal
h = ARGV[0]
cdbusr = ARGV[1]
cdbpwd = ARGV[2]
cdb = ARGV[3]
if h.nil? || cdb.nil? || cdbusr.nil? || cdbpwd.nil? then
puts 'Please execute command as "ruby unassigned_npid.rb.rb host_ip_address couchdb_username couchdb_password couchdb_person_database_name" '.colorize(:red)
exit
end
get_source_data(h,cdbusr,cdbpwd,cdb)