Class: Mutx::Database::MongoConnector

Inherits:
Object
  • Object
show all
Defined in:
lib/mutx/database/mongo_connector.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {host: "localhost", port: 27017, username: nil, pass: nil}) ⇒ MongoConnector

include Mongo



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mutx/database/mongo_connector.rb', line 10

def initialize(opts={host: "localhost", port: 27017, username: nil, pass: nil})
  set_client(opts)
  set_db
  authenticate(opts)
  set_task_collection
  set_custom_param_collection
  set_config_collection
  set_results_collection
  set_commits_collection
  set_documentation_collection
end

Class Method Details

.active_tasksObject



282
283
284
# File 'lib/mutx/database/mongo_connector.rb', line 282

def self.active_tasks
  self.all_tasks
end

.all_resultsArray

Returns all result

Returns:

  • (Array)

    results from results coll



451
452
453
# File 'lib/mutx/database/mongo_connector.rb', line 451

def self.all_results
  @@results.find({}).sort('_id' => -1).to_a
end

.all_results_idsObject



455
456
457
# File 'lib/mutx/database/mongo_connector.rb', line 455

def self.all_results_ids
  @@results.find({},{"_id" => 1}, :sort => ['_id', -1]).to_a
end

.all_tasksObject

Returns all active tasks



256
257
258
# File 'lib/mutx/database/mongo_connector.rb', line 256

def self.all_tasks
  self.tasks
end

.all_testsObject



260
261
262
# File 'lib/mutx/database/mongo_connector.rb', line 260

def self.all_tests
  self.tasks "test"
end

.clean_documentationObject

Removes all documents of documentation from the DB



83
84
85
86
87
# File 'lib/mutx/database/mongo_connector.rb', line 83

def self.clean_documentation
  Mutx::Support::Log.debug "Cleanning db documentation collection" if Mutx::Support::Log
  @@documentation.drop
  # @@db.drop_collection("documentation")
end

.collectionsObject

Returns a list of collections



141
142
143
# File 'lib/mutx/database/mongo_connector.rb', line 141

def self.collections
  ["tasks","results","custom_params","commit","configuration"]
end

.configurationObject



480
481
482
483
# File 'lib/mutx/database/mongo_connector.rb', line 480

def self.configuration
  Mutx::Support::Log.debug "Getting db configuration data" if Mutx::Support::Log
  @@configuration.find({}).to_a.first
end

.connected?Boolean

COMMONS

Returns:

  • (Boolean)


127
128
129
130
131
132
133
# File 'lib/mutx/database/mongo_connector.rb', line 127

def self.connected?
  begin
    @@db and true
  rescue
    false
  end
end

.cron_tasksObject

Get cronneable tasks



248
249
250
251
252
253
# File 'lib/mutx/database/mongo_connector.rb', line 248

def self.cron_tasks
  Mutx::Support::Log.debug "Getting cronneable tasks" if Mutx::Support::Log
  criteria = {}
  criteria["cronneable"]="on"
  @@tasks.find(criteria).sort("last_result" => -1).to_a
end

.custom_param_for_name(custom_param_name) ⇒ Object



342
343
344
345
# File 'lib/mutx/database/mongo_connector.rb', line 342

def self.custom_param_for_name custom_param_name
  Mutx::Support::Log.debug "Getting db custom param data for custom param name [#{custom_param_name}]" if Mutx::Support::Log
  @@custom_params.find({"name" => custom_param_name}).to_a.first
end

.custom_params_listObject

CUSTOM PARAMS



308
309
310
311
# File 'lib/mutx/database/mongo_connector.rb', line 308

def self.custom_params_list
  Mutx::Support::Log.debug "Getting db custom params list" if Mutx::Support::Log
  @@custom_params.find({}).to_a
end

.delete_custom_param(custom_param_id) ⇒ Object



357
358
359
360
361
362
# File 'lib/mutx/database/mongo_connector.rb', line 357

