-
Notifications
You must be signed in to change notification settings - Fork 481
/
contact_rollups_v2.rb
83 lines (67 loc) · 2.67 KB
/
contact_rollups_v2.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
require 'cdo/log_collector'
class ContactRollupsV2
def initialize
@log_collector = LogCollector.new('Contact Rollups')
end
def collect_and_process_contacts
number_of_extractions = 2
successful_extraction_count = 0
@log_collector.time_and_raise!('Deletes intermediate content from previous runs') do
truncate_or_delete_table ContactRollupsRaw
truncate_or_delete_table ContactRollupsProcessed
end
@log_collector.time_and_continue('Extracts data from dashboard email_preferences') do
ContactRollupsRaw.extract_email_preferences
successful_extraction_count += 1
end
@log_collector.time_and_continue('Extracts parent emails from dashboard.users') do
ContactRollupsRaw.extract_parent_emails
successful_extraction_count += 1
end
return unless successful_extraction_count
@log_collector.time_and_raise!('Processes all extracted data') do
ContactRollupsProcessed.import_from_raw_table
end
# Update the final table only if all the steps above have passed to avoid saving incomplete data.
if successful_extraction_count != number_of_extractions
@log_collector.info("Skips overwriting contact_rollups_final_table")
return
end
@log_collector.time_and_raise!("Overwrites contact_rollups_final table") do
truncate_or_delete_table ContactRollupsFinal
ContactRollupsFinal.insert_from_processed_table
end
rescue StandardError => e
@log_collector.record_exception(e)
end
def sync_contacts_with_pardot
@log_collector.time!('Downloads new email-Pardot ID mappings') do
ContactRollupsPardotMemory.download_pardot_ids
end
@log_collector.time!('Creates new Pardot prospects') do
# TODO: prevent uploading data to Pardot
ContactRollupsPardotMemory.create_new_pardot_prospects
end
@log_collector.time!('Updates existing Pardot prospects') do
ContactRollupsPardotMemory.update_pardot_prospects
end
@log_collector.time!('Downloads new email-Pardot ID mappings (again)') do
ContactRollupsPardotMemory.download_pardot_ids
end
end
def report_results
# TODO: Add reporting to log file, slack channel and AWS CloudWatch.
puts @log_collector
end
private
# Using truncate allows us to re-use row IDs,
# which is important in production so we don't overflow the table.
# Deletion is required in test environments, as tests generally do
# not allow you to execute TRUNCATE statements.
def truncate_or_delete_table(model)
CDO.rack_env == :production ? truncate_table(model) : model.delete_all
end
def truncate_table(model)
ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{model.table_name}")
end
end