forked from AlekSi/mongo_analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.rb
More file actions
113 lines (90 loc) · 3.32 KB
/
Copy pathcore.rb
File metadata and controls
113 lines (90 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
#####
# Copyright 2011 Lennart Koopmann <lennart@socketfeed.com>
#
# This file is part of mongo_analyzer.
#
# mongo_analyzer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# mongo_analyzer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with mongo_analyzer. If not, see <http://www.gnu.org/licenses/>.
#####
require 'erb'
require 'uri'
require 'yaml'
config = YAML.load_file("config.yml")
db = Mongo::Connection.new(config["mongodb"]["host"], config["mongodb"]["port"], {:slave_ok => true}).db(config["mongodb"]["database"])
if config["mongodb"]["use_auth"]
db.authenticate(config["mongodb"]["username"], config["mongodb"]["password"], true)
end
skipped_collections = ["system.users", "system.indexes", "system.profile"]
get '/' do
@database_name = config["mongodb"]["database"]
@collection_names = db.collection_names
skipped_collections.each { |collection| @collection_names.delete(collection) }
case db.profiling_level
when :off then @profiling_level = "Off"
when :slow_only then @profiling_level = "Slow only"
when :all then @profiling_level = "Full"
else @profiling_level = "Unknown"
end
# Slow queries. (Converted to array and flipped for better readable order)
@slow_queries = db.profiling_info.to_a.reverse
@stats = db.stats
erb :home
end
get '/indexes/:collection' do
coll = db.collection(params[:collection])
@indexes = coll.index_information
erb :indexes
end
# should be a post, but fuck this shit.
get '/profiling_level/:what' do
allowed_types = [:off, :slow_only, :all]
what = params[:what].to_sym
if allowed_types.include?(what)
db.profiling_level = what
redirect "/"
else
"Not allowed profiling level: #{what}. Allowed: #{allowed_types.inspect}"
end
end
get '/clear_query_log' do
# We must disable profiling before dropping the profile collection.
old_profile_level = db.profiling_level
db.profiling_level = :off
# Drop the logs.
db.collection("system.profile").drop
# Set profiling level back to old state.
db.profiling_level = old_profile_level
redirect "/"
end
get '/drop_index/:collection/:index' do
coll = db.collection(params[:collection])
coll.drop_index(params[:index])
redirect "/indexes/#{URI.escape(params[:collection])}"
end
post '/add_index/:collection' do
if params[:ordering] == nil or params[:ordering].length == 0
ordering = Mongo::ASCENDING
else
case (params[:ordering])
when "ascending" then ordering = Mongo::ASCENDING
when "descending" then ordering = Mongo::DESCENDING
else ordering = Mongo::ASCENDING
end
end
options = Hash.new
options[:unique] = true if params[:unique] != nil and params[:unique] == "true"
options[:background] = true if params[:background] != nil and params[:background] == "true"
coll = db.collection(params[:collection])
coll.create_index([[params[:index], ordering]], options)
redirect "/indexes/#{URI.escape(params[:collection])}"
end