def self.delete_custom_param custom_param_id
  Mutx::Support::Log.debug "Deleting db custom param for custom param id [#{custom_param_id}]" if Mutx::Support::Log
  custom_param_id = custom_param_id.to_i if custom_param_id.respond_to? :to_i
  @@custom_params.delete_one({"_id" => custom_param_id})
  self.update_tasks_with_custom_param_id(custom_param_id)
end

.delete_task(task_id) ⇒ Object



286
287
288
289
290
291
292
# File 'lib/mutx/database/mongo_connector.rb', line 286

def self.delete_task task_id
  Mutx::Support::Log.debug "Deletting db task with id #{task_id}" if Mutx::Support::Log
  #Fix because MD5
  #task_id = task_id.to_i if task_id.respond_to? :to_i
  res = @@tasks.delete_one({"_id" => task_id})
  res.n==1
end

.drop_collections(hard = false) ⇒ Object

Drops all mutx collections def self.drop_collections hard=false

db = self.mutx_db
collections_to_drop = collections
unless hard
  ["custom_params","tasks"].each{|col| collections_to_drop.delete(col)}
end
collections_to_drop.each do |collection|
  db.drop_collection(collection) if collection != "documentation"
end

end



157
158
159
160
161
162
163
# File 'lib/mutx/database/mongo_connector.rb', line 157

def self.drop_collections hard=false
  collections_to_drop = self.collections
  ["custom_params","tasks"].each{|col| collections_to_drop.delete(col)} unless hard
  collections_to_drop.each do |collection|
    self..drop_collection(collection) if collection != "documentation"
  end
end

.ensure_int(value) ⇒ Fixnum

Returns value as Fixnum if it is not

Parameters:

  • value (Object)

Returns:

  • (Fixnum)


444
445
446
447
# File 'lib/mutx/database/mongo_connector.rb', line 444

def self.ensure_int(value)
  value = value.to_i if value.respond_to? :to_i
  value
end

.exist_custom_param_id?(param_id) ⇒ Boolean

Returns:

  • (Boolean)


351
352
353
354
355
# File 'lib/mutx/database/mongo_connector.rb', line 351

def self.exist_custom_param_id? param_id
  param_id = param_id.to_i if param_id.respond_to? :to_i
  # !@@custom_params.find({"_id" => param_id}).nil?
  @@custom_params.find({"_id" => param_id})
end

.exist_custom_param_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


347
348
349
# File 'lib/mutx/database/mongo_connector.rb', line 347

def self.exist_custom_param_name? name
  !self.param_id_for_name(name).nil?
end

.exist_task_with_custom_param_id?(custom_param_id) ⇒ Boolean

Returns:

  • (Boolean)


370
371
372
# File 'lib/mutx/database/mongo_connector.rb', line 370

def self.exist_task_with_custom_param_id? custom_param_id
  !self.tasks_with_custom_param_id(custom_param_id).empty?
end

.find_results_for_key(key) ⇒ Object



459
460
461
# File 'lib/mutx/database/mongo_connector.rb', line 459

