Class: Perus::Server::Stats
- Inherits:
-
Object
- Object
- Perus::Server::Stats
- Defined in:
- lib/perus/server/stats.rb
Constant Summary collapse
- MAX_HISTORY =
10
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
Instance Method Summary collapse
- #alerts_cache_time ⇒ Object
- #average_alerts_cache_time ⇒ Object
- #average_clean_time ⇒ Object
- #average_vacuum_time ⇒ Object
- #clean_time ⇒ Object
-
#initialize ⇒ Stats
constructor
data format (json): { “vacuums”: [ [“ISO time”, duration (int) || ‘failed’], … ] }.
-
#last_alerts_cache ⇒ Object
alerts caching.
-
#last_clean ⇒ Object
cleaning.
-
#last_vacuum ⇒ Object
vacuuming.
- #save ⇒ Object
- #vacuum_time ⇒ Object
Constructor Details
#initialize ⇒ Stats
data format (json):
"vacuums": [
["ISO time", duration (int) || 'failed'],
...
]
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/perus/server/stats.rb', line 16 def initialize if File.exists?(Server..stats_path) data = IO.read(Server..stats_path) unless data.empty? @data = JSON.parse(data) return end end @data = { 'vacuums' => [], 'alerts_caches' => [], 'cleans' => [] } end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
6 7 8 |
# File 'lib/perus/server/stats.rb', line 6 def data @data end |
Class Method Details
.alerts_cached!(time) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/perus/server/stats.rb', line 86 def self.alerts_cached!(time) stats = Stats.new list = stats.data['alerts_caches'] list << [Time.now.to_s, time] if list.length > MAX_HISTORY list.drop(list.length - MAX_HISTORY) end stats.save end |
.cleaned!(time) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/perus/server/stats.rb', line 116 def self.cleaned!(time) stats = Stats.new list = stats.data['cleans'] list << [Time.now.to_s, time] if list.length > MAX_HISTORY list.drop(list.length - MAX_HISTORY) end stats.save end |
.vacuumed!(time) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/perus/server/stats.rb', line 56 def self.vacuumed!(time) stats = Stats.new list = stats.data['vacuums'] list << [Time.now.to_s, time] if list.length > MAX_HISTORY list.drop(list.length - MAX_HISTORY) end stats.save end |
Instance Method Details
#alerts_cache_time ⇒ Object
74 75 76 77 |
# File 'lib/perus/server/stats.rb', line 74 def alerts_cache_time entry = @data['alerts_caches'].last entry ? entry.last : nil end |
#average_alerts_cache_time ⇒ Object
79 80 81 82 83 84 |
# File 'lib/perus/server/stats.rb', line 79 def average_alerts_cache_time entries = @data['alerts_caches'] return nil if entries.empty? ints = entries.map(&:last).select {|time| time.is_a?(Numeric)} ints.reduce(:+).to_f / ints.length end |
#average_clean_time ⇒ Object
109 110 111 112 113 114 |
# File 'lib/perus/server/stats.rb', line 109 def average_clean_time entries = @data['cleans'] return nil if entries.empty? ints = entries.map(&:last).select {|time| time.is_a?(Numeric)} ints.reduce(:+).to_f / ints.length end |
#average_vacuum_time ⇒ Object
49 50 51 52 53 54 |
# File 'lib/perus/server/stats.rb', line 49 def average_vacuum_time entries = @data['vacuums'] return nil if entries.empty? ints = entries.map(&:last).select {|time| time.is_a?(Numeric)} ints.reduce(:+).to_f / ints.length end |
#clean_time ⇒ Object
104 105 106 107 |
# File 'lib/perus/server/stats.rb', line 104 def clean_time entry = @data['cleans'].last entry ? entry.last : nil end |
#last_alerts_cache ⇒ Object
alerts caching
69 70 71 72 |
# File 'lib/perus/server/stats.rb', line 69 def last_alerts_cache entry = @data['alerts_caches'].last entry ? entry.first : nil end |
#last_clean ⇒ Object
cleaning
99 100 101 102 |
# File 'lib/perus/server/stats.rb', line 99 def last_clean entry = @data['cleans'].last entry ? entry.first : nil end |
#last_vacuum ⇒ Object
vacuuming
39 40 41 42 |
# File 'lib/perus/server/stats.rb', line 39 def last_vacuum entry = @data['vacuums'].last entry ? entry.first : nil end |
#save ⇒ Object
32 33 34 35 36 |
# File 'lib/perus/server/stats.rb', line 32 def save File.open(Server..stats_path, 'w') do |f| f.write(JSON.dump(@data)) end end |
#vacuum_time ⇒ Object
44 45 46 47 |
# File 'lib/perus/server/stats.rb', line 44 def vacuum_time entry = @data['vacuums'].last entry ? entry.last : nil end |