mongogems 0.0.1 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e52d3b09b0c7415673e2fb1ee195a179299157132d571ff13c992379018de51f
4
- data.tar.gz: 57811b85a6d413440b726e8cab1e574b9834d45cfa76fc1b30bfeca9f0f0f0c7
3
+ metadata.gz: d6b110efba52c4efdfb689e7af26b0e97870e13ac53c03624649c4f4b8f3c7a2
4
+ data.tar.gz: eec7fa33083d44430f3f9b2d4934ba1bea15ba714284fb07b58c3852dbb24a59
5
5
  SHA512:
6
- metadata.gz: 2b887419212f6a2f6b9343d8e46771d12d4d854e10d8ec9cf8c5c1fbb2fe2cca856be3def29fd507eaac184a79d642689a195b7e569c798a37403d3019557ab6
7
- data.tar.gz: dfd7d7c251d96ca5dbf3313682bd97193112d6cb83dd6140ff5c66cff5ddfe0634cd2ac98ff9b83cdfce96d7d9ca0a09f3add292955eaf1e25f8ea83bab1c1dc
6
+ metadata.gz: 6d93488d894bc85eb43b3713b98b00a8d13cc448c60649ecad754efab53c61ccbc99534d1b77da74de3e1a36b3c79e70ef24b0bef15c36585724cbef798df180
7
+ data.tar.gz: 2a26b5a049cba6a60519d8045dae7edba880308a236eec7405fee626544008f466c17b152cfc4be51f6d913c27739f922e3de97b10fc78db85e714f74dad7455
data/bin/mg ADDED
@@ -0,0 +1,69 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+ require_relative '../lib/config'
4
+ require 'shellwords'
5
+
6
+ options = {}
7
+
8
+ case ARGV.shift
9
+ when 'config'
10
+ options[:cmd] = 'config'
11
+ case ARGV.shift
12
+ when 'set'
13
+ options[:subcmd] = 'set'
14
+ key = ARGV.shift
15
+ value = ARGV.join ' '
16
+ options[:key] = key
17
+ options[:val] = value
18
+
19
+ write_config_entry options[:key], options[:val]
20
+ when 'get'
21
+ options[:subcmd] = 'get'
22
+ key = ARGV.shift
23
+ options[:key] = key
24
+
25
+ val = read_config_entry options[:key]
26
+ puts "Config for #{options[:key]} is #{val}"
27
+ when 'help'
28
+ puts '
29
+ Usage: mg config [subcommand] ...
30
+ Commands:
31
+ get KEY
32
+ set KEY VALUE
33
+ '
34
+ exit
35
+ else
36
+ puts "Unrecognized #{options[:cmd]} command"
37
+ exit -1
38
+ end
39
+ when 'help', '--help', '-h', '-?'
40
+ puts '
41
+ Usage: mg [command] [subcommand] ...
42
+ Commands:
43
+ config
44
+ help
45
+ '
46
+ exit
47
+ when 'shell', 's'
48
+ options[:cmd] = 'shell'
49
+ m_uri = read_config_entry 'uri'
50
+ if m_uri.nil?
51
+ puts 'URI not found in config.
52
+ Must set URI with: mg config set uri YOUR_URI_HERE
53
+ Example: mg config set uri \'mongodb://username:password@localhost:27017\'
54
+ '
55
+ exit
56
+ end
57
+ remaining_cmds = ARGV.map{|arg| Shellwords.escape arg}.join ' '
58
+ cmdstr = "mongo #{m_uri} #{remaining_cmds}"
59
+ exec(cmdstr)
60
+ # when 'echo'
61
+ # remaining_cmds = ARGV.map{|arg| Shellwords.escape arg}.join ' '
62
+ # puts remaining_cmds
63
+ # exec("echo #{remaining_cmds}")
64
+ else
65
+ puts 'Unrecognized command'
66
+ exit -1
67
+ end
68
+
69
+
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+ require_relative '../lib/indexes'
4
+
5
+ options = {}
6
+
7
+ OptionParser.new do |opts|
8
+ opts.banner = 'Usage: mongoindex2file [options]'
9
+
10
+ opts.on('-o', '--output=OUTFILE', 'Output file to write to') do |val|
11
+ options[:output] = val
12
+ end
13
+
14
+ opts.on('-uri', '--uri=URI', 'URI to MongoDB') do |val|
15
+ options[:uri] = val
16
+ end
17
+
18
+ opts.on('-d', '--database=DB', '(optional) Database to fetch indexes from') do |val|
19
+ options[:db] = val
20
+ end
21
+
22
+ opts.on('-c', '--collection=COLL', '(optional) Collection to fetch indexes from') do |val|
23
+ options[:coll] = val
24
+ end
25
+
26
+ opts.on('-h', '--help', 'Help') do |val|
27
+ puts opts
28
+ exit
29
+ end
30
+ end.parse!
31
+
32
+ if options[:output].nil?
33
+ puts 'Missing arguments. Execute with -h for help'
34
+ exit
35
+ end
36
+
37
+ get_indexes_script options[:uri], options[:db], options[:coll], options[:output]
data/bin/mongolog2file ADDED
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+ require_relative '../lib/logs'
4
+
5
+ options = {}
6
+
7
+ OptionParser.new do |opts|
8
+ opts.banner = 'Usage: mongolog2file [options]'
9
+
10
+ opts.on('-o', '--output=OUTFILE', 'Output file to write to') do |val|
11
+ options[:output] = val
12
+ end
13
+
14
+ opts.on('-uri', '--uri=URI', 'URI to MongoDB') do |val|
15
+ options[:uri] = val
16
+ end
17
+
18
+ opts.on('-h', '--help', 'Help') do |val|
19
+ puts opts
20
+ exit
21
+ end
22
+ end.parse!
23
+
24
+ if options[:output].nil?
25
+ puts 'Missing arguments. Execute with -h for help'
26
+ exit
27
+ end
28
+
29
+ get_logs options[:uri], options[:output]
30
+
data/lib/config.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'yaml'
2
+
3
+ $config_filename = File.join(Dir.home, '.mongogems')
4
+ $has_config = lambda { File.exists? $config_filename }
5
+
6
+ # Write a key-value pair to the config file
7
+ def write_config_entry(key, value)
8
+ if $has_config.call then
9
+ curr_config = YAML.load(File.read($config_filename))
10
+ else
11
+ curr_config = {}
12
+ end
13
+
14
+ curr_config[key] = value
15
+
16
+ File.open($config_filename, 'w') { |f| f.write(curr_config.to_yaml) }
17
+ end
18
+
19
+ # Reads a key-value pair from the config file
20
+ def read_config_entry(key)
21
+ if $has_config.call then
22
+ curr_config = YAML.load(File.read($config_filename))
23
+ else
24
+ return nil
25
+ end
26
+
27
+ curr_config[key]
28
+ end
29
+
30
+ # write_config_entry 'mongouri', 'mongodb://localhost'
31
+ # p (read_config_entry 'mongouri')
data/lib/indexes.rb ADDED
@@ -0,0 +1,64 @@
1
+ require 'mongo'
2
+ require 'json'
3
+
4
+ def get_indexes_script(uri, db_name=nil, coll_name=nil, outfile)
5
+ client = Mongo::Client.new(uri)
6
+
7
+ db_list = []
8
+ if not db_name.nil?
9
+ db_list.push db_name
10
+ else
11
+ client.use('admin').command(listDatabases: 1).first['databases'].each do |iter| # read the name property
12
+ iter_db_name = iter['name']
13
+ db_list.push iter_db_name
14
+ end
15
+ end
16
+
17
+ scripts = []
18
+
19
+ db_list.each do |iter_db|
20
+ client = client.use iter_db
21
+ scripts.push "db = db.getSiblingDB('#{iter_db}')';"
22
+ if coll_name.nil?
23
+ colls = client.collections
24
+ else
25
+ colls = [
26
+ client[coll_name]
27
+ ]
28
+ end
29
+
30
+ colls.each do |coll|
31
+ coll.indexes.each do |idx|
32
+
33
+ #Skip the _id index
34
+ l_idx_cols = idx['key'].to_h.keys
35
+ if l_idx_cols.length == 1 and l_idx_cols.first == '_id'
36
+ next
37
+ end
38
+
39
+ txt = gen_idx_script idx, coll.name
40
+ scripts.push txt
41
+ end
42
+ end
43
+ end
44
+
45
+ txtscripts = scripts.join "\r\n"
46
+
47
+ File.open outfile, 'w' do |f|
48
+ f.write txtscripts
49
+ end
50
+ end
51
+
52
+ def gen_idx_script(idIndex, collname)
53
+ idx_name = idIndex['name']
54
+ idx_spec = JSON.dump(idIndex['key'])
55
+ idx_options = {}
56
+ idIndex.each do |k, v|
57
+ if ['v', 'key'].include? k then
58
+ next
59
+ end
60
+ idx_options[k] = v
61
+ end
62
+ opt_spec = JSON.dump idx_options
63
+ txt = "db.#{collname}.createIndex(#{idx_spec}, #{opt_spec});"
64
+ end
data/lib/logs.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'mongo'
2
+ require 'json'
3
+
4
+ def get_logs(uri, filename)
5
+ client = Mongo::Client.new(uri)
6
+ cur = client.command getLog: 'global'
7
+ rset = cur.to_a
8
+ # rset = JSON.parse(JSON.dump(rset))
9
+ File.open(filename, 'w') do |file|
10
+ # file.puts rset
11
+ rset.first['log'].each do |iter|
12
+ file.write(JSON.dump(JSON.parse(iter)))
13
+ file.write "\n"
14
+ end
15
+ end
16
+ end
17
+
18
+ get_logs 'mongodb://localhost', 'logs.txt'
data/lib/savedPipeline.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'json'
2
2
 
