Skip to content

Commit

Permalink
Merge pull request #6 from AndyObtiva/development
Browse files Browse the repository at this point in the history
Fix i18n issues + Refactorings
  • Loading branch information
mperham authored Nov 28, 2023
2 parents dd000db + 74b984f commit 1d3813b
Show file tree
Hide file tree
Showing 18 changed files with 169 additions and 130 deletions.
1 change: 1 addition & 0 deletions bin/kuiq
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
$LOAD_PATH.unshift File.expand_path(File.join(__dir__, '..', 'lib'))

require "kuiq/sidekiq_ui"

Kuiq::SidekiqUI.launch
2 changes: 1 addition & 1 deletion lib/kuiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

module Kuiq
WINDOW_WIDTH = 900
WINDOW_HEIGHT = 450
WINDOW_HEIGHT = 500
end
50 changes: 0 additions & 50 deletions lib/kuiq/control.rb

This file was deleted.

28 changes: 28 additions & 0 deletions lib/kuiq/ext/kernel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Any methods defined here are available everywhere (both Models & Views) to provide extra convenience
# for handling cross-cutting concerns like logging and i18n.
#
# That said, defining Kernel methods must be done sparingly as it can be tough for codebase
# newcomers to figure out that global methods live here, and it would be better for maintainability
# in general to call methods on objects instead of inheriting global utility methods.
module Kernel
# We will return Sidekiq.logger instead of Glimmer::Config.logger
# because in general, GUI error logging is at the error level
# and Sidekiq logging is at the info level, and we do not want
# to see GUI info logging as it can be very verbose.
#
# In the future, if there is a need to unite the two loggers, we could
# set `Glimmer::Config.logger = Sidekiq.logger` right after loading
# both the sidekiq and glimmer-dsl-libui gems.
def logger
Sidekiq.logger
end

def t(msg, options = {})
Kuiq::I18n.t(msg, options)
end

# Inverse-translates (e.g. for "Tableau de Bord" in fr, we get "Dashboard")
def it(msg, options = {})
Kuiq::I18n.it(msg, options)
end
end
62 changes: 62 additions & 0 deletions lib/kuiq/i18n.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# frozen_string_literal: true

module Kuiq
class I18n
LOCALES = "./locales"

class << self
# Use Sidekiq's i18n with locale files in sidekiq/web/locales
# Note task in Rakefile to refresh locale files.
def current_locale
@locale ||= begin
x = (ENV["LANGUAGE"] || ENV["LANG"] || "en").downcase.tr("_", "-")
loop do
break "en" if x.size < 2
break x if File.exist?("#{LOCALES}/#{x}.yml")
# dumb brute force heuristic: look for locale files
# that match the longest LANG prefix, necessary to serve
# more complex lang groups like zh and pt.
x = x[0...-1]
end
end
end

# Translates msg string for current locale (e.g. for "Dashboard", we get "Tableau de Bord" in fr)
def t(msg, options = {})
string = strings(current_locale)[msg] || msg
if options.empty?
string
else
string % options
end
end

# Inverse-translates msg string for current locale (e.g. for "Tableau de Bord" in fr, we get "Dashboard")
def it(msg, options = {})
inverted_strings = inverted_strings(current_locale)
msg_without_underscores = msg.to_s.sub('_', ' ')
string = inverted_strings[msg] || inverted_strings[msg_without_underscores] || msg
if options.empty?
string
else
string % options
end
end

private def strings(lang)
@strings ||= {}
@strings[lang] ||= [LOCALES].each_with_object({}) do |path, global|
Dir["#{path}/#{lang}.yml"].each do |file|
strs = YAML.safe_load_file(file)
global.merge!(strs[lang])
end
end
end

private def inverted_strings(lang)
@inverted_strings ||= {}
@inverted_strings[lang] ||= strings(lang).invert
end
end
end
end
9 changes: 8 additions & 1 deletion lib/kuiq/model/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,19 @@ def sorted_entry
end

def respond_to_missing?(method_name, include_private = false)
super || redis_hash.include?(method_name.to_s)
super ||
redis_hash.include?(it(method_name.to_s.capitalize).downcase) ||
redis_hash.include?(method_name.to_s)
end

def method_missing(method_name, *args, &block)
inverse_translated_method_name = it(method_name.to_s.capitalize).underscore
if redis_hash.include?(method_name.to_s)
redis_hash[method_name.to_s].to_s
elsif respond_to?(inverse_translated_method_name) && !redis_hash.include?(inverse_translated_method_name)
send(inverse_translated_method_name)
elsif redis_hash.include?(inverse_translated_method_name)
redis_hash[inverse_translated_method_name].to_s
else
super
end
Expand Down
14 changes: 8 additions & 6 deletions lib/kuiq/model/job_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ class JobManager
REDIS_PROPERTIES = %w[redis_version uptime_in_days connected_clients used_memory_human used_memory_peak_human]

attr_accessor :jobs, :polling_interval
attr_reader :redis_url, :current_time, :docs_url, :locale, :locale_url, :redis_info
attr_reader :redis_url, :redis_info, :current_time

def initialize
@jobs = []
@polling_interval = 5
@redis_url = Sidekiq.redis { |c| c.config.server_url }
@current_time = Time.now.utc
@docs_url = "https://github.com/sidekiq/sidekiq/wiki"
@locale_url = "https://github.com/sidekiq/sidekiq/tree/main/web/locales"
@locale = "en"
@redis_info = Sidekiq.default_configuration.redis_info
@current_time = Time.now.utc
end

