-
Notifications
You must be signed in to change notification settings - Fork 14
/
search_index.rb
250 lines (231 loc) · 8.33 KB
/
search_index.rb
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env ruby
# Usage:
# ruby search_index.rb
# So that the data retured are ordered properly some fields are mapped
# into common field names. Otherwise the ordering will tend to segragate
# by record type.
#
require_relative './environment.rb'
require 'algoliasearch'
require 'optparse'
require 'soda/client'
def contributors_to_committee(name, id, election)
contrib = Committee.where(["\"Filer_ID\" = ?", id]).first
&.calculation(:contribution_list)
return nil if contrib.nil?
return contrib.map do |contributor|
{
type: :contributor,
contributor_name: contributor['Tran_NamF'] ?
(contributor['Tran_NamF'] + ' ' + contributor['Tran_NamL']).squish
: contributor['Tran_NamL'],
amount: contributor['Tran_Amt1'],
committee_name: name,
committee_id: id,
election_slug: election.name,
election_location: election.location,
election_date: election.date,
election_title: election.title,
}
end
end
client = Algolia::Client.new(
application_id: ENV['ALGOLIASEARCH_APPLICATION_ID'],
api_key: ENV['ALGOLIASEARCH_API_KEY'],
)
if !client.list_indexes()['items'].include?('election')
puts "Initializing index: "
end
if ENV['ALGOLIASEARCH_SAMPLE_DATA']
puts "contributions"
index = client.init_index('contributions')
else
puts "election"
index = client.init_index('election')
end
oak_client = SODA::Client.new({:domain => "data.oklandca.gov", :app_token => "4FYL4zxMOncsLeANaeDzP455z"})
oak_response = oak_client.get("https://data.oaklandca.gov/resource/f4dq-mk8d").body
charity_data = oak_response.map do |donation|
{
type: :donation,
name: donation.official,
office_title: donation.office,
contributor_name: donation.payor,
location: donation.payor_city,
payee: donation.payee,
amount: donation.amount.to_i,
# merge this field with the election_date so sorting is more consistent
election_date: donation.payment_date[0,10],
description: donation.description,
url: if donation.url.nil?
nil
else
donation.url["url"].to_s
end,
}
end
puts "Indexing #{charity_data.length} behested donations"
candidate_data = Candidate.includes(:election, :office_election).map do |candidate|
{
type: :candidate,
name: candidate['Candidate'],
office_label: candidate.office_election.label,
office_title: candidate.office_election.title,
office_slug: slugify(candidate.office_election.title),
candidate_slug: slugify(candidate['Candidate']),
election_slug: candidate.election.name,
election_location: candidate.election.location,
election_date: candidate.election.date,
election_title: candidate.election.title,
}
end
puts "Indexing #{candidate_data.length} Candidates..."
iec_data = []
iec_contrib = []
contributor_data = []
Candidate.includes(:election, :committee, :office_election).find_each do |candidate|
next if candidate.committee.nil?
list = candidate.committee.calculation(:contribution_list).map do |contributor|
{
type: :contributor,
contributor_name: contributor['Tran_NamF'] ?
(contributor['Tran_NamF'] + ' ' + contributor['Tran_NamL']).squish
: contributor['Tran_NamL'],
amount: contributor['Tran_Amt1'],
name: candidate['Candidate'],
candidate_slug: slugify(candidate['Candidate']),
office_label: candidate.office_election.label,
office_title: candidate.office_election.title,
office_slug: slugify(candidate.office_election.title),
election_slug: candidate.election.name,
election_location: candidate.election.location,
election_date: candidate.election.date,
election_title: candidate.election.title,
}
end
unless list.nil?
contributor_data += list
end
[
[:support_list, "Supporting"],
[:opposition_list, "Opposing"],
].each do |calculation, supporting|
list = candidate.calculation(calculation)
next if list.nil?
list.each do |iec|
iec_data +=
[{
type: :iec,
committee_name: iec['Filer_NamL'],
committee_id: iec['Filer_ID'],
supporting: supporting,
amount: iec['Total'],
name: candidate['Candidate'],
candidate_slug: slugify(candidate['Candidate']),
office_label: candidate.office_election.label,
office_title: candidate.office_election.title,
office_slug: slugify(candidate.office_election.title),
election_slug: candidate.election.name,
election_location: candidate.election.location,
election_date: candidate.election.date,
election_title: candidate.election.title,
}]
contrib = contributors_to_committee(iec['Filer_NamL'], iec['Filer_ID'], candidate.election)
next if contrib.nil?
iec_contrib += contrib
end
end
end
# A committee can support/oppose multiple candidates
# Delete duplicates, not too efficient but keeps the code simple.
iec_contrib.uniq!
#temporary till we straighten out Filer_ID reuse
contributor_data.uniq!
puts "Indexing #{contributor_data.length} Contributors..."
puts "Indexing #{iec_data.length} Independent Committies..."
puts "Indexing #{iec_contrib.length} IC Contributors..."
referendum_data = []
ballot_committees = []
ballot_contrib = []
Referendum.includes(:election).find_each do |referendum|
referendum_data += [{
type: :referendum,
title: referendum['Short_Title'],
measure: "Measure: " + referendum['Measure_number'],
slug: slugify(referendum['Short_Title']),
election_slug: referendum.election.name,
election_location: referendum.election.location,
election_date: referendum.election.date,
election_title: referendum.election.title,
}]
[
[:supporting_organizations, "Supporting"],
[:opposing_organizations, "Opposing"],
].each do |calculation, supporting|
list = referendum.calculation(calculation)
next if list.nil?
list.each do |committee|
ballot_committees += [{
type: :committee,
committee_name: committee['name'],
committee_id: committee['id'],
supporting: supporting,
amount: committee['amount'],
title: referendum['Short_Title'],
measure: "Measure: " + referendum['Measure_number'],
slug: slugify(referendum['Short_Title']),
election_slug: referendum.election.name,
election_location: referendum.election.location,
election_date: referendum.election.date,
election_title: referendum.election.title,
}]
contrib = contributors_to_committee(committee['name'], committee['id'], referendum.election)
next if contrib.nil?
ballot_contrib += contrib
end
end
end
ballot_contrib.uniq!
puts "Indexing #{referendum_data.length} Referendums..."
puts "Indexing #{ballot_committees.length} Ballot Committees..."
puts "Indexing #{ballot_contrib.length} Ballot Contributors..."
committee_data = []
committee_contrib = []
Committee.where(_Committee_Type: 'GPC').find_each do |committee|
next if ballot_committees.any? {|c| c[:committee_id] == committee['Filer_ID'] }
next if iec_data.any? {|c| c[:committee_id] == committee['Filer_ID'] }
election = Election.where(name: committee['Ballot_Measure_Election']).first
committee_data += [{
type: :committee,
committee_name: committee['Filer_NamL'],
committee_id: committee['Filer_ID'],
amount: committee.calculation(:contribution_list_total),
election_slug: election.name,
election_location: election.location,
election_date: election.date,
election_title: election.title,
}]
contrib = contributors_to_committee(committee['Filer_NamL'], committee['Filer_ID'], election)
next if contrib.nil?
committee_contrib += contrib
end
puts "Indexing #{committee_data.length} Active Committees..."
puts "Indexing #{committee_contrib.length} Active Committee Contributors..."
all_data = ballot_contrib + ballot_committees + referendum_data +
contributor_data + candidate_data + iec_data + iec_contrib +
committee_data + committee_contrib + charity_data
puts "total records: #{all_data.length}"
# Test code so we don't burn all of our allocation on Aloglia
if ENV['ALGOLIASEARCH_SAMPLE_DATA']
sampled_data = []
i = 0
all_data.each do |data|
i += 1
next if i.modulo(4) != 0
sampled_data += [data]
end
puts "sampled records: #{sampled_data.length}"
index.replace_all_objects(sampled_data)
else
index.replace_all_objects(all_data)
end