2021-04-15 08:09:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Clusters
|
|
|
|
module Integrations
|
|
|
|
class Prometheus < ApplicationRecord
|
|
|
|
include ::Clusters::Concerns::PrometheusClient
|
2021-05-09 20:10:37 -04:00
|
|
|
include AfterCommitQueue
|
2021-04-15 08:09:05 -04:00
|
|
|
|
|
|
|
self.table_name = 'clusters_integration_prometheus'
|
|
|
|
self.primary_key = :cluster_id
|
|
|
|
|
|
|
|
belongs_to :cluster, class_name: 'Clusters::Cluster', foreign_key: :cluster_id
|
|
|
|
|
|
|
|
validates :cluster, presence: true
|
|
|
|
validates :enabled, inclusion: { in: [true, false] }
|
|
|
|
|
2021-10-19 02:09:36 -04:00
|
|
|
# Periodically checked and kept up to date for Monitor demo projects
|
|
|
|
enum health_status: {
|
|
|
|
unknown: 0,
|
|
|
|
healthy: 1,
|
|
|
|
unhealthy: 2
|
|
|
|
}
|
|
|
|
|
2021-05-09 20:10:37 -04:00
|
|
|
attr_encrypted :alert_manager_token,
|
|
|
|
mode: :per_attribute_iv,
|
|
|
|
key: Settings.attr_encrypted_db_key_base_32,
|
|
|
|
algorithm: 'aes-256-gcm'
|
|
|
|
|
|
|
|
default_value_for(:alert_manager_token) { SecureRandom.hex }
|
|
|
|
|
2021-10-06 11:11:48 -04:00
|
|
|
scope :enabled, -> { where(enabled: true) }
|
|
|
|
|
2021-05-09 20:10:37 -04:00
|
|
|
after_destroy do
|
|
|
|
run_after_commit do
|
2021-06-18 17:10:06 -04:00
|
|
|
deactivate_project_integrations
|
2021-05-09 20:10:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after_save do
|
|
|
|
next unless enabled_before_last_save != enabled
|
|
|
|
|
|
|
|
run_after_commit do
|
|
|
|
if enabled
|
2021-06-18 17:10:06 -04:00
|
|
|
activate_project_integrations
|
2021-05-09 20:10:37 -04:00
|
|
|
else
|
2021-06-18 17:10:06 -04:00
|
|
|
deactivate_project_integrations
|
2021-05-09 20:10:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-15 08:09:05 -04:00
|
|
|
def available?
|
|
|
|
enabled?
|
|
|
|
end
|
2021-05-09 20:10:37 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-06-18 17:10:06 -04:00
|
|
|
def activate_project_integrations
|
2021-05-09 20:10:37 -04:00
|
|
|
::Clusters::Applications::ActivateServiceWorker
|
2021-06-18 11:10:16 -04:00
|
|
|
.perform_async(cluster_id, ::Integrations::Prometheus.to_param)
|
2021-05-09 20:10:37 -04:00
|
|
|
end
|
|
|
|
|
2021-06-18 17:10:06 -04:00
|
|
|
def deactivate_project_integrations
|
2021-05-09 20:10:37 -04:00
|
|
|
::Clusters::Applications::DeactivateServiceWorker
|
2021-06-18 11:10:16 -04:00
|
|
|
.perform_async(cluster_id, ::Integrations::Prometheus.to_param)
|
2021-05-09 20:10:37 -04:00
|
|
|
end
|
2021-04-15 08:09:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|