dashing-contrib 0.0.5 → 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.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/README.md +88 -32
  4. data/dashing-contrib.gemspec +3 -1
  5. data/lib/dashing-contrib.rb +11 -1
  6. data/lib/dashing-contrib/assets/widgets.scss +31 -1
  7. data/lib/dashing-contrib/assets/widgets/dashing_state/dashing_state.coffee +11 -0
  8. data/lib/dashing-contrib/assets/widgets/dashing_state/dashing_state.html +24 -0
  9. data/lib/dashing-contrib/assets/widgets/dashing_state/dashing_state.scss +48 -0
  10. data/lib/dashing-contrib/assets/widgets/kue_status/kue_status.html +10 -8
  11. data/lib/dashing-contrib/assets/widgets/kue_status/kue_status.scss +10 -8
  12. data/lib/dashing-contrib/assets/widgets/nagios_list/nagios_list.coffee +49 -0
  13. data/lib/dashing-contrib/assets/widgets/nagios_list/nagios_list.html +37 -0
  14. data/lib/dashing-contrib/assets/widgets/nagios_list/nagios_list.scss +61 -0
  15. data/lib/dashing-contrib/assets/widgets/pingdom_uptime/pingdom_uptime.html +33 -33
  16. data/lib/dashing-contrib/assets/widgets/pingdom_uptime/pingdom_uptime.scss +1 -1
  17. data/lib/dashing-contrib/assets/widgets/sidekiq/README.md +12 -18
  18. data/lib/dashing-contrib/assets/widgets/sidekiq/sidekiq.html +10 -8
  19. data/lib/dashing-contrib/assets/widgets/sidekiq/sidekiq.scss +10 -45
  20. data/lib/dashing-contrib/bottles/dashing.rb +40 -0
  21. data/lib/dashing-contrib/bottles/nagios.rb +1 -0
  22. data/lib/dashing-contrib/bottles/nagios/client.rb +4 -0
  23. data/lib/dashing-contrib/bottles/nagios/status.rb +29 -0
  24. data/lib/dashing-contrib/bottles/pingdom/checks.rb +1 -1
  25. data/lib/dashing-contrib/jobs/dashing-state.rb +19 -0
  26. data/lib/dashing-contrib/jobs/kue.rb +33 -0
  27. data/lib/dashing-contrib/jobs/nagios_list.rb +34 -0
  28. data/lib/dashing-contrib/jobs/pingdom_uptime.rb +58 -0
  29. data/lib/dashing-contrib/jobs/sidekiq.rb +32 -0
  30. data/lib/dashing-contrib/routes.rb +7 -0
  31. data/lib/dashing-contrib/runnable_job.rb +88 -0
  32. data/lib/dashing-contrib/version.rb +1 -1
  33. data/spec/jobs/sidekiq_spec.rb +25 -0
  34. data/spec/runnable_job_spec.rb +22 -0
  35. data/spec/spec_helper.rb +1 -0
  36. metadata +48 -2
