forked from alphagov/forms-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_event_service.rb
64 lines (54 loc) · 1.69 KB
/
log_event_service.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
class LogEventService
def initialize(current_context, step, request, changing_answer, answers)
@current_context = current_context
@step = step
@request = request
@changing_answer = changing_answer
@answers = answers
end
def self.log_form_start
EventLogger.log_form_event("visit")
end
def self.log_submit(context, requested_email_confirmation:, preview:, submission_type:)
if preview
EventLogger.log_form_event("preview_submission", { submission_type: })
else
# Logging to Splunk
EventLogger.log_form_event("submission", { submission_type: })
EventLogger.log_form_event("requested_email_confirmation") if requested_email_confirmation
# Logging to CloudWatch
begin
CloudWatchService.log_form_submission(form_id: context.form.id)
rescue StandardError => e
Sentry.capture_exception(e)
end
end
end
def log_page_save
EventLogger.log_page_event(log_event, @step.question.question_text, skipped_question?)
if is_starting_form?
begin
CloudWatchService.log_form_start(form_id: @current_context.form.id) # Logging to CloudWatch
rescue StandardError => e
Sentry.capture_exception(e)
end
end
end
private
def log_event
page_optional = @step.question.is_optional? ? "optional" : "page"
if @changing_answer
"change_answer_#{page_optional}_save"
elsif is_starting_form?
"first_#{page_optional}_save"
else
"#{page_optional}_save"
end
end
def skipped_question?
@step.question.is_optional? ? @answers.each_value.map.none?(&:present?) : nil
end
def is_starting_form?
@current_context.form.start_page == @step.id
end
end