mongodb_logger 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. data/.gitignore +2 -1
  2. data/.rvmrc +1 -1
  3. data/.travis.yml +5 -4
  4. data/Appraisals +9 -0
  5. data/CHANGELOG.md +20 -3
  6. data/README.md +58 -35
  7. data/Rakefile +33 -96
  8. data/app/assets/javascripts/analytics.js.coffee +5 -4
  9. data/app/assets/javascripts/logs.js.coffee +11 -11
  10. data/app/assets/javascripts/mongodb_logger.js +2 -1
  11. data/app/assets/javascripts/vendors/jquery-1.9.1.min.js +5 -0
  12. data/app/assets/javascripts/vendors/jquery.pjax.min.js +15 -13
  13. data/app/assets/javascripts/vendors/mustache.min.js +10 -0
  14. data/app/assets/stylesheets/layout.css +20 -15
  15. data/app/assets/stylesheets/mongodb_logger.css +0 -3
  16. data/bin/mongodb_logger_web +1 -2
  17. data/features/rails.feature +20 -8
  18. data/features/step_definitions/mongodb_logger_web_steps.rb +2 -3
  19. data/features/step_definitions/rails_application_steps.rb +84 -58
  20. data/features/step_definitions/rake_steps.rb +10 -0
  21. data/features/support/aruba.rb +5 -0
  22. data/features/support/env.rb +11 -5
  23. data/features/support/rails.rb +55 -67
  24. data/gemfiles/rails31.gemfile +8 -0
  25. data/gemfiles/rails32.gemfile +8 -0
  26. data/lib/mongodb_logger.rb +10 -3
  27. data/lib/mongodb_logger/adapters/base.rb +5 -1
  28. data/lib/mongodb_logger/adapters/mongo.rb +17 -13
  29. data/lib/mongodb_logger/adapters/moped.rb +17 -11
  30. data/lib/mongodb_logger/capistrano.rb +8 -0
  31. data/lib/mongodb_logger/logger.rb +13 -26
  32. data/lib/mongodb_logger/rack_middleware.rb +24 -0
  33. data/lib/mongodb_logger/railtie.rb +4 -4
  34. data/lib/mongodb_logger/server.rb +43 -68
  35. data/lib/mongodb_logger/server/helpers.rb +5 -0
  36. data/lib/mongodb_logger/server/{content_for.rb → helpers/content_for.rb} +0 -0
  37. data/lib/mongodb_logger/server/helpers/mustache_helpers.rb +66 -0
  38. data/lib/mongodb_logger/server/{partials.rb → helpers/partials.rb} +0 -0
  39. data/lib/mongodb_logger/server/{sprokets.rb → helpers/sprokets.rb} +4 -4
  40. data/lib/mongodb_logger/server/{view_helpers.rb → helpers/view_helpers.rb} +22 -38
  41. data/lib/mongodb_logger/server/model.rb +4 -0
  42. data/lib/mongodb_logger/server/model/additional_filter.rb +15 -23
  43. data/lib/mongodb_logger/server/model/analytic.rb +24 -32
  44. data/lib/mongodb_logger/server/model/base.rb +21 -0
  45. data/lib/mongodb_logger/server/model/filter.rb +12 -20
  46. data/lib/mongodb_logger/server/mustache/logs/info.rb +15 -0
  47. data/lib/mongodb_logger/server/templates/logs/info.mustache +25 -0
  48. data/lib/mongodb_logger/server/views/layout.erb +28 -26
  49. data/lib/mongodb_logger/server/views/shared/_log.erb +1 -1
  50. data/lib/mongodb_logger/server/views/shared/_tabs.erb +2 -2
  51. data/lib/mongodb_logger/server/views/shared/layout/_mustache.erb +6 -0
  52. data/lib/mongodb_logger/server/views/show_log.erb +2 -2
  53. data/lib/mongodb_logger/server_config.rb +4 -4
  54. data/lib/mongodb_logger/utils/migrate.rb +50 -0
  55. data/lib/mongodb_logger/utils/progressbar.rb +79 -0
  56. data/lib/mongodb_logger/version.rb +1 -1
  57. data/mongodb_logger.gemspec +15 -8
  58. data/{test/config/samples → spec/factories/config}/database.yml +0 -0
  59. data/{test/config/samples → spec/factories/config}/database_no_file_logging.yml +0 -0
  60. data/{test/config/samples → spec/factories/config}/database_replica_set.yml +0 -0
  61. data/{test/config/samples → spec/factories/config}/database_with_auth.yml +0 -0
  62. data/spec/factories/config/database_with_capsize.yml +9 -0
  63. data/{test/config/samples → spec/factories/config}/database_with_collection.yml +0 -0
  64. data/{test/config/samples → spec/factories/config}/database_with_url.yml +0 -0
  65. data/{test/config/samples → spec/factories/config}/mongodb_logger.yml +0 -0
  66. data/{test/config/samples → spec/factories/config}/mongoid.yml +0 -0
  67. data/{test/config/samples → spec/factories/config}/server_config.yml +0 -0
  68. data/spec/javascripts/support/jasmine.yml +1 -5
  69. data/spec/mongodb_logger_spec.rb +257 -0
  70. data/spec/rails_spec/controllers/tests_controller_spec_rails.rb +128 -0
  71. data/spec/rails_spec/spec_helper_rails.rb +13 -0
  72. data/spec/spec_helper.rb +17 -0
  73. data/{test/test_helper.rb → spec/support/mongodb_logger_helper.rb} +44 -63
  74. data/spec/support/mongodb_logger_macros.rb +22 -0
  75. data/spec/support/rails.rb +40 -0
  76. data/spec/utils/migrate_spec.rb +32 -0
  77. data/{lib/tasks → tasks}/mongodb_logger.rake +14 -6
  78. metadata +198 -78
  79. data/SUPPORTED_RAILS_VERSIONS +0 -16
  80. data/TESTING.md +0 -24
  81. data/app/assets/javascripts/vendors/jquery-1.8.3.min.js +0 -2
  82. data/features/support/terminal.rb +0 -95
  83. data/lib/mongodb_logger/server/views/shared/_log_info.erb +0 -27
  84. data/test/Gemfile_tests +0 -9
  85. data/test/active_record.rb +0 -13
  86. data/test/log/.gitkeep +0 -0
  87. data/test/rails.rb +0 -22
  88. data/test/rails/app/controllers/order_controller.rb +0 -23
  89. data/test/rails/test/functional/order_controller_test.rb +0 -116
  90. data/test/rails/test/test_helper.rb +0 -10
  91. data/test/shoulda_macros/log_macros.rb +0 -13
  92. data/test/test.sh +0 -7
  93. data/test/unit/mongodb_logger_replica_test.rb +0 -56
  94. data/test/unit/mongodb_logger_test.rb +0 -307
