Class: Brewer::Stats::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/brewer/stats/logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogger

Returns a new instance of Logger.



8
9
10
11
12
13
14
# File 'lib/brewer/stats/logger.rb', line 8

def initialize
  @controller = Brewer::Controller.new
  @state = {}

  create_stats_directory
  create_log_file
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



5
6
7
# File 'lib/brewer/stats/logger.rb', line 5

def controller
  @controller
end

#stateObject

Returns the value of attribute state.



6
7
8
# File 'lib/brewer/stats/logger.rb', line 6

def state
  @state
end

#stats_dirObject (readonly)

Returns the value of attribute stats_dir.



5
6
7
# File 'lib/brewer/stats/logger.rb', line 5

def stats_dir
  @stats_dir
end

Instance Method Details

#capture_snapshotObject

:nocov:



26
27
28
29
# File 'lib/brewer/stats/logger.rb', line 26

def capture_snapshot
  @state['pv'] = @controller.pv
  @state
end

#clear_log_fileObject



50
51
52
53
# File 'lib/brewer/stats/logger.rb', line 50

def clear_log_file
  File.open($log_file, 'w') {|file| file.truncate(0) }
  true
end

#create_log_fileObject



45
46
47
48
# File 'lib/brewer/stats/logger.rb', line 45

def create_log_file
  File.open($log_file, 'w') unless File.exists?($log_file)
  true
end

#create_stats_directoryObject



40
41
42
43
# File 'lib/brewer/stats/logger.rb', line 40

def create_stats_directory
  Dir.mkdir($stats_dir) unless File.exists?($stats_dir)
  true
end

#log(interval: 15) ⇒ Object

:nocov:



17
18
19
20
21
22
23
# File 'lib/brewer/stats/logger.rb', line 17

def log(interval: 15)
  while true do
    capture_snapshot
    store
    sleep(interval)
  end
end

#storeObject



31
32
33
34
35
36
37
38
# File 'lib/brewer/stats/logger.rb', line 31

def store
  store = YAML::Store.new $log_file
  store.transaction {
    @state.each do |k, v|
      store[Time.now.to_i] = {k => v}
    end
  }
end