sidekiq-statistic 1.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sidekiq-statistic might be problematic. Click here for more details.

@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2c4eb622f47e1018cc66fcd471d4b6cff9c2e9c2
4
+ data.tar.gz: 933f7ff73ff792308ca852e0e7d1cdfe8217c706
5
+ SHA512:
6
+ metadata.gz: 5814fd4ba22afb951a089ffa6a8cc560a1792ea63faad2d1b13e09c53bcc63c1b5bacf5d9ef33665eb98d44bfdf66325dff071ff212593aa2e02f658ca49f5ea
7
+ data.tar.gz: c748d18674cbb9c0d95d8ae4bdc92079d5895fa130dc2dd29e2d49f4e7fe6e0ba3a7b0d03ae3caedfed30033a0caa05cf24d4944bfb82363f525d756889310d8
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /dump.rdb
@@ -0,0 +1,17 @@
1
+ language: ruby
2
+ sudo: false
3
+ cache: bundler
4
+ services:
5
+ - redis-server
6
+ rvm:
7
+ - 2.1
8
+ - 2.0.0
9
+ - 2.2
10
+ - jruby-19mode
11
+ - jruby
12
+ - rbx-2
13
+ matrix:
14
+ allow_failures:
15
+ - rvm: rbx-2
16
+ - rvm: jruby
17
+ bundler_args: --without test --jobs 3 --retry 3
@@ -0,0 +1,86 @@
1
+ ## v1.0
2
+ * 19.08.2015: Middleware refactoring (#45)
3
+
4
+ *Mike Perham*
5
+ * 19.08.2015: Use redis lists for save all job runtimes
6
+
7
+ *Anton Davydov*
8
+ * 12.08.2015: Add filters (by worker) for realtime charts
9
+
10
+ *Anton Davydov*
11
+ * 11.08.2015: Realtime chart for each worker and job
12
+
13
+ *Anton Davydov*
14
+ * 31.07.2015: Add JSON API
15
+
16
+ *Anton Davydov*
17
+ * 29.07.2015: Add localizations for plugin
18
+
19
+ *Anton Davydov*
20
+ * 28.07.2015: Read first 1_000 lines from changelog
21
+
22
+ *Anton Davydov*
23
+ * 28.07.2015: Rename plugin to sidekiq-statistic
24
+
25
+ *Anton Davydov*
26
+ * 23.07.2015: Use native redis hash instead json serialization
27
+
28
+ *Anton Davydov*
29
+ * 15.07.2015: Improve integration with active job
30
+
31
+ *Anton Davydov*
32
+ * 01.07.2015: New realisation for thread safe history middleware
33
+
34
+ *Anton Davydov*
35
+ * 13.05.2015: Add ability to change any date range on any history page
36
+
37
+ *Anton Davydov*
38
+ * 12.05.2015: Add last job status data parameter for each worker
39
+
40
+ *Anton Davydov*
41
+ * 11.05.2015: Add page woth worker data table for each day
42
+
43
+ *Anton Davydov*
44
+ * 28.04.2015: Formating worker date in web UI
45
+
46
+ *Anton Davydov*
47
+ * 27.04.2015: Set specific color for any worker
48
+
49
+ *Anton Davydov*
50
+ * 10.04.2015: Add search field on worker page
51
+
52
+ *Anton Davydov*
53
+ * 04.04.2015: Fix livereload button in index page
54
+
55
+ *Anton Davydov*
56
+ * 23.03.2015: Add max runtime column to worker web table
57
+
58
+ *Anton Davydov*
59
+ * 19.03.2015: Add functionality for adding custom css and js files to web page
60
+
61
+ *Anton Davydov*
62
+ * 18.03.2015: Add configuration class with log_file options
63
+
64
+ *Anton Davydov*
65
+ * 16.03.2015: Add worker page where user can see log for this worker
66
+
67
+ *Anton Davydov*
68
+ * 15.03.2015: Add worker statistic table to index history page
69
+
70
+ *Anton Davydov*
71
+ * 08.03.2015: Add charts for each passed and failed jobs for each worker.
72
+
73
+ *Anton Davydov*
74
+ * 08.03.2015: Add Statistic class which provide statistics
75
+ for each day and each worker.
76
+
77
+ Sidekiq::History::Statistic.new(0).workers_hash
78
+ # =>[{"2015-03-07"=>{"HistoryWorker"=>{:failed=>1, :passed=>1}}}]
79
+
80
+ *Anton Davydov*
81
+ * 08.03.2015: Save in redis json with failed and passed jobs for each worker.
82
+
83
+ *Anton Davydov*
84
+ * 04.03.2015: Created simple midelware and static page.
85
+
86
+ *Anton Davydov*
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Anton Davydov
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,134 @@
1
+ # Sidekiq statistic
2
+
3
+ [![Build Status](https://travis-ci.org/davydovanton/sidekiq-statistic.svg)](https://travis-ci.org/davydovanton/sidekiq-statistic) [![Code Climate](https://codeclimate.com/github/davydovanton/sidekiq-history/badges/gpa.svg)](https://codeclimate.com/github/davydovanton/sidekiq-history) [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/davydovanton/sidekiq-history?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
4
+
5
+ Improved display of statistics for your sidekiq workers and jobs.
6
+
7
+ **This gem work only with sidekiq version more than [3.3.4](https://github.com/mperham/sidekiq/releases/tag/v3.3.4)**
8
+
9
+ ## Screenshots
10
+ Also you can check <a href="https://sidekiq-history-gem.herokuapp.com/sidekiq/statistic" target="_blank">heroku application</a> with rails app with this sidekiq plugin
11
+
12
+ ### Index page:
13
+ ![sidekiq-history_index](https://cloud.githubusercontent.com/assets/1147484/8071172/1708e3b0-0f10-11e5-84cf-86a910f5ecc2.png)
14
+
15
+ ### Worker page with table (per day):
16
+ ![sidekiq-history_worker](https://cloud.githubusercontent.com/assets/1147484/8071171/1706924a-0f10-11e5-9ddc-8aeeb7f5c794.png)
17
+
18
+ ### Worker page with log:
19
+ ![screenshot 2015-06-10 01 27 50](https://cloud.githubusercontent.com/assets/1147484/8071166/0edd7688-0f10-11e5-9841-0572ab5704e3.jpg)
20
+
21
+ ## Installation
22
+ Add this line to your application's Gemfile:
23
+
24
+ gem 'sidekiq-statistic', github: 'davydovanton/sidekiq-statistic'
25
+
26
+ And then execute:
27
+
28
+ $ bundle
29
+
30
+ ## Usage
31
+ Open in your browser `<sidekiq-path>/statistic` page.
32
+
33
+ ### Not rails application
34
+ First step: read [sidekiq documentation](https://github.com/mperham/sidekiq/wiki/Monitoring#standalone).
35
+
36
+ Second step: add `require 'sidekiq-statistic'` to you `config.ru`. For example:
37
+ ``` ruby
38
+ # config.ru
39
+ require 'sidekiq/web'
40
+ require 'sidekiq-statistic'
41
+
42
+ use Rack::Session::Cookie, secret: 'some unique secret string here'
43
+ run Sidekiq::Web
44
+ ```
45
+
46
+ ## Configuration
47
+ Sidekiq statistic gem have `log_file` option. This option lets you specify a custom path to sidekiq log file. By default this option equal `log/sidekiq.log`
48
+
49
+ ``` ruby
50
+ Sidekiq::Statistic.configure do |config|
51
+ config.log_file = 'test/helpers/logfile.log'
52
+ end
53
+ ```
54
+
55
+ ## JSON API
56
+ ### /api/statistic.json
57
+ Returns statistic for each worker.
58
+
59
+ Params:
60
+ * `dateFrom` - Date start (format: `yyyy-mm-dd`)
61
+ * `dateTo` - Date end (format: `yyyy-mm-dd`)
62
+
63
+ Example:
64
+ ```
65
+ $ curl http://example.com/sidekiq/api/statistic.json?dateFrom=2015-07-30&dateTo=2015-07-31
66
+
67
+ # =>
68
+ {
69
+ "workers": [
70
+ {
71
+ "name": "Worker",
72
+ "last_job_status": "passed",
73
+ "number_of_calls": {
74
+ "success": 1,
75
+ "failure": 0,
76
+ "total": 1
77
+ },
78
+ "runtime": {
79
+ "last": "2015-07-31 10:42:13 UTC",
80
+ "max": 4.002,
81
+ "min": 4.002,
82
+ "average": 4.002,
83
+ "total": 4.002
84
+ }
85
+ },
86
+
87
+ ...
88
+ ]
89
+ }
90
+ ```
91
+
92
+ ### /api/statistic/:worker_name.json
93
+ Returns worker statistic for each day in range.
94
+
95
+ Params:
96
+ * `dateFrom` - Date start (format: `yyyy-mm-dd`)
97
+ * `dateTo` - Date end (format: `yyyy-mm-dd`)
98
+
99
+ Example:
100
+ ```
101
+ $ curl http://example.com/sidekiq/api/statistic/Worker.json?dateFrom=2015-07-30&dateTo=2015-07-31
102
+
103
+ # =>
104
+ {
105
+ "days": [
106
+ {
107
+ "date": "2015-07-31",
108
+ "failure": 0,
109
+ "success": 1,
110
+ "total": 1,
111
+ "last_job_status": "passed",
112
+ "runtime": {
113
+ "last": null,
114
+ "max": 0,
115
+ "min": 0,
116
+ "average": 0,
117
+ "total": 0
118
+ }
119
+ },
120
+
121
+ ...
122
+ ]
123
+ }
124
+ ```
125
+
126
+ ## How it works
127
+ ![how-it-works](https://cloud.githubusercontent.com/assets/1147484/8802272/fc0a1302-2fc8-11e5-86a5-817409259338.png)
128
+
129
+ ## Contributing
130
+ 1. Fork it ( https://github.com/davydovanton/sidekiq-statistic/fork )
131
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
132
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
133
+ 4. Push to the branch (`git push origin my-new-feature`)
134
+ 5. Create a new Pull Request
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rake/testtask'
4
+
5
+ task default: :test
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.libs << 'lib'
9
+ t.libs << 'test'
10
+ t.test_files = FileList['test/**/*_test.rb']
11
+ t.verbose = true
12
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sidekiq/history"
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
@@ -0,0 +1 @@
1
+ require 'sidekiq/statistic'
@@ -0,0 +1,49 @@
1
+ begin
2
+ require 'sidekiq/web'
3
+ rescue LoadError
4
+ # client-only usage
5
+ end
6
+
7
+ require 'sidekiq/api'
8
+ require 'sidekiq/statistic/configuration'
9
+ require 'sidekiq/statistic/log_parser'
10
+ require 'sidekiq/statistic/middleware'
11
+ require 'sidekiq/statistic/base'
12
+ require 'sidekiq/statistic/statistic/charts'
13
+ require 'sidekiq/statistic/statistic/realtime'
14
+ require 'sidekiq/statistic/statistic/runtime'
15
+ require 'sidekiq/statistic/statistic/workers'
16
+ require 'sidekiq/statistic/version'
17
+ require 'sidekiq/statistic/web_extension'
18
+ require 'sidekiq/statistic/web_api_extension'
19
+ require 'sidekiq/statistic/web_extension_helper'
20
+
21
+ module Sidekiq
22
+ module Statistic
23
+ REDIS_HASH = 'sidekiq:statistic'.freeze
24
+
25
+ class << self
26
+ attr_writer :configuration
27
+ end
28
+
29
+ def self.configuration
30
+ @configuration ||= Configuration.new
31
+ end
32
+
33
+ def self.configure
34
+ yield(configuration)
35
+ end
36
+ end
37
+ end
38
+
39
+ Sidekiq.configure_server do |config|
40
+ config.server_middleware do |chain|
41
+ chain.add Sidekiq::Statistic::Middleware
42
+ end
43
+ end
44
+
45
+ if defined?(Sidekiq::Web)
46
+ Sidekiq::Web.register Sidekiq::Statistic::WebApiExtension
47
+ Sidekiq::Web.register Sidekiq::Statistic::WebExtension
48
+ Sidekiq::Web.tabs['Statistic'] = 'statistic'
49
+ end
@@ -0,0 +1,103 @@
1
+ module Sidekiq
2
+ module Statistic
3
+ class Base
4
+ def initialize(days_previous, start_date = nil)
5
+ @start_date = start_date || Time.now.utc.to_date
6
+ @end_date = @start_date - days_previous
7
+ end
8
+
9
+ def statistic_for(worker)
10
+ statistic_hash.map{ |h| h.values.first[worker] || {} }
11
+ end
12
+
13
+ def worker_names
14
+ @worker_names ||= statistic_hash.flat_map{ |h| h.values.first.keys }.uniq
15
+ end
16
+
17
+ def statistic_hash
18
+ @redis_hash ||= Sidekiq.redis do |conn|
19
+ redis_hash = {}
20
+ get_statistic_hash(conn, redis_hash)
21
+ update_time_values(conn, redis_hash)
22
+ desired_dates.map { |key| result_hash(redis_hash, key) }
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def get_statistic_hash(conn, redis_hash)
29
+ conn
30
+ .hgetall(REDIS_HASH)
31
+ .each do |keys, value|
32
+ *keys, last = keys.split(':'.freeze)
33
+ keys.inject(redis_hash, &key_or_empty_hash)[last.to_sym] = to_number(value)
34
+ end
35
+ end
36
+
37
+ def key_or_empty_hash
38
+ ->(h, k) { h[k] || h[k] = {} }
39
+ end
40
+
41
+ def desired_dates
42
+ (@end_date..@start_date).map(&:to_s)
43
+ end
44
+
45
+ def result_hash(redis_hash, key)
46
+ redis_hash.fetch(key, {}).each { |_, v| update_hash_statments v }
47
+ { key => (redis_hash[key] || {}) }
48
+ end
49
+
50
+ def update_hash_statments(hash)
51
+ hash[:passed] ||= 0
52
+ hash[:failed] ||= 0
53
+ end
54
+
55
+ def to_number(value)
56
+ case value
57
+ when /\A[\d.]+\z/ then value.to_f
58
+ when /\A\d+\z/ then value.to_i
59
+ else value
60
+ end
61
+ end
62
+
63
+ def update_time_values(conn, redis_hash)
64
+ redis_hash.each do |time, workers|
65
+ workers.each do |worker, _|
66
+ worker_key = "#{time}:#{worker}"
67
+
68
+ timeslist, _ = conn.multi do |multi|
69
+ multi.lrange("#{worker_key}:timeslist", 0, -1)
70
+ multi.del("#{worker_key}:timeslist")
71
+ end
72
+
73
+ timeslist.map!(&:to_f)
74
+ redis_hash[time][worker].merge! time_hash(timeslist, worker_key)
75
+ end
76
+ end
77
+ end
78
+
79
+ def time_hash(timeslist, worker_key)
80
+ return {} if timeslist.empty?
81
+ statistics = time_statistics(timeslist)
82
+
83
+ Sidekiq.redis do |redis|
84
+ redis.hmset REDIS_HASH,
85
+ statistics.flat_map{ |(k, v)| ["#{worker_key}:#{k}", v] }
86
+ end
87
+
88
+ statistics
89
+ end
90
+
91
+ def time_statistics(timeslist)
92
+ total = timeslist.inject(:+)
93
+
94
+ {
95
+ average_time: total / timeslist.count,
96
+ min_time: timeslist.min,
97
+ max_time: timeslist.max,
98
+ total_time: total
99
+ }
100
+ end
101
+ end
102
+ end
103
+ end