/
exporter.rb
356 lines (290 loc) · 10.2 KB
/
exporter.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
require 'csv'
class Spreadsheet::Exporter
SNAPSHOT_DURATION = 15
attr_accessor :snapshot_counter, :event_start_time, :event_end_time
def initialize(snapshot_counter:, event_start_time:, event_end_time:)
self.snapshot_counter = snapshot_counter
self.event_start_time = event_start_time
self.event_end_time = event_end_time
end
def export
csv << headers
insert_rows
close_csv
filename
end
def current_snapshot_start_time
event_start_time + ((snapshot_counter - 1) * SNAPSHOT_DURATION).minutes
end
def current_snapshot_end_time
current_snapshot_start_time + SNAPSHOT_DURATION.minutes
end
def previous_snapshot_start_time
current_snapshot_start_time - SNAPSHOT_DURATION.minutes
end
def previous_snapshot_end_time
current_snapshot_end_time - SNAPSHOT_DURATION.minutes
end
private
def csv
@csv ||= ::CSV.open(filename, 'w')
end
def close_csv
csv.close
@csv = nil
end
def filename
Rails.root.join "tmp", "tweet_export_#{snapshot_counter}.csv"
end
def insert_rows
Person.order(:last).each do |person|
puts "\n\n\n********************\n\n\n"
puts "Inserting rows for #{person.name}"
puts "\n\n\n********************\n\n\n"
csv << Row.new(self, person).build
end
end
def headers
[
'Candidate_Name',
'Candidate_Last_Name',
'hashtage_search',
'Snapshot_Counter',
'Most_Recent_Time_Stamp',
'Time_Elapsed',
'Overall_Tweet_Percentage',
'Overall_Tweet_Mentions',
'Overall_Tweet_Mention_Rank',
'Most_Recent_Tweet_Percentage',
'Most_Recent_Tweet_Mentions',
'Most_Recent_Tweet_Mention_Rank',
'Last_Tweet_Percentage',
'Last_Tweet_Mentions',
'Last_Tweet_Mention_Rank',
'Overall_Sentiment_Score',
'Overall_Sentiment_Score_Rank',
'Most_Recent_Sentiment_Score',
'Most_Recent_Sentiment_Score_Rank',
'Last_Sentiment_Score',
'Last_Sentiment_Score_Rank',
'Most_Recent_Top_Topic',
'Most_Recent_Top_Topic_Percentage',
'Most_Recent_Top_Topic_Tweets',
'Most_Recent_Top_Linked_Candidate',
'Most_Recent_Top_Linked_Candidate_Tweets',
'Most_Recent_Top_Linked_Candidate_Sentiment_Score',
'Most_Recent_Top_Hashtag',
'Most_Recent_Top_Hashtag_Tweet_Percentage',
'Most_Recent_Top_Hashtag_Tweets',
]
end
class Row
attr_accessor :person, :exporter
delegate :event_start_time, :event_end_time, :current_snapshot_start_time,
:current_snapshot_end_time, :previous_snapshot_start_time, :previous_snapshot_end_time,
:snapshot_counter, to: :exporter
def initialize(exporter, person)
self.exporter = exporter
self.person = person
end
def build
[
person.name,
person.last,
EVENT_HASHTAG,
snapshot_counter,
most_recent_time_stamp,
"#{SNAPSHOT_DURATION} minutes",
overall_tweet_percentage,
overall_tweet_mentions,
overall_tweet_mentions_rank,
current_snapshot_tweet_percentage,
current_snapshot_tweet_mentions,
current_snapshot_tweet_mentions_rank,
previous_snapshot_tweet_percentage,
previous_snapshot_tweet_mentions,
previous_snapshot_tweet_mentions_rank,
overall_sentiment_score,
overall_sentiment_score_rank,
current_snapshot_sentiment_score,
current_snapshot_sentiment_score_rank,
previous_snapshot_sentiment_score,
previous_snapshot_sentiment_score_rank,
current_snapshot_top_topic,
current_snapshot_top_topic_percentage,
current_snapshot_top_topic_count,
current_snapshot_top_referenced_person,
current_snapshot_top_referenced_person_count,
current_snapshot_top_referenced_person_sentiment_score,
current_snapshot_top_hashtag,
current_snapshot_top_hashtag_percentage,
current_snapshot_top_hashtag_count,
]
end
private
# Methods referenced in build
def most_recent_time_stamp
current_snapshot_person_tweets.maximum(:created_at)
end
def overall_tweet_percentage
percentage overall_tweet_mentions, overall_tweet_count
end
def overall_tweet_mentions
overall_person_tweets.count
end
def overall_tweet_mentions_rank
tweet_mentions_rank(event_start_time, event_end_time)
end
def current_snapshot_tweet_percentage
percentage current_snapshot_tweet_mentions, current_snapshot_tweet_count
end
def current_snapshot_tweet_mentions
current_snapshot_person_tweets.count
end
def current_snapshot_tweet_mentions_rank
tweet_mentions_rank(current_snapshot_start_time, current_snapshot_end_time)
end
def previous_snapshot_tweet_percentage
percentage previous_snapshot_tweet_mentions, previous_snapshot_tweet_count
end
def previous_snapshot_tweet_mentions
previous_snapshot_person_tweets.count
end
def previous_snapshot_tweet_mentions_rank
tweet_mentions_rank(previous_snapshot_start_time, previous_snapshot_end_time)
end
def overall_sentiment_score
overall_person_tweets.average(:sentiment_score)
end
def overall_sentiment_score_rank
sentiment_score_rank(event_start_time, event_end_time)
end
def current_snapshot_sentiment_score
current_snapshot_person_tweets.average(:sentiment_score)
end
def current_snapshot_sentiment_score_rank
sentiment_score_rank(current_snapshot_start_time, current_snapshot_end_time)
end
def previous_snapshot_sentiment_score
previous_snapshot_person_tweets.average(:sentiment_score)
end
def previous_snapshot_sentiment_score_rank
sentiment_score_rank(previous_snapshot_start_time, previous_snapshot_end_time)
end
def current_snapshot_top_topic
current_snapshot_topic_counts.first.try(:first).try(:name)
end
def current_snapshot_top_topic_percentage
percentage current_snapshot_top_topic_count, current_snapshot_tweet_count
end
def current_snapshot_top_topic_count
current_snapshot_topic_counts.first.try(:last)
end
def current_snapshot_top_referenced_person
current_snapshot_referenced_people_tweets.first.try(:first).try(:last)
end
def current_snapshot_top_referenced_person_count
current_snapshot_top_referenced_person_tweets.try(:size)
end
def current_snapshot_top_referenced_person_sentiment_score
return if current_snapshot_top_referenced_person_tweets.nil?
sum = current_snapshot_top_referenced_person_tweets.map(&:sentiment_score).reduce(&:+).to_f
percentage sum, current_snapshot_top_referenced_person_tweets.size
end
def current_snapshot_top_hashtag
current_snapshot_hashtag_counts.first.try(:first).try(:name)
end
def current_snapshot_top_hashtag_percentage
percentage current_snapshot_top_hashtag_count, current_snapshot_tweet_count
end
def current_snapshot_top_hashtag_count
current_snapshot_hashtag_counts.first.try(:last)
end
# Helpers
def overall_person_tweets
person.tweets.between(event_start_time, event_end_time)
end
def overall_tweets
Tweet.between(event_start_time, event_end_time)
end
def overall_tweet_count
@overall_tweet_count ||= overall_tweets.count
end
def current_snapshot_person_tweets
person.tweets.between(current_snapshot_start_time, current_snapshot_end_time)
end
def current_snapshot_tweets
Tweet.between(current_snapshot_start_time, current_snapshot_end_time)
end
def current_snapshot_tweet_count
@current_snapshot_tweet_count ||= current_snapshot_tweets.count
end
def previous_snapshot_person_tweets
person.tweets.between(previous_snapshot_start_time, previous_snapshot_end_time)
end
def previous_snapshot_tweets
Tweet.between(previous_snapshot_start_time, previous_snapshot_end_time)
end
def previous_snapshot_tweet_count
previous_snapshot_tweets.count
end
def tweet_mentions_rank(start_time, end_time)
ranks = Person.all.map do |p|
[p, p.tweets.between(start_time, end_time).count]
end
ranks.sort_by{ |ary| ary[1] }.index{|ary| ary[0] == person} + 1
end
def sentiment_score_rank(start_time, end_time)
ranks = Person.all.map do |p|
[p, p.tweets.between(start_time, end_time).average(:sentiment_score)]
end
ranks.sort_by{ |ary| ary[1] }.index{|ary| ary[0] == person} + 1
end
def current_snapshot_topic_counts
if @current_snapshot_topic_counts.nil?
@current_snapshot_topic_counts = {}
current_snapshot_person_tweets.each do |tweet|
tweet.topics.each do |topic|
@current_snapshot_topic_counts[topic] ||= 0
@current_snapshot_topic_counts[topic] += 1
end
end
@current_snapshot_topic_counts.sort_by {|topic, count| count}.reverse
end
@current_snapshot_topic_counts
end
def current_snapshot_referenced_people_tweets
if @current_snapshot_referenced_people.nil?
@current_snapshot_referenced_people = {}
current_snapshot_person_tweets.each do |tweet|
tweet.people.excluding(person).each do |p|
@current_snapshot_referenced_people[p] ||= []
@current_snapshot_referenced_people[p] << tweet
end
end
@current_snapshot_referenced_people.sort_by {|topic, tweets| tweets.size}.reverse
end
@current_snapshot_referenced_people
end
def current_snapshot_top_referenced_person_tweets
current_snapshot_referenced_people_tweets.first.try(:last)
end
def current_snapshot_hashtag_counts
if @current_snapshot_hashtag_counts.nil?
@current_snapshot_hashtag_counts = {}
current_snapshot_person_tweets.each do |tweet|
tweet.hashtags.each do |hashtag|
@current_snapshot_hashtag_counts[hashtag] ||= 0
@current_snapshot_hashtag_counts[hashtag] += 1
end
end
@current_snapshot_hashtag_counts.sort_by {|hashtag, count| count}.reverse
end
@current_snapshot_hashtag_counts
end
def percentage(sum, count)
return if count == 0
(sum.to_f / count).round(2)
end
end
end