Class: RailsServerMonitor::ChartForServer
- Inherits:
-
Object
- Object
- RailsServerMonitor::ChartForServer
- Defined in:
- app/services/rails_server_monitor/chart_for_server.rb
Constant Summary collapse
- AVAILABLE_TIMELINES =
%w(today week month all)
Instance Attribute Summary collapse
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#timeline ⇒ Object
readonly
Returns the value of attribute timeline.
Instance Method Summary collapse
- #fill_hash_for_attribute(attr) ⇒ Object
-
#initialize(server, timeline:) ⇒ ChartForServer
constructor
A new instance of ChartForServer.
- #last_record ⇒ Object
- #render_chart ⇒ Object
- #scope ⇒ Object
- #today? ⇒ Boolean
Constructor Details
#initialize(server, timeline:) ⇒ ChartForServer
Returns a new instance of ChartForServer.
7 8 9 10 |
# File 'app/services/rails_server_monitor/chart_for_server.rb', line 7 def initialize(server, timeline:) @server = server @timeline = timeline.blank? ? "today" : timeline end |
Instance Attribute Details
#server ⇒ Object (readonly)
Returns the value of attribute server.
6 7 8 |
# File 'app/services/rails_server_monitor/chart_for_server.rb', line 6 def server @server end |
#timeline ⇒ Object (readonly)
Returns the value of attribute timeline.
6 7 8 |
# File 'app/services/rails_server_monitor/chart_for_server.rb', line 6 def timeline @timeline end |
Instance Method Details
#fill_hash_for_attribute(attr) ⇒ Object
49 50 51 52 53 54 55 |
# File 'app/services/rails_server_monitor/chart_for_server.rb', line 49 def fill_hash_for_attribute(attr) {}.tap do |h| scope.each do |snapshot| h[snapshot.created_at] = snapshot.send(attr) end end end |
#last_record ⇒ Object
30 31 32 |
# File 'app/services/rails_server_monitor/chart_for_server.rb', line 30 def last_record @last_record = scope.last end |
#render_chart ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'app/services/rails_server_monitor/chart_for_server.rb', line 12 def render_chart [ { name: "% CPU usage", data: fill_hash_for_attribute(:cpu_usage_percentage), }, { name: "% RAM usage", data: fill_hash_for_attribute(:ram_usage_percentage) }, { name: "% HDD usage", data: fill_hash_for_attribute(:hdd_usage_percentage) } ] end |
#scope ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/services/rails_server_monitor/chart_for_server.rb', line 34 def scope return @scope if defined? @scope query = server.server_snapshots.order(id: :asc) if today? query = query.where("created_at > ?", 1.day.ago) elsif timeline == "week" query = query.where("created_at > ?", 7.day.ago) elsif timeline == "month" query = query.where("created_at > ?", 30.day.ago) else query = query.all end @scope = query.to_a end |
#today? ⇒ Boolean
26 27 28 |
# File 'app/services/rails_server_monitor/chart_for_server.rb', line 26 def today? timeline == "today" end |