@@ -0,0 +1,58 @@
1
+ require 'dashing-contrib/bottles/pingdom'
2
+
3
+ module DashingContrib
4
+ module Jobs
5
+ module PingdomUptime
6
+ extend DashingContrib::RunnableJob
7
+
8
+ def self.metrics(options)
9
+ client = DashingContrib::Pingdom::Client.new(
10
+ username: options[:username],
11
+ password: options[:password],
12
+ api_key: options[:api_key]
13
+ )
14
+
15
+ user_opt = self.default_date_ranges.merge(options)
16
+
17
+ id = user_opt[:check_id]
18
+ current_uptime = client.uptime(id, user_opt[:default_date_range], user_opt[:now])
19
+ first_uptime = client.uptime(id, user_opt[:first_date_range], user_opt[:now])
20
+ second_uptime = client.uptime(id, user_opt[:second_date_range], user_opt[:now])
21
+ status = client.checks(id)
22
+
23
+ # returns this dataset
24
+ {
25
+ current: current_uptime.to_s,
26
+ first: first_uptime.to_s,
27
+ first_title: user_opt[:first_title],
28
+ second: second_uptime.to_s,
29
+ second_title: user_opt[:second_title],
30
+ is_up: status[:check][:status] == 'up',
31
+ current_response_time: status[:check][:lastresponsetime],
32
+ last_downtime: ::DashingContrib::Time.readable_diff(::Time.at(status[:check][:lasterrortime]))
33
+ }
34
+ end
35
+
36
+ def self.validate_state(metrics, options = {})
37
+ return DashingContrib::RunnableJob::OK if metrics[:is_up]
38
+ DashingContrib::RunnableJob::CRITICAL
39
+ end
40
+
41
+ private
42
+ # a default date ranges to use for uptime metrics
43
+ def self.default_date_ranges
44
+ now_time = ::Time.now.to_i
45
+ t24_hours = 86400
46
+ t1_month = t24_hours * 4
47
+ {
48
+ now: now_time,
49
+ default_date_range: now_time - t24_hours,
50
+ first_title: '4 Months',
51
+ first_date_range: now_time - (t1_month * 4),
52
+ second_title: 'YTD',
53
+ second_date_range: now_time - (t1_month * 12)
54
+ }
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,32 @@
1
+ require 'sidekiq/api'
2
+ module DashingContrib
3
+ module Jobs
4
+ module Sidekiq
5
+ extend DashingContrib::RunnableJob
6
+
7
+ def self.metrics(options)
8
+ stats = ::Sidekiq::Stats.new
9
+ metrics = [
10
+ { label: 'Processed', value: stats.processed },
11
+ { label: 'Failed', value: stats.failed },
12
+ { label: 'Retries', value: stats.retry_size },
13
+ { label: 'Dead', value: stats.dead_size },
14
+ { label: 'Enqueued', value: stats.enqueued }
15
+ ]
16
+ { metrics: metrics }
17
+ end
18
+
19
+ def self.validate_state(metrics, options = {})
20
+ default = { failed_warning_at: 100, failed_critical_at: 1000 }
21
+ user_options = default.merge(options)
22
+
23
+ failed_stats = metrics[:metrics].select { |v| v[:label] == 'Failed' }.first
24
+
25
+ value = failed_stats[:value]
26
+ return DashingContrib::RunnableJob::OK if value < user_options[:failed_warning_at]
27
+ return DashingContrib::RunnableJob::WARNING if value >= user_options[:failed_warning_at] && value < user_options[:failed_critical_at]
28
+ DashingContrib::RunnableJob::CRITICAL
29
+ end
30
+ end
31
+ end
32
+ end
@@ -11,4 +11,11 @@ get '/views/:widget?.html' do
11
11
  contrib_file = File.join(__dir__, 'assets', 'widgets', widget_name, file_name)
12
12
  return engines.first.new(contrib_file).render if File.exist? contrib_file
13
13
  end
14
+ end
15
+
16
+ get '/api/states' do
17
+ protected!
18
+ content_type :json
19
+
20
+ DashingContrib::Dashing.states.to_json
14
21
  end
