Make GeoNode::Status a proper class rather than a struct

parent 77f15d65
class GeoNode < ActiveRecord::Base
include Presentable
Status = Struct.new(:health, :repositories, :repositories_synced, :repositories_failed)
belongs_to :geo_node_key, dependent: :destroy
belongs_to :oauth_application, class_name: 'Doorkeeper::Application', dependent: :destroy
belongs_to :system_hook, dependent: :destroy
......
class GeoNodeStatus
include ActiveModel::Model
attr_writer :health
def health
@health ||= HealthCheck::Utils.process_checks(['geo'])
end
def healthy?
health.blank?
end
def repositories
@repositories ||= Project.count
end
def repositories=(value)
@repositories = value.to_i
end
def repositories_synced
@repositories_synced ||= Geo::ProjectRegistry.synced.count
end
def repositories_synced=(value)
@repositories_synced = value.to_i
end
def repositories_synced_in_percentage
return 0 if repositories.zero?
(repositories_synced.to_f / repositories.to_f) * 100.0
end
def repositories_failed
@repositories_failed ||= Geo::ProjectRegistry.failed.count
end
def repositories_failed=(value)
@repositories_failed = value.to_i
end
end
class GeoNodePresenter < Gitlab::View::Presenter::Delegated
presents :geo_node
def healthy?
health.blank?
end
def health
status.health
end
def repositories
status.repositories.to_i
end
def repositories_synced
status.repositories_synced.to_i
end
def repositories_synced_in_percentage
return 0 if repositories.zero?
(repositories_synced.to_f / repositories.to_f) * 100.0
end
def repositories_failed
status.repositories_failed.to_i
end
delegate :healthy?, :health, :repositories, :repositories_synced,
:repositories_synced_in_percentage, :repositories_failed,
to: :status
private
......
......@@ -6,23 +6,25 @@ module Geo
default_timeout Gitlab.config.gitlab.geo_status_timeout
def call(status_url)
response = self.class.get(status_url,
headers: {
'Content-Type' => 'application/json',
'PRIVATE-TOKEN' => private_token
})
keys = %w(health repositories repositories_synced repositories_failed)
values =
if response.code >= 200 && response.code < 300
keys = GeoNode::Status.members.map(&:to_s)
response.parsed_response.values_at(*keys)
else
["Could not connect to Geo node - HTTP Status Code: #{response.code}"]
begin
response = self.class.get(status_url,
headers: {
'Content-Type' => 'application/json',
'PRIVATE-TOKEN' => private_token
})
if response.code >= 200 && response.code < 300
response.parsed_response.values_at(*keys)
else
["Could not connect to Geo node - HTTP Status Code: #{response.code}"]
end
rescue HTTParty::Error, Errno::ECONNREFUSED => e
[e.message]
end
GeoNode::Status.new(*values)
rescue HTTParty::Error, Errno::ECONNREFUSED => e
GeoNode::Status.new(e.message)
GeoNodeStatus.new(keys.zip(values).to_h)
end
private
......
......@@ -36,14 +36,7 @@ module API
authenticated_as_admin!
require_node_to_be_secondary!
status = GeoNode::Status.new(
HealthCheck::Utils.process_checks(['geo']),
Project.count,
::Geo::ProjectRegistry.synced.count,
::Geo::ProjectRegistry.failed.count
)
present status, with: Entities::GeoNodeStatus
present GeoNodeStatus.new, with: Entities::GeoNodeStatus
end
# Enqueue a batch of IDs of wiki's projects to have their
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment