diff --git a/CHANGES.md b/CHANGES.md index c980ed0..c06b0e5 100755 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,6 +3,7 @@ - 1.4.0 (unreleased) - track CPU, memory, storage: https://github.com/igorkasyanchuk/rails_performance/pull/111 - use UTC time: https://github.com/igorkasyanchuk/rails_performance/pull/114 + - custom expiration time for system monitoring report: https://github.com/igorkasyanchuk/rails_performance/pull/115/files - 1.3.3 - little improvements and bug fixes diff --git a/Gemfile.lock b/Gemfile.lock index aac30fe..59a0448 100755 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - rails_performance (1.4.0.alpha4) + rails_performance (1.4.0.alpha5) browser railties redis diff --git a/README.md b/README.md index 78271a8..c409e62 100755 --- a/README.md +++ b/README.md @@ -117,6 +117,10 @@ RailsPerformance.setup do |config| # To monitor custom events with `RailsPerformance.measure` block # config.include_custom_events = true + + # To monitor system resources (CPU, memory, disk) + # to enabled add required gems (see README) + # config.system_monitor_duration = 24.hours end if defined?(RailsPerformance) ``` diff --git a/app/controllers/rails_performance/rails_performance_controller.rb b/app/controllers/rails_performance/rails_performance_controller.rb index 5a5c7ad..6fe54e7 100755 --- a/app/controllers/rails_performance/rails_performance_controller.rb +++ b/app/controllers/rails_performance/rails_performance_controller.rb @@ -15,7 +15,7 @@ def index end def resources - @datasource = RailsPerformance::DataSource.new(**prepare_query(params), type: :resources) + @datasource = RailsPerformance::DataSource.new(**prepare_query(params), type: :resources, days: RailsPerformance::Utils.days(RailsPerformance.system_monitor_duration)) db = @datasource.db @resources_report = RailsPerformance::Reports::ResourcesReport.new(db) diff --git a/lib/generators/rails_performance/install/templates/initializer.rb b/lib/generators/rails_performance/install/templates/initializer.rb index 20a5b05..3430230 100644 --- a/lib/generators/rails_performance/install/templates/initializer.rb +++ b/lib/generators/rails_performance/install/templates/initializer.rb @@ -52,5 +52,9 @@ config.skipable_rake_tasks = ["webpacker:compile"] config.include_rake_tasks = false config.include_custom_events = true + + # If enabled, the system monitor will be displayed on the dashboard + # to enabled add required gems (see README) + # config.system_monitor_duration = 24.hours end end diff --git a/lib/rails_performance.rb b/lib/rails_performance.rb index fd51543..9bea4cf 100755 --- a/lib/rails_performance.rb +++ b/lib/rails_performance.rb @@ -119,6 +119,14 @@ def self.ignored_paths=(paths) mattr_accessor :ignore_trace_headers @@ignore_trace_headers = ["datetimei"] + # System monitor duration (expiration time) + mattr_accessor :system_monitor_duration + @@system_monitor_duration = 24.hours + + # -- internal usage -- + # + # + # to store the resource monitor instance mattr_accessor :_resource_monitor @@_resource_monitor = nil diff --git a/lib/rails_performance/data_source.rb b/lib/rails_performance/data_source.rb index f02e638..61dea49 100755 --- a/lib/rails_performance/data_source.rb +++ b/lib/rails_performance/data_source.rb @@ -10,19 +10,20 @@ class DataSource resources: RailsPerformance::Models::ResourceRecord } - attr_reader :q, :klass, :type + attr_reader :q, :klass, :type, :days - def initialize(type:, q: {}) + def initialize(type:, q: {}, days: RailsPerformance::Utils.days(RailsPerformance.duration)) @type = type @klass = KLASSES[type] q[:on] ||= Date.today @q = q + @days = days end def db result = RailsPerformance::Models::Collection.new now = RailsPerformance::Utils.time - (0..(RailsPerformance::Utils.days)).to_a.reverse_each do |e| + (0..days).to_a.reverse_each do |e| RailsPerformance::DataSource.new(q: q.merge({on: (now - e.days).to_date}), type: type).add_to(result) end result diff --git a/lib/rails_performance/models/resource_record.rb b/lib/rails_performance/models/resource_record.rb index adf024c..08c07b8 100644 --- a/lib/rails_performance/models/resource_record.rb +++ b/lib/rails_performance/models/resource_record.rb @@ -40,7 +40,8 @@ def record_hash def save key = "resource|server|#{server}|context|#{context}|role|#{role}|datetime|#{datetime}|datetimei|#{datetimei}|END|#{RailsPerformance::SCHEMA}" - Utils.save_to_redis(key, json) + # with longer expiration time + Utils.save_to_redis(key, json, RailsPerformance.system_monitor_duration.to_i) end end end diff --git a/lib/rails_performance/reports/base_report.rb b/lib/rails_performance/reports/base_report.rb index 991bee9..ee80791 100755 --- a/lib/rails_performance/reports/base_report.rb +++ b/lib/rails_performance/reports/base_report.rb @@ -63,14 +63,14 @@ def calculate_data # 1732125550000 => 0, # .... # } - def nil_data + def nil_data(duration = RailsPerformance.duration) @nil_data ||= begin result = {} now = RailsPerformance::Utils.time now = now.change(sec: 0, usec: 0) stop = now # Time.at(60 * (now.to_i / 60)) offset = 0 # RailsPerformance::Reports::BaseReport.time_in_app_time_zone(now).utc_offset - current = stop - RailsPerformance.duration + current = stop - duration while current <= stop current.strftime(RailsPerformance::FORMAT) @@ -86,8 +86,8 @@ def nil_data # 1732125540000 => 1, # 1732125550000 => 0, # } - def nullify_data(input) - nil_data.merge(input).sort + def nullify_data(input, duration = RailsPerformance.duration) + nil_data(duration).merge(input).sort end end end diff --git a/lib/rails_performance/reports/resources_report.rb b/lib/rails_performance/reports/resources_report.rb index ef9a9e0..f93a62c 100644 --- a/lib/rails_performance/reports/resources_report.rb +++ b/lib/rails_performance/reports/resources_report.rb @@ -5,13 +5,13 @@ def data @data ||= db.data .collect { |e| e.record_hash } .group_by { |e| e[:server] + "///" + e[:context] + "///" + e[:role] } - .transform_values { |v| v.sort { |a, b| b[sort] <=> a[sort] } } + # .transform_values { |v| v.sort { |a, b| b[sort] <=> a[sort] } } .transform_values { |v| v.map { |e| e.merge({datetimei: e[:datetimei].to_i}) } } end def cpu @cpu ||= data.transform_values do |v| - nullify_data(v.each_with_object({}) do |e, res| + prepare_report(v.each_with_object({}) do |e, res| res[e[:datetimei] * 1000] = e[:cpu]["one_min"].to_f.round(2) end) end @@ -19,7 +19,7 @@ def cpu def memory @memory ||= data.transform_values do |v| - nullify_data(v.each_with_object({}) do |e, res| + prepare_report(v.each_with_object({}) do |e, res| res[e[:datetimei] * 1000] = e[:memory].to_f.round(2) end) end @@ -27,11 +27,17 @@ def memory def disk @disk ||= data.transform_values do |v| - nullify_data(v.each_with_object({}) do |e, res| + prepare_report(v.each_with_object({}) do |e, res| res[e[:datetimei] * 1000] = e[:disk]["available"].to_f.round(2) end) end end + + private + + def prepare_report(input) + nullify_data(input, RailsPerformance.system_monitor_duration) + end end end end diff --git a/lib/rails_performance/utils.rb b/lib/rails_performance/utils.rb index 40a8597..2e4ae9c 100755 --- a/lib/rails_performance/utils.rb +++ b/lib/rails_performance/utils.rb @@ -40,8 +40,8 @@ def self.save_to_redis(key, value, expire = RailsPerformance.duration.to_i) RailsPerformance.redis.set(key, value.to_json, ex: expire.to_i) end - def self.days - (RailsPerformance.duration / 1.day) + 1 + def self.days(duration = RailsPerformance.duration) + (duration / 1.day) + 1 end def self.median(array) diff --git a/lib/rails_performance/version.rb b/lib/rails_performance/version.rb index e0832a8..e0b5f01 100755 --- a/lib/rails_performance/version.rb +++ b/lib/rails_performance/version.rb @@ -1,4 +1,4 @@ module RailsPerformance - VERSION = "1.4.0.alpha4" + VERSION = "1.4.0.alpha5" SCHEMA = "1.0.2" end diff --git a/test/dummy/config/initializers/rails_performance.rb b/test/dummy/config/initializers/rails_performance.rb index a3566ae..f65521d 100755 --- a/test/dummy/config/initializers/rails_performance.rb +++ b/test/dummy/config/initializers/rails_performance.rb @@ -35,5 +35,9 @@ config.include_rake_tasks = true config.include_custom_events = true + + # If enabled, the system monitor will be displayed on the dashboard + # to enabled add required gems (see README) + config.system_monitor_duration = 24.hours end end