@@ -0,0 +1,88 @@
1
+ # Creates a overall framework to define a reusable job.
2
+ #
3
+ # A custom job module can extend `RunnableJob` and overrides `metrics` and `validate_state`
4
+ module DashingContrib
5
+ module RunnableJob
6
+
7
+ extend self
8
+ WARNING = 'warning'.freeze
9
+ CRITICAL = 'critical'.freeze
10
+ OK = 'ok'.freeze
11
+
12
+ class Context
13
+
14
+ def initialize(interval, rufus_opts, event_name, user_options, this_module)
15
+ @interval = interval
16
+ @rufus_opts = rufus_opts
17
+ @event_name = event_name
18
+ @user_options = user_options
19
+ @this_module = this_module
20
+ end
21
+
22
+ def schedule!
23
+ _scheduler.every @interval, @rufus_opts do
24
+ current_metrics = @this_module.metrics(@user_options)
25
+ current_state = @this_module.validate_state(current_metrics, @user_options)
26
+ # including title and state
27
+ additional_info = { state: current_state }
28
+ additional_info[:title] = @user_options[:title] if @user_options[:title]
29
+ send_event(@event_name, current_metrics.merge(additional_info))
30
+ end
31
+ end
32
+
33
+ private
34
+ def _scheduler
35
+ SCHEDULER
36
+ end
37
+ end
38
+
39
+ def run(options = {}, &block)
40
+
41
+ user_options = _merge_options(options)
42
+ interval = user_options.delete(:every)
43
+ rufus_opt = {
44
+ first_in: user_options[:first_in]
45
+ }
46
+
47
+ event_name = user_options.delete(:event)
48
+ block.call if block_given?
49
+ Context.new(interval, rufus_opt, event_name, user_options, self).schedule!
50
+ end
51
+
52
+ # Overrides this method to fetch and generate metrics
53
+ # Return value should be the final metrics to be used in the user interface
54
+ # Arguments:
55
+ # options :: options provided by caller in `run` method
56
+ def metrics(options)
57
+ {}
58
+ end
59
+
60
+ # Always return a common state, override this with your custom logic
61
+ # Common states are WARNING, CRITICAL, OK
62
+ # Arguments:
63
+ # metrics :: calculated metrics provided `metrics` method
64
+ # user_options :: hash provided by user options
65
+ def validate_state(metrics, user_options)
66
+ OK
67
+ end
68
+
69
+ private
70
+ def _merge_options(options)
71
+ raise ':event String is required to identify a job name' if options[:event].nil?
72
+ _default_scheduler_options.merge(options)
73
+ end
74
+
75
+ def _default_scheduler_options
76
+ {
77
+ every: '30s',
78
+ first_in: 0
79
+ }
80
+ end
81
+ end
82
+ end
83
+
84
+ # dotenv doesn't seem to be loaded in dashing job
85
+ unless defined?(Dotenv)
86
+ require 'dotenv'
87
+ Dotenv.load
88
+ end
@@ -1,3 +1,3 @@
1
1
  module DashingContrib
2
- VERSION = '0.0.5'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ module DashingContrib
4
+ describe Jobs::Sidekiq do
5
+ describe '#validate_state' do
6
+ let(:metrics) { { metrics: [{ label: 'Failed', value: failed_value }] } }
7
+
8
+ context 'when failed is below warning' do
9
+ let(:failed_value) { 12 }
10
+ it { expect(Jobs::Sidekiq.validate_state(metrics, {})).to eq(DashingContrib::RunnableJob::OK) }
11
+ end
12
+
13
+ context 'when failed count is at warning level' do
14
+ let(:failed_value) { 133 }
15
+ it { expect(Jobs::Sidekiq.validate_state(metrics, {})).to eq(DashingContrib::RunnableJob::WARNING) }
16
+ end
17
+
18
+ context 'when failed count is above critical' do
19
+ let(:failed_value) { 20123 }
20
+ it { expect(Jobs::Sidekiq.validate_state(metrics, {})).to eq(DashingContrib::RunnableJob::CRITICAL) }
21
+ end
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe DashingContrib::RunnableJob do
4
+
5
+ describe 'Common States' do
6
+ it { expect(DashingContrib::RunnableJob::WARNING).to eq 'warning' }
7
+ it { expect(DashingContrib::RunnableJob::CRITICAL).to eq 'critical' }
8
+ it { expect(DashingContrib::RunnableJob::OK).to eq 'ok' }
9
+ end
10
+
11
+ describe '#run' do
12
+ context 'when event name is not provided' do
13
+ it { expect { DashingContrib::RunnableJob.run }.to raise_exception(':event String is required to identify a job name') }
14
+ end
15
+
16
+ context 'when event name is provided' do
17
+ let(:event_name) { 'test-event' }
18
+ after(:each) { DashingContrib::RunnableJob.run(event: event_name) }
19
+ it { expect(SCHEDULER).to receive(:every).with('30s', { first_in: 0 }) }
20
+ end
21
+ end
22
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
3
  require 'dashing-contrib'
4
+ require 'dashing'
4
5
 
5
6
  RSpec.configure do |config|
6
7
  config.order = :random
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dashing-contrib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jing Dong
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-17 00:00:00.000000000 Z
11
+ date: 2014-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dotenv
@@ -110,6 +110,34 @@ dependencies:
110
110
  version: 4.1.1
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: sinatra
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 1.4.4
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 1.4.4
125
+ - !ruby/object:Gem::Dependency
126
+ name: dashing
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 1.3.4
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 1.3.4
139
+ - !ruby/object:Gem::Dependency
140
+ name: nagiosharder
113
141
  requirement: !ruby/object:Gem::Requirement
