Class: Perus::Server::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/perus/server/stats.rb

Constant Summary collapse

MAX_HISTORY =
10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStats

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.options.stats_path)
        data = IO.read(Server.options.stats_path)
        unless data.empty?
            @data = JSON.parse(data)
            return
        end
    end

    @data = {
        'vacuums' => [],
        'alerts_caches' => [],
        'cleans' => []
    }
end

Instance Attribute Details

#dataObject (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_timeObject



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_timeObject



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_timeObject



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_timeObject



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_timeObject



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_cacheObject

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_cleanObject

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_vacuumObject

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

#saveObject



32
33
34
35
36
# File 'lib/perus/server/stats.rb', line 32

def save
    File.open(Server.options.stats_path, 'w') do |f|
        f.write(JSON.dump(@data))
    end
end

#vacuum_timeObject



44
45
46
47
# File 'lib/perus/server/stats.rb', line 44

def vacuum_time
    entry = @data['vacuums'].last
    entry ? entry.last : nil
end