running_count 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +162 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/running_count/callbacks.rb +35 -0
- data/lib/running_count/counter.rb +98 -0
- data/lib/running_count/format.rb +21 -0
- data/lib/running_count/statement.rb +100 -0
- data/lib/running_count/storage.rb +27 -0
- data/lib/running_count/version.rb +3 -0
- data/lib/running_count.rb +20 -0
- data/running_count.gemspec +49 -0
- metadata +193 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 281c3126d219fffe217bea334f4ee81b040bb875ea1d5b120a5443eff7db48fa
|
4
|
+
data.tar.gz: b236cc36eaec448d824371484cb1c0505d7fc1170324ba105cd04722de566a15
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0ce7b753f6c6d2a2a8b6334bb17aaed7b3856a499716a85a01d4c6af6df24896b043f6af381fc2d314c5c548dc191777ee186cb19fe9d9462e44ee4e4416f725
|
7
|
+
data.tar.gz: afe63389d76ee143a0df841d4c6480e23e3560e913bddf7603c4f6f6555710f61fc62fe4ac993a677c7d2c7e7a5f21a45f210c99cabc17b6550497497fbcf944
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -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 progressions@gmail.com. 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
data/Gemfile.lock
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
running_count (0.1.0)
|
5
|
+
activesupport
|
6
|
+
pg (= 0.20.0)
|
7
|
+
redis-rails
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
actionmailer (4.2.11)
|
13
|
+
actionpack (= 4.2.11)
|
14
|
+
actionview (= 4.2.11)
|
15
|
+
activejob (= 4.2.11)
|
16
|
+
mail (~> 2.5, >= 2.5.4)
|
17
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
18
|
+
actionpack (4.2.11)
|
19
|
+
actionview (= 4.2.11)
|
20
|
+
activesupport (= 4.2.11)
|
21
|
+
rack (~> 1.6)
|
22
|
+
rack-test (~> 0.6.2)
|
23
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
24
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
25
|
+
actionview (4.2.11)
|
26
|
+
activesupport (= 4.2.11)
|
27
|
+
builder (~> 3.1)
|
28
|
+
erubis (~> 2.7.0)
|
29
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
30
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
31
|
+
activejob (4.2.11)
|
32
|
+
activesupport (= 4.2.11)
|
33
|
+
globalid (>= 0.3.0)
|
34
|
+
activemodel (4.2.11)
|
35
|
+
activesupport (= 4.2.11)
|
36
|
+
builder (~> 3.1)
|
37
|
+
activerecord (4.2.11)
|
38
|
+
activemodel (= 4.2.11)
|
39
|
+
activesupport (= 4.2.11)
|
40
|
+
arel (~> 6.0)
|
41
|
+
activesupport (4.2.11)
|
42
|
+
i18n (~> 0.7)
|
43
|
+
minitest (~> 5.1)
|
44
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
45
|
+
tzinfo (~> 1.1)
|
46
|
+
arel (6.0.4)
|
47
|
+
builder (3.2.3)
|
48
|
+
byebug (10.0.2)
|
49
|
+
coderay (1.1.2)
|
50
|
+
concurrent-ruby (1.1.4)
|
51
|
+
crass (1.0.4)
|
52
|
+
database_cleaner (1.7.0)
|
53
|
+
diff-lcs (1.3)
|
54
|
+
erubis (2.7.0)
|
55
|
+
globalid (0.4.2)
|
56
|
+
activesupport (>= 4.2.0)
|
57
|
+
i18n (0.9.5)
|
58
|
+
concurrent-ruby (~> 1.0)
|
59
|
+
loofah (2.2.3)
|
60
|
+
crass (~> 1.0.2)
|
61
|
+
nokogiri (>= 1.5.9)
|
62
|
+
mail (2.7.1)
|
63
|
+
mini_mime (>= 0.1.1)
|
64
|
+
method_source (0.9.2)
|
65
|
+
mini_mime (1.0.1)
|
66
|
+
mini_portile2 (2.4.0)
|
67
|
+
minitest (5.11.3)
|
68
|
+
nokogiri (1.10.1)
|
69
|
+
mini_portile2 (~> 2.4.0)
|
70
|
+
pg (0.20.0)
|
71
|
+
pry (0.12.2)
|
72
|
+
coderay (~> 1.1.0)
|
73
|
+
method_source (~> 0.9.0)
|
74
|
+
pry-byebug (3.6.0)
|
75
|
+
byebug (~> 10.0)
|
76
|
+
pry (~> 0.10)
|
77
|
+
pry-rails (0.3.9)
|
78
|
+
pry (>= 0.10.4)
|
79
|
+
rack (1.6.11)
|
80
|
+
rack-test (0.6.3)
|
81
|
+
rack (>= 1.0)
|
82
|
+
rails (4.2.11)
|
83
|
+
actionmailer (= 4.2.11)
|
84
|
+
actionpack (= 4.2.11)
|
85
|
+
actionview (= 4.2.11)
|
86
|
+
activejob (= 4.2.11)
|
87
|
+
activemodel (= 4.2.11)
|
88
|
+
activerecord (= 4.2.11)
|
89
|
+
activesupport (= 4.2.11)
|
90
|
+
bundler (>= 1.3.0, < 2.0)
|
91
|
+
railties (= 4.2.11)
|
92
|
+
sprockets-rails
|
93
|
+
rails-deprecated_sanitizer (1.0.3)
|
94
|
+
activesupport (>= 4.2.0.alpha)
|
95
|
+
rails-dom-testing (1.0.9)
|
96
|
+
activesupport (>= 4.2.0, < 5.0)
|
97
|
+
nokogiri (~> 1.6)
|
98
|
+
rails-deprecated_sanitizer (>= 1.0.1)
|
99
|
+
rails-html-sanitizer (1.0.4)
|
100
|
+
loofah (~> 2.2, >= 2.2.2)
|
101
|
+
railties (4.2.11)
|
102
|
+
actionpack (= 4.2.11)
|
103
|
+
activesupport (= 4.2.11)
|
104
|
+
rake (>= 0.8.7)
|
105
|
+
thor (>= 0.18.1, < 2.0)
|
106
|
+
rake (10.5.0)
|
107
|
+
redis (4.1.0)
|
108
|
+
redis-actionpack (5.0.2)
|
109
|
+
actionpack (>= 4.0, < 6)
|
110
|
+
redis-rack (>= 1, < 3)
|
111
|
+
redis-store (>= 1.1.0, < 2)
|
112
|
+
redis-activesupport (5.0.7)
|
113
|
+
activesupport (>= 3, < 6)
|
114
|
+
redis-store (>= 1.3, < 2)
|
115
|
+
redis-rack (2.0.5)
|
116
|
+
rack (>= 1.5, < 3)
|
117
|
+
redis-store (>= 1.2, < 2)
|
118
|
+
redis-rails (5.0.2)
|
119
|
+
redis-actionpack (>= 5.0, < 6)
|
120
|
+
redis-activesupport (>= 5.0, < 6)
|
121
|
+
redis-store (>= 1.2, < 2)
|
122
|
+
redis-store (1.6.0)
|
123
|
+
redis (>= 2.2, < 5)
|
124
|
+
rspec (3.8.0)
|
125
|
+
rspec-core (~> 3.8.0)
|
126
|
+
rspec-expectations (~> 3.8.0)
|
127
|
+
rspec-mocks (~> 3.8.0)
|
128
|
+
rspec-core (3.8.0)
|
129
|
+
rspec-support (~> 3.8.0)
|
130
|
+
rspec-expectations (3.8.2)
|
131
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
132
|
+
rspec-support (~> 3.8.0)
|
133
|
+
rspec-mocks (3.8.0)
|
134
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
135
|
+
rspec-support (~> 3.8.0)
|
136
|
+
rspec-support (3.8.0)
|
137
|
+
sprockets (3.7.2)
|
138
|
+
concurrent-ruby (~> 1.0)
|
139
|
+
rack (> 1, < 3)
|
140
|
+
sprockets-rails (3.2.1)
|
141
|
+
actionpack (>= 4.0)
|
142
|
+
activesupport (>= 4.0)
|
143
|
+
sprockets (>= 3.0.0)
|
144
|
+
thor (0.20.3)
|
145
|
+
thread_safe (0.3.6)
|
146
|
+
tzinfo (1.2.5)
|
147
|
+
thread_safe (~> 0.1)
|
148
|
+
|
149
|
+
PLATFORMS
|
150
|
+
ruby
|
151
|
+
|
152
|
+
DEPENDENCIES
|
153
|
+
database_cleaner
|
154
|
+
pry-byebug
|
155
|
+
pry-rails
|
156
|
+
rails (~> 4.0)
|
157
|
+
rake (~> 10.0)
|
158
|
+
rspec (~> 3.0)
|
159
|
+
running_count!
|
160
|
+
|
161
|
+
BUNDLED WITH
|
162
|
+
1.17.3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Isaac Priestley
|
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,43 @@
|
|
1
|
+
# RunningCount
|
2
|
+
|
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/running_count`. 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
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'running_count'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install running_count
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
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
|
+
|
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/progressions/running_count. 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.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the RunningCount project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/progressions/running_count/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "running_count"
|
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,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RunningCount
|
4
|
+
module Callbacks
|
5
|
+
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
def enqueue_count
|
9
|
+
Counter.enqueue_change(self, self.class._counter_data)
|
10
|
+
end
|
11
|
+
|
12
|
+
def enqueue_sum
|
13
|
+
Counter.enqueue_sum(self, self.class._counter_data)
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
|
18
|
+
def keep_running_count(relation, opts = {})
|
19
|
+
Counter.add_callbacks(self, opts)
|
20
|
+
|
21
|
+
@counter_data = Counter.counter_data(self.name, self.table_name, relation, opts)
|
22
|
+
end
|
23
|
+
|
24
|
+
def reconcile_changes
|
25
|
+
Counter.reconcile_changes(self._counter_data)
|
26
|
+
end
|
27
|
+
|
28
|
+
def _counter_data
|
29
|
+
@counter_data
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RunningCount
|
4
|
+
module Counter
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def enqueue_sum(record, counter_data)
|
9
|
+
aggregated_field = counter_data[:aggregated_field]
|
10
|
+
|
11
|
+
return true unless record.previous_changes.has_key?(aggregated_field)
|
12
|
+
|
13
|
+
diff = record.previous_changes[aggregated_field].last.to_i - record.previous_changes[aggregated_field].first.to_i
|
14
|
+
|
15
|
+
if diff != 0
|
16
|
+
Counter.enqueue_change(record, counter_data.merge(amount: diff))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def enqueue_change(record, counter_data)
|
21
|
+
destination = record.send(counter_data[:relation])
|
22
|
+
item = Format.item(destination)
|
23
|
+
|
24
|
+
Storage.add_item(item, counter_data[:running_set_name], counter_data.fetch(:amount, 1))
|
25
|
+
rescue StandardError => e
|
26
|
+
binding.pry
|
27
|
+
end
|
28
|
+
|
29
|
+
def reconcile_changes(counter_data)
|
30
|
+
Statement.prepare_statement(counter_data)
|
31
|
+
|
32
|
+
begin
|
33
|
+
Storage
|
34
|
+
.scores(counter_data[:running_set_name])
|
35
|
+
.each { |item, _score| Statement.reconcile_item(item, counter_data) }
|
36
|
+
ensure
|
37
|
+
Statement.release_statement(counter_data)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def running_count(destination, running_set_name)
|
42
|
+
item = Format.item(destination)
|
43
|
+
|
44
|
+
Storage.scores(running_set_name, item).to_i
|
45
|
+
end
|
46
|
+
|
47
|
+
def counter_data(name, table_name, relation, opts = {})
|
48
|
+
destination_class = destination_class_name(relation, opts)
|
49
|
+
|
50
|
+
counter_column = opts.fetch(:counter_column, "#{name.underscore.pluralize}_count")
|
51
|
+
running_set_name = "running_#{counter_column}"
|
52
|
+
statement = "update_#{counter_column}"
|
53
|
+
|
54
|
+
destination_class.redefine_method(running_set_name) do
|
55
|
+
self.send(counter_column) + Counter.running_count(self, running_set_name)
|
56
|
+
end
|
57
|
+
|
58
|
+
sql = Statement.statement_sql(
|
59
|
+
table_name,
|
60
|
+
statement,
|
61
|
+
destination_class.table_name,
|
62
|
+
counter_column,
|
63
|
+
relation,
|
64
|
+
opts,
|
65
|
+
)
|
66
|
+
|
67
|
+
{
|
68
|
+
counter_column: counter_column,
|
69
|
+
relation: relation,
|
70
|
+
source: opts[:source],
|
71
|
+
destination: table_name,
|
72
|
+
running_set_name: running_set_name,
|
73
|
+
statement: statement,
|
74
|
+
release_sql: Statement.release_sql(statement),
|
75
|
+
aggregated_field: opts[:aggregated_field],
|
76
|
+
statement_sql: sql,
|
77
|
+
if: opts[:if],
|
78
|
+
}
|
79
|
+
end
|
80
|
+
|
81
|
+
def add_callbacks(klass, opts)
|
82
|
+
if opts[:aggregated_field]
|
83
|
+
klass.after_commit :enqueue_sum, on: [:create, :update], if: opts[:if]
|
84
|
+
else
|
85
|
+
klass.after_commit :enqueue_count, on: [:create, :update], if: opts[:if]
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
def destination_class_name(relation, opts)
|
92
|
+
opts[:class_name] ? opts[:class_name].to_s.constantize : relation.to_s.camelcase.constantize
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RunningCount
|
4
|
+
module Statement
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def statement_sql(table_name, statement, destination_table_name, set_name, relation, opts)
|
9
|
+
basic_sql(
|
10
|
+
statement,
|
11
|
+
destination_table_name,
|
12
|
+
set_name,
|
13
|
+
inner_sql(table_name, relation, opts),
|
14
|
+
)
|
15
|
+
end
|
16
|
+
|
17
|
+
def release_sql(name)
|
18
|
+
%( DEALLOCATE #{name} )
|
19
|
+
end
|
20
|
+
|
21
|
+
def prepare_statement(counter_data)
|
22
|
+
ActiveRecord::Base.connection.exec_query(counter_data[:statement_sql])
|
23
|
+
end
|
24
|
+
|
25
|
+
def release_statement(counter_data)
|
26
|
+
ActiveRecord::Base.connection.exec_query(counter_data[:release_sql])
|
27
|
+
end
|
28
|
+
|
29
|
+
def reconcile_item(item, counter_data)
|
30
|
+
Storage.clear_item(item, counter_data[:running_set_name])
|
31
|
+
|
32
|
+
destination_id = Format.parse(item)
|
33
|
+
ActiveRecord::Base.connection.exec_query("EXECUTE #{counter_data[:statement]}(#{destination_id})")
|
34
|
+
rescue StandardError => e
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def extra_sql(opts)
|
40
|
+
if opts[:sql]
|
41
|
+
opts[:sql].map { |sql| "AND #{sql}" }.join(" ")
|
42
|
+
else
|
43
|
+
nil
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def inner_sql(table_name, relation, opts)
|
48
|
+
if opts[:scope]
|
49
|
+
opts[:scope].call
|
50
|
+
elsif opts[:aggregated_field]
|
51
|
+
sum_inner_sql(table_name, relation, opts)
|
52
|
+
else
|
53
|
+
count_inner_sql(table_name, relation, opts)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def sum_inner_sql(table_name, relation, opts)
|
58
|
+
%(
|
59
|
+
SELECT SUM(#{opts[:aggregated_field]}) FROM "#{table_name}"
|
60
|
+
WHERE "#{table_name}"."#{relation}_id" = $1
|
61
|
+
#{extra_sql(opts)}
|
62
|
+
)
|
63
|
+
end
|
64
|
+
|
65
|
+
def count_inner_sql(table_name, relation, opts)
|
66
|
+
%(
|
67
|
+
SELECT COUNT(*) FROM "#{table_name}"
|
68
|
+
WHERE "#{table_name}"."#{relation}_id" = $1
|
69
|
+
#{extra_sql(opts)}
|
70
|
+
)
|
71
|
+
end
|
72
|
+
|
73
|
+
def sum_statement_sql(table_name, statement, destination_table_name, set_name, relation, opts)
|
74
|
+
inner_sql = sum_inner_sql(table_name, relation, opts)
|
75
|
+
|
76
|
+
basic_sql(statement, destination_table_name, set_name, inner_sql)
|
77
|
+
end
|
78
|
+
|
79
|
+
def count_statement_sql(table_name, statement, destination_table_name, set_name, relation, opts)
|
80
|
+
inner_sql = count_inner_sql(table_name, relation, opts)
|
81
|
+
|
82
|
+
basic_sql(statement, destination_table_name, set_name, inner_sql)
|
83
|
+
end
|
84
|
+
|
85
|
+
def basic_sql(statement, destination_table_name, set_name, inner_sql)
|
86
|
+
# source_table is the table where the counted values are coming from
|
87
|
+
# destination_table is the table where the counted values are being saved
|
88
|
+
#
|
89
|
+
%(
|
90
|
+
PREPARE #{statement} (int) AS
|
91
|
+
UPDATE "#{destination_table_name}" SET "#{set_name}" = (
|
92
|
+
#{inner_sql}
|
93
|
+
) WHERE "#{destination_table_name}"."id" = $1
|
94
|
+
)
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RunningCount
|
4
|
+
module Storage
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def scores(running_set_name, item = nil)
|
9
|
+
if item
|
10
|
+
$redis.zscore(running_set_name, item)
|
11
|
+
else
|
12
|
+
$redis.zrange(running_set_name, 0, -1, with_scores: true)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_item(item, running_set_name, amount)
|
17
|
+
$redis.zincrby(running_set_name, amount || 1, item)
|
18
|
+
end
|
19
|
+
|
20
|
+
def clear_item(item, running_set_name)
|
21
|
+
$redis.zrem(running_set_name, item)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/concern'
|
4
|
+
require 'active_support/lazy_load_hooks'
|
5
|
+
require 'redis'
|
6
|
+
|
7
|
+
require "running_count/callbacks"
|
8
|
+
require "running_count/counter"
|
9
|
+
require "running_count/statement"
|
10
|
+
require "running_count/format"
|
11
|
+
require "running_count/storage"
|
12
|
+
|
13
|
+
module RunningCount
|
14
|
+
class Error < StandardError; end
|
15
|
+
end
|
16
|
+
|
17
|
+
# extend ActiveRecord with our own code here
|
18
|
+
ActiveSupport.on_load(:active_record) do
|
19
|
+
include RunningCount::Callbacks
|
20
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "running_count/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "running_count"
|
8
|
+
spec.version = RunningCount::VERSION
|
9
|
+
spec.authors = ["Isaac Priestley"]
|
10
|
+
spec.email = ["isaac@teachable.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Write a short summary, because RubyGems requires one.}
|
13
|
+
spec.description = %q{Write a longer description or delete this line.}
|
14
|
+
spec.homepage = "http://www.teachable.com"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
|
22
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
23
|
+
spec.metadata["source_code_uri"] = "http://github.com/usefedora/running_count"
|
24
|
+
spec.metadata["changelog_uri"] = "http://github.com/usefedora/running_count"
|
25
|
+
else
|
26
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
27
|
+
"public gem pushes."
|
28
|
+
end
|
29
|
+
|
30
|
+
# Specify which files should be added to the gem when it is released.
|
31
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
32
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
33
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
34
|
+
end
|
35
|
+
spec.bindir = "exe"
|
36
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
37
|
+
spec.require_paths = ["lib"]
|
38
|
+
|
39
|
+
spec.add_dependency "activesupport"
|
40
|
+
spec.add_dependency "redis-rails"
|
41
|
+
spec.add_dependency "pg", "0.20.0"
|
42
|
+
|
43
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
44
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
45
|
+
spec.add_development_dependency "rails", "~> 4.0"
|
46
|
+
spec.add_development_dependency "database_cleaner"
|
47
|
+
spec.add_development_dependency "pry-rails"
|
48
|
+
spec.add_development_dependency "pry-byebug"
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,193 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: running_count
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Isaac Priestley
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-01-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
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: redis-rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pg
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.20.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.20.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rails
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '4.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '4.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: database_cleaner
|
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
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: pry-rails
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: pry-byebug
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description: Write a longer description or delete this line.
|
140
|
+
email:
|
141
|
+
- isaac@teachable.com
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- ".gitignore"
|
147
|
+
- ".rspec"
|
148
|
+
- ".travis.yml"
|
149
|
+
- CODE_OF_CONDUCT.md
|
150
|
+
- Gemfile
|
151
|
+
- Gemfile.lock
|
152
|
+
- LICENSE.txt
|
153
|
+
- README.md
|
154
|
+
- Rakefile
|
155
|
+
- bin/console
|
156
|
+
- bin/setup
|
157
|
+
- lib/running_count.rb
|
158
|
+
- lib/running_count/callbacks.rb
|
159
|
+
- lib/running_count/counter.rb
|
160
|
+
- lib/running_count/format.rb
|
161
|
+
- lib/running_count/statement.rb
|
162
|
+
- lib/running_count/storage.rb
|
163
|
+
- lib/running_count/version.rb
|
164
|
+
- running_count.gemspec
|
165
|
+
homepage: http://www.teachable.com
|
166
|
+
licenses:
|
167
|
+
- MIT
|
168
|
+
metadata:
|
169
|
+
allowed_push_host: https://rubygems.org
|
170
|
+
homepage_uri: http://www.teachable.com
|
171
|
+
source_code_uri: http://github.com/usefedora/running_count
|
172
|
+
changelog_uri: http://github.com/usefedora/running_count
|
173
|
+
post_install_message:
|
174
|
+
rdoc_options: []
|
175
|
+
require_paths:
|
176
|
+
- lib
|
177
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
178
|
+
requirements:
|
179
|
+
- - ">="
|
180
|
+
- !ruby/object:Gem::Version
|
181
|
+
version: '0'
|
182
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
183
|
+
requirements:
|
184
|
+
- - ">="
|
185
|
+
- !ruby/object:Gem::Version
|
186
|
+
version: '0'
|
187
|
+
requirements: []
|
188
|
+
rubyforge_project:
|
189
|
+
rubygems_version: 2.7.4
|
190
|
+
signing_key:
|
191
|
+
specification_version: 4
|
192
|
+
summary: Write a short summary, because RubyGems requires one.
|
193
|
+
test_files: []
|