Class: RailsServerMonitor::ChartForServer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#serverObject (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

#timelineObject (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_recordObject



30
31
32
# File 'app/services/rails_server_monitor/chart_for_server.rb', line 30

def last_record
  @last_record = scope.last
end

#render_chartObject



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

#scopeObject



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

Returns:

  • (Boolean)


26
27
28
# File 'app/services/rails_server_monitor/chart_for_server.rb', line 26

def today?
  timeline == "today"
end