3
- # filename = '/Users/nitin/Downloads/Zaloni-DQ-A/607d7460cb21963a143d1d0f.json'
4
3
 
4
+ # Exports the pipeline to a .aggregate() Mongo shell script
5
5
  def pipeline_to_mongosh_script(infile, outfile)
6
6
  txt = File.read(infile)
7
7
  data = JSON.parse(txt)
@@ -10,17 +10,29 @@ def pipeline_to_mongosh_script(infile, outfile)
10
10
  dbname = ns_split[0]
11
11
  collname = ns_split[1]
12
12
 
13
- the_pipeline = []
13
+ the_pipeline = "[\n"
14
+ is_first = true
14
15
  data['pipeline'].each do |pstage|
15
- the_pipeline.push pstage['executor']
16
+ if not pstage["isEnabled"]
17
+ next
18
+ end
19
+
20
+ if is_first
21
+ is_first = false
22
+ else
23
+ the_pipeline += ', '
24
+ end
25
+ stage_txt = pstage['stage']
26
+ stage_txt = stage_txt.gsub("\\r", "\r").gsub("\\n", "\n")
27
+ the_pipeline += "{ #{pstage['stageOperator']}: " + stage_txt + " }"
16
28
  end
29
+ the_pipeline += "\n]"
17
30
 
