Class: Fluent::SimpleArithmeticOutput
- Inherits:
-
Output
- Object
- Output
- Fluent::SimpleArithmeticOutput
- Defined in:
- lib/fluent/plugin/out_simple_arithmetic.rb
Instance Attribute Summary collapse
-
#_formulas ⇒ Object
Returns the value of attribute _formulas.
Instance Method Summary collapse
- #calculate(record) ⇒ Object
- #configure(conf) ⇒ Object
- #create_func(var, expr) ⇒ Object
- #emit(tag, es, chain) ⇒ Object
- #exec_func(record, f_argv, formula) ⇒ Object
- #has_all_keys?(record, argv) ⇒ Boolean
-
#initialize ⇒ SimpleArithmeticOutput
constructor
A new instance of SimpleArithmeticOutput.
-
#replace_symbols(record) ⇒ Object
functions for symbols.
- #restore_symbols(record) ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ SimpleArithmeticOutput
Returns a new instance of SimpleArithmeticOutput.
20 21 22 23 |
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 20 def initialize super require 'time' end |
Instance Attribute Details
#_formulas ⇒ Object
Returns the value of attribute _formulas.
18 19 20 |
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 18 def _formulas @_formulas end |
Instance Method Details
#calculate(record) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 111 def calculate(record) record = replace_symbols(record) @_formulas.each {|var, f_argv, formula| if not has_all_keys?(record, f_argv) if @undefined_variables == 'nil' record[var] = nil end next end begin record[var] = exec_func(record, f_argv, formula) rescue StandardError => error case @how_to_process_error when 'error_string' record[var] = error.to_s when 'nil' record[var] = nil end end } restore_symbols(record) end |
#configure(conf) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 33 def configure(conf) super # Check configuration {'undefined_variables' => %w{nil undefined}, 'how_to_process_error' => %w{nil undefined error_string}}.each_pair{|attr, choices| param = instance_variable_get('@' + attr) if not choices.include? param raise Fluent::ConfigError, \ "Invalid setting at #{attr}: `#{param}`. Choices: %s" % choices.join(', ') end } # Create functions @_formulas = [] def create_func(var, expr) begin f_argv = expr.scan(/[a-zA-Z\_][\w\d\.\_]*/).uniq.select{|x| not x.start_with?('iso8601', 'Time.iso8601')} f = eval('lambda {|' + f_argv.join(',') + '| ' + expr + '}') return [f, f_argv] rescue SyntaxError raise Fluent::ConfigError, "SyntaxError at formula `#{var}`: #{expr}" end end conf.elements.select { |element| element.name == 'formulas' }.each { |element| element.each_pair { |var, expr| element.has_key?(var) # to suppress unread configuration warning formula, f_argv = create_func(var, expr) @_formulas.push [var, f_argv, formula] } } if @_formulas.empty? raise Fluent::ConfigError, "No formulas found" end end |
#create_func(var, expr) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 48 def create_func(var, expr) begin f_argv = expr.scan(/[a-zA-Z\_][\w\d\.\_]*/).uniq.select{|x| not x.start_with?('iso8601', 'Time.iso8601')} f = eval('lambda {|' + f_argv.join(',') + '| ' + expr + '}') return [f, f_argv] rescue SyntaxError raise Fluent::ConfigError, "SyntaxError at formula `#{var}`: #{expr}" end end |
#emit(tag, es, chain) ⇒ Object
137 138 139 140 141 142 |
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 137 def emit(tag, es, chain) chain.next es.each { |time, record| Fluent::Engine.emit(@tag, time, calculate(record)) } end |
#exec_func(record, f_argv, formula) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 82 def exec_func(record, f_argv, formula) argv = [] f_argv.each {|v| argv.push(record[v]) } return formula.call(*argv) end |
#has_all_keys?(record, argv) ⇒ Boolean
73 74 75 76 77 78 79 80 |
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 73 def has_all_keys?(record, argv) argv.each {|var| if not record.has_key?(var) return false end } true end |
#replace_symbols(record) ⇒ Object
functions for symbols
91 92 93 94 95 96 97 98 99 |
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 91 def replace_symbols(record) # 'var-1' -> 'var__HYPHEN__1' new_record = {} record.each_pair {|key, value| new_key = key.gsub('-', @replace_hyphen).gsub('$', @replace_dollar).gsub('@', @replace_at) new_record[new_key] = value } return new_record end |
#restore_symbols(record) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 101 def restore_symbols(record) # 'var__HYPHEN__1' -> 'var-1' new_record = {} record.each_pair {|key, value| new_key = key.gsub(@replace_hyphen, '-').gsub(@replace_dollar, '$').gsub(@replace_at, '@') new_record[new_key] = value } new_record end |
#shutdown ⇒ Object
29 30 31 |
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 29 def shutdown super end |
#start ⇒ Object
25 26 27 |
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 25 def start super end |