@@ -0,0 +1,10 @@
1
+ Given /I've prepared the Rakefile/ do
2
+ rakefile = File.join(PROJECT_ROOT, 'features', 'support', 'rake', 'Rakefile')
3
+ target = File.join(TEMP_DIR, 'Rakefile')
4
+ FileUtils.cp(rakefile, target)
5
+ end
6
+
7
+ When /I run rake with (.+)/ do |command|
8
+ command = "rake #{command.gsub(' ','_')}"
9
+ step %{I run `#{command}`}
10
+ end
@@ -0,0 +1,5 @@
1
+ Before do
2
+ @aruba_timeout_seconds = 30
3
+ @aruba_io_wait_seconds = 5
4
+ @dirs = ["tmp"]
5
+ end
@@ -1,15 +1,21 @@
1
1
  require 'active_support'
2
2
  require 'rspec'
3
+ require 'aruba/cucumber'
3
4
 
4
5
  PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..', '..')).freeze
5
6
  TEMP_DIR = File.join(PROJECT_ROOT, 'tmp').freeze
6
7
  LOCAL_RAILS_ROOT = File.join(TEMP_DIR, 'rails_root').freeze
7
- BUILT_GEM_ROOT = File.join(TEMP_DIR, 'built_gems').freeze
8
- LOCAL_GEM_ROOT = File.join(TEMP_DIR, 'local_gems').freeze
9
8
 
