-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conflicts: lib/rnotifier.rb
- Loading branch information
Showing
43 changed files
with
869 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
*.gem | ||
*.rbc | ||
*.swp | ||
.DS_Store | ||
.bundle | ||
.config | ||
|
@@ -19,3 +20,4 @@ tmp | |
log | ||
config/ | ||
*.diff | ||
diff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,90 @@ | ||
#!/usr/bin/env ruby | ||
|
||
ARGV[0] = 'help' if ARGV.empty? | ||
require 'optparse' | ||
|
||
case ARGV[0] | ||
when 'help' | ||
puts %Q{ | ||
---- HELP ---- | ||
test #Test rnotifier configuration is correct and send test exception. | ||
class RnotofierCLI | ||
attr_accessor :options | ||
|
||
install api-key [environments] #Create config/rnotifier.yaml with api_key and enabled for supplied environments.environments are optionbal, default is production. | ||
} | ||
when 'test' | ||
def initialize | ||
@options = {:type => ARGV[0]} | ||
end | ||
|
||
def option_parser | ||
@option_parser ||= OptionParser.new do |opts| | ||
opts.banner = "Usage: rnotifier install -k api-key -i app-id [-e environments]" | ||
|
||
opts.on('-h', 'Help') do | ||
puts opts | ||
puts 'i.e rnotifier install -k API-KEY -i APP-ID staging,production' | ||
exit | ||
end | ||
|
||
opts.on("-k api_key") do |v| | ||
self.options[:api_key] = v | ||
end | ||
|
||
opts.on("-i app_id") do |v| | ||
self.options[:app_id] = v | ||
end | ||
|
||
opts.on("-e [environments]") do |v| | ||
self.options[:environments] = v if v | ||
end | ||
end | ||
end | ||
|
||
def generate | ||
case options[:type] | ||
when 'install' | ||
install | ||
when 'test' | ||
send_test_exception | ||
else | ||
ARGV[0] = '-h' | ||
option_parser.parse! | ||
end | ||
end | ||
|
||
def install | ||
option_parser.parse! | ||
|
||
if options[:app_id].nil? || options[:api_key].nil? | ||
ARGV[0] = '-h' | ||
option_parser.parse! | ||
end | ||
|
||
Dir.mkdir('config') unless Dir.exists?('config') | ||
|
||
File.open('config/rnotifier.yaml', 'w') do |f| | ||
f.puts("apikey: '#{options[:api_key]}'\n") | ||
f.puts("app_id: '#{options[:app_id]}'\n") | ||
f.puts("environments: #{options[:environments]}") if options[:environments] | ||
f.puts("capture_code: false") | ||
f.close | ||
end | ||
|
||
puts "Rnotofier configuration written in config/rnotifier.yaml" | ||
end | ||
|
||
def send_test_exception | ||
#$:.unshift(File.dirname(__FILE__) + '/../lib/') | ||
require 'rack' | ||
require 'rnotifier' | ||
require 'rnotifier/config_test.rb' | ||
|
||
ENV['RACK_ENV'] = 'rnotifier_test' | ||
|
||
Rnotifier.load_config('config/rnotifier.yaml') | ||
Rnotifier::Config.environments = [ENV['RACK_ENV']] | ||
Rnotifier::Config.init | ||
|
||
if Rnotifier::Config.valid? | ||
Rnotifier::ConfigTest.test | ||
else | ||
puts 'Configuration is invalid. Check api key or config' | ||
end | ||
when 'install' | ||
if (api_key = ARGV[1]).nil? | ||
puts "'api-key' is required. Use like i.e rnotifier install api-key [environments]" | ||
else | ||
environments = ARGV[2..-1] | ||
Dir.mkdir('config') unless Dir.exists?('config') | ||
|
||
File.open('config/rnotifier.yaml', 'w') do |f| | ||
f.puts("apikey: #{api_key}\n") | ||
f.puts("environments: #{environments.join(', ')}") unless environments.empty? | ||
f.puts("capture_code: false") | ||
f.close | ||
end | ||
|
||
puts "Rnotofier configuration written in config/rnotifier.yaml" | ||
puts 'Configuration is invalid. Check config/rnotifier.yaml' | ||
end | ||
end | ||
end | ||
|
||
RnotofierCLI.new.generate | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module Rnotifier | ||
class Benchmark | ||
|
||
RnotifierException = Class.new(Exception) | ||
|
||
BM_FIELDS = [:exception, :args] | ||
|
||
def self.it(bm_name, opts = {}, &block) | ||
output = nil | ||
exception = nil | ||
|
||
raise RnotifierException.new("Code block require :#{bm_name} for benchmarking.") unless block_given? | ||
|
||
t1 = Time.now | ||
begin | ||
output = yield | ||
rescue Exception => e | ||
exception = e | ||
opts[:exception] = {:message => exception.message, :line => exception.backtrace.first} | ||
end | ||
|
||
self._collect(bm_name, Time.now - t1, opts) | ||
|
||
raise exception if exception | ||
return output | ||
end | ||
|
||
def self._collect(bm_name, time, opts) | ||
return if opts[:time_condition] && opts[:time_condition] >= time | ||
|
||
data = { :time => time} | ||
BM_FIELDS.each{|f| data[f] = opts[f] if opts[f]} | ||
|
||
bm_name = "#{opts[:class]}##{bm_name}" if opts[:class] | ||
|
||
Rnotifier::Message.new(bm_name, Rnotifier::Message::BENCHMARK, data).enq | ||
end | ||
|
||
def self.web_request(bm_name, time, request_url, opts) | ||
return if opts[:time_condition] && opts[:time_condition] >= time | ||
|
||
data = { :time => time, :request_url => request_url, :type => 'web', :b_token => opts[:b_token] } | ||
|
||
Rnotifier::Message.new(bm_name, Rnotifier::Message::BENCHMARK, data).enq | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Rnotifier | ||
class BenchmarkProxy < BasicObject | ||
|
||
instance_methods.each do |m| | ||
undef_method(m) if m.to_s !~ /(?:^__|^nil\?$|^send$|^object_id$)/ | ||
end | ||
|
||
RBenchmark = ::Object.const_get('Rnotifier::Benchmark') | ||
Klass = ::Object.const_get('Class') | ||
|
||
def initialize(target, opts = {}) | ||
@target = target | ||
@opts = opts | ||
end | ||
|
||
def method_missing(method, *args, &block) | ||
@opts[:args] = args if @opts[:args] | ||
@opts[:class] = @target.is_a?(Klass) ? @target.name : @target.class.to_s | ||
RBenchmark.it(method, @opts){ @target.send(method, *args, &block) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Rnotifier | ||
class BenchmarkView | ||
class << self | ||
|
||
def browser_token | ||
Digest::MD5.hexdigest("#{Rnotifier::Config.app_id}#{Process.pid}#{Thread.current.object_id}#{Time.now.to_i}") | ||
end | ||
|
||
def tag(token) | ||
q = ["b_token=#{token}", "app_id=#{Rnotifier::Config.app_id}"].join("&") | ||
%Q{<script>$.get('#{Rnotifier::Config.api_host}/#{Rnotifier::Config.browser_path}?#{q}&o_at='+(new Date()).getTime())</script>} | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module BenchmarkMethods | ||
def benchmark(opts = {}) | ||
Rnotifier::BenchmarkProxy.new(self, opts) | ||
end | ||
end | ||
|
||
Object.send(:include, BenchmarkMethods) |
Oops, something went wrong.