fluent-plugin-simplearithmetic 0.0.1.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f7a35e0538f55c58d65296c4c8338ecf00a9667b
4
+ data.tar.gz: dbcec7416b39c15cc4a0f17bb6a8b923dae573fd
5
+ SHA512:
6
+ metadata.gz: 285494a14b1989efb7521c7aecc4e307fb1d0c75eeb88f3325bcd70ea6d62ad2bc4a88e8ff736fd946f78c5a7b5f4db22e654d44348a9838868d084e0d32eadb
7
+ data.tar.gz: 93a694065302c235b457bc01ccf629145b4ffec7a414a0c890279690667d606d78ff2090fc84c856e28630a755097048afc160016d38343b82ccdae102badbbc
data/.gitignore ADDED
@@ -0,0 +1,26 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ *~
24
+ \#*
25
+ .\#*
26
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fluent-plugin-simplearithmetic.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2014- Takahiro Kamatani
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ fluent-plugin-simplearithmetic
2
+ ================================
3
+ This fluentd output plugin helps you to calculate messages.
4
+
5
+ This plugin is based on [fluent-plugin-datacalculator](https://github.com/muddydixon/fluent-plugin-datacalculator) written by Muddy Dixon. This plugin doesn't have a summarize function which provided by fluent-plugin-datacalculator.
6
+
7
+
8
+ ## Installaion
9
+
10
+ ```
11
+ $ fluent-gem install fluent-plugin-simplearithmetic
12
+ ```
13
+
14
+ ## Tutorial
15
+
16
+ Suppose you have a message like:
17
+
18
+ ```
19
+ {
20
+ 'apple': 7,
21
+ 'orange': 3,
22
+ 'time_start': '2001-02-03T04:05:06Z',
23
+ 'time_finish': '2001-02-03T04:06:12Z',
24
+ }
25
+ ```
26
+
27
+ Now you can calculate with this `td-agent.conf`:
28
+
29
+ ```
30
+ <match arithmetic.test>
31
+ type simple_arithmetic
32
+ tag calculated.test
33
+
34
+ <formulas>
35
+ total_price apple * 200 - orange * 100
36
+
37
+ # Calculation order is from up to down.
38
+ budget 2000 - total_price
39
+
40
+ # You can also use Time.iso8601
41
+ time_elapsed Time.iso8601(time_finish) - Time.iso8601(time_start)
42
+ </formulas>
43
+ </match>
44
+
45
+ <match calculated.test>
46
+ type stdout
47
+ </match>
48
+ ```
49
+
50
+ Calculated results will be:
51
+
52
+ ```
53
+ {
54
+ "apple": 7,
55
+ "orange": 3,
56
+ "time_start": "2001-02-03T04:05:06Z",
57
+ "time_finish": "2001-02-03T04:06:12Z",
58
+ "total_price": 1100,
59
+ "budget": 900,
60
+ "time_elapsed": 66.0
61
+ }
62
+ ```
63
+
64
+
65
+ ## Configuration
66
+
67
+
68
+ ### undefined_variables
69
+ 1. `nil`
70
+
71
+ 2. `undefined` (default)
72
+
73
+ ### how_to_process_error
74
+ 1. `nil`
75
+
76
+ 2. `undefined`
77
+
78
+ 3. `error_string` (default)
79
+
80
+
81
+ ### tag
82
+ The tag prefix for emitted event messages. Default is `simple_arithmetic`.
83
+
84
+
85
+ ## Copyright
86
+
87
+ Copyright:: Copyright (c) 2014- Takahiro Kamatani
88
+
89
+ License:: Apache License, Version 2.0
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib' << 'test'
6
+ test.pattern = 'test/**/test_*.rb'
7
+ test.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "fluent-plugin-simplearithmetic"
6
+ spec.version = "0.0.1.pre"
7
+ spec.authors = ["Takahiro Kamatani"]
8
+ spec.email = ["buhii314@gmail.com"]
9
+ spec.description = %q{Fluent plugin to calculate messages.}
10
+ spec.summary = spec.description
11
+ spec.homepage = "https://github.com/buhii/fluent-plugin-simplearithmetic"
12
+ spec.license = "Apache License, Version 2.0"
13
+ spec.has_rdoc = false
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "fluentd", "~> 0.10.0"
21
+ spec.add_development_dependency "rake"
22
+ end
@@ -0,0 +1,113 @@
1
+ module Fluent
2
+
3
+ class SimpleArithmeticOutput < Output
4
+ Fluent::Plugin.register_output('simple_arithmetic', self)
5
+
6
+ config_param :tag, :string, :default => "simple_arithmetic"
7
+
8
+ # 'nil', 'undefined'
9
+ config_param :undefined_variables, :string, :default => 'undefined'
10
+
11
+ # 'nil', 'undefined', 'error_string'
12
+ config_param :how_to_process_error, :string, :default => 'error_string'
13
+
14
+ attr_accessor :_formulas
15
+
16
+ def initialize
17
+ super
18
+ require 'time'
19
+ end
20
+
21
+ def start
22
+ super
23
+ end
24
+
25
+ def shutdown
26
+ super
27
+ end
28
+
29
+ def configure(conf)
30
+ super
31
+
32
+ # Check configuration
33
+ {'undefined_variables' => %w{nil undefined},
34
+ 'how_to_process_error' => %w{nil undefined error_string}}.each_pair{|attr, choices|
35
+ param = instance_variable_get('@' + attr)
36
+ if not choices.include? param
37
+ raise Fluent::ConfigError, \
38
+ "Invalid setting at #{attr}: `#{param}`. Choices: %s" % choices.join(', ')
39
+ end
40
+ }
41
+
42
+ # Create functions
43
+ @_formulas = []
44
+
45
+ def create_func(var, expr)
46
+ begin
47
+ f_argv = expr.scan(/[a-zA-Z][\w\d\.]*/).uniq.select{|x| not x.start_with?('Time.iso8601')}
48
+ f = eval('lambda {|' + f_argv.join(',') + '| ' + expr + '}')
49
+ return [f, f_argv]
50
+ rescue SyntaxError
51
+ raise Fluent::ConfigError, "SyntaxError at formula `#{var}`: #{expr}"
52
+ end
53
+ end
54
+
55
+ conf.elements.select { |element|
56
+ element.name == 'formulas'
57
+ }.each { |element|
58
+ element.each_pair { |var, expr|
59
+ element.has_key?(var) # to suppress unread configuration warning
60
+ formula, f_argv = create_func(var, expr)
61
+ @_formulas.push [var, f_argv, formula]
62
+ }
63
+ }
64
+ end
65
+
66
+ def has_all_keys?(record, argv)
67
+ argv.each {|var|
68
+ if not record.has_key?(var)
69
+ return false
70
+ end
71
+ }
72
+ true
73
+ end
74
+
75
+ def exec_func(record, f_argv, formula)
76
+ argv = []
77
+ f_argv.each {|v|
78
+ argv.push(record[v])
79
+ }
80
+ return formula.call(*argv)
81
+ end
82
+
83
+ def calculate(record)
84
+ @_formulas.each {|var, f_argv, formula|
85
+ if not has_all_keys?(record, f_argv)
86
+ if @undefined_variables == 'nil'
87
+ record[var] = nil
88
+ end
89
+ next
90
+ end
91
+
92
+ begin
93
+ record[var] = exec_func(record, f_argv, formula)
94
+ rescue StandardError => error
95
+ case @how_to_process_error
96
+ when 'error_string'
97
+ record[var] = error.to_s
98
+ when 'nil'
99
+ record[var] = nil
100
+ end
101
+ end
102
+ }
103
+ record
104
+ end
105
+
106
+ def emit(tag, es, chain)
107
+ chain.next
108
+ es.each { |time, record|
109
+ Fluent::Engine.emit(@tag, time, calculate(record))
110
+ }
111
+ end
112
+ end
113
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+
12
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
13
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
14
+ require 'fluent/test'
15
+ unless ENV.has_key?('VERBOSE')
16
+ nulllogger = Object.new
17
+ nulllogger.instance_eval {|obj|
18
+ def method_missing(method, *args)
19
+ # pass
20
+ end
21
+ }
22
+ $log = nulllogger
23
+ end
24
+
25
+ require 'fluent/plugin/out_simplearithmetic'
26
+
27
+ class Test::Unit::TestCase
28
+ end
@@ -0,0 +1,60 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ class SimpleArithmeticOutputTest < Test::Unit::TestCase
5
+ def setup
6
+ Fluent::Test.setup
7
+ end
8
+
9
+ CONFIG = %[
10
+ type simple_arithmetic
11
+ tag calculated.test
12
+ undefined_variables nil # nil, undefined
13
+ how_to_process_error error_string # nil, undefined, error_string
14
+
15
+ <formulas>
16
+ x3 x1 * 100 - x2
17
+ var1 Time.iso8601(t1) - Time.iso8601(t2)
18
+ var2 x3 - var1
19
+ </formulas>
20
+ ]
21
+
22
+ def create_driver(conf = CONFIG, tag='test.input')
23
+ Fluent::Test::OutputTestDriver.new(Fluent::SimpleArithmeticOutput, tag).configure(conf)
24
+ end
25
+
26
+ def test_configure
27
+ assert_raise(Fluent::ConfigError) {
28
+ d = create_driver('')
29
+ }
30
+ # no variables for calculation
31
+ assert_raise(Fluent::ConfigError) {
32
+ d = create_driver %[
33
+ <formulas>
34
+
35
+ </formulas>
36
+ ]
37
+ }
38
+ # Syntax Error
39
+ assert_raise(Fluent::ConfigError) {
40
+ d = create_driver %[
41
+ <formulas>
42
+ var_undefined
43
+ </formulas>
44
+ ]
45
+ }
46
+ d = create_driver %[
47
+ <formulas>
48
+ var1 var2 * var3
49
+ </formulas>
50
+ ]
51
+ end
52
+
53
+ def test_create_formula
54
+ d = create_driver
55
+ end
56
+
57
+ def test_write
58
+ d = create_driver
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-simplearithmetic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.pre
5
+ platform: ruby
6
+ authors:
7
+ - Takahiro Kamatani
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fluentd
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.10.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.10.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Fluent plugin to calculate messages.
42
+ email:
43
+ - buhii314@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - fluent-plugin-simplearithmetic.gemspec
54
+ - lib/fluent/plugin/out_simple_arithmetic.rb
55
+ - test/helper.rb
56
+ - test/plugin/test_out_simplearithmetic.rb
57
+ homepage: https://github.com/buhii/fluent-plugin-simplearithmetic
58
+ licenses:
59
+ - Apache License, Version 2.0
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">"
73
+ - !ruby/object:Gem::Version
74
+ version: 1.3.1
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.2.2
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Fluent plugin to calculate messages.
81
+ test_files:
82
+ - test/helper.rb
83
+ - test/plugin/test_out_simplearithmetic.rb