Class: PeakFlowUtils::ActiveRecordQuery

Inherits:
Object
  • Object
show all
Defined in:
app/services/peak_flow_utils/active_record_query.rb

Defined Under Namespace

Classes: SlowSQLError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event) ⇒ ActiveRecordQuery

Returns a new instance of ActiveRecordQuery.



6
7
8
# File 'app/services/peak_flow_utils/active_record_query.rb', line 6

def initialize(event)
  @event = event
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



4
5
6
# File 'app/services/peak_flow_utils/active_record_query.rb', line 4

def event
  @event
end

Instance Method Details

#duration_secondsObject



14
15
16
# File 'app/services/peak_flow_utils/active_record_query.rb', line 14

def duration_seconds
  @duration_seconds ||= event.duration / 1000
end

#performObject



10
11
12
# File 'app/services/peak_flow_utils/active_record_query.rb', line 10

def perform
  report_slow_sql if duration_seconds >= 3
end

#report_slow_sqlObject



18
19
20
21
22
23
24
# File 'app/services/peak_flow_utils/active_record_query.rb', line 18

def report_slow_sql
  PeakFlowUtils::Notifier.with_parameters(sql: sql, duration_seconds: duration_seconds) do
    raise SlowSQLError, "Slow SQL: #{sql_as_single_line}"
  rescue StandardError => e
    PeakFlowUtils::Notifier.notify(error: e)
  end
end

#sqlObject



26
27
28
# File 'app/services/peak_flow_utils/active_record_query.rb', line 26

def sql
  @sql ||= event.payload.fetch(:sql)
end

#sql_as_single_lineObject



30
31
32
# File 'app/services/peak_flow_utils/active_record_query.rb', line 30

def sql_as_single_line
  @sql_as_single_line ||= sql.tr("\n", " ")
end