Class: CliApplication::Stat
- Inherits:
-
Object
- Object
- CliApplication::Stat
- Defined in:
- lib/cli_application/stat.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#stat ⇒ Object
readonly
Returns the value of attribute stat.
Instance Method Summary collapse
- #create_folder ⇒ Object
- #description=(val) ⇒ Object
- #executed_at=(at) ⇒ Object
-
#exitcode=(code) ⇒ Object
————————————————————-.
- #init_stat ⇒ Object
-
#initialize(folders) ⇒ Stat
constructor
A new instance of Stat.
- #last_started_at ⇒ Object
- #last_started_at=(at) ⇒ Object
- #last_started_at_human ⇒ Object
- #load_stat ⇒ Object
- #make_averages(avg) ⇒ Object
- #make_last(last) ⇒ Object
- #releasedate=(val) ⇒ Object
- #save ⇒ Object
- #shortdescription=(val) ⇒ Object
- #startes_human ⇒ Object
- #timezone=(val) ⇒ Object
- #update_stat ⇒ Object
- #version=(val) ⇒ Object
Constructor Details
#initialize(folders) ⇒ Stat
Returns a new instance of Stat.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/cli_application/stat.rb', line 5 def initialize(folders) @stat_filename = File.join([folders[:class], 'stat', ::StTools::System.exename.gsub(/\.rb$/, '.yml')]) @stat_folder = File.join([folders[:class], 'stat']) create_folder init_stat folders[:stat] = @stat_folder @stat[:folders] = folders @prev = load_stat end |
Instance Attribute Details
#stat ⇒ Object (readonly)
Returns the value of attribute stat.
3 4 5 |
# File 'lib/cli_application/stat.rb', line 3 def stat @stat end |
Instance Method Details
#create_folder ⇒ Object
86 87 88 89 90 |
# File 'lib/cli_application/stat.rb', line 86 def create_folder unless Dir.exist?(@stat_folder) Dir.mkdir(@stat_folder, 0777) end end |
#description=(val) ⇒ Object
39 40 41 |
# File 'lib/cli_application/stat.rb', line 39 def description=(val) @stat[:description] = val end |
#executed_at=(at) ⇒ Object
26 27 28 |
# File 'lib/cli_application/stat.rb', line 26 def executed_at=(at) @stat[:last][:executed_at] = at end |
#exitcode=(code) ⇒ Object
Функции настройки приложения
22 23 24 |
# File 'lib/cli_application/stat.rb', line 22 def exitcode=(code) @stat[:last][:exitcode] = code end |
#init_stat ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/cli_application/stat.rb', line 118 def init_stat @stat = Hash.new @stat[:name] = ::StTools::System.exename @stat[:shortdescription] = '' @stat[:version] = '' @stat[:releasedate] = '' @stat[:timezone] = '' @stat[:last_started_at] = '' @stat[:folders] = @folders @stat[:avg] = Hash.new @stat[:avg][:starts] = 0 @stat[:avg][:executed_at] = 0 @stat[:avg][:executed_at_human] = '' @stat[:avg][:memory] = 0 @stat[:last] = Hash.new @stat[:last][:started_at] = nil @stat[:last][:executed_at] = 0 @stat[:last][:executed_at_human] = '' @stat[:last][:memory] = '' @stat[:last][:exitcode] = 0 @stat[:last10] = Array.new @stat end |
#last_started_at ⇒ Object
70 71 72 |
# File 'lib/cli_application/stat.rb', line 70 def last_started_at @prev[:last][:started_at] || ::Time.zone.now end |
#last_started_at=(at) ⇒ Object
30 31 32 33 |
# File 'lib/cli_application/stat.rb', line 30 def last_started_at=(at) @stat[:last][:started_at] = at.to_s @stat[:last_started_at] = at.to_s end |
#last_started_at_human ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/cli_application/stat.rb', line 56 def last_started_at_human res = last_started_at if res.nil? || res == '' 'Ранее не запускалось' else "Последний запуск: #{res.to_time.human_datetime} (#{res.to_time.human_ago})" end end |
#load_stat ⇒ Object
74 75 76 77 78 |
# File 'lib/cli_application/stat.rb', line 74 def load_stat YAML.load_file(@stat_filename) rescue Marshal.load( Marshal.dump(init_stat)) end |
#make_averages(avg) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/cli_application/stat.rb', line 146 def make_averages(avg) if avg[:starts] == 0 avg[:starts] = 1 avg[:executed_at] = @stat[:last][:executed_at] avg[:executed_at_human] = ::StTools::Human.ago_in_words_pair(avg[:executed_at].to_i).join(' ') avg[:memory] = ::StTools::System.memory.to_i else mul = avg[:starts] avg[:starts] += 1 avg[:executed_at] = ((avg[:executed_at]*mul + @stat[:last][:executed_at]).to_f / avg[:starts]).round(6) avg[:executed_at_human] = ::StTools::Human.ago_in_words_pair(avg[:executed_at]).join(' ') avg[:memory] = (avg[:memory]*mul + ::StTools::System.memory).to_i / avg[:starts] end end |
#make_last(last) ⇒ Object
161 162 163 164 165 166 167 |
# File 'lib/cli_application/stat.rb', line 161 def make_last(last) last[:started_at] = @stat[:last_started_at] last[:executed_at] = @stat[:last][:executed_at] last[:executed_at_human] = ::StTools::Human.ago_in_words_pair(last[:executed_at]).join(' ') last[:memory] = ::StTools::Human.memory last[:exitcode] = @stat[:last][:exitcode] end |
#releasedate=(val) ⇒ Object
47 48 49 |
# File 'lib/cli_application/stat.rb', line 47 def releasedate=(val) @stat[:releasedate] = val end |
#save ⇒ Object
80 81 82 83 84 |
# File 'lib/cli_application/stat.rb', line 80 def save @prev = load_stat update_stat File.open(@stat_filename, 'w') {|f| f.write @prev.to_yaml } end |
#shortdescription=(val) ⇒ Object
43 44 45 |
# File 'lib/cli_application/stat.rb', line 43 def shortdescription=(val) @stat[:shortdescription] = val end |
#startes_human ⇒ Object
65 66 67 68 |
# File 'lib/cli_application/stat.rb', line 65 def startes_human res = @prev[:avg][:starts] "Всего было #{res} запусков" end |
#timezone=(val) ⇒ Object
92 93 94 |
# File 'lib/cli_application/stat.rb', line 92 def timezone=(val) @stat[:timezone] = val end |
#update_stat ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/cli_application/stat.rb', line 96 def update_stat @prev[:name] = @stat[:name] @prev[:shortdescription] = @stat[:shortdescription] @prev[:version] = @stat[:version] @prev[:releasedate] = @stat[:releasedate] @prev[:timezone] = ::Time.zone.name @prev[:last_started_at] = @stat[:last_started_at] @prev[:folders] = @stat[:folders] make_averages(@prev[:avg]) make_last(@prev[:last]) tmp = Array.new tmp << @prev[:last_started_at] tmp << @prev[:last][:exitcode] tmp << @prev[:last][:executed_at] tmp << @prev[:last][:memory] @prev[:last10].unshift(tmp.join(',')) @prev[:last10] = @prev[:last10][0,10] end |
#version=(val) ⇒ Object
35 36 37 |
# File 'lib/cli_application/stat.rb', line 35 def version=(val) @stat[:version] = val end |