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
21
22
23
24
25
26
27
28
29
# File 'lib/mutx/database/mongo_connector.rb', line 10

def initialize(opts={host: "localhost", port: 27017, username: nil, pass: nil})
  project_name = Dir.pwd.split("/").last
  mongo_connection = MongoConnection.instance
  mongo_connection.client(host: opts[:host],
                          port: opts[:port],
                          database: "#{project_name}_mutx",
                          username: opts[:username],
                          password: opts[:pass])

  $db = mongo_connection.db
  set_task_collection
  set_custom_param_collection
  set_config_collection
  set_results_collection
  set_commits_collection
  set_documentation_collection
  set_input_collection
  set_repos_collection
  set_logger
end

Class Method Details

.active_tasksObject



373
374
375
# File 'lib/mutx/database/mongo_connector.rb', line 373

def self.active_tasks
  self.all_tasks
end

.all_reposObject

REPOS



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

def self.all_repos
  res = $repos.find({}).sort({"repo_name" => 1})
  res.to_a if res.respond_to? :to_a
  res
end

.all_resultsObject

Returns all result $return [Array] results from results coll



612
613
614
# File 'lib/mutx/database/mongo_connector.rb', line 612

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

.all_results_idsObject



616
617
618
# File 'lib/mutx/database/mongo_connector.rb', line 616

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

.all_tasksObject

Returns all active tasks



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

def self.all_tasks
  self.tasks
end

.all_testsObject



287
288
289
# File 'lib/mutx/database/mongo_connector.rb', line 287

def self.all_tests
  self.tasks "test"
end

.before_last_commit_webhookObject



195
196
197
198
199
200
# File 'lib/mutx/database/mongo_connector.rb', line 195

def self.before_last_commit_webhook
  data = $commits.find({}).to_a || []
  unless data.empty?
    data[data.size - 2]
  end
end

.clean_documentationObject

Removes all documents of documentation from the DB



106
107
108
109
110
# File 'lib/mutx/database/mongo_connector.rb', line 106

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

.closeObject



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

def self.close
  MongoConnection.instance.close
end

.collectionsObject

Returns a list of collections



169
170
171
# File 'lib/mutx/database/mongo_connector.rb', line 169

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

.configurationObject



654
655
656
657
# File 'lib/mutx/database/mongo_connector.rb', line 654

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)


150
151
152
153
154
155
156
# File 'lib/mutx/database/mongo_connector.rb', line 150

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

.cron_tasksObject

Get cronneable tasks



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

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



504
505
506
507
# File 'lib/mutx/database/mongo_connector.rb', line 504

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



471
472
473
474
# File 'lib/mutx/database/mongo_connector.rb', line 471

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



518
519
520
521
522
523
# File 'lib/mutx/database/mongo_connector.rb', line 518

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_params.delete_one({"_id" => custom_param_id})

  self.update_tasks_with_custom_param_id(custom_param_id)
end

.delete_repo(repo_id) ⇒ Object



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

def self.delete_repo repo_id
  Mutx::Support::Log.debug "REPO BORRADO" if Mutx::Support::Log
  res = $repos.delete_one({"_id" => repo_id})
  res.n==1
end

.delete_started_result(result_id) ⇒ Object



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

def self.delete_started_result result_id
  Mutx::Support::Log.debug "Deletting db started result with id #{result_id}" if Mutx::Support::Log
  #Fix because MD5
  #task_id = task_id.to_i if task_id.respond_to? :to_i
  res = $results.delete_one({"_id" => result_id})
  res.n==1
end

.delete_task(task_id) ⇒ Object



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

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



173
174
175
176
177
178
179
# File 'lib/mutx/database/mongo_connector.rb', line 173

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) ⇒ Object

Returns value as Fixnum if it is not $param [Object] value $return [Fixnum]



605
606
607
608
# File 'lib/mutx/database/mongo_connector.rb', line 605

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)


513
514
515
516
# File 'lib/mutx/database/mongo_connector.rb', line 513

def self.exist_custom_param_id? param_id
  # !$custom_params.find({"_id" => param_id}).nil?
  $custom_params.find({"_id" => param_id})
end

.exist_custom_param_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


509
510
511
# File 'lib/mutx/database/mongo_connector.rb', line 509

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)


531
532
533
# File 'lib/mutx/database/mongo_connector.rb', line 531

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

.file_attached(result_id) ⇒ Object



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

