fluent-plugin-datacalculator 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -7,5 +7,12 @@ Rake::TestTask.new(:test) do |test|
7
7
  test.verbose = true
8
8
  end
9
9
 
10
+ desc "fomulas test"
11
+ Rake::TestTask.new(:fomulas) do |test|
12
+ test.libs << 'lib' << 'test'
13
+ test.pattern = 'test/**/test_out_datacalculator_fomulas.rb'
14
+ test.verbose = true
15
+ end
16
+
10
17
  task :default => :test
11
18
 
@@ -2,6 +2,21 @@
2
2
  type forward
3
3
  </source>
4
4
 
5
+
6
+
7
+ <match payment.install>
8
+ type datacalculator
9
+ tag result.install
10
+ count_interval 5s
11
+ aggregate keys area_id, mission_id
12
+ formulas sum = amount * price, cnt = 1, total = amount
13
+ finalizer name = "area_" + cnt.to_s
14
+ <unmatched>
15
+ type file
16
+ path unmatched
17
+ </unmatched>
18
+ </match>
19
+
5
20
  <match payment.quest>
6
21
  type datacalculator
7
22
  tag result.quest
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "fluent-plugin-datacalculator"
6
- s.version = "0.0.3"
6
+ s.version = "0.0.4"
7
7
  s.authors = ["Muddy Dixon"]
8
8
  s.email = ["muddydixon@gmail.com"]
9
9
  s.homepage = "https://github.com/muddydixon/fluent-plugin-datacalculator"
@@ -70,8 +70,12 @@ class Fluent::DataCalculatorOutput < Fluent::Output
70
70
  def createFunc (cnt, str)
71
71
  str.strip!
72
72
  left, right = str.split(/\s*=\s*/, 2)
73
- # Fluent moduleだけはOK
74
- rights = right.scan(/[a-zA-Z][\w\d_\.\$\:\@]*/).uniq.select{|x| x.index('Fluent') != 0}
73
+
74
+ rights = right.gsub(/([^\\]|^)\".+[^\\]\"/, '')
75
+ rights = rights.scan(/[a-zA-Z][\w\d_\.\$\:\@]*/).uniq.select{|x| not x.match(/^[A-Z]/)}
76
+ rights = rights.map do|var|
77
+ var.sub(/[\.:].+$/, '')
78
+ end
75
79
 
76
80
  begin
77
81
  f = eval('lambda {|'+rights.join(',')+'| '+right + '}')
@@ -94,7 +98,11 @@ class Fluent::DataCalculatorOutput < Fluent::Output
94
98
  end
95
99
  _argv.push obj[arg]
96
100
  }
97
- formula.call(*_argv)
101
+ begin
102
+ formula.call(*_argv)
103
+ rescue ArgumentError
104
+ raise Fluent::RuntimeError, "argument error " + _argv + " are adapted to "+ formula
105
+ end
98
106
  end
99
107
 
100
108
  @_formulas = []
@@ -0,0 +1,79 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ class DataCalculatorOutputTest < Test::Unit::TestCase
5
+ def setup
6
+ Fluent::Test.setup
7
+ end
8
+
9
+ CONFIG = %[
10
+ unit minute
11
+ aggregate tag
12
+ input_tag_remove_prefix test
13
+ formulas sum = amount * price, amounts = amount, record = 1
14
+ finalizer ave = amounts > 0 ? 1.0 * sum / amounts : 0
15
+ ]
16
+
17
+ def create_driver(conf = CONFIG, tag='test.input')
18
+ Fluent::Test::OutputTestDriver.new(Fluent::DataCalculatorOutput, tag).configure(conf)
19
+ end
20
+
21
+ def test_create_formula
22
+
23
+ # case: common case
24
+ d = create_driver %[
25
+ aggregate all
26
+ formulas sum = amount * price, cnt = amount
27
+ ]
28
+ assert_equal 0, d.instance._formulas[0][0]
29
+ assert_equal 'sum', d.instance._formulas[0][1]
30
+ assert_equal ['amount', 'price'], d.instance._formulas[0][2]
31
+ assert_equal 1, d.instance._formulas[1][0]
32
+ assert_equal 'cnt', d.instance._formulas[1][1]
33
+ assert_equal ['amount'], d.instance._formulas[1][2]
34
+
35
+ assert_equal 200, d.instance._formulas[0][3].call(10, 20)
36
+ assert_equal 20, d.instance._formulas[1][3].call(20)
37
+ assert_raise(ArgumentError){
38
+ d.instance._formulas[0][3].call(10)
39
+ }
40
+
41
+ # case: use Class
42
+ d = create_driver %[
43
+ aggregate all
44
+ formulas time = Fluent::Engine.now
45
+ ]
46
+ assert_equal 0, d.instance._formulas[0][0]
47
+ assert_equal 'time', d.instance._formulas[0][1]
48
+ assert_equal [], d.instance._formulas[0][2]
49
+ assert_equal Time.now.to_i, d.instance._formulas[0][3].call()
50
+
51
+ # case: use instance.method
52
+ d = create_driver %[
53
+ aggregate all
54
+ formulas sum = amount.to_i * price, cnt = amount
55
+ ]
56
+ assert_equal 0, d.instance._formulas[0][0]
57
+ assert_equal 'sum', d.instance._formulas[0][1]
58
+ assert_equal ['amount', 'price'], d.instance._formulas[0][2]
59
+ assert_equal 1, d.instance._formulas[1][0]
60
+ assert_equal 'cnt', d.instance._formulas[1][1]
61
+ assert_equal ['amount'], d.instance._formulas[1][2]
62
+
63
+ assert_equal 200, d.instance._formulas[0][3].call("10", 20)
64
+ assert_equal 20, d.instance._formulas[1][3].call(20)
65
+ assert_raise(ArgumentError){
66
+ d.instance._formulas[0][3].call(10)
67
+ }
68
+
69
+ # case: use string
70
+ d = create_driver %[
71
+ aggregate all
72
+ formulas cnt = 1
73
+ finalizer name = "muddy" + "dixon" + cnt.to_s
74
+ ]
75
+ assert_equal 0, d.instance._finalizer[0]
76
+ assert_equal ['cnt'], d.instance._finalizer[2]
77
+ assert_equal "muddydixon20", d.instance._finalizer[3].call(20)
78
+ end
79
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-datacalculator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-18 00:00:00.000000000 Z
12
+ date: 2012-06-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fluentd
@@ -61,6 +61,7 @@ files:
61
61
  - lib/fluent/plugin/out_datacalculator.rb
62
62
  - test/helper.rb
63
63
  - test/plugin/test_out_datacalculator.rb
64
+ - test/plugin/test_out_datacalculator_fomulas.rb
64
65
  homepage: https://github.com/muddydixon/fluent-plugin-datacalculator
65
66
  licenses: []
66
67
  post_install_message:
@@ -88,3 +89,4 @@ summary: Output filter plugin to calculate messages that matches specified condi
88
89
  test_files:
89
90
  - test/helper.rb
90
91
  - test/plugin/test_out_datacalculator.rb
92
+ - test/plugin/test_out_datacalculator_fomulas.rb