Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#gen_idx_script(idIndex, collname) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/indexes.rb', line 52

def gen_idx_script(idIndex, collname)
  idx_name = idIndex['name']
  idx_spec = JSON.dump(idIndex['key'])
  idx_options = {}
  idIndex.each do |k, v|
    if ['v', 'key'].include? k then
      next
    end
    idx_options[k] = v
  end
  opt_spec = JSON.dump idx_options
  txt = "db.#{collname}.createIndex(#{idx_spec}, #{opt_spec});"
end

#get_indexes_script(uri, db_name = nil, coll_name = nil, outfile) ⇒ Object



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
# File 'lib/indexes.rb', line 4

def get_indexes_script(uri, db_name=nil, coll_name=nil, outfile)
  client = Mongo::Client.new(uri)

  db_list = []
  if not db_name.nil?
    db_list.push db_name
  else
    client.use('admin').command(listDatabases: 1).first['databases'].each do |iter|  # read the name property
      iter_db_name = iter['name']
      db_list.push iter_db_name
    end
  end

  scripts = []

  db_list.each do |iter_db|
    client = client.use iter_db
    scripts.push "db = db.getSiblingDB('#{iter_db}')';"
    if coll_name.nil?
      colls = client.collections
    else
      colls = [
        client[coll_name]
      ]
    end

    colls.each do |coll|
      coll.indexes.each do |idx|

        #Skip the _id index
        l_idx_cols = idx['key'].to_h.keys
        if l_idx_cols.length == 1 and l_idx_cols.first == '_id'
          next
        end

        txt = gen_idx_script idx, coll.name
        scripts.push txt
      end
    end
  end

  txtscripts = scripts.join "\r\n"

  File.open outfile, 'w' do |f|
    f.write txtscripts
  end
end

#get_logs(uri, filename) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/logs.rb', line 4

def get_logs(uri, filename)
  client = Mongo::Client.new(uri)
  cur = client.command getLog: 'global'
  rset = cur.to_a
  # rset = JSON.parse(JSON.dump(rset))
  File.open(filename, 'w') do |file|
    # file.puts rset
    rset.first['log'].each do |iter|
      file.write(JSON.dump(JSON.parse(iter)))
      file.write "\n"
    end
  end
end

#pipeline_to_mongosh_script(infile, outfile) ⇒ Object

Exports the pipeline to a .aggregate() Mongo shell script



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
# File 'lib/savedPipeline.rb', line 5

def pipeline_to_mongosh_script(infile, outfile)
  txt = File.read(infile)
  data = JSON.parse(txt)

  ns_split = data['namespace'].split '.', 2
  dbname = ns_split[0]
  collname = ns_split[1]

  the_pipeline = "[\n"
  is_first = true
  data['pipeline'].each do |pstage|
    if not pstage["isEnabled"]
      next
    end

    if is_first
      is_first = false
    else
      the_pipeline += ', '
    end
    stage_txt = pstage['stage']
    stage_txt = stage_txt.gsub("\\r", "\r").gsub("\\n", "\n")
    the_pipeline += "{ #{pstage['stageOperator']}: " + stage_txt + " }"
  end
  the_pipeline += "\n]"

  mongosh_script_out =
"//Query name: #{data['name']}

db.getSiblingDB('#{dbname}').#{collname}.aggregate(
" + the_pipeline + "
, {allowDiskUse: true}
);
"

  File.write(outfile, mongosh_script_out)
end

#pipeline_to_view_mongosh_script(infile, viewname, outfile) ⇒ Object

Exports the pipeline to a createView Mongo shell script



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
# File 'lib/savedPipeline.rb', line 45

def pipeline_to_view_mongosh_script(infile, viewname, outfile)
  txt = File.read(infile)
  data = JSON.parse(txt)

  ns_split = data['namespace'].split '.', 2
  dbname = ns_split[0]
  collname = ns_split[1]

  the_pipeline = "[\n"
  is_first = true
  data['pipeline'].each do |pstage|
    if not pstage["isEnabled"]
      next
    end

    if is_first
      is_first = false
    else
      the_pipeline += ', '
    end
    stage_txt = pstage['stage']
    stage_txt = stage_txt.gsub("\\r", "\r").gsub("\\n", "\n")
    the_pipeline += "{ #{pstage['stageOperator']}: " + stage_txt + " }"
  end
  the_pipeline += "\n]"

  mongosh_script_out =
    "//Query name: #{data['name']}

db.getSiblingDB('#{dbname}').createView('#{viewname}', '#{collname}',
" + the_pipeline + "
);
"

  File.write(outfile, mongosh_script_out)
end

#read_config_entry(key) ⇒ Object

Reads a key-value pair from the config file



20
21
22
23
24
25
26
27
28
# File 'lib/config.rb', line 20

def read_config_entry(key)
  if $has_config.call then
    curr_config = YAML.load(File.read($config_filename))
  else
    return nil
  end

  curr_config[key]
end

#write_config_entry(key, value) ⇒ Object

Write a key-value pair to the config file



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/config.rb', line 7

def write_config_entry(key, value)
  if $has_config.call then
    curr_config = YAML.load(File.read($config_filename))
  else
    curr_config = {}
  end

  curr_config[key] = value

  File.open($config_filename, 'w') { |f| f.write(curr_config.to_yaml) }
end