Class: Kaya::Database::MongoConnector
- Inherits:
-
Object
- Object
- Kaya::Database::MongoConnector
- Includes:
- Mongo
- Defined in:
- lib/kaya/database/mongo_connector.rb
Class Method Summary collapse
- .active_suites ⇒ Object
-
.all_results ⇒ Array
Returns all result.
- .all_results_ids ⇒ Object
-
.all_suites ⇒ Object
Returns all active suites.
-
.clean_documentation ⇒ Object
Removes all documents of documentation from the DB.
-
.collections ⇒ Object
Returns a list of collections.
-
.connected? ⇒ Boolean
COMMONS.
-
.drop_collections ⇒ Object
Drops all kaya collections.
-
.ensure_int(value) ⇒ Fixnum
Returns value as Fixnum if it is not.
- .find_results_for_ip(ip) ⇒ Object
- .find_results_for_key(key) ⇒ Object
- .find_results_for_status(status) ⇒ Object
- .generate_id ⇒ Object
-
.get_all_documentation ⇒ Object
Return an array with all documents of documentation.
-
.help_body(page) ⇒ Object
Returns the body html of a page.
-
.help_search(title) ⇒ Object
Returns a document from the DB for a certain title.
-
.help_title(page) ⇒ Object
Returns the title of a page.
-
.insert_commit(commit_info) ⇒ Object
Saves commit information.
-
.insert_documentation(document) ⇒ Object
Inserts a document of documentation in the DB.
-
.insert_result(result_data) ⇒ String
Creates a result data andc returns de id for that register.
-
.insert_suite(suite_data) ⇒ Object
Inserts a suite in suites collection.
- .kaya_db ⇒ Object
-
.last_commit ⇒ Hash
Returns last saved commit info.
- .result_data_for_id(result_id) ⇒ Object
-
.results_for(suite_id) ⇒ Object
Returns all results for a given suite_id.
- .results_for_suite_id_and_ip(suite_id, ip) ⇒ Object
-
.suite_data_for(suite_id) ⇒ Hash
Returns the entire record for a given suite name.
- .suite_data_for_name(suite_name) ⇒ Object
-
.suite_id_for(suite_name, active = nil) ⇒ String
Returns the _id for a given suite name.
- .suites(active = true) ⇒ Object
-
.update_result(result_data_structure) ⇒ Object
Updates result register with the given data.
-
.update_suite(suite_data) ⇒ Object
Update record for a given suite.
Instance Method Summary collapse
- #authenticate(opts) ⇒ Object
-
#initialize(opts = {host: "localhost", port: 27017, username: nil, pass: nil}) ⇒ MongoConnector
constructor
A new instance of MongoConnector.
- #set_client(opts) ⇒ Object
- #set_commits_collection ⇒ Object
- #set_db ⇒ Object
- #set_db_name ⇒ Object
- #set_documentation_collection ⇒ Object
- #set_results_collection ⇒ Object
- #set_suite_collection ⇒ Object
Constructor Details
#initialize(opts = {host: "localhost", port: 27017, username: nil, pass: nil}) ⇒ MongoConnector
Returns a new instance of MongoConnector.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/kaya/database/mongo_connector.rb', line 9 def initialize(opts={host: "localhost", port: 27017, username: nil, pass: nil}) set_db_name set_client(opts) set_db authenticate(opts) set_suite_collection set_results_collection set_commits_collection set_documentation_collection end |
Class Method Details
.active_suites ⇒ Object
200 201 202 |
# File 'lib/kaya/database/mongo_connector.rb', line 200 def self.active_suites self.suites true end |
.all_results ⇒ Array
Returns all result
269 270 271 272 273 274 275 276 277 |
# File 'lib/kaya/database/mongo_connector.rb', line 269 def self.all_results @@results.ensure_index({"started_at" => 1, "_id" => 1}) res = @@results.find({}, :sort => ['_id', -1]) if res res.to_a else [] end end |
.all_results_ids ⇒ Object
279 280 281 |
# File 'lib/kaya/database/mongo_connector.rb', line 279 def self.all_results_ids all_results.map{|res| res['_id']} end |
.all_suites ⇒ Object
Returns all active suites
196 197 198 |
# File 'lib/kaya/database/mongo_connector.rb', line 196 def self.all_suites @@suites.find({"active" => true}).to_a end |
.clean_documentation ⇒ Object
Removes all documents of documentation from the DB
63 64 65 |
# File 'lib/kaya/database/mongo_connector.rb', line 63 def self.clean_documentation @@db.drop_collection("documentation") end |
.collections ⇒ Object
Returns a list of collections
113 114 115 |
# File 'lib/kaya/database/mongo_connector.rb', line 113 def self.collections ["suites","results"] end |
.connected? ⇒ Boolean
COMMONS
100 101 102 103 104 105 106 |
# File 'lib/kaya/database/mongo_connector.rb', line 100 def self.connected? begin @@db and true rescue false end end |
.drop_collections ⇒ Object
Drops all kaya collections
118 119 120 121 122 123 |
# File 'lib/kaya/database/mongo_connector.rb', line 118 def self.drop_collections db = self.kaya_db collections.each do |collection| db.drop_collection(collection) if collection != "documentation" end end |
.ensure_int(value) ⇒ Fixnum
Returns value as Fixnum if it is not
263 264 265 |
# File 'lib/kaya/database/mongo_connector.rb', line 263 def self.ensure_int(value) value and value.to_i if value.respond_to? :to_i end |
.find_results_for_ip(ip) ⇒ Object
292 293 294 |
# File 'lib/kaya/database/mongo_connector.rb', line 292 def self.find_results_for_ip ip @@results.find({"ip" => ip}, :sort =>["started_at", -1]).to_a end |
.find_results_for_key(key) ⇒ Object
283 284 285 286 287 288 289 290 |
# File 'lib/kaya/database/mongo_connector.rb', line 283 def self.find_results_for_key key all_actual_results = self.all_results if !all_actual_results.empty? all_actual_results.select do |result| result["suite"]["name"].include?(key) or result["execution_name"].include?(key) or result["summary"].include?(key) or result["command"].include?(key) end end end |
.find_results_for_status(status) ⇒ Object
296 297 298 299 300 301 302 303 |
# File 'lib/kaya/database/mongo_connector.rb', line 296 def self.find_results_for_status status all_actual_results = self.all_results if !all_actual_results.empty? all_actual_results.select do |result| result["summary"].include?(status) or result["status"].include?(status) end.map{|result| result["_id"]} end end |
.generate_id ⇒ Object
108 109 110 |
# File 'lib/kaya/database/mongo_connector.rb', line 108 def self.generate_id Time.now.to_f.to_s.gsub( ".","")[0..12].to_i end |
.get_all_documentation ⇒ Object
Return an array with all documents of documentation
73 74 75 |
# File 'lib/kaya/database/mongo_connector.rb', line 73 def self.get_all_documentation @@documentation.find().to_a end |
.help_body(page) ⇒ Object
Returns the body html of a page
78 79 80 81 |
# File 'lib/kaya/database/mongo_connector.rb', line 78 def self.help_body page result = @@documentation.find({"title" => page}).to_a.first result["body"].to_s if result != nil end |
.help_search(title) ⇒ Object
Returns a document from the DB for a certain title
90 91 92 |
# File 'lib/kaya/database/mongo_connector.rb', line 90 def self.help_search title @@documentation.find({:$or => [{ "title" => /#{title}/ },{ "title" => /#{title.upcase}/ },{ "body" => /#{title}/ }]}).to_a end |
.help_title(page) ⇒ Object
Returns the title of a page
84 85 86 87 |
# File 'lib/kaya/database/mongo_connector.rb', line 84 def self.help_title page result = @@documentation.find({"title" => page}).to_a.first result["title"].to_s.gsub('_', ' ').capitalize if result != nil end |
.insert_commit(commit_info) ⇒ Object
Saves commit information
138 139 140 |
# File 'lib/kaya/database/mongo_connector.rb', line 138 def self.insert_commit commit_info @@commits.insert({"_id" => self.generate_id, "log" => commit_info}) end |
.insert_documentation(document) ⇒ Object
Inserts a document of documentation in the DB
68 69 70 |
# File 'lib/kaya/database/mongo_connector.rb', line 68 def self.insert_documentation document @@documentation.insert(document) end |
.insert_result(result_data) ⇒ String
Creates a result data andc returns de id for that register
215 216 217 218 219 220 221 222 |
# File 'lib/kaya/database/mongo_connector.rb', line 215 def self.insert_result(result_data) begin @@results.insert(result_data) true rescue false end end |
.insert_suite(suite_data) ⇒ Object
Inserts a suite in suites collection
155 156 157 |
# File 'lib/kaya/database/mongo_connector.rb', line 155 def self.insert_suite suite_data @@suites.insert(suite_data) end |
.kaya_db ⇒ Object
125 126 127 |
# File 'lib/kaya/database/mongo_connector.rb', line 125 def self.kaya_db MongoClient.new().db(@@db_name) end |
.last_commit ⇒ Hash
Returns last saved commit info
144 145 146 147 |
# File 'lib/kaya/database/mongo_connector.rb', line 144 def self.last_commit data = @@commits.find({}).to_a.last data["log"] if data end |
.result_data_for_id(result_id) ⇒ Object
255 256 257 |
# File 'lib/kaya/database/mongo_connector.rb', line 255 def self.result_data_for_id(result_id) @@results.find({"_id" => ensure_int(result_id)}).to_a.first end |
.results_for(suite_id) ⇒ Object
Returns all results for a given suite_id
225 226 227 228 229 230 231 232 233 |
# File 'lib/kaya/database/mongo_connector.rb', line 225 def self.results_for suite_id @@results.ensure_index({"started_at" => 1, "_id" => 1}) res = @@results.find({}, :sort => ["started_at", -1]).to_a unless res.empty? res.select{|result| result["suite"]["id"]==ensure_int(suite_id)} else [] end end |
.results_for_suite_id_and_ip(suite_id, ip) ⇒ Object
246 247 248 249 250 251 252 253 |
# File 'lib/kaya/database/mongo_connector.rb', line 246 def self.results_for_suite_id_and_ip suite_id, ip results = self.find_results_for_ip(ip) if results results.select{|result| result["suite"]["id"]==suite_id} else [] end end |
.suite_data_for(suite_id) ⇒ Hash
Returns the entire record for a given suite name
168 169 170 171 |
# File 'lib/kaya/database/mongo_connector.rb', line 168 def self.suite_data_for suite_id suite_id = suite_id.to_i if suite_id.respond_to? :to_i @@suites.find({"_id" => suite_id}).to_a.first end |
.suite_data_for_name(suite_name) ⇒ Object
173 174 175 176 |
# File 'lib/kaya/database/mongo_connector.rb', line 173 def self.suite_data_for_name(suite_name) id = self.suite_id_for(suite_name) @@suites.find({"_id" => id}).to_a.first end |
.suite_id_for(suite_name, active = nil) ⇒ String
Returns the _id for a given suite name
181 182 183 184 185 186 |
# File 'lib/kaya/database/mongo_connector.rb', line 181 def self.suite_id_for suite_name, active=nil criteria = {"name" => suite_name} criteria.store("active",true) if active result = @@suites.find(criteria).to_a.first result.nil? ? nil : result["_id"] end |
.suites(active = true) ⇒ Object
188 189 190 191 192 193 |
# File 'lib/kaya/database/mongo_connector.rb', line 188 def self.suites active=true criteria ={} criteria["active"]=active if active @@suites.ensure_index({"last_result" => 1}) @@suites.find(criteria, :sort => ["last_result", -1]).to_a || [] end |
.update_result(result_data_structure) ⇒ Object
Updates result register with the given data
237 238 239 240 241 242 243 244 |
# File 'lib/kaya/database/mongo_connector.rb', line 237 def self.update_result result_data_structure begin @@results.update( {"_id" => result_data_structure["_id"]}, result_data_structure) true rescue false end end |
.update_suite(suite_data) ⇒ Object
Update record for a given suite
161 162 163 |
# File 'lib/kaya/database/mongo_connector.rb', line 161 def self.update_suite suite_data @@suites.update( {"_id" => suite_data["_id"]}, suite_data) end |
Instance Method Details
#authenticate(opts) ⇒ Object
33 34 35 |
# File 'lib/kaya/database/mongo_connector.rb', line 33 def authenticate opts @@auth = @@db.authenticate(opts[:username], opts[:pass]) if opts[:username] and opts[:pass] end |
#set_client(opts) ⇒ Object
25 26 27 |
# File 'lib/kaya/database/mongo_connector.rb', line 25 def set_client opts @@client = MongoClient.new(opts[:host], opts[:port]) end |
#set_commits_collection ⇒ Object
42 43 44 |
# File 'lib/kaya/database/mongo_connector.rb', line 42 def set_commits_collection @@commits = @@db.collection("commits") end |
#set_db ⇒ Object
29 30 31 |
# File 'lib/kaya/database/mongo_connector.rb', line 29 def set_db @@db = @@client.db(@@db_name) end |
#set_db_name ⇒ Object
20 21 22 23 |
# File 'lib/kaya/database/mongo_connector.rb', line 20 def set_db_name project_name = Dir.pwd.split("/").last @@db_name = "#{project_name}_kaya" end |
#set_documentation_collection ⇒ Object
52 53 54 |
# File 'lib/kaya/database/mongo_connector.rb', line 52 def set_documentation_collection @@documentation = @@db.collection("documentation") end |
#set_results_collection ⇒ Object
46 47 48 49 50 |
# File 'lib/kaya/database/mongo_connector.rb', line 46 def set_results_collection @@results = @@db.collection("results") @@results.ensure_index({"_id" => 1}) @@results.ensure_index({"started_at" => 1, "_id" => 1}) end |
#set_suite_collection ⇒ Object
37 38 39 40 |
# File 'lib/kaya/database/mongo_connector.rb', line 37 def set_suite_collection @@suites = @@db.collection("suites") @@suites.ensure_index({"last_result" => 1}) end |