Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Oct 28, 2020. It is now read-only.

Commit cf55e0f

Browse files
author
Владислав Промзелев
committed
Merge pull request #1 in ~V.PROMZELEV/activerecord-overflow_signalizer from CNAV-6892 to master
* commit 'a08d8eac92be8d0e292385f8b9221f682997ea3b': update README.md do not retrive pk if table is empty specify max values of types as hash fix variable name move predicate to method support custom signalizers use appraisals in travis update .gitignore use appraisals for testing with different versions of activerecord update README.md It will be placed on github.com basic functionality settings for test database actualize naming debugging gems doesnt log anything if all ok
2 parents ace49d2 + a08d8ea commit cf55e0f

File tree

13 files changed

+352
-22
lines changed

13 files changed

+352
-22
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
/pkg/
88
/spec/reports/
99
/tmp/
10+
/gemfiles/*.lock
1011

1112
# rspec failure tracking
1213
.rspec_status

‎.travis.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
sudo: false
22
language: ruby
3+
gemfile:
4+
- gemfiles/ar32.gemfile
5+
- gemfiles/ar42.gemfile
6+
- gemfiles/ar50.gemfile
37
rvm:
48
- 2.3.1
59
before_install: gem install bundler -v 1.14.4

‎Appraisals‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

‎README.md‎

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
# Activerecord::OverflowSignalizer
1+
# ActiveRecord::OverflowSignalizer
22

3-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/activerecord/overflow_signalizer`. To experiment with that code, run `bin/console` for an interactive prompt.
4-
5-
TODO: Delete this and the text above, and describe your gem
3+
One day primary key field will overflow, but if you use this gem, you will know about it before it happened.
64

75
## Installation
86

@@ -22,18 +20,59 @@ Or install it yourself as:
2220

2321
## Usage
2422

25-
TODO: Write usage instructions here
23+
Just placed it somewhere in your app:
24+
```ruby
25+
ActiveRecord::OverflowSignalizer.new.analyse!
26+
```
2627

27-
## Development
28+
By default it check all models in your application and log if some primary key will overflow soon or overflowed.
2829

29-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
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.
3032

31-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
33+
Also you can pass some parameters in initializer:
3234

33-
## Contributing
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
3464

35-
Bug reports and pull requests are welcome on GitHub at https://github.com/v.promzelev/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.
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
3674

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.
3776

3877
## License
3978

‎activerecord-overflow_signalizer.gemspec‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ require 'activerecord/overflow_signalizer/version'
55

66
Gem::Specification.new do |spec|
77
spec.name = 'activerecord-overflow_signalizer'
8-
spec.version = Activerecord::OverflowSignalizer::VERSION
8+
spec.version = ActiveRecord::OverflowSignalizer::VERSION
99
spec.authors = ['v.promzelev']
1010
spec.email = ['v.promzelev@fun-box.ru']
1111

1212
spec.summary = 'Signalize when some primary key overflow soon.'
1313
spec.description = 'Signalize when some primary key overflow soon.'
14-
spec.homepage = "TODO: Put your gem's website or public repo URL here."
14+
spec.homepage = 'https://github.com/funbox/activerecord-overflow_signalizer'
1515
spec.license = 'MIT'
1616

1717
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
@@ -30,7 +30,13 @@ Gem::Specification.new do |spec|
3030
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
3131
spec.require_paths = ['lib']
3232

33+
# spec.add_runtime_dependency 'activerecord', '~> 3.2'
34+
spec.add_runtime_dependency 'pg'
35+
3336
spec.add_development_dependency 'bundler', '~> 1.14'
3437
spec.add_development_dependency 'rake', '~> 10.0'
3538
spec.add_development_dependency 'rspec', '~> 3.0'
39+
spec.add_development_dependency 'appraisal', '~> 2.0'
40+
spec.add_development_dependency 'byebug'
41+
spec.add_development_dependency 'pry'
3642
end

‎gemfiles/ar32.gemfile‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 => "../"

‎gemfiles/ar42.gemfile‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 => "../"

‎gemfiles/ar50.gemfile‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 => "../"
Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,70 @@
11
require 'activerecord/overflow_signalizer/version'
2+
require 'active_record'
23

34
module ActiveRecord
4-
module OverflowSignalizer
5-
# Your code goes here...
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+
16+
MAX_VALUE = {
17+
'integer' => 2_147_483_647,
18+
'bigint' => 9_223_372_036_854_775_807
19+
}.freeze
20+
21+
def initialize(logger: nil, models: nil, days_count: 60, signalizer: nil)
22+
@logger = logger || ActiveRecord::Base.logger
23+
@models = models || ActiveRecord::Base.descendants
24+
@days_count = days_count
25+
@signalizer = signalizer
26+
end
27+
28+
def analyse!
29+
@models.group_by(&:table_name).each do |table, models|
30+
model = models.first
31+
next if model.last.nil?
32+
pk = model.columns.select { |c| c.name == model.primary_key }.first
33+
max = MAX_VALUE.fetch(pk.sql_type) { |type| raise UnsupportedType, type }
34+
if overflow_soon?(max, model)
35+
signalize(table, model.last.public_send(pk.name), max)
36+
end
37+
end
38+
end
39+
40+
private
41+
42+
def overflow_soon?(max, model)
43+
(max - model.last.id) / avg(model) <= @days_count
44+
end
45+
46+
def avg(model)
47+
now = Time.now
48+
week_records = (1..7).map do |t|
49+
from = now - DAY * t
50+
to = from + DAY
51+
model.where(created_at: from..to).count
52+
end
53+
week_records.reduce(:+) / week_records.keep_if { |v| v > 0 }.size
54+
end
55+
56+
def signalize(table, current_value, max_value)
57+
if current_value == max_value
58+
msg = "Primary key in table #{table} overflowed! #{current_value} from #{max_value}"
59+
else
60+
msg = "Primary key in table #{table} will overflow soon! #{current_value} from #{max_value}"
61+
end
62+
if @logger && @logger.respond_to?(:warn)
63+
@logger.warn(msg)
64+
end
65+
if @signalizer && @signalizer.respond_to?(:signalize)
66+
@signalizer.signalize(msg)
67+
end
68+
end
669
end
770
end
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
module Activerecord
2-
module OverflowSignalizer
3-
VERSION = '0.1.0'
1+
module ActiveRecord
2+
class OverflowSignalizer
3+
VERSION = '0.1.0'.freeze
44
end
55
end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /