action_counter 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6427c0423a5a137d50a9a404b00cbcfb099df32b
4
+ data.tar.gz: b5149bc02589c3ae6a15c45f34b88d237fe8cbc0
5
+ SHA512:
6
+ metadata.gz: a2062cf1eb162a38b22565c880cdb297fa4cf1807b3d7101e550bc885c00260818cc360f9abfc63955461cbaf29335ba7076db1a2b221f6fec9834e9e8cad99a
7
+ data.tar.gz: 12ffc9947065e2d46aec211e4c4a7e88f1623c11d9e67ba5f3fa3cf82245c9d4b8fca46a044640df87ce5aad26cd9e6239836e3fc7699a03ba358a86ec89734f
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in action_counter.gemspec
6
+ gemspec
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ action_counter (0.1.0)
5
+ activesupport
6
+ redis-objects
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activesupport (5.2.1.1)
12
+ concurrent-ruby (~> 1.0, >= 1.0.2)
13
+ i18n (>= 0.7, < 2)
14
+ minitest (~> 5.1)
15
+ tzinfo (~> 1.1)
16
+ byebug (10.0.2)
17
+ coderay (1.1.2)
18
+ concurrent-ruby (1.1.3)
19
+ diff-lcs (1.3)
20
+ fakeredis (0.7.0)
21
+ redis (>= 3.2, < 5.0)
22
+ i18n (1.1.1)
23
+ concurrent-ruby (~> 1.0)
24
+ method_source (0.9.2)
25
+ minitest (5.11.3)
26
+ pry (0.12.2)
27
+ coderay (~> 1.1.0)
28
+ method_source (~> 0.9.0)
29
+ pry-byebug (3.6.0)
30
+ byebug (~> 10.0)
31
+ pry (~> 0.10)
32
+ rake (10.5.0)
33
+ redis (4.0.3)
34
+ redis-objects (1.4.3)
35
+ redis (~> 4.0)
36
+ rspec (3.8.0)
37
+ rspec-core (~> 3.8.0)
38
+ rspec-expectations (~> 3.8.0)
39
+ rspec-mocks (~> 3.8.0)
40
+ rspec-core (3.8.0)
41
+ rspec-support (~> 3.8.0)
42
+ rspec-expectations (3.8.2)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.8.0)
45
+ rspec-mocks (3.8.0)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.8.0)
48
+ rspec-support (3.8.0)
49
+ thread_safe (0.3.6)
50
+ tzinfo (1.2.5)
51
+ thread_safe (~> 0.1)
52
+
53
+ PLATFORMS
54
+ ruby
55
+
56
+ DEPENDENCIES
57
+ action_counter!
58
+ bundler (~> 1.16)
59
+ fakeredis
60
+ pry-byebug
61
+ rake (~> 10.0)
62
+ rspec (~> 3.0)
63
+
64
+ BUNDLED WITH
65
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Akira Kusumoto
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.
@@ -0,0 +1,49 @@
1
+ # ActionCounter
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'action_counter'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install action_counter
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require 'action_counter'
23
+
24
+ # array = [] # in memory array
25
+ array = Redis::List.new('redis_key')
26
+
27
+ counter = ActionCounter.new(array)
28
+ counter.audit('action1') { sleep 1 }
29
+ counter.audit('action1') { sleep 2 }
30
+ counter.audit('action2') { sleep 1 }
31
+
32
+ counter.results(sort_key: :sum) # [{action_name: action1, count: 2, sum: 3, min: 1, max: 2, avg: 1.5},
33
+ # {action_name: action2, count: 1, sum: 1, min: 1, max: 1, avg: 1}]
34
+ ```
35
+
36
+
37
+ ## Development
38
+
39
+ 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.
40
+
41
+ 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).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bluerabbit/action_counter.
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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,32 @@
1
+
2
+ lib = File.expand_path('lib', __dir__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'action_counter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'action_counter'
8
+ spec.version = ActionCounter::VERSION
9
+ spec.authors = ['Akira Kusumoto']
10
+ spec.email = ['akirakusumo10@gmail.com']
11
+
12
+ spec.summary = 'Action Counter'
13
+ spec.description = 'Action Counter'
14
+ spec.homepage = 'https://github.com/bluerabbit/action_counter'
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 'activesupport'
25
+ spec.add_runtime_dependency 'redis-objects'
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.16'
28
+ spec.add_development_dependency 'fakeredis'
29
+ spec.add_development_dependency 'pry-byebug'
30
+ spec.add_development_dependency 'rake', '~> 10.0'
31
+ spec.add_development_dependency 'rspec', '~> 3.0'
32
+ end
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ require 'bundler/setup'
3
+ require 'action_counter'
4
+
5
+ require 'pry'
6
+ Pry.start
@@ -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,106 @@
1
+ require 'action_counter/version'
2
+
3
+ require 'active_support/all'
4
+ require 'redis-objects'
5
+ require 'action_counter/version'
6
+
7
+ class ActionCounter
8
+ def initialize(list)
9
+ @list = list
10
+ @enable = true
11
+ end
12
+
13
+ def disabled!
14
+ @enable = false
15
+ end
16
+
17
+ def enable!
18
+ @enable = true
19
+ end
20
+
21
+ def audit(action_name)
22
+ audit = build_audit(action_name)
23
+
24
+ if block_given?
25
+ begin
26
+ audit.start
27
+ return yield
28
+ ensure
29
+ audit.stop
30
+ end
31
+ end
32
+ audit
33
+ end
34
+
35
+ def results(sort_key: :sum)
36
+ groups = rows.group_by { |v| v['action_name'] }
37
+
38
+ stats = groups.each.with_object([]) do |(action_name, array), results|
39
+ stat = { action_name: action_name, count: array.size, sum: 0, min: 0, max: 0, avg: 0 }
40
+
41
+ array.each do |hash|
42
+ time = hash['time_sec']
43
+
44
+ stat[:sum] += time
45
+ stat[:min] = time if stat[:min].zero? || stat[:min] > time
46
+
47
+ stat[:max] = time if time > stat[:max]
48
+ end
49
+
50
+ stat[:avg] = stat[:sum] / stat[:count]
51
+ results << stat
52
+ end
53
+
54
+ sort_key = sort_key.to_sym
55
+ stats.sort { |a, b| b[sort_key] - a[sort_key] }
56
+ end
57
+
58
+ def reset
59
+ @list.clear
60
+ end
61
+
62
+ private
63
+
64
+ # ex) [{action_name: 'hoge', time_sec: 1.1}]
65
+ def rows
66
+ @list.map { |json| JSON.parse(json) }
67
+ end
68
+
69
+ def build_audit(action_name)
70
+ if @enable
71
+ ActionCounter::Audit.new(action_name: action_name, list: @list)
72
+ else
73
+ ActionCounter::AuditNullObject.new
74
+ end
75
+ end
76
+
77
+ class Audit
78
+ def initialize(action_name:, list:)
79
+ @action_name = action_name
80
+ @list = list
81
+ end
82
+
83
+ def start
84
+ @start_time = Time.now
85
+ self
86
+ end
87
+
88
+ def stop
89
+ @end_time = Time.now
90
+ time_sec = @end_time - @start_time
91
+ h = { action_name: @action_name, time_sec: time_sec }
92
+ @list << h.to_json
93
+ h
94
+ end
95
+ end
96
+
97
+ class AuditNullObject
98
+ def initialize; end
99
+
100
+ def start
101
+ self
102
+ end
103
+
104
+ def stop; end
105
+ end
106
+ end
@@ -0,0 +1,3 @@
1
+ class ActionCounter
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: action_counter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Akira Kusumoto
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-30 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-objects
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: fakeredis
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ description: Action Counter
112
+ email:
113
+ - akirakusumo10@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - Gemfile.lock
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - action_counter.gemspec
127
+ - bin/console
128
+ - bin/setup
129
+ - lib/action_counter.rb
130
+ - lib/action_counter/version.rb
131
+ homepage: https://github.com/bluerabbit/action_counter
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.6.13
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Action Counter
155
+ test_files: []