10
9
  Before do
11
- FileUtils.mkdir_p(TEMP_DIR)
12
- FileUtils.rm_rf(BUILT_GEM_ROOT)
13
10
  FileUtils.rm_rf(LOCAL_RAILS_ROOT)
14
- FileUtils.mkdir_p(BUILT_GEM_ROOT)
11
+ end
12
+
13
+ When /^I reset Bundler environment variable$/ do
14
+ BUNDLE_ENV_VARS.each do |key|
15
+ ENV[key] = nil
16
+ end
17
+ end
18
+
19
+ def prepend_path(path)
20
+ ENV['PATH'] = path + ":" + ENV['PATH']
15
21
  end
@@ -1,98 +1,86 @@
1
+ BUNDLE_ENV_VARS = %w(RUBYOPT BUNDLE_PATH BUNDLE_BIN_PATH BUNDLE_GEMFILE)
2
+ ORIGINAL_BUNDLE_VARS = Hash[ENV.select{ |key,value| BUNDLE_ENV_VARS.include?(key) }]
3
+
4
+ ENV['RAILS_ENV'] = 'test'
5
+
6
+ Before do
7
+ ENV['BUNDLE_GEMFILE'] = File.join(Dir.pwd, ENV['BUNDLE_GEMFILE']) unless ENV['BUNDLE_GEMFILE'].start_with?(Dir.pwd)
8
+ @framework_version = nil
9
+ end
10
+
11
+ After do |s|
12
+ ORIGINAL_BUNDLE_VARS.each_pair do |key, value|
13
+ ENV[key] = value
14
+ end
15
+ Cucumber.wants_to_quit = true if s.failed?
16
+ end
17
+
1
18
  module RailsHelpers
2
19
  def rails_root_exists?
3
20
  File.exists?(environment_path)
4
21
  end
5
-
6
- def environment_path
7
- File.join(rails_root, 'config', 'environment.rb')
8
- end
9
22
 
10
23
  def application_controller_filename
11
24
  controller_filename = File.join(rails_root, 'app', 'controllers', "application_controller.rb")
12
25
  end
13
26
 
27
+ def rails3?
28
+ rails_version =~ /^3/
29
+ end
30
+
14
31
  def rails_root
15
32
  LOCAL_RAILS_ROOT
16
33
  end
17
-
18
- def rails30?
19
- rails_version =~ /^3.0/
20
- end
21
-
22
- def rails31?
23
- rails_version =~ /^3.1/
34
+
35
+ def rails_uses_rack?
36
+ rails3? || rails_version =~ /^2\.3/
24
37
  end
25
38
 
26
39
  def rails_version
27
40
  @rails_version ||= begin
28
- rails_version = open(gemfile_path).read.match(/gem.*rails["'].*["'](.+)["']/)[1]
41
+ if ENV["RAILS_VERSION"]
42
+ ENV["RAILS_VERSION"]
43
+ elsif bundler_manages_gems?
44
+ rails_version = open(gemfile_path).read.match(/gem.*rails["'].*["'](.+)["']/)[1]
45
+ else
46
+ environment_file = File.join(rails_root, 'config', 'environment.rb')
47
+ rails_version = `grep RAILS_GEM_VERSION #{environment_file}`.match(/[\d.]+/)[0]
48
+ end
29
49
  end
30
50
  end
31
51
 
32
- def gemfile_path
33
- File.join(rails_root, 'Gemfile')
52
+ def bundler_manages_gems?
53
+ File.exists?(gemfile_path)
34
54
  end
35
55
 
36
- def rakefile_path
37
- File.join(rails_root, 'Rakefile')
38
- end
39
-
40
- def routes_path
41
- File.join(rails_root, 'config', 'routes.rb')
56
+ def gemfile_path
57
+ gemfile = File.join(rails_root, 'Gemfile')
42
58
  end
