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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/logstash/filters/math.rb', line 41
def register
functions = {}
[
[MathFunctions::Add.new, '+', 'add', 'plus'],
[MathFunctions::Subtract.new, '-', 'sub', 'subtract'],
[MathFunctions::Multiply.new, '*', 'mpx', 'times', 'multiply'],
[MathFunctions::Round.new, 'round'],
[MathFunctions::Power.new, '**', '^', 'to the power of'],
[MathFunctions::Divide.new, '/', 'div', 'divide'],
[MathFunctions::Modulo.new, 'mod', 'modulo'],
[MathFunctions::FloatDivide.new, 'fdiv', 'float divide']
].each do |list|
value = list.shift
list.each{|key| functions[key] = value}
end
@calculate_copy = []
all_function_keys = functions.keys
@register = []
calculate.each do |calculation|
if calculation.size != 4
raise LogStash::ConfigurationError, I18n.t(
"logstash.runner.configuration.invalid_plugin_register",
:plugin => "filter",
:type => "math",
:error => "Invalid number of elements in a calculation setting: expected 4, got: #{calculation.size}. You specified: #{calculation}"
)
end
function_key, operand1, operand2, target = calculation
if !all_function_keys.include?(function_key)
raise LogStash::ConfigurationError, I18n.t(
"logstash.runner.configuration.invalid_plugin_register",
:plugin => "filter",
:type => "math",
:error => "Invalid first element of a calculation: expected one of #{all_function_keys.join(',')}, got: #{function_key}. You specified: #{calculation.join(',')}"
)
end
function = functions[function_key]
left_element = MathCalculationElements.build(operand1, 1, @register)
right_element = MathCalculationElements.build(operand2, 2, @register)
if right_element.literal?
lhs = left_element.literal? ? left_element.get : 1
warning = function.invalid?(lhs, right_element.get)
unless warning.nil?
raise LogStash::ConfigurationError, I18n.t(
"logstash.runner.configuration.invalid_plugin_register",
:plugin => "filter",
:type => "math",
:error => "Numeric literals are specified as in the calculation but the function invalidates with '#{warning}'. You specified: #{calculation.join(',')}"
)
end
end
result_element = MathCalculationElements.build(target, 3, @register)
@calculate_copy << [function, left_element, right_element, result_element]
end
if @calculate_copy.last.last.is_a?(MathCalculationElements::RegisterElement)
raise LogStash::ConfigurationError, I18n.t(
"logstash.runner.configuration.invalid_plugin_register",
:plugin => "filter",
:type => "math",
:error => "The final target is a Register, the overall calculation result will not be set in the event"
)
end
end
|