def stats
Expand Down Expand Up @@ -73,10 +70,15 @@ def scheduled_jobs
end

def refresh
@current_time = Time.now.utc
refresh_time
refresh_stats
refresh_redis_properties
end

def refresh_time
@current_time = Time.now.utc
notify_observers(:current_time)
end

def refresh_stats
Job::STATUSES.each do |status|
Expand Down
6 changes: 3 additions & 3 deletions lib/kuiq/sidekiq_ui.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
require "glimmer-dsl-libui"

require "kuiq"
require "kuiq/control"
require "kuiq/i18n"
require "kuiq/ext/kernel"
require "kuiq/model/job_manager"
require "kuiq/view/dashboard"
require "kuiq/view/retries"
Expand All @@ -12,10 +13,9 @@
module Kuiq
class SidekiqUI
include Glimmer::LibUI::Application
include Kuiq::Control

before_body do
logger.info { "Welcome to Kuiq #{Kuiq::VERSION}, using the #{current_locale.upcase} locale" }
logger.info { "Welcome to Kuiq #{Kuiq::VERSION}, using the #{I18n.current_locale.upcase} locale" }
logger.info { RUBY_DESCRIPTION }

@job_manager = Model::JobManager.new
Expand Down
55 changes: 30 additions & 25 deletions lib/kuiq/view/dashboard.rb
Original file line number Diff line number Diff line change
@@ -1,52 +1,57 @@
require "kuiq/view/global_stats"
require "kuiq/view/dashboard_graph"
require "kuiq/view/footer"
require "kuiq/view/graphical_label"

module Kuiq
module View
class Dashboard
include Kuiq::Control
include Glimmer::LibUI::CustomControl

option :job_manager

body {
vertical_box {
global_stats(model: job_manager, attributes: Model::Job::STATUSES) {
global_stats(group_title: t("Summary"), model: job_manager, attributes: Model::Job::STATUSES) {
stretchy false
}

horizontal_box {
graphical_label(label_text: t("Dashboard"), width: 200, font_properties: {size: 30})

# filler
label

group(t("Dashboard")) {
margined false

vertical_box {
horizontal_box {
label(t("PollingInterval")) {
stretchy false
}

label {
text <= [job_manager, :polling_interval,
on_read: ->(val) { "#{val} sec" }]
stretchy false

# filler
label
label

vertical_box {
horizontal_box {
label(t("PollingInterval")) {
stretchy false
}

label {
text <= [job_manager, :polling_interval,
on_read: ->(val) { "#{val} sec" }]
}
}

slider(1, 10) {
value <=> [job_manager, :polling_interval]
}
}
}

slider(1, 10) {
value <=> [job_manager, :polling_interval]
}

dashboard_graph(job_manager: job_manager)
}
}

dashboard_graph(job_manager: job_manager)

graphical_label(label_text: "Redis", width: 200, font_properties: {size: 30})

global_stats(model: job_manager.redis_info, attributes: Model::JobManager::REDIS_PROPERTIES) {
global_stats(group_title: "Redis", model: job_manager.redis_info, attributes: Model::JobManager::REDIS_PROPERTIES) {
stretchy false
}


horizontal_separator {
stretchy false
Expand Down
2 changes: 1 addition & 1 deletion lib/kuiq/view/dashboard_graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Kuiq
module View
class DashboardGraph
include Kuiq::Control
include Glimmer::LibUI::CustomControl

option :job_manager

Expand Down
11 changes: 5 additions & 6 deletions lib/kuiq/view/footer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Kuiq
module View
class Footer
include Kuiq::Control
include Glimmer::LibUI::CustomControl

option :job_manager

Expand All @@ -13,13 +13,12 @@ class Footer
label(job_manager.redis_url) {
stretchy false
}
label(job_manager.current_time.strftime("%T UTC")) {
label {
stretchy false

text <= [job_manager, :current_time, on_read: -> (val) { val.strftime("%T UTC") }]
}
label("docs") {
stretchy false
}
label(job_manager.locale) {
label(I18n.current_locale) {
stretchy false
}
}
Expand Down
7 changes: 4 additions & 3 deletions lib/kuiq/view/global_stat.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module Kuiq
module View
class GlobalStat
include Kuiq::Control
include Glimmer::LibUI::CustomControl

ATTRIBUTE_CUSTOM_TEXT = {
"redis_version" => "Version",
"uptime_in_days" => "Uptime",
"connected_clients" => "Connections",
"used_memory_human" => "Used Memory",
"used_memory_peak_human" => "Peak Used Memory"
"used_memory_human" => "MemoryUsage",
"used_memory_peak_human" => "PeakMemoryUsage"
}

option :model
Expand Down
15 changes: 10 additions & 5 deletions lib/kuiq/view/global_stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
module Kuiq
module View
class GlobalStats
include Kuiq::Control
include Glimmer::LibUI::CustomControl

option :group_title
option :model
option :attributes

body {
horizontal_box {
attributes.each do |attribute|
global_stat(model: model, attribute: attribute)
end
group(group_title) {
margined false

horizontal_box {
attributes.each do |attribute|
global_stat(model: model, attribute: attribute)
end
}
}
}
end
Expand Down
Loading

0 comments on commit 1d3813b

Please sign in to comment.