def self.file_attached result_id
  $results.update_one({"_id" => result_id}, {"$set" => {"file_attached" => "yes"}})
end

.find_results_for_key(key) ⇒ Object



620
621
622
# File 'lib/mutx/database/mongo_connector.rb', line 620

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



633
634
635
# File 'lib/mutx/database/mongo_connector.rb', line 633

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

.find_results_for_task(task_name) ⇒ Object



628
629
630
631
# File 'lib/mutx/database/mongo_connector.rb', line 628

def self.find_results_for_task task_name
  task_id = self.task_id_for task_name
  self.results_for task_id
end

.force_closeObject



42
43
44
# File 'lib/mutx/database/mongo_connector.rb', line 42

def self.force_close
  $client.close if $client
end

.generate_idObject



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

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

.generate_tokenObject



163
164
165
166
# File 'lib/mutx/database/mongo_connector.rb', line 163

def self.generate_token
  value = Time.now.to_f.to_s.gsub( ".","")[0..12].to_i + 10
  Digest::MD5.hexdigest(value.to_s)
end

.get_all_documentationObject

Return an array with all documents of documentation



119
120
121
122
# File 'lib/mutx/database/mongo_connector.rb', line 119

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) ⇒ Object

Returns the entire record for a given id $param [String] custom_param_id $return [Hash] all custom param data



492
493
494
495
496
# File 'lib/mutx/database/mongo_connector.rb', line 492

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

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

.help_body(page) ⇒ Object

Returns the body html of a page



125
126
127
128
129
# File 'lib/mutx/database/mongo_connector.rb', line 125

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



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

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



132
133
134
135
136
# File 'lib/mutx/database/mongo_connector.rb', line 132

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

.input_data_for_id(id) ⇒ Object



404
405
406
407
408
# File 'lib/mutx/database/mongo_connector.rb', line 404

def self.input_data_for_id(id)
  res = $inputs.find({"_id" => id})
  res = res.to_a.first if res.respond_to? :to_a
  res
end

.insert_commit(commit_info) ⇒ Object

Saves commit information $param [Hash] commit_info = => Fixnum, “commit_id” => String, “info” => String



191
192
193
# File 'lib/mutx/database/mongo_connector.rb', line 191

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

.insert_config(config_object) ⇒ Object

3 CONFIG



649
650
651
652
# File 'lib/mutx/database/mongo_connector.rb', line 649

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



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

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



113
114
115
116
# File 'lib/mutx/database/mongo_connector.rb', line 113

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

.insert_input(input_data) ⇒ Object



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

def self.insert_input input_data
  Mutx::Support::Log.debug "Inserting input [#{input_data}]" if Mutx::Support::Log
  $inputs.insert_one(input_data)
end

.insert_repo(repo_data) ⇒ Object



421
422
423
424
425
# File 'lib/mutx/database/mongo_connector.rb', line 421

def self.insert_repo repo_data
  res = $repos.find({"repo_name" => repo_data["repo_name"]})
  (Mutx::Support::Log.debug "Inserting repo [#{repo_data}]" if Mutx::Support::Log
  $repos.insert_one(repo_data)) if res.to_a.empty?
end

.insert_result(result_data) ⇒ Object

Creates a result data andc returns de id for that register $param [Hash] execution_data $return [String] id for created result



557
558
559
560
561
562
563
564
# File 'lib/mutx/database/mongo_connector.rb', line 557

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 $param [Hash] task_data (see task_data_structure method)



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

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_commitObject

Returns last saved commit info $return [Hash] if exist



211
212
213
214
215
216
217
# File 'lib/mutx/database/mongo_connector.rb', line 211

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_commit_webhookObject



202
203
204
205
206
207
# File 'lib/mutx/database/mongo_connector.rb', line 202

def self.last_commit_webhook
  data = $commits.find({}).to_a || []
  unless data.empty?
    data.last
  end
end

.last_notified(quantity) ⇒ Object

get last N notified results



360
361
362
363
# File 'lib/mutx/database/mongo_connector.rb', line 360

def self.last_notified(quantity) #get last N notified results
  quantity = 5 if ( (quantity.to_i.eql? 0) || (quantity.to_i <= 0) ) 
  $results.find({"notified" => "yes"}).limit(quantity.to_i).sort({"started_at" => -1}).to_a
end

.last_result_for_task(task_id) ⇒ Object



624
625
626
# File 'lib/mutx/database/mongo_connector.rb', line 624

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

.mark_notified(result_id) ⇒ Object