43
-
44
- def logs_path
45
- File.join(rails_root, 'log')
59
+
60
+ def rails_manages_gems?
61
+ rails_version =~ /^2\.[123]/
46
62
  end
47
63
 
48
- def bundle_gem(gem_name, version = nil)
49
- File.open(gemfile_path, 'a') do |file|
50
- gem = "gem '#{gem_name}'"
51
- gem += ", '#{version}'" if version
52
- file.puts(gem)
53
- end
64
+ def rails_supports_initializers?
65
+ rails3? || rails_version =~ /^2\./
54
66
  end
55
-
56
- def require_thread
57
- content = File.read(rakefile_path)
58
- content = "require 'thread'\n#{content}"
59
- File.open(rakefile_path, 'wb') { |file| file.write(content) }
67
+
68
+ def rails_finds_generators_in_gems?
69
+ rails3? || rails_version =~ /^2\./
60
70
  end
61
-
62
- def add_routes
63
- content = File.read(routes_path)
64
- flag = Regexp.escape("Application.routes.draw do\n")
65
- order_routes = <<STR
66
- resources :order do
67
- collection do
68
- post :test_post
71
+
72
+ def version_string
73
+ ENV['RAILS_VERSION'] || `tail -n 1 SUPPORTED_RAILS_VERSIONS` # use latest version if ENV["RAILS_VERSION"] is undefined
69
74
  end
70
- end
71
- STR
72
- content.gsub!(/#{flag}/m, '\0 ' + order_routes)
73
- File.open(routes_path, 'wb') { |file| file.write(content) }
75
+
76
+ def environment_path
77
+ File.join(rails_root, 'config', 'environment.rb')
74
78
  end
75
-
76
- def copy_tests
77
- FileUtils.cp(
78
- File.join(PROJECT_ROOT, 'test', 'rails', 'app', 'controllers', 'order_controller.rb'),
79
- File.join(rails_root, 'app', 'controllers', 'order_controller.rb')
80
- )
81
- FileUtils.cp(
82
- File.join(PROJECT_ROOT, 'test', 'config', 'samples', 'database.yml'),
83
- File.join(rails_root, 'config', 'database.yml')
84
- )
85
- FileUtils.cp(
86
- File.join(PROJECT_ROOT, 'test', 'rails', 'test', 'test_helper.rb'),
87
- File.join(rails_root, 'test', 'test_helper.rb')
88
- )
89
- FileUtils.cp(
90
- File.join(PROJECT_ROOT, 'test', 'rails', 'test', 'functional', 'order_controller_test.rb'),
91
- File.join(rails_root, 'test', 'functional', 'order_controller_test.rb')
92
- )
93
- FileUtils.chmod 0777, logs_path
79
+
80
+ def rakefile_path
81
+ File.join(rails_root, 'Rakefile')
94
82
  end
95
-
83
+
96
84
  end
97
85
 
98
- World(RailsHelpers)
86
+ World(RailsHelpers)
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", "~> 3.1.11"
6
+ gem "mongodb_logger", :path=>"../"
7
+
8
+ gemspec :path=>"../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", "~> 3.2.12"
6
+ gem "mongodb_logger", :path=>"../"
7
+
8
+ gemspec :path=>"../"
@@ -4,6 +4,7 @@ require 'mongodb_logger/config'
4
4
  require 'mongodb_logger/logger'
5
5
  require 'mongodb_logger/railtie' if defined?(Rails::Railtie)
6
6
  require 'mongodb_logger/engine' if defined?(Rails::Engine)
7
+ require 'mongodb_logger/rack_middleware'
7
8
  require 'mongodb_logger/version'
8
9
 
9
10
  module MongodbLogger
@@ -11,7 +12,13 @@ module MongodbLogger
11
12
  extend Config
12
13
 
13
14
  def self.included(base)
14
- base.class_eval { around_filter :enable_mongodb_logger }
15
+ base.class_eval do
16
+ begin
17
+ around_action :enable_mongodb_logger
18
+ rescue
19
+ around_filter :enable_mongodb_logger
20
+ end
21
+ end
15
22
  end
16
23
 
17
24
  def enable_mongodb_logger
@@ -36,11 +43,11 @@ module MongodbLogger
36
43
  }) { yield }
