class Perus::Server::Stats

Constants

MAX_HISTORY

Attributes

data[R]

Public Class Methods

alerts_cached!(time) click to toggle source
# 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) click to toggle source
# 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
new() click to toggle source

data format (json): {

"vacuums": [
  ["ISO time", duration (int) || 'failed'],
  ...
]

}

# 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
vacuumed!(time) click to toggle source
# 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

Public Instance Methods

alerts_cache_time() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/perus/server/stats.rb, line 104
def clean_time
    entry = @data['cleans'].last
    entry ? entry.last : nil
end
last_alerts_cache() click to toggle source

alerts caching

# File lib/perus/server/stats.rb, line 69
def last_alerts_cache
    entry = @data['alerts_caches'].last
    entry ? entry.first : nil
end
last_clean() click to toggle source

cleaning

# File lib/perus/server/stats.rb, line 99
def last_clean
    entry = @data['cleans'].last
    entry ? entry.first : nil
end
last_vacuum() click to toggle source

vacuuming

# File lib/perus/server/stats.rb, line 39
def last_vacuum
    entry = @data['vacuums'].last
    entry ? entry.first : nil
end
save() click to toggle source
# 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_time() click to toggle source
# File lib/perus/server/stats.rb, line 44
def vacuum_time
    entry = @data['vacuums'].last
    entry ? entry.last : nil
end