Module: MagentWeb::MongoHelper

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

Instance Method Summary collapse

Instance Method Details

#channel_name_for(queue_id) ⇒ Object



84
85
86
# File 'lib/magent_web/mongo_helper.rb', line 84

def channel_name_for(queue_id)
  queue_id.to_s.match("magent\.([^\.]+)")[1]
end

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



23
24
25
# File 'lib/magent_web/mongo_helper.rb', line 23

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

#file_size(database) ⇒ Object



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

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

#humanize(v, quote = false) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/magent_web/mongo_helper.rb', line 54

def humanize(v, quote = false)
  if v.nil? && quote
    "null"
  elsif v.kind_of?(Hash)
    buffer = ""
    v.each do |k,v|
      buffer << "#{k.inspect}: #{v.inspect} <br />"
    end
    buffer
  elsif v.kind_of?(Array)
    v.map{|e| e.nil? ? "null" : e }.join("<br />")
  elsif v.kind_of?(Time)
    v.strftime("%d %B %Y %H:%M:%S").inspect
  elsif quote
    v.inspect
  else
    v
  end
end

#humanize_messages(messages) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/magent_web/mongo_helper.rb', line 74

def humanize_messages(messages)
  return "" if !messages.kind_of?(Array)

  messages.map do |e|
    name = e.first
    args = e.last.join(", ")
    "#{name}(#{args})"
  end.join(" -> ")
end

#normalize_stats(stats) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/magent_web/mongo_helper.rb', line 31

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

#query_optionsObject



14
15
16
17
18
19
20
21
# File 'lib/magent_web/mongo_helper.rb', line 14

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

#queue_path(queue) ⇒ Object



88
89
90
91
92
# File 'lib/magent_web/mongo_helper.rb', line 88

def queue_path(queue)
  queue = queue.name if !queue.kind_of?(String)

  "#{ENV["MAGENT_WEB_PATH"]}/queues/#{queue}"
end

#queuesObject



3
4
5
6
7
8
9
10
11
12
# File 'lib/magent_web/mongo_helper.rb', line 3

def queues
  q = []
  Magent.database.collections.each do |collection|
    if collection.name =~ /^magent\./ && collection.name !~ /(errors|stats)$/
      q << collection
    end
  end

  q
end

#server_statusObject



50
51
52
# File 'lib/magent_web/mongo_helper.rb', line 50

def server_status
  Magent.database.command(:serverStatus => 1)
end