18
31
  mongosh_script_out =
19
- "
20
- //Query name: #{data['name']}
21
- use #{dbname};
22
- db.#{collname}.aggregate(
23
- #{JSON.generate the_pipeline}
32
+ "//Query name: #{data['name']}
33
+
34
+ db.getSiblingDB('#{dbname}').#{collname}.aggregate(
35
+ " + the_pipeline + "
24
36
  , {allowDiskUse: true}
25
37
  );
26
38
  "
@@ -28,4 +40,41 @@ db.#{collname}.aggregate(
28
40
  File.write(outfile, mongosh_script_out)
29
41
  end
30
42
 
31
- # pipeline_to_mongosh_script filename, '/dev/null'
43
+
44
+ # Exports the pipeline to a createView Mongo shell script
45
+ def pipeline_to_view_mongosh_script(infile, viewname, outfile)
46
+ txt = File.read(infile)
47
+ data = JSON.parse(txt)
48
+
49
+ ns_split = data['namespace'].split '.', 2
50
+ dbname = ns_split[0]
51
+ collname = ns_split[1]
52
+
53
+ the_pipeline = "[\n"
54
+ is_first = true
55
+ data['pipeline'].each do |pstage|
56
+ if not pstage["isEnabled"]
57
+ next
58
+ end
59
+
60
+ if is_first
61
+ is_first = false
62
+ else
63
+ the_pipeline += ', '
64
+ end
65
+ stage_txt = pstage['stage']
66
+ stage_txt = stage_txt.gsub("\\r", "\r").gsub("\\n", "\n")
67
+ the_pipeline += "{ #{pstage['stageOperator']}: " + stage_txt + " }"
68
+ end
69
+ the_pipeline += "\n]"
70
+
71
+ mongosh_script_out =
72
+ "//Query name: #{data['name']}
73
+
74
+ db.getSiblingDB('#{dbname}').createView('#{viewname}', '#{collname}',
75
+ " + the_pipeline + "
76
+ );
77
+ "
78
+
79
+ File.write(outfile, mongosh_script_out)
80
+ end
metadata CHANGED
@@ -1,23 +1,74 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongogems
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katkam Nitin Reddy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-03 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2021-05-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mongo
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.14.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.14.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: os
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.1.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: shellwords
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.0
13
55
  description:
14
56
  email:
15
57
  executables:
16
58
  - compass2mongosh
59
+ - mongolog2file
60
+ - mongoindex2file
61
+ - mg
17
62
  extensions: []
18
63
  extra_rdoc_files: []
19
64
  files:
20
65
  - bin/compass2mongosh
66
+ - bin/mg
67
+ - bin/mongoindex2file
68
+ - bin/mongolog2file
69
+ - lib/config.rb
70
+ - lib/indexes.rb
71
+ - lib/logs.rb
21
72
  - lib/savedPipeline.rb
22
73
  homepage:
23
74
  licenses: []