37
44
  end
38
45
  # session keys can be with dots. It is invalid keys for BSON
39
- def mongo_fix_session_keys(session)
46
+ def mongo_fix_session_keys(session = {})
40
47
  new_session = {}
41
48
  session.each do |i, j|
42
49
  new_session[i.gsub(/\./i, "|")] = j.inspect
43
- end
50
+ end if session
44
51
  new_session
45
52
  end
46
53
  end
@@ -18,13 +18,17 @@ module MongodbLogger
18
18
  @collection = @connection[@configuration['collection']]
19
19
  end
20
20
 
21
+ def rename_collection_command(admin_session, to, drop_target = false)
22
+ admin_session.command({ renameCollection: "#{@configuration['database']}.#{collection_name}", to: "#{@configuration['database']}.#{to}", dropTarget: drop_target })
23
+ end
24
+
21
25
  def reset_collection
22
26
  if @connection && @collection
23
27
  @collection.drop
24
28
  create_collection
25
29
  end
26
30
  end
27
-
31
+
28
32
  def collection_stats_hash(stats)
29
33
  {
30
34
  :is_capped => (stats["capped"] && ([1, true].include?(stats["capped"]))),
@@ -1,7 +1,7 @@
1
1
  module MongodbLogger
2
2
  module Adapers
3
3
  class Mongo < Base
4
-
4
+
5
5
  def initialize(options = {})
6
6
  @authenticated = false
7
7
  @configuration = options
@@ -19,29 +19,33 @@ module MongodbLogger
19
19
  end
20
20
  end
21
21
  end
22
-
22
+
23
23
  def create_collection
24
24
  @connection.create_collection(collection_name,
25
25
  {:capped => true, :size => @configuration['capsize'].to_i})
26
26
  end
27
-
27
+
28
28
  def insert_log_record(record, options = {})
29
29
  @collection.insert(record, options[:write_options])
30
30
  end
31
-
31
+
32
32
  def collection_stats
33
33
  collection_stats_hash(@collection.stats)
34
34
  end
35
-
35
+
36
+ def rename_collection(to, drop_target = false)
37
+ rename_collection_command(mongo_connection_object.db("admin"), to, drop_target)
38
+ end
39
+
36
40
  # filter
37
41
  def filter_by_conditions(filter)
38
42
  @collection.find(filter.get_mongo_conditions).sort('$natural', -1).limit(filter.get_mongo_limit)
39
43
  end
40
-
44
+
41
45
  def find_by_id(id)
42
46
  @collection.find_one(::BSON::ObjectId(id))
43
47
  end
44
-
48
+
45
49
  def tail_log_from_params(params = {})
46
50
  logs = []
47
51
  last_id = nil
@@ -56,19 +60,19 @@ module MongodbLogger
56
60
  log = @collection.find_one({}, {:sort => ['$natural', -1]})
57
61
  log_last_id = log["_id"].to_s unless log.blank?
58
62
  end
59
- {
60
- :log_last_id => log_last_id,
63
+ {
64
+ :log_last_id => log_last_id,
61
65
  :time => Time.now.strftime("%F %T"),
62
66
  :logs => logs
63
67
  }
64
68
  end
65
-
69
+
66
70
  def calculate_mapreduce(map, reduce, params = {})
67
71
  @collection.map_reduce(map, reduce, {:query => params[:conditions], :sort => ['$natural', -1], :out => {:inline => true}, :raw => true}).find()
68
72
  end
69
-
73
+
70
74
  private
71
-
75
+
72
76
  def mongo_connection_object
73
77
  if @configuration['hosts']
74
78
  conn = ::Mongo::ReplSetConnection.new(*(@configuration['hosts'] <<
@@ -85,7 +89,7 @@ module MongodbLogger
85
89
  @connection_type = conn.class
86
90
  conn
87
91
  end
88
-
92
+
89
93
  end
90
94
  end
91
95
  end
@@ -1,7 +1,7 @@
1
1
  module MongodbLogger
2
2
  module Adapers
3
3
  class Moped < Base
4
-
4
+
5
5
  def initialize(options = {})
6
6
  @configuration = options
7
7
  if @configuration['url']
@@ -20,11 +20,11 @@ module MongodbLogger
20
20
  end
21
21
  end
22
22
  end
23
-
23
+
24
24
  def create_collection
25
25
  @connection.command(create: collection_name, capped: true, size: @configuration['capsize'].to_i)
26
26
  end
27
-
27
+
28
28
  def insert_log_record(record, options = {})
29
29
  record[:_id] = ::Moped::BSON::ObjectId.new
30
30
  @connection.with(safe: options[:write_options])[collection_name].insert(record)
@@ -33,16 +33,22 @@ module MongodbLogger
33
33
  def collection_stats
34
34
  collection_stats_hash(@connection.command(collStats: collection_name))
35
35
  end
36
-
36
+
37
+ def rename_collection(to, drop_target = false)
38
+ @connection.with(database: "admin", consistency: :strong) do |session|
39
+ rename_collection_command(session, to, drop_target)
40
+ end
41
+ end
42
+
37
43
  # filter
38
44
  def filter_by_conditions(filter)
39
45
  @collection.find(filter.get_mongo_conditions).sort('$natural' => -1).limit(filter.get_mongo_limit)
40
46
  end
41
-
47
+
42
48
  def find_by_id(id)
43
49
  @collection.find("_id" => ::Moped::BSON::ObjectId.from_string(id)).first
44
50
  end
45
-
51
+
46
52
  def tail_log_from_params(params = {})
47
53
  logs = []
48
54
  last_id = nil
@@ -57,13 +63,13 @@ module MongodbLogger
57
63
  log = @collection.find.sort('$natural' => -1).first
58
64
  log_last_id = log["_id"].to_s unless log.blank?
59
65
  end
60
- {
61
- :log_last_id => log_last_id,
66
+ {
67
+ :log_last_id => log_last_id,
62
68
  :time => Time.now.strftime("%F %T"),
63
69
  :logs => logs
64
70
  }
65
71
  end
66
-
72
+
67
73
  def calculate_mapreduce(map, reduce, params = {})
68
74
  @connection.command(
69
75
  mapreduce: collection_name,
@@ -76,7 +82,7 @@ module MongodbLogger
76
82
  end
77
83
 
78
84
  private
79
-
85
+
80
86
  def mongo_connection_object
81
87
  if @configuration['hosts']
82
88
  conn = ::Moped::Session.new(@configuration['hosts'].map{|(host,port)| "#{host}:#{port}"}, :timeout => 6)
@@ -89,7 +95,7 @@ module MongodbLogger
89
95
  @connection_type = conn.class
90
96
  conn
91
97
  end
92
-
98
+
93
99
  end
94
100
  end
95
101
  end
@@ -3,6 +3,8 @@ Capistrano::Configuration.instance.load do
3
3
  _cset :mongodb_logger_assets_role, [:app]
4
4
  _cset :mongodb_logger_assets_dir, "public/assets"
5
5
 
6
+ _cset :mongodb_logger_db_role, [:app]
7
+
6
8
  namespace :mongodb_logger do
7
9
 
8
10
  desc <<-DESC
@@ -12,5 +14,11 @@ Capistrano::Configuration.instance.load do
12
14
  run "cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{mongodb_logger_asset_env} mongodb_logger:assets:compile[#{mongodb_logger_assets_dir}]"
13
15
  end
14
16
 
17
+ desc <<-DESC
18
+ Run collection migrate rake task.
19
+ DESC
20
+ task :migrate, :roles => mongodb_logger_db_role, :except => { :no_release => true } do
21
+ run "cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} mongodb_logger:migrate"
22
+ end
15
23
  end
16
24
  end