activerecord-overflow_signalizer 0.1.1

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: 7ede4a07b9ede07b3890201238b00ca9672ad29b
4
+ data.tar.gz: 40f9de6c873f70b82b76ff4b0bcace62d0617f6e
5
+ SHA512:
6
+ metadata.gz: 6454229cc6a4a9c67e5366a02be8dd06c0c8bbd64cb31ea6a0c5e6b221d184253955e52e98399f4eab56ec9f77d26b11359387279425ef66197274b2f4d54133
7
+ data.tar.gz: adeab12a104c11059bce4d10a249d667c7a1ff61f0db471dc4862f11b02e57a76dcc557e9f756e9e23636624669a58a67ad7191a2f6e12a0c70b9bff5224d03b
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /gemfiles/*.lock
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ sudo: false
2
+ language: ruby
3
+ gemfile:
4
+ - gemfiles/ar32.gemfile
5
+ - gemfiles/ar42.gemfile
6
+ - gemfiles/ar50.gemfile
7
+ rvm:
8
+ - 2.3.1
9
+ before_install: gem install bundler -v 1.14.4
data/Appraisals ADDED
@@ -0,0 +1,11 @@
1
+ appraise 'ar50' do
2
+ gem 'activerecord', '~> 5.0'
3
+ end
4
+
5
+ appraise 'ar42' do
6
+ gem 'activerecord', '~> 4.2'
7
+ end
8
+
9
+ appraise 'ar32' do
10
+ gem 'activerecord', '~> 3.2'
11
+ end
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at v.promzelev@fun-box.ru. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-overflow_signalizer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 v.promzelev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # ActiveRecord::OverflowSignalizer
2
+
3
+ One day primary key field will overflow, but if you use this gem, you will know about it before it happened.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'activerecord-overflow_signalizer'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install activerecord-overflow_signalizer
20
+
21
+ ## Usage
22
+
23
+ Just placed it somewhere in your app:
24
+ ```ruby
25
+ ActiveRecord::OverflowSignalizer.new.analyse!
26
+ ```
27
+
28
+ By default it check all models in your application and log if some primary key will overflow soon or overflowed.
29
+
30
+ You can placed it in some job and perform it by [clockwork](https://github.com/adamwiggins/clockwork)
31
+ or just run it when app started in separated thread.
32
+
33
+ Also you can pass some parameters in initializer:
34
+
35
+ + Specify logger
36
+ ```ruby
37
+ ActiveRecord::OverflowSignalizer.new(logger: SomeCoolLogger)
38
+ ```
39
+ By default ActiveRecord::Base.logger
40
+
41
+ + Specify list of models
42
+ ```ruby
43
+ ActiveRecord::OverflowSignalizer.new(models: [ModelName])
44
+ ```
45
+ By default it retrieve all descendants of ActiveRecord::Base
46
+
47
+ + Specify count of days. Gem start notify you if some primary key will overflow over the next numbers of days.
48
+ ```ruby
49
+ ActiveRecord::OverflowSignalizer.new(days_count: 360)
50
+ ```
51
+ 60 days by default
52
+
53
+ + You can use own signalizer for sending notification to e-mail, slack, hipchat, etc.
54
+ ```ruby
55
+ class MyAwesomeSignalizer
56
+ def initialize(some_params)
57
+ @notifier = SomeChatNotifier.new(some_params)
58
+ end
59
+
60
+ def signalize(msg)
61
+ @notifier.send_msg(msg)
62
+ end
63
+ end
64
+
65
+ ActiveRecord::OverflowSignalizer.new(signalizer: MyAwesomeSignalizer.new(some_params))
66
+ ```
67
+ By default it use only logging
68
+
69
+ ## Development
70
+
71
+ For tests you need postgresql connection specified in `spec/database.yml`.
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/funbox/activerecord-overflow_signalizer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
80
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'activerecord/overflow_signalizer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'activerecord-overflow_signalizer'
8
+ spec.version = ActiveRecord::OverflowSignalizer::VERSION
9
+ spec.authors = ['v.promzelev']
10
+ spec.email = ['v.promzelev@fun-box.ru']
11
+
12
+ spec.summary = 'Signalize when some primary key overflow soon.'
13
+ spec.description = 'Signalize when some primary key overflow soon.'
14
+ spec.homepage = 'https://github.com/funbox/activerecord-overflow_signalizer'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ # spec.add_runtime_dependency 'activerecord', '~> 3.2'
25
+ spec.add_runtime_dependency 'pg'
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.14'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+ spec.add_development_dependency 'rspec', '~> 3.0'
30
+ spec.add_development_dependency 'appraisal', '~> 2.0'
31
+ spec.add_development_dependency 'byebug'
32
+ spec.add_development_dependency 'pry'
33
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "activerecord/overflow_signalizer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 3.2"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 4.2"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 5.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,75 @@
1
+ require 'activerecord/overflow_signalizer/version'
2
+ require 'active_record'
3
+
4
+ module ActiveRecord
5
+ class OverflowSignalizer
6
+ class UnsupportedType < StandardError
7
+ attr_reader :type
8
+
9
+ def initialize(type = nil)
10
+ @type = type
11
+ end
12
+ end
13
+
14
+ DAY = 24 * 60 * 60
15
+ DEFAULT_AVG = 100_000
16
+
17
+ MAX_VALUE = {
18
+ 'integer' => 2_147_483_647,
19
+ 'bigint' => 9_223_372_036_854_775_807
20
+ }.freeze
21
+
22
+ def initialize(logger: nil, models: nil, days_count: 60, signalizer: nil)
23
+ @logger = logger || ActiveRecord::Base.logger
24
+ @models = models || ActiveRecord::Base.descendants
25
+ @days_count = days_count
26
+ @signalizer = signalizer
27
+ end
28
+
29
+ def analyse!
30
+ @models.group_by(&:table_name).each do |table, models|
31
+ model = models.first
32
+ next if model.abstract_class? || model.last.nil?
33
+ pk = model.columns.select { |c| c.name == model.primary_key }.first
34
+ max = MAX_VALUE.fetch(pk.sql_type) { |type| raise UnsupportedType, type }
35
+ if overflow_soon?(max, model)
36
+ signalize(table, model.last.public_send(pk.name), max)
37
+ end
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def overflow_soon?(max, model)
44
+ if model.columns.select { |c| c.name == 'created_at' }.empty?
45
+ (max - model.last.id) / DEFAULT_AVG <= @days_count
46
+ else
47
+ (max - model.last.id) / avg(model) <= @days_count
48
+ end
49
+ end
50
+
51
+ def avg(model)
52
+ now = model.last.created_at
53
+ week_records = (1..7).map do |t|
54
+ from = now - DAY * t
55
+ to = from + DAY
56
+ model.where(created_at: from..to).count
57
+ end
58
+ week_records.reduce(:+) / week_records.keep_if { |v| v > 0 }.size
59
+ end
60
+
61
+ def signalize(table, current_value, max_value)
62
+ if current_value == max_value
63
+ msg = "Primary key in table #{table} overflowed! #{current_value} from #{max_value}"
64
+ else
65
+ msg = "Primary key in table #{table} will overflow soon! #{current_value} from #{max_value}"
66
+ end
67
+ if @logger && @logger.respond_to?(:warn)
68
+ @logger.warn(msg)
69
+ end
70
+ if @signalizer && @signalizer.respond_to?(:signalize)
71
+ @signalizer.signalize(msg)
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ class OverflowSignalizer
3
+ VERSION = '0.1.1'.freeze
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-overflow_signalizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - v.promzelev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pg
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: appraisal
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Signalize when some primary key overflow soon.
112
+ email:
113
+ - v.promzelev@fun-box.ru
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Appraisals
122
+ - CODE_OF_CONDUCT.md
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - activerecord-overflow_signalizer.gemspec
128
+ - bin/console
129
+ - bin/setup
130
+ - gemfiles/ar32.gemfile
131
+ - gemfiles/ar42.gemfile
132
+ - gemfiles/ar50.gemfile
133
+ - lib/activerecord/overflow_signalizer.rb
134
+ - lib/activerecord/overflow_signalizer/version.rb
135
+ homepage: https://github.com/funbox/activerecord-overflow_signalizer
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.5.1
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: Signalize when some primary key overflow soon.
159
+ test_files: []