Class: PgInsights::HealthCheckService

Inherits:
Object
  • Object
show all
Defined in:
app/services/pg_insights/health_check_service.rb

Class Method Summary collapse

Class Method Details

.check_missing_indexes(limit: 10) ⇒ Object



11
12
13
# File 'app/services/pg_insights/health_check_service.rb', line 11

def self.check_missing_indexes(limit: 10)
  get_cached_result("missing_indexes") || execute_missing_indexes_query(limit)
end

.check_parameter_settingsObject



27
28
29
# File 'app/services/pg_insights/health_check_service.rb', line 27

def self.check_parameter_settings
  get_cached_result("parameter_settings") || execute_parameter_settings_query
end

.check_sequential_scans(limit: 10) ⇒ Object



15
16
17
# File 'app/services/pg_insights/health_check_service.rb', line 15

def self.check_sequential_scans(limit: 10)
  get_cached_result("sequential_scans") || execute_sequential_scans_query(limit)
end

.check_slow_queries(limit: 10) ⇒ Object



19
20
21
# File 'app/services/pg_insights/health_check_service.rb', line 19

def self.check_slow_queries(limit: 10)
  get_cached_result("slow_queries") || execute_slow_queries_query(limit)
end

.check_table_bloat(limit: 10) ⇒ Object



23
24
25
# File 'app/services/pg_insights/health_check_service.rb', line 23

def self.check_table_bloat(limit: 10)
  get_cached_result("table_bloat") || execute_table_bloat_query(limit)
end

.check_unused_indexes(limit: 10) ⇒ Object



7
8
9
# File 'app/services/pg_insights/health_check_service.rb', line 7

def self.check_unused_indexes(limit: 10)
  get_cached_result("unused_indexes") || execute_unused_indexes_query(limit)
end

.collect_database_snapshotObject



31
32
33
34
35
# File 'app/services/pg_insights/health_check_service.rb', line 31

def self.collect_database_snapshot
  return unless PgInsights.snapshots_available?

  get_cached_result("database_snapshot") || execute_database_snapshot_query
end

.execute_all_checks_synchronouslyObject



67
68
69
70
71
# File 'app/services/pg_insights/health_check_service.rb', line 67

def self.execute_all_checks_synchronously
  HealthCheckResult::VALID_CHECK_TYPES.each do |check_type|
    execute_and_cache_check(check_type)
  end
end

.execute_and_cache_check(check_type, limit = 10) ⇒ Object



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
# File 'app/services/pg_insights/health_check_service.rb', line 73

def self.execute_and_cache_check(check_type, limit = 10)
  return unless HealthCheckResult::VALID_CHECK_TYPES.include?(check_type)

  result = HealthCheckResult.create!(
    check_type: check_type,
    status: "running",
    executed_at: Time.current
  )

  start_time = Time.current

  begin
    data = execute_health_check_query(check_type, limit)
    execution_time = ((Time.current - start_time) * 1000).to_i

    result.update!(
      status: "success",
      result_data: data,
      execution_time_ms: execution_time
    )

    data
  rescue => e
    execution_time = ((Time.current - start_time) * 1000).to_i

    result.update!(
      status: "error",
      error_message: e.message,
      execution_time_ms: execution_time
    )

    Rails.logger.error "PgInsights: Health check failed for #{check_type}: #{e.message}"
    { error: e.message }
  end
end

.execute_database_snapshot_queryObject



133
134
135
# File 'app/services/pg_insights/health_check_service.rb', line 133

def self.execute_database_snapshot_query
  execute_health_check_query("database_snapshot")
end

.execute_health_check_query(check_type, limit = 10) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/services/pg_insights/health_check_service.rb', line 137

def self.execute_health_check_query(check_type, limit = 10)
  case check_type
  when "unused_indexes"
    execute_unused_indexes_sql(limit)
  when "missing_indexes"
    execute_missing_indexes_sql(limit)
  when "sequential_scans"
    execute_sequential_scans_sql(limit)
  when "slow_queries"
    execute_slow_queries_sql(limit)
  when "table_bloat"
    execute_table_bloat_sql(limit)
  when "parameter_settings"
    execute_parameter_settings_sql
  when "database_snapshot"
    execute_database_snapshot_sql
  else
    raise ArgumentError, "Unknown check type: #{check_type}"
  end
end

.execute_missing_indexes_query(limit) ⇒ Object



113
114
115
# File 'app/services/pg_insights/health_check_service.rb', line 113

def self.execute_missing_indexes_query(limit)
  execute_health_check_query("missing_indexes", limit)
end

.execute_parameter_settings_queryObject



129
130
131
# File 'app/services/pg_insights/health_check_service.rb', line 129

def self.execute_parameter_settings_query
  execute_health_check_query("parameter_settings")
end

.execute_sequential_scans_query(limit) ⇒ Object



117
118
119
# File 'app/services/pg_insights/health_check_service.rb', line 117

def self.execute_sequential_scans_query(limit)
  execute_health_check_query("sequential_scans", limit)
end

.execute_slow_queries_query(limit) ⇒ Object



121
122
123
# File 'app/services/pg_insights/health_check_service.rb', line 121

def self.execute_slow_queries_query(limit)
  execute_health_check_query("slow_queries", limit)
end

.execute_table_bloat_query(limit) ⇒ Object



125
126
127
# File 'app/services/pg_insights/health_check_service.rb', line 125

def self.execute_table_bloat_query(limit)
  execute_health_check_query("table_bloat", limit)
end

.execute_unused_indexes_query(limit) ⇒ Object



109
110
111
# File 'app/services/pg_insights/health_check_service.rb', line 109

def self.execute_unused_indexes_query(limit)
  execute_health_check_query("unused_indexes", limit)
end

.refresh_all!(force_synchronous: false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/services/pg_insights/health_check_service.rb', line 37

def self.refresh_all!(force_synchronous: false)
  if force_synchronous || !PgInsights.background_jobs_available?
    execute_all_checks_synchronously
  else
    PgInsights.execute_with_fallback(
      HealthCheckSchedulerJob,
      :perform_later
    ) do
      Rails.logger.info "PgInsights: Falling back to synchronous health check execution"
      execute_all_checks_synchronously
    end
  end
end

.refresh_check!(check_type, limit: 10, force_synchronous: false) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/services/pg_insights/health_check_service.rb', line 51

def self.refresh_check!(check_type, limit: 10, force_synchronous: false)
  if force_synchronous || !PgInsights.background_jobs_available?
    execute_and_cache_check(check_type, limit)
  else
    PgInsights.execute_with_fallback(
      HealthCheckJob,
      :perform_later,
      check_type,
      { "limit" => limit }
    ) do
      Rails.logger.info "PgInsights: Falling back to synchronous execution for #{check_type}"
      execute_and_cache_check(check_type, limit)
    end
  end
end