Module: MagentWeb::MongoHelper
- Included in:
- App
- Defined in:
- lib/magent_web/mongo_helper.rb
Instance Method Summary collapse
- #channel_name_for(queue_id) ⇒ Object
- #document_list(collection, query = {}) ⇒ Object
- #file_size(database) ⇒ Object
- #humanize(v, quote = true) ⇒ Object
- #humanize_messages(messages) ⇒ Object
- #normalize_stats(stats) ⇒ Object
- #query_options ⇒ Object
- #queues ⇒ Object
- #server_status ⇒ Object
Instance Method Details
#channel_name_for(queue_id) ⇒ Object
80 81 82 |
# File 'lib/magent_web/mongo_helper.rb', line 80 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, ) 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 = true) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/magent_web/mongo_helper.rb', line 54 def humanize(v, quote = true) if v.nil? && quote "null" elsif v.kind_of?(Hash) JSON.pretty_generate(v) elsif v.kind_of?(Array) JSON.pretty_generate(v) 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
70 71 72 73 74 75 76 77 78 |
# File 'lib/magent_web/mongo_helper.rb', line 70 def () return "" if !.kind_of?(Array) .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_options ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/magent_web/mongo_helper.rb', line 14 def = {} [:limit] = params[:limit].nil? ? 25 : params[:limit].to_i [:sort] = [["_id", (params[:descending] == "true" ? 'descending' : 'ascending')]] [:skip] = params[:skip].nil? ? 0 : params[:skip].to_i end |
#queues ⇒ Object
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$/ q << collection end end q end |
#server_status ⇒ Object
50 51 52 |
# File 'lib/magent_web/mongo_helper.rb', line 50 def server_status Magent.database.command(:serverStatus => 1) end |