logstash-filter-alkivi_prices 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9ca68756a27ad08268cd1f4aeab58719c9138c4b1baae0a064d9cb62c42a2103
4
+ data.tar.gz: 3c2baed431de0cb52cc4c24c6c537f843beb4e0a8174a6e2cfd0401277981246
5
+ SHA512:
6
+ metadata.gz: '092963c69937664debf40e67b80e6dcae861ca218c3696c1503f469c5e2a33e495273b1ba5101d0d3f7f45bfe35803e4737dbab6b7b99196a5cc49ed5e3ebf49'
7
+ data.tar.gz: 1bfb5ddcfe84f6dc100d529810c5213f5312751aa7c7877c6bfac107d186df7e558d5d9d9665906d4b5569c9b0ab7c640423e6f14221794872ea1ac0c2b3fac2
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ## 0.1.0
2
+ - Plugin created with the logstash plugin generator
data/CONTRIBUTORS ADDED
@@ -0,0 +1,10 @@
1
+ The following is a list of people who have contributed ideas, code, bug
2
+ reports, or in general have helped logstash along its way.
3
+
4
+ Contributors:
5
+ * Alkivi - admin@alkivi.fr
6
+
7
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
8
+ Logstash, and you aren't on the list above and want to be, please let us know
9
+ and we'll make sure you're here. Contributions from folks like you are what make
10
+ open source awesome.
data/DEVELOPER.md ADDED
@@ -0,0 +1,2 @@
1
+ # logstash-filter-phonenumber
2
+ Example filter plugin. This should help bootstrap your effort to write your own filter plugin!
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
data/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ Licensed under the Apache License, Version 2.0 (the "License");
2
+ you may not use this file except in compliance with the License.
3
+ You may obtain a copy of the License at
4
+
5
+ http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software
8
+ distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ See the License for the specific language governing permissions and
11
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Logstash Plugin
2
+
3
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
4
+
5
+ It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way.
6
+
7
+ ## Documentation
8
+
9
+ Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elastic.co/guide/en/logstash/current/).
10
+
11
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
12
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
13
+
14
+ ## Need Help?
15
+
16
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
17
+
18
+ ## Developing
19
+
20
+ ### 1. Plugin Developement and Testing
21
+
22
+ #### Code
23
+ - To get started, you'll need JRuby with the Bundler gem installed.
24
+
25
+ - Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization. We also provide [example plugins](https://github.com/logstash-plugins?query=example).
26
+
27
+ - Install dependencies
28
+ ```sh
29
+ bundle install
30
+ ```
31
+
32
+ #### Test
33
+
34
+ - Update your dependencies
35
+
36
+ ```sh
37
+ bundle install
38
+ ```
39
+
40
+ - Run tests
41
+
42
+ ```sh
43
+ bundle exec rspec
44
+ ```
45
+
46
+ ### 2. Running your unpublished Plugin in Logstash
47
+
48
+ #### 2.1 Run in a local Logstash clone
49
+
50
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
51
+ ```ruby
52
+ gem "logstash-filter-phonenumber", :path => "/your/local/logstash-filter-phonenumber"
53
+ ```
54
+ - Install plugin
55
+ ```sh
56
+ bin/logstash-plugin install --no-verify
57
+ ```
58
+ - Run Logstash with your plugin
59
+ ```sh
60
+ bin/logstash -e 'filter {phonenumber {}}'
61
+ ```
62
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
63
+
64
+ #### 2.2 Run in an installed Logstash
65
+
66
+ You can use the same **2.1** method to run your plugin in an installed Logstash by editing its `Gemfile` and pointing the `:path` to your local plugin development directory or you can build the gem and install it using:
67
+
68
+ - Build your plugin gem
69
+ ```sh
70
+ gem build logstash-filter-phonenumber.gemspec
71
+ ```
72
+ - Install the plugin from the Logstash home
73
+ ```sh
74
+ bin/logstash-plugin install /your/local/plugin/logstash-filter-phonenumber.gem
75
+ ```
76
+ - Start Logstash and proceed to test the plugin
77
+
78
+ ## Contributing
79
+
80
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
81
+
82
+ Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here.
83
+
84
+ It is more important to the community that you are able to contribute.
85
+
86
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,108 @@
1
+ # encoding: utf-8
2
+ require "logstash/filters/base"
3
+ require 'csv'
4
+
5
+ # This filter will replace the contents of the default
6
+ # message field with whatever you specify in the configuration.
7
+ #
8
+ # It is only intended to be used as an .
9
+ class LogStash::Filters::AlkiviPrices < LogStash::Filters::Base
10
+
11
+ # Setting the config_name here is required. This is how you
12
+ # configure this filter from your Logstash config.
13
+ #
14
+ # filter {
15
+ # {
16
+ # message => "My message..."
17
+ # }
18
+ # }
19
+ #
20
+ config_name "alkivi_prices"
21
+
22
+ default :codec, "plain"
23
+
24
+ # Replace the message with this value.
25
+ config :source_country, :validate => :string
26
+ config :source_type, :validate => :string
27
+
28
+ public
29
+ def register
30
+ costs_file = '/etc/logstash/carriersip_costs.csv'
31
+ if ENV.key?('ALKIVI_COSTS_FILE')
32
+ costs_file = ENV["ALKIVI_COSTS_FILE"]
33
+ end
34
+
35
+ prices_file = '/etc/logstash/carriersip_prices.csv'
36
+ if ENV.key?('ALKIVI_PRICES_FILE')
37
+ prices_file = ENV["ALKIVI_PRICES_FILE"]
38
+ end
39
+
40
+ costs_data = CSV.parse(File.read(costs_file), headers: true)
41
+ @costs_data = {}
42
+ costs_data.each do |i|
43
+ @costs_data[i["code"]] = i
44
+ end
45
+
46
+ prices_data = CSV.parse(File.read(prices_file), headers: true)
47
+ @prices_data = {}
48
+ prices_data.each do |i|
49
+ @prices_data[i["code"]] = i
50
+ end
51
+
52
+ end # def register
53
+
54
+ public
55
+ def filter(event)
56
+
57
+ source_country = event.get(@source_country)
58
+ source_type = event.get(@source_type)
59
+ billsec = event.get("billsec")
60
+
61
+ wanted_key = "1"
62
+ if source_type == "Mobile"
63
+ wanted_key = "2"
64
+ end
65
+
66
+ csv_code = source_country + "-" + wanted_key
67
+
68
+ costs_line = nil
69
+ if @costs_data.key?(csv_code)
70
+ costs_line = @costs_data[csv_code]
71
+ end
72
+
73
+ prices_line = nil
74
+ if @prices_data.key?(csv_code)
75
+ prices_line = @prices_data[csv_code]
76
+ end
77
+
78
+ cost0 = 0.0
79
+ cost1 = 0.0
80
+ cost2 = 0.0
81
+ price0 = 0.0
82
+ price1 = 0.0
83
+ price2 = 0.0
84
+
85
+ if billsec
86
+ if costs_line
87
+ cost0 = billsec.to_f / 60 * costs_line["prix0"].to_f
88
+ cost1 = billsec.to_f / 60 * costs_line["prix1"].to_f
89
+ cost2 = billsec.to_f / 60 * costs_line["prix2"].to_f
90
+ end
91
+ if prices_line
92
+ price0 = billsec.to_f / 60 * prices_line["prix0"].to_f
93
+ price1 = billsec.to_f / 60 * prices_line["prix1"].to_f
94
+ price2 = billsec.to_f / 60 * prices_line["prix2"].to_f
95
+ end
96
+ end
97
+
98
+ event.set("alkivi_cost_0", cost0)
99
+ event.set("alkivi_cost_1", cost1)
100
+ event.set("alkivi_cost_2", cost2)
101
+ event.set("customer_cost_0", price0)
102
+ event.set("customer_cost_1", price1)
103
+ event.set("customer_cost_2", price2)
104
+
105
+ # filter_matched should go in the last line of our successful code
106
+ filter_matched(event)
107
+ end # def filter
108
+ end # class LogStash::Filters::AlkiviPrices
@@ -0,0 +1,22 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-filter-alkivi_prices'
3
+ s.version = '1.0.0'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = 'Parse CRD to add cost and prices.'
6
+ s.homepage = 'https://github.com/alkivi-sas/logstash-filter-alkivi_prices'
7
+ s.authors = ['Alkivi']
8
+ s.email = 'anthony@alkivi.fr'
9
+ s.require_paths = ['lib']
10
+
11
+ # Files
12
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
13
+ # Tests
14
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
15
+
16
+ # Special flag to let us know this is actually a logstash plugin
17
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }
18
+
19
+ # Gem dependencies
20
+ s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.0"
21
+ s.add_development_dependency 'logstash-devutils'
22
+ end
@@ -0,0 +1,97 @@
1
+ # encoding: utf-8
2
+ require_relative '../spec_helper'
3
+ require "logstash/filters/alkivi_prices"
4
+
5
+ describe LogStash::Filters::AlkiviPrices do
6
+ describe "French Mobile" do
7
+ let(:config) do <<-CONFIG
8
+ filter {
9
+ alkivi_prices {
10
+ source_country => 'destination_number_country'
11
+ source_type => 'destination_number_type'
12
+ }
13
+ }
14
+ CONFIG
15
+ end
16
+
17
+ sample({"destination_number_country" => "FR", "destination_number_type" => "Mobile", "billsec" => "60"}) do
18
+ expect(subject).to include("alkivi_cost_0")
19
+ expect(subject.get('alkivi_cost_0')).to eq(10.0)
20
+
21
+ expect(subject).to include("alkivi_cost_1")
22
+ expect(subject.get('alkivi_cost_1')).to eq(20.0)
23
+
24
+ expect(subject).to include("alkivi_cost_2")
25
+ expect(subject.get('alkivi_cost_2')).to eq(30.0)
26
+
27
+ expect(subject).to include("customer_cost_0")
28
+ expect(subject.get('customer_cost_0')).to eq(10.0)
29
+
30
+ expect(subject).to include("customer_cost_1")
31
+ expect(subject.get('customer_cost_1')).to eq(20.0)
32
+
33
+ expect(subject).to include("customer_cost_2")
34
+ expect(subject.get('customer_cost_2')).to eq(30.0)
35
+ end
36
+
37
+ sample({"destination_number_country" => "FR", "destination_number_type" => "LandLine", "billsec" => "60"}) do
38
+ expect(subject).to include("alkivi_cost_0")
39
+ expect(subject.get('alkivi_cost_0')).to eq(1.0)
40
+
41
+ expect(subject).to include("alkivi_cost_1")
42
+ expect(subject.get('alkivi_cost_1')).to eq(2.0)
43
+
44
+ expect(subject).to include("alkivi_cost_2")
45
+ expect(subject.get('alkivi_cost_2')).to eq(3.0)
46
+
47
+ expect(subject).to include("customer_cost_0")
48
+ expect(subject.get('customer_cost_0')).to eq(1.0)
49
+
50
+ expect(subject).to include("customer_cost_1")
51
+ expect(subject.get('customer_cost_1')).to eq(2.0)
52
+
53
+ expect(subject).to include("customer_cost_2")
54
+ expect(subject.get('customer_cost_2')).to eq(3.0)
55
+ end
56
+
57
+ sample({"destination_number_country" => "Fake", "destination_number_type" => "LandLine", "billsec" => "60"}) do
58
+ expect(subject).to include("alkivi_cost_0")
59
+ expect(subject.get('alkivi_cost_0')).to eq(0.0)
60
+
61
+ expect(subject).to include("alkivi_cost_1")
62
+ expect(subject.get('alkivi_cost_1')).to eq(0.0)
63
+
64
+ expect(subject).to include("alkivi_cost_2")
65
+ expect(subject.get('alkivi_cost_2')).to eq(0.0)
66
+
67
+ expect(subject).to include("customer_cost_0")
68
+ expect(subject.get('customer_cost_0')).to eq(0.0)
69
+
70
+ expect(subject).to include("customer_cost_1")
71
+ expect(subject.get('customer_cost_1')).to eq(0.0)
72
+
73
+ expect(subject).to include("customer_cost_2")
74
+ expect(subject.get('customer_cost_2')).to eq(0.0)
75
+ end
76
+
77
+ sample({"destination_number_country" => "FR", "destination_number_type" => "LandLine", "billsec" => ""}) do
78
+ expect(subject).to include("alkivi_cost_0")
79
+ expect(subject.get('alkivi_cost_0')).to eq(0.0)
80
+
81
+ expect(subject).to include("alkivi_cost_1")
82
+ expect(subject.get('alkivi_cost_1')).to eq(0.0)
83
+
84
+ expect(subject).to include("alkivi_cost_2")
85
+ expect(subject.get('alkivi_cost_2')).to eq(0.0)
86
+
87
+ expect(subject).to include("customer_cost_0")
88
+ expect(subject.get('customer_cost_0')).to eq(0.0)
89
+
90
+ expect(subject).to include("customer_cost_1")
91
+ expect(subject.get('customer_cost_1')).to eq(0.0)
92
+
93
+ expect(subject).to include("customer_cost_2")
94
+ expect(subject.get('customer_cost_2')).to eq(0.0)
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,18 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ require "logstash/devutils/rspec/spec_helper"
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-alkivi_prices
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alkivi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-01-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.0'
19
+ name: logstash-core-plugin-api
20
+ prerelease: false
21
+ type: :runtime
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ name: logstash-devutils
34
+ prerelease: false
35
+ type: :development
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email: anthony@alkivi.fr
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - CHANGELOG.md
48
+ - CONTRIBUTORS
49
+ - DEVELOPER.md
50
+ - Gemfile
51
+ - LICENSE
52
+ - README.md
53
+ - lib/logstash/filters/alkivi_prices.rb
54
+ - logstash-filter-alkivi_prices.gemspec
55
+ - spec/filters/alkivi_prices_spec.rb
56
+ - spec/spec_helper.rb
57
+ homepage: https://github.com/alkivi-sas/logstash-filter-alkivi_prices
58
+ licenses:
59
+ - Apache-2.0
60
+ metadata:
61
+ logstash_plugin: 'true'
62
+ logstash_group: filter
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.7.10
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Parse CRD to add cost and prices.
83
+ test_files:
84
+ - spec/filters/alkivi_prices_spec.rb
85
+ - spec/spec_helper.rb