def self.find_results_for_key key
  @@results.find({$or => [{"task.name" => /#{key}/}, {"execution_name" => /#{key}/ }, {"summary" => /#{key}/ }, {"command" => /#{key}/ }]}).to_a
end

.find_results_for_status(status) ⇒ Object



467
468
469
# File 'lib/mutx/database/mongo_connector.rb', line 467

def self.find_results_for_status status
  @@results.find({$or => [{"summary" => /#{status}/}, {"status" => /#{status}/ }]},{"_id" => 1}).to_a
end

.generate_idObject



135
136
137
138
# File 'lib/mutx/database/mongo_connector.rb', line 135

def self.generate_id
  value = Time.now.to_f.to_s.gsub( ".","")[0..12].to_i
  Digest::MD5.hexdigest(value.to_s) #Added MD5 to generate a mongo id
end

.get_all_documentationObject

Return an array with all documents of documentation



96
97
98
99
# File 'lib/mutx/database/mongo_connector.rb', line 96

def self.get_all_documentation
  Mutx::Support::Log.debug "getting db all documentation" if Mutx::Support::Log
  @@documentation.find().to_a
end

.get_custom_param(custom_param_id) ⇒ Hash

Returns the entire record for a given id

Parameters:

  • custom_param_id (String)

Returns:

  • (Hash)

    all custom param data



329
330
331
332
333
334
# File 'lib/mutx/database/mongo_connector.rb', line 329

def self.get_custom_param custom_param_id
  Mutx::Support::Log.debug "Getting db custom param data for custom param id #{custom_param_id}" if Mutx::Support::Log
  custom_param_id = custom_param_id.to_i if custom_param_id.respond_to? :to_i

  res = @@custom_params.find({"_id" => custom_param_id}).to_a.first
end

.help_body(page) ⇒ Object

Returns the body html of a page



102
103
104
105
106
# File 'lib/mutx/database/mongo_connector.rb', line 102

def self.help_body page
  Mutx::Support::Log.debug "getting db help body" if Mutx::Support::Log
  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



116
117
118
119
# File 'lib/mutx/database/mongo_connector.rb', line 116

def self.help_search title
  Mutx::Support::Log.debug "Searching on db help colection for title #{title}" if Mutx::Support::Log
  @@documentation.find({:$or => [{ "title" => /#{title}/ },{ "title" => /#{title.upcase}/ },{ "body" => /#{title}/ }]}).to_a
end

.help_title(page) ⇒ Object

Returns the title of a page



109
110
111
112
113
# File 'lib/mutx/database/mongo_connector.rb', line 109

def self.help_title page
  Mutx::Support::Log.debug "getting db help title" if Mutx::Support::Log        
  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

Parameters:

  • commit_info (Hash)

    => Fixnum, “commit_id” => String, “info” => String



178
179
180
# File 'lib/mutx/database/mongo_connector.rb', line 178

def self.insert_commit commit_info
  @@commits.insert_one({"_id" => self.generate_id, "log" => commit_info})
end

.insert_config(config_object) ⇒ Object

3 CONFIG



475
476
477
478
# File 'lib/mutx/database/mongo_connector.rb', line 475

def self.insert_config config_object
  Mutx::Support::Log.debug "db insert configuration [#{document}]" if Mutx::Support::Log
  @@configuration.insert_one(config_object)
end

.insert_custom_param(custom_param_data) ⇒ Object



313
314
315
316
# File 'lib/mutx/database/mongo_connector.rb', line 313

def self.insert_custom_param custom_param_data
  Mutx::Support::Log.debug "Inserting custom param [#{custom_param_data}]" if Mutx::Support::Log
  @@custom_params.insert_one(custom_param_data)
end

.insert_documentation(document) ⇒ Object

Inserts a document of documentation in the DB



90
91
92
93
# File 'lib/mutx/database/mongo_connector.rb', line 90

def self.insert_documentation document
  Mutx::Support::Log.debug "db insert documentation [#{document}]" if Mutx::Support::Log
  @@documentation.insert_one(document)
end

.insert_result(result_data) ⇒ String

Creates a result data andc returns de id for that register

Parameters:

  • execution_data (Hash)

Returns:

  • (String)

    id for created result



396
397
398
399
400
401
402
403
# File 'lib/mutx/database/mongo_connector.rb', line 396

def self.insert_result(result_data)
  begin
    @@results.insert_one(result_data)
    true
  rescue
    false
  end
end

.insert_task(task_data) ⇒ Object

Inserts a task in tasks collection

Parameters:

  • task_data (Hash)

    (see task_data_structure method)



198
199
200
201
# File 'lib/mutx/database/mongo_connector.rb', line 198

def self.insert_task task_data
  Mutx::Support::Log.debug "Inserting task [#{task_data}]" if Mutx::Support::Log
  @@tasks.insert_one(task_data)
end

.last_commitHash

Returns last saved commit info

Returns:

  • (Hash)

    if exist



184
185
186
187
188
189
190
# File 'lib/mutx/database/mongo_connector.rb', line 184

def self.last_commit
  Mutx::Support::Log.debug "Getting last commit" if Mutx::Support::Log
  data = @@commits.find({}).to_a || []
  unless data.empty?
    data.last["log"]
  end
end

.last_result_for_task(task_id) ⇒ Object



463
464
465
# File 'lib/mutx/database/mongo_connector.rb', line 463

def self.last_result_for_task task_id
  @@results.find({}, :sort => ['_id', -1])
end

.param_id_for_name(custom_param_name) ⇒ Object



336
337
338
339
340
# File 'lib/mutx/database/mongo_connector.rb', line 336

def self.param_id_for_name custom_param_name
  Mutx::Support::Log.debug "Getting db custom param id for custom param name [#{custom_param_name}]" if Mutx::Support::Log
  res = self.custom_param_for_name(custom_param_name)
  res["_id"] if res
end

.required_params_idsObject



366
367
368
# File 'lib/mutx/database/mongo_connector.rb', line 366

def self.required_params_ids
  @@custom_params.find({"required" => true},{:fields => ["_id"]})
end

.result_data_for_id(result_id) ⇒ Object



424
425
426
427
428
429
430
# File 'lib/mutx/database/mongo_connector.rb', line 424

def self.result_data_for_id(result_id)
  #FIXED
  #result_id = self.ensure_int(result_id) #Cant convert MD5 to integer
  res = @@results.find({"_id" => result_id})
  res = res.to_a.first if res.respond_to? :to_a
  res
end

.results_for(task_id) ⇒ Object

Returns all results for a given task_id



406
407
408
409
410
411
# File 'lib/mutx/database/mongo_connector.rb', line 406

def self.results_for task_id
  #FIXED
  #@@results.find({"task.id" => ensure_int(task_id)}).sort("started_at" => -1).to_a #Cant convert MD5 to integer
  @@results.find({"task.id" => (task_id)}).sort("started_at" => -1).to_a

end

.running(type) ⇒ Object



272
273
274
275
# File 'lib/mutx/database/mongo_connector.rb', line 272

def self.running type
  Mutx::Support::Log.debug "Getting db running tasks" if Mutx::Support::Log
  @@tasks.find({"status" => "RUNNING", "type" => type}).to_a
end

.running_for_task(task_name) ⇒ Object



277
278
279
280
# File 'lib/mutx/database/mongo_connector.rb', line 277

def self.running_for_task task_name
  Mutx::Support::Log.debug "Getting db running for task name #{task_name}" if Mutx::Support::Log
  @@tasks.find({"name" => task_name, "status" => "RUNNING"}).to_a
end

.running_resultsObject



436
437
438
# File 'lib/mutx/database/mongo_connector.rb', line 436

def self.running_results
  @@results.find({"status" => /running|started/}).to_a
end

.running_results_for_task_id(task_id) ⇒ Object



432
433
434
# File 'lib/mutx/database/mongo_connector.rb', line 432

def self.running_results_for_task_id task_id
  @@results.find({"task.id" => task_id, "status" => /running|started/}).sort("started_at" => -1).to_a
end

.running_tasksObject



264
265
266
# File 'lib/mutx/database/mongo_connector.rb', line 264

def self.running_tasks
  self.running "task"
end

.running_testsObject



268
269
270
# File 'lib/mutx/database/mongo_connector.rb', line 268

def self.running_tests
  self.running "test"
end

.task_data_for(task_id) ⇒ Hash

Returns the entire record for a given task name

Parameters:

  • task_name (String)

Returns:

  • (Hash)

    all task data



217
218
219
220
221
222
# File 'lib/mutx/database/mongo_connector.rb', line 217

def self.task_data_for task_id
  #task_id = task_id.to_i if task_id.respond_to? :to_i
  res = @@tasks.find({"_id" => task_id}).to_a.first
  Mutx::Support::Log.debug "Getting db task data for task id #{task_id} => res = [#{res}]" if Mutx::Support::Log
  res
end

.task_data_for_name(task_name) ⇒ Object



224
225
226
227
# File 'lib/mutx/database/mongo_connector.rb', line 224

def self.task_data_for_name(task_name)
  Mutx::Support::Log.debug "Getting db task data for name #{task_name}" if Mutx::Support::Log
  @@tasks.find({"name" => task_name}).to_a.first
end

.task_id_for(task_name, type = nil) ⇒ String

Returns the _id for a given task name

Parameters:

  • task_name (String)

Returns:

  • (String)

    _id



232
233
234
235
236
237
238
# File 'lib/mutx/database/mongo_connector.rb', line 232

def self.task_id_for task_name, type=nil
  Mutx::Support::Log.debug "Getting db task id for task name #{task_name} [type: #{type}]" if Mutx::Support::Log
  criteria = {"name" => task_name}
  criteria["type"] = type if type
  res = @@tasks.find(criteria, {:fields => ["_id"]}).to_a.first
  res["_id"] if res
end

.tasks(type = nil) ⇒ Object



240
241
242
243
244
245
# File 'lib/mutx/database/mongo_connector.rb', line 240

def self.tasks type=nil
  Mutx::Support::Log.debug "Getting db tasks list [type: #{type}]" if Mutx::Support::Log
  criteria = {}
  criteria["type"]=type if type
  @@tasks.find(criteria).sort("last_result" => -1).to_a
end

.tasks_names_with_custom_param_id(custom_param_id) ⇒ Object



374
375
376
377
378
# File 'lib/mutx/database/mongo_connector.rb', line 374

def self.tasks_names_with_custom_param_id custom_param_id
  self.tasks_with_custom_param_id(custom_param_id).map do |task|
    task["name"]
  end
end

.tasks_with_custom_param_id(custom_param_id) ⇒ Object



380
381
382
383
384
# File 'lib/mutx/database/mongo_connector.rb', line 380

def self.tasks_with_custom_param_id custom_param_id
  @@tasks.find({}).to_a.select do |task|
    task["custom_params"].include? custom_param_id
  end
end

.update_custom_param(custom_param_data) ⇒ Object

Update record for a given custom param

Parameters:

  • custom_param_data (Hash)


320
321
322
323
324
# File 'lib/mutx/database/mongo_connector.rb', line 320

def self.update_custom_param custom_param_data
  Mutx::Support::Log.debug "Updating db custom param [#{custom_param_data}]" if Mutx::Support::Log
  id = custom_param_data["_id"].to_i if custom_param_data["_id"].respond_to? :to_i
  @@custom_params.update_one( {"_id" => custom_param_data["_id"]}, custom_param_data)
end

.update_result(result_data_structure) ⇒ Object

Updates result register with the given data



415
416
417
418
419
420
421
422
# File 'lib/mutx/database/mongo_connector.rb', line 415

def self.update_result result_data_structure
  begin
    @@results.update_one( {"_id" => result_data_structure["_id"]}, result_data_structure)
    true
  rescue
    false
  end
end

.update_task(task_data) ⇒ Object

Update record for a given task

Parameters:

  • task_data (Hash)


205
206
207
208
209
210
211
212
# File 'lib/mutx/database/mongo_connector.rb', line 205

def self.update_task task_data
  #Fix because MD5 as _id
  #task_data["_id"] = task_data["_id"].to_i
  task_data.delete("action")
  Mutx::Support::Log.debug "Updating task [#{task_data}]" if Mutx::Support::Log
  res = @@tasks.update_one( {"_id" => task_data["_id"]}, task_data)
  res.n == 1
end

.update_tasks_with_custom_param_id(custom_param_id) ⇒ Object



294
295
296
297
298
299
300
# File 'lib/mutx/database/mongo_connector.rb', line 294

def self.update_tasks_with_custom_param_id custom_param_id
  custom_param_id = custom_param_id.to_i if custom_param_id.respond_to? :to_i
  self.tasks_with_custom_param_id(custom_param_id).each do |task_data|
    task_data["custom_params"].delete(custom_param_id)
    self.update_task task_data
  end
end

Instance Method Details

#authenticate(opts) ⇒ Object



38
39
40
41
# File 'lib/mutx/database/mongo_connector.rb', line 38

def authenticate opts
  Mutx::Support::Log.debug "db authenticating" if Mutx::Support::Log
  @@auth = @@db.authenticate(opts[:username], opts[:pass]) if opts[:username] and opts[:pass]
end

#set_client(opts) ⇒ Object



28
29
30
31
# File 'lib/mutx/database/mongo_connector.rb', line 28

def set_client opts
  Mutx::Support::Log.debug "Setting db client" if Mutx::Support::Log
  @@client = Mongo::Client.new("mongodb://#{opts[:host]}:#{opts[:port]}/#{set_db_name}")
end

#set_commits_collectionObject



56
57
58
59
# File 'lib/mutx/database/mongo_connector.rb', line 56

def set_commits_collection
  Mutx::Support::Log.debug "Setting db commits collection" if Mutx::Support::Log
  @@commits   = @@db.collection("commits")
end

#set_config_collectionObject



73
74
75
76
# File 'lib/mutx/database/mongo_connector.rb', line 73

def set_config_collection
  Mutx::Support::Log.debug "Setting db configuration collection" if Mutx::Support::Log
  @@configuration = @@db.collection("configuration")
end

#set_custom_param_collectionObject



50
51
52
53
54
# File 'lib/mutx/database/mongo_connector.rb', line 50

def set_custom_param_collection
  Mutx::Support::Log.debug "Setting db custom param collection" if Mutx::Support::Log
  @@custom_params  = @@db.collection("custom_params")
  # @@custom_params.ensure_index({"name" => 1})
end

#set_dbObject



33
34
35
36
# File 'lib/mutx/database/mongo_connector.rb', line 33

def set_db
  Mutx::Support::Log.debug "Setting db" if Mutx::Support::Log
  @@db = @@client.database
end

#set_db_nameObject



22
23
24
25
26
# File 'lib/mutx/database/mongo_connector.rb', line 22

def set_db_name
  project_name = Dir.pwd.split("/").last
  Mutx::Support::Log.debug "Setting db name: #{project_name}_mutx" if Mutx::Support::Log
  @@db_name = "#{project_name}_mutx"
end

#set_documentation_collectionObject



68
69
70
71
# File 'lib/mutx/database/mongo_connector.rb', line 68

def set_documentation_collection
  Mutx::Support::Log.debug "Setting db documentation collection" if Mutx::Support::Log
  @@documentation = @@db.collection("documentation")
end

#set_results_collectionObject



61
62
63
64
65
66
# File 'lib/mutx/database/mongo_connector.rb', line 61

def set_results_collection
  Mutx::Support::Log.debug "Setting db results collection" if Mutx::Support::Log
  @@results   = @@db.collection("results")
  # @@results.ensure_index({"started_at" => 1})
  # @@results.ensure_index({"_id" => 1})
end

#set_task_collectionObject



43
44
45
46
47
48
# File 'lib/mutx/database/mongo_connector.rb', line 43

def set_task_collection
  Mutx::Support::Log.debug "Setting db tasks collection" if Mutx::Support::Log
  @@tasks  = @@db.collection("tasks")
  # @@tasks.ensure_index({"name" => 1})
  # @@tasks.indexes.create_one({ "name" => 1 }, :unique => true)
end