114
142
  requirements:
115
143
  - - ">="
@@ -183,11 +211,17 @@ files:
183
211
  - lib/dashing-contrib/assets/stylesheets/font-awesome/scss/font-awesome.scss
184
212
  - lib/dashing-contrib/assets/widgets.coffee
185
213
  - lib/dashing-contrib/assets/widgets.scss
214
+ - lib/dashing-contrib/assets/widgets/dashing_state/dashing_state.coffee
215
+ - lib/dashing-contrib/assets/widgets/dashing_state/dashing_state.html
216
+ - lib/dashing-contrib/assets/widgets/dashing_state/dashing_state.scss
186
217
  - lib/dashing-contrib/assets/widgets/kue_status/README.md
187
218
  - lib/dashing-contrib/assets/widgets/kue_status/kue_status.coffee
188
219
  - lib/dashing-contrib/assets/widgets/kue_status/kue_status.html
189
220
  - lib/dashing-contrib/assets/widgets/kue_status/kue_status.scss
190
221
  - lib/dashing-contrib/assets/widgets/kue_status/preview.png
222
+ - lib/dashing-contrib/assets/widgets/nagios_list/nagios_list.coffee
223
+ - lib/dashing-contrib/assets/widgets/nagios_list/nagios_list.html
224
+ - lib/dashing-contrib/assets/widgets/nagios_list/nagios_list.scss
191
225
  - lib/dashing-contrib/assets/widgets/pingdom_uptime/README.md
192
226
  - lib/dashing-contrib/assets/widgets/pingdom_uptime/pingdom_uptime.coffee
193
227
  - lib/dashing-contrib/assets/widgets/pingdom_uptime/pingdom_uptime.html
@@ -202,10 +236,12 @@ files:
202
236
  - lib/dashing-contrib/assets/widgets/sidekiq/sidekiq.coffee
203
237
  - lib/dashing-contrib/assets/widgets/sidekiq/sidekiq.html
204
238
  - lib/dashing-contrib/assets/widgets/sidekiq/sidekiq.scss
239
+ - lib/dashing-contrib/bottles/dashing.rb
205
240
  - lib/dashing-contrib/bottles/kue.rb
206
241
  - lib/dashing-contrib/bottles/kue/client.rb
207
242
  - lib/dashing-contrib/bottles/nagios.rb
208
243
  - lib/dashing-contrib/bottles/nagios/client.rb
244
+ - lib/dashing-contrib/bottles/nagios/status.rb
209
245
  - lib/dashing-contrib/bottles/pingdom.rb
210
246
  - lib/dashing-contrib/bottles/pingdom/checks.rb
211
247
  - lib/dashing-contrib/bottles/pingdom/client.rb
@@ -214,9 +250,17 @@ files:
214
250
  - lib/dashing-contrib/bottles/time.rb
215
251
  - lib/dashing-contrib/configuration.rb
216
252
  - lib/dashing-contrib/history.rb
253
+ - lib/dashing-contrib/jobs/dashing-state.rb
254
+ - lib/dashing-contrib/jobs/kue.rb
255
+ - lib/dashing-contrib/jobs/nagios_list.rb
256
+ - lib/dashing-contrib/jobs/pingdom_uptime.rb
257
+ - lib/dashing-contrib/jobs/sidekiq.rb
217
258
  - lib/dashing-contrib/routes.rb
259
+ - lib/dashing-contrib/runnable_job.rb
218
260
  - lib/dashing-contrib/version.rb
219
261
  - spec/bottles/kue/client_spec.rb
262
+ - spec/jobs/sidekiq_spec.rb
263
+ - spec/runnable_job_spec.rb
220
264
  - spec/spec_helper.rb
221
265
  homepage: https://github.com/QubitProducts/dashing-contrib
222
266
  licenses:
@@ -245,4 +289,6 @@ summary: An extension to Dashing that makes easier to maintaining, sharing widge
245
289
  and test common tasks
246
290
  test_files:
247
291
  - spec/bottles/kue/client_spec.rb
292
+ - spec/jobs/sidekiq_spec.rb
293
+ - spec/runnable_job_spec.rb
248
294
  - spec/spec_helper.rb