Module: Conscriptor
- Includes:
- Histogram, Say
- Defined in:
- lib/conscriptor/hierarchical_context_logger.rb,
lib/conscriptor.rb,
lib/conscriptor/say.rb,
lib/conscriptor/version.rb,
lib/conscriptor/histogram.rb,
lib/conscriptor/event_counter.rb,
lib/conscriptor/progress_reporter.rb
Overview
this will include hierarchical context ONLY when you log. so you can add parents and grandparents of a lesson, and only when you call “info” will those print out.
Allows you to track layers of context, e.g. multiple levels of iteration schools -> conference_reports -> widgets.
Historical context: there’s a bunch of code in lesson importing that was managing hierarchical logging before this code was born, and we could probably use this here.
Defined Under Namespace
Modules: Histogram, Say
Classes: EventCounter, HierarchicalContextLogger, ProgressReporter
Constant Summary
collapse
- VERSION =
'0.2.1'.freeze
Instance Method Summary
collapse
Methods included from Histogram
#histogram
Methods included from Say
#say
Instance Method Details
#app_backtrace(exception) ⇒ Object
43
44
45
46
|
# File 'lib/conscriptor.rb', line 43
def app_backtrace(exception)
bc = ActiveSupport::BacktraceCleaner.new
bc.clean(exception.backtrace)
end
|
#catch_exceptions(message = '') ⇒ Object
53
54
55
56
57
58
|
# File 'lib/conscriptor.rb', line 53
def catch_exceptions(message='')
yield
rescue StandardError
print message.red
print_backtrace
end
|
#clog ⇒ Object
12
13
14
|
# File 'lib/conscriptor.rb', line 12
def clog
@clog ||= Conscriptor::HierarchicalContextLogger.new
end
|
#commit_or_rollback ⇒ Object
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/conscriptor.rb', line 60
def commit_or_rollback
puts "\nCommit? (y/n)"
if $stdin.gets.chomp == 'y'
puts 'Committing.'
else
puts 'Rolling Back.'
raise ActiveRecord::Rollback
end
end
|
#counts ⇒ Object
16
17
18
|
# File 'lib/conscriptor.rb', line 16
def counts
$counts ||= Conscriptor::EventCounter.new
end
|
#inc_timer(by: 1) ⇒ Object
95
96
97
|
# File 'lib/conscriptor.rb', line 95
def inc_timer(by: 1)
@progress.inc(by: by)
end
|
#num_lines(file) ⇒ Object
100
101
102
|
# File 'lib/conscriptor.rb', line 100
def num_lines(file)
`wc -l < #{file}`.to_i
end
|
#print_error ⇒ Object
48
49
50
51
|
# File 'lib/conscriptor.rb', line 48
def print_error
puts $ERROR_INFO.message.red
puts app_backtrace($ERROR_INFO).map { |b| " #{b}" }.join("\n").red
end
|
#print_times ⇒ Object
82
83
84
85
86
87
|
# File 'lib/conscriptor.rb', line 82
def print_times
puts 'Printing Times'.bold
@times.each do |name, times|
puts " #{name} (#{times.count} times) : #{times.sum / times.count}s (avg)"
end
end
|
#safe_transaction ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/conscriptor.rb', line 20
def safe_transaction
ActiveRecord::Base.transaction do
begin
yield
say "job's done"
rescue StandardError
say 'nope'
raise
end
counts.dump
commit_or_rollback
end
end
|
#save_even_with_errors(obj) ⇒ Object
save an active record, even if it has errors
36
37
38
39
40
41
|
# File 'lib/conscriptor.rb', line 36
def save_even_with_errors(obj)
return if obj.save
counts.record_and_print('!'.yellow, "Error saving: #{obj.errors.full_messages.join(',')}")
obj.save validate: false
end
|
#start_timer(name: nil, total: nil, report_every: nil, logger: nil) ⇒ Object
usage: start_timer total: Lesson.count, report_every: 100
91
92
93
|
# File 'lib/conscriptor.rb', line 91
def start_timer(name: nil, total: nil, report_every: nil, logger: nil)
@progress = Conscriptor::ProgressReporter.new(name: name, total: total, report_every: report_every, logger: logger)
end
|
#time(name) ⇒ Object
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/conscriptor.rb', line 71
def time(name)
start = Time.now
retval = nil
begin
retval = yield
ensure
((@times ||= {})[name] ||= []) << Time.now - start
end
retval
end
|