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



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/perus/server/stats.rb', line 84

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



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/perus/server/stats.rb', line 114

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



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/perus/server/stats.rb', line 54

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



72
73
74
75
# File 'lib/perus/server/stats.rb', line 72

def alerts_cache_time
    entry = @data['alerts_caches'].last
    entry ? entry.last : nil
end

#average_alerts_cache_timeObject



77
78
79
80
81
82
# File 'lib/perus/server/stats.rb', line 77

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



107
108
109
110
111
112
# File 'lib/perus/server/stats.rb', line 107

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



47
48
49
50
51
52
# File 'lib/perus/server/stats.rb', line 47

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



102
103
104
105
# File 'lib/perus/server/stats.rb', line 102

def clean_time
    entry = @data['cleans'].last
    entry ? entry.last : nil
end

#last_alerts_cacheObject

alerts caching



67
68
69
70
# File 'lib/perus/server/stats.rb', line 67

def last_alerts_cache
    entry = @data['alerts_caches'].last
    entry ? entry.first : nil
end

#last_cleanObject

cleaning



97
98
99
100
# File 'lib/perus/server/stats.rb', line 97

def last_clean
    entry = @data['cleans'].last
    entry ? entry.first : nil
end

#last_vacuumObject

vacuuming



37
38
39
40
# File 'lib/perus/server/stats.rb', line 37

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

#saveObject



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

def save
    IO.write(Server.options.stats_path, JSON.dump(@data))
end

#vacuum_timeObject



42
43
44
45
# File 'lib/perus/server/stats.rb', line 42

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