356
357
358
# File 'lib/mutx/database/mongo_connector.rb', line 356

def self.mark_notified result_id
  $results.update_one({"_id" => result_id}, {"$set" => {"notified" => "yes"}})
end

.only_running_for_task(task_name) ⇒ Object



334
335
336
337
# File 'lib/mutx/database/mongo_connector.rb', line 334

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

.only_started_for_task(task_name) ⇒ Object



317
318
319
320
# File 'lib/mutx/database/mongo_connector.rb', line 317

def self.only_started_for_task task_name
  Mutx::Support::Log.debug "Getting db running for task name #{task_name}" if Mutx::Support::Log
  $results.find({"status" => "started", "task.name" => task_name}).to_a
end

.param_id_for_name(custom_param_name) ⇒ Object



498
499
500
501
502
# File 'lib/mutx/database/mongo_connector.rb', line 498

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

.repo_data_for_id(id) ⇒ Object



452
453
454
455
456
# File 'lib/mutx/database/mongo_connector.rb', line 452

def self.repo_data_for_id(id)
  res = $repos.find({"_id" => id})
  res = res.to_a.first if res.respond_to? :to_a
  res
end

.repo_data_for_name(name) ⇒ Object



446
447
448
449
450
# File 'lib/mutx/database/mongo_connector.rb', line 446

def self.repo_data_for_name(name)
  res = $repos.find({"repo_name" => name})
  res = res.to_a.first if res.respond_to? :to_a
  res
end

.required_params_idsObject



527
528
529
# File 'lib/mutx/database/mongo_connector.rb', line 527

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

.result_data_for_id(result_id) ⇒ Object



585
586
587
588
589
590
591
# File 'lib/mutx/database/mongo_connector.rb', line 585

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



567
568
569
570
571
572
# File 'lib/mutx/database/mongo_connector.rb', line 567

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



304
305
306
307
308
309
# File 'lib/mutx/database/mongo_connector.rb', line 304

def self.running type
  Mutx::Support::Log.debug "Getting db running tasks" if Mutx::Support::Log
  $results.find({"status" => /started|running/, "task.type" => type}).to_a.map do |result|
    self.task_data_for result["task"]["id"]
  end.uniq
end

.running_for_task(task_name) ⇒ Object

Cron_start



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

def self.running_for_task task_name
  Mutx::Support::Log.debug "Getting db running for task name #{task_name}" if Mutx::Support::Log
  $results.find({"status" => /started|running/, "task.name" => task_name}).to_a
end

.running_nowObject



299
300
301
302
# File 'lib/mutx/database/mongo_connector.rb', line 299

def self.running_now
  Mutx::Support::Log.debug "Getting db running tasks" if Mutx::Support::Log
  $results.find({"status" => /started|running/}).to_a
end

.running_resultsObject



597
598
599
# File 'lib/mutx/database/mongo_connector.rb', line 597

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

.running_results_for_task_id(task_id) ⇒ Object



593
594
595
# File 'lib/mutx/database/mongo_connector.rb', line 593

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



291
292
293
# File 'lib/mutx/database/mongo_connector.rb', line 291

def self.running_tasks
  self.running "task"
end

.running_testsObject



295
296
297
# File 'lib/mutx/database/mongo_connector.rb', line 295

def self.running_tests
  self.running "test"
end

.started!Object

def self.update_only_started

$results.update_many({"status" => "started"}, {"$set" => {"status" => "never_started"}})

end



326
327
328
# File 'lib/mutx/database/mongo_connector.rb', line 326

def self.started!
  $results.find({"status" => "started"}).to_a
end

.status_for_result(result_id) ⇒ Object



637
638
639
640
641
642
643
# File 'lib/mutx/database/mongo_connector.rb', line 637

def self.status_for_result result_id
  begin
    $results.find({"_id" => result_id}).to_a.first["status"]
  rescue
    nil
  end
end

.task_data_for(task_id) ⇒ Object

Returns the entire record for a given task name $param [String] task_name $return [Hash] all task data



244
245
246
247
248
249
# File 'lib/mutx/database/mongo_connector.rb', line 244

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



251
252
253
254
# File 'lib/mutx/database/mongo_connector.rb', line 251

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) ⇒ Object

Returns the _id for a given task name $param [String] task_name $return [String] _id



259
260
261
262
263
264
265
# File 'lib/mutx/database/mongo_connector.rb', line 259

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



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

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



535
536
537
538
539
# File 'lib/mutx/database/mongo_connector.rb', line 535

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



541
542
543
544
545
# File 'lib/mutx/database/mongo_connector.rb', line 541

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 $param [Hash] custom_param_data



483
484
485
486
487
# File 'lib/mutx/database/mongo_connector.rb', line 483

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"]
  $custom_params.update_one( {"_id" => custom_param_data["_id"]}, custom_param_data)
end

.update_last_exec_timeObject



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

def self.update_last_exec_time
  $results.update_many({"cronneable" => "on"}, {"$set" => {"last_exec_time" => Time.now.utc}})
end

.update_repo_data(repo_data) ⇒ Object



427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/mutx/database/mongo_connector.rb', line 427

def self.update_repo_data repo_data
=begin
  aux = repo_data["value"].to_json
  aux_1 = JSON.parse(aux) #=> HASH
  repo_data["value"] = JSON.parse aux_1#value en json!
=end
  aux = JSON.parse repo_data["value"].gsub('=>', ':')
  repo_data["value"] = aux
  #only update if repo exists and token is valid
  res = $repos.find({"repo_name" => repo_data["repo_name"]})
  (Mutx::Support::Log.debug "Updating repo #{repo_data["repo_name"]}" if Mutx::Support::Log
  $repos.update_one({"repo_name" => repo_data["repo_name"]}, {"$set" => {"value" => repo_data["value"], "last_update" => repo_data["last_update"]} }) ) if ( (!res.to_a.empty?) && (res.to_a[0]["repo_token"].eql? repo_data["repo_token"]) )
end

.update_repo_name(id, name) ⇒ Object



441
442
443
444
# File 'lib/mutx/database/mongo_connector.rb', line 441

def self.update_repo_name id, name
  res = $repos.find({"_id" => id})
  ($repos.update_one({"_id" => id}, {"$set" => {"repo_name" => name} }) ) if !res.to_a.empty?
end

.update_result(result_data_structure) ⇒ Object

Updates result register with the given data



576
577
578
579
580
581
582
583
# File 'lib/mutx/database/mongo_connector.rb', line 576

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_status(task_id, status) ⇒ Object



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

def self.update_status task_id, status
  $tasks.update_one({"_id" => task_id}, {"$set" => {"task_status" => status}})
end

.update_stop_bots_off(task_id) ⇒ Object

Cron_end



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

def self.update_stop_bots_off task_id
  $tasks.update_one({"_id" => task_id}, {"$set" => {"stop_bots" => "off"}})
end

.update_stop_bots_on(task_id) ⇒ Object



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

def self.update_stop_bots_on task_id
  $tasks.update_one({"_id" => task_id}, {"$set" => {"stop_bots" => "on"}})
end

.update_task(task_data) ⇒ Object

Update record for a given task $param [Hash] task_data



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

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



385
386
387
388
389
390
# File 'lib/mutx/database/mongo_connector.rb', line 385

def self.update_tasks_with_custom_param_id custom_param_id
  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



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

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_commits_collectionObject



77
78
79
80
# File 'lib/mutx/database/mongo_connector.rb', line 77

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

#set_config_collectionObject



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

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



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

def set_custom_param_collection
  Mutx::Support::Log.debug "Setting db custom param collection" if Mutx::Support::Log
  $custom_params  = $db.collection("custom_params")
end

#set_db_nameObject



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

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



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

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

#set_input_collectionObject



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

def set_input_collection
  Mutx::Support::Log.debug "Setting db Input collection" if Mutx::Support::Log
  $inputs = $db.collection("inputs")
  $inputs.indexes.create_one({"reference" => 1})
end

#set_loggerObject



31
32
33
34
# File 'lib/mutx/database/mongo_connector.rb', line 31

def set_logger
  #Mongo::Logger.logger = ::Logger.new("mutx/logs/mongo.log")
  Mongo::Logger.logger.level = ::Logger::INFO
end

#set_repos_collectionObject



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

def set_repos_collection
  Mutx::Support::Log.debug "Setting db Repos collection" if Mutx::Support::Log
  $repos = $db.collection("repos")
end

#set_results_collectionObject



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

def set_results_collection
  Mutx::Support::Log.debug "Setting db results collection" if Mutx::Support::Log
  $results   = $db.collection("results")
  $results.indexes.create_many([
    { :key => { "_id" => 1 }, unique: true },
    { :key => { "started_at": -1 } }
  ])
end

#set_task_collectionObject



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

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