Class: PgReports::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_reports/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



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
# File 'lib/pg_reports/configuration.rb', line 45

def initialize
  # Telegram
  @telegram_bot_token = ENV.fetch("PG_REPORTS_TELEGRAM_TOKEN", nil)
  @telegram_chat_id = ENV.fetch("PG_REPORTS_TELEGRAM_CHAT_ID", nil)

  # Query thresholds
  @slow_query_threshold_ms = 100
  @heavy_query_threshold_calls = 1000
  @expensive_query_threshold_ms = 10_000

  # Index thresholds
  @unused_index_threshold_scans = 50
  @inefficient_index_threshold_ratio = 10

  # Table thresholds
  @bloat_threshold_percent = 20
  @dead_rows_threshold = 10_000

  # Connection
  @connection_pool = nil

  # Output
  @max_query_length = 200

  # Dashboard
  @dashboard_auth = nil

  # Assets / privacy
  @load_external_fonts = ActiveModel::Type::Boolean.new.cast(ENV.fetch("PG_REPORTS_LOAD_EXTERNAL_FONTS", false))

  # Development/testing
  @fake_source_data = ENV.fetch("PG_REPORTS_FAKE_SOURCE_DATA", "false") == "true"

  # Query monitoring
  @query_monitor_log_file = defined?(Rails) ? Rails.root.join("log", "pg_reports.log") : nil
  @query_monitor_max_queries = 100
  @query_monitor_backtrace_filter = ->(location) {
    # Exclude gem paths, framework paths
    !location.path.match?(%r{/(gems|ruby|railties)/})
  }

  # Security
  @allow_raw_query_execution = ActiveModel::Type::Boolean.new.cast(
    ENV.fetch("PG_REPORTS_ALLOW_RAW_QUERY_EXECUTION", false)
  )
end

Instance Attribute Details

#allow_raw_query_executionObject

Security settings



43
44
45
# File 'lib/pg_reports/configuration.rb', line 43

def allow_raw_query_execution
  @allow_raw_query_execution
end

#bloat_threshold_percentObject

Table analysis thresholds



19
20
21
# File 'lib/pg_reports/configuration.rb', line 19

def bloat_threshold_percent
  @bloat_threshold_percent
end

#connection_poolObject

Connection settings



23
24
25
# File 'lib/pg_reports/configuration.rb', line 23

def connection_pool
  @connection_pool
end

#dashboard_authObject

Dashboard settings



29
30
31
# File 'lib/pg_reports/configuration.rb', line 29

def dashboard_auth
  @dashboard_auth
end

#dead_rows_thresholdObject

Tables with more dead rows need vacuum



20
21
22
# File 'lib/pg_reports/configuration.rb', line 20

def dead_rows_threshold
  @dead_rows_threshold
end

#expensive_query_threshold_msObject

Total time threshold for expensive queries



12
13
14
# File 'lib/pg_reports/configuration.rb', line 12

def expensive_query_threshold_ms
  @expensive_query_threshold_ms
end

#fake_source_dataObject

Development/testing settings



35
36
37
# File 'lib/pg_reports/configuration.rb', line 35

def fake_source_data
  @fake_source_data
end

#heavy_query_threshold_callsObject

Queries with more calls than this are heavy



11
12
13
# File 'lib/pg_reports/configuration.rb', line 11

def heavy_query_threshold_calls
  @heavy_query_threshold_calls
end

#inefficient_index_threshold_ratioObject

Read/fetch ratio above this is inefficient



16
17
18
# File 'lib/pg_reports/configuration.rb', line 16

def inefficient_index_threshold_ratio
  @inefficient_index_threshold_ratio
end

#load_external_fontsObject

Assets / privacy settings



32
33
34
# File 'lib/pg_reports/configuration.rb', line 32

def load_external_fonts
  @load_external_fonts
end

#max_query_lengthObject

Output settings



26
27
28
# File 'lib/pg_reports/configuration.rb', line 26

def max_query_length
  @max_query_length
end

#query_monitor_backtrace_filterObject

Proc to filter backtrace lines



40
41
42
# File 'lib/pg_reports/configuration.rb', line 40

def query_monitor_backtrace_filter
  @query_monitor_backtrace_filter
end

#query_monitor_log_fileObject

Query monitoring settings



38
39
40
# File 'lib/pg_reports/configuration.rb', line 38

def query_monitor_log_file
  @query_monitor_log_file
end

#query_monitor_max_queriesObject

Maximum number of queries to keep in buffer



39
40
41
# File 'lib/pg_reports/configuration.rb', line 39

def query_monitor_max_queries
  @query_monitor_max_queries
end

#slow_query_threshold_msObject

Query analysis thresholds



10
11
12
# File 'lib/pg_reports/configuration.rb', line 10

def slow_query_threshold_ms
  @slow_query_threshold_ms
end

#telegram_bot_tokenObject

Telegram settings



6
7
8
# File 'lib/pg_reports/configuration.rb', line 6

def telegram_bot_token
  @telegram_bot_token
end

#telegram_chat_idObject

Returns the value of attribute telegram_chat_id.



7
8
9
# File 'lib/pg_reports/configuration.rb', line 7

def telegram_chat_id
  @telegram_chat_id
end

#unused_index_threshold_scansObject

Index analysis thresholds



15
16
17
# File 'lib/pg_reports/configuration.rb', line 15

def unused_index_threshold_scans
  @unused_index_threshold_scans
end

Instance Method Details

#connectionObject



92
93
94
# File 'lib/pg_reports/configuration.rb', line 92

def connection
  @connection_pool || ActiveRecord::Base.connection
end

#telegram_configured?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/pg_reports/configuration.rb', line 96

def telegram_configured?
  telegram_bot_token.present? && telegram_chat_id.present?
end