Module: Mongobile::MongoHelper

Included in:
App
Defined in:
lib/mongobile/mongo_helper.rb

Instance Method Summary collapse

Instance Method Details

#connectionObject



3
4
5
6
7
8
9
# File 'lib/mongobile/mongo_helper.rb', line 3

def connection
  @connection ||= begin
    host = ARGV[0] || "127.0.0.1"
    port = ARGV[1] || 27017
    Mongo::Connection.new(host, port)
  end
end

#db(d = nil) ⇒ Object



11
12
13
# File 'lib/mongobile/mongo_helper.rb', line 11

def db(d=nil)
  connection.db(d || params[:id]) rescue nil
end

#document_list(collection, query = {}) ⇒ Object



40
41
42
# File 'lib/mongobile/mongo_helper.rb', line 40

def document_list(collection, query = {})
  collection.find(query, query_options)
end

#file_size(database) ⇒ Object



44
45
46
# File 'lib/mongobile/mongo_helper.rb', line 44

def file_size(database)
  "%0.2f MB" % (database.stats["fileSize"] / 1024**2)
end

#find_doc(id, collection) ⇒ Object



27
28
29
# File 'lib/mongobile/mongo_helper.rb', line 27

def find_doc(id, collection)
  collection.find_one({ "_id" => get_key(id) }) || collection.find_one({ "_id" => id.to_s })
end

#get_key(key) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mongobile/mongo_helper.rb', line 15

def get_key(key)
  $stderr.puts "GetKey : #{key}"
  return nil if key.nil?

  key.gsub!('"', '')
  if key =~ /^[0-9]+$/
    return key.to_i
  else
    return BSON::ObjectID.from_string(key) rescue key
  end
end

#normalize_stats(stats) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mongobile/mongo_helper.rb', line 48

def normalize_stats(stats)
  r={}
  stats.each do |k,v|
    if k =~ /size/i
      if v.kind_of?(Hash)
        v.each do |ki,vi|
          v[ki]="%0.2f MB" % (vi.to_f/1024**2)
        end
        r[k] = v
      else
        r[k]="%0.2f MB" % (v.to_f/1024**2)
      end
    else
      r[k]=v
    end
  end
  r
end

#ops_per_secondObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mongobile/mongo_helper.rb', line 71

def ops_per_second
  c1 = c2 = nil
  delay = Benchmark.realtime do
    c1 = server_status["opcounters"]
  end
  t1 = Time.now

  sleep 1
  delay += Benchmark.realtime do
    c2 = server_status["opcounters"]
  end
  t2 = Time.now

  ops_average = {"query" => 0, "insert" => 0, "command" => 0, "update" => 0, "getmore" => 0, "delete" => 0}
  ops_average.keys.each do |k|
    ops_average[k] = ((c2[k] - c1[k])/(t2-t1-delay)).round
  end


#       4.times do
#         delays << Benchmark.realtime do
#           counters << server_status["opcounters"]
#         end
#         time_list << Time.now
#
#         sleep 1
#       end
#
#       ops_average = {"query" => 0, "insert" => 0, "command" => 0, "update" => 0, "getmore" => 0, "delete" => 0}
#       time_average = 0
#       1.upto(counters.size-1) do |index|
#         ops_average.keys.each do |k|
#           ops_average[k] += (counters[index][k] - counters[index-1][k])
#         end
#         time_average += (time_list[index] - time_list[index-1]) - delays[index]
#       end
#
#       time_average /= counters.size
#
#       ops_average.each do |k,v|
#         ops_average[k] = ((ops_average[k] / counters.size)/time_average).round
#       end

  ops_average
end

#query_optionsObject



31
32
33
34
35
36
37
38
# File 'lib/mongobile/mongo_helper.rb', line 31

def query_options
  options = {}
  options[:limit] = params[:limit].nil? ? 25 : params[:limit].to_i
  options[:sort] = [["_id", (params[:descending] == "true" ? 'descending' : 'ascending')]]
  options[:skip] = params[:skip].nil? ? 0 : params[:skip].to_i

  options
end

#server_statusObject



67
68
69
# File 'lib/mongobile/mongo_helper.rb', line 67

def server_status
  db("test").command(:serverStatus => 1)
end