Class: Fluent::SimpleArithmeticOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_simple_arithmetic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSimpleArithmeticOutput

Returns a new instance of SimpleArithmeticOutput.



19
20
21
22
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 19

def initialize
  super
  require 'time'
end

Instance Attribute Details

#_formulasObject

Returns the value of attribute _formulas.



17
18
19
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 17

def _formulas
  @_formulas
end

Instance Method Details

#calculate(record) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 110

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



32
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
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 32

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?('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



47
48
49
50
51
52
53
54
55
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 47

def create_func(var, expr)
  begin
    f_argv = expr.scan(/[a-zA-Z\_][\w\d\.\_]*/).uniq.select{|x| not x.start_with?('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



136
137
138
139
140
141
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 136

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



81
82
83
84
85
86
87
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 81

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

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 72

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



90
91
92
93
94
95
96
97
98
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 90

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)
    new_record[new_key] = value
  }
  return new_record
end

#restore_symbols(record) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 100

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, '$')
    new_record[new_key] = value
  }
  new_record
end

#shutdownObject



28
29
30
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 28

def shutdown
  super
end

#startObject



24
25
26
# File 'lib/fluent/plugin/out_simple_arithmetic.rb', line 24

def start
  super
end