Class: NeoScout::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/neoscout/main.rb

Defined Under Namespace

Classes: SimpleConsoleLogger

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMain

Returns a new instance of Main.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/neoscout/main.rb', line 27

def initialize
  @opt_report      = 0
  @opt_webservice  = false
  @opt_output_file = nil
  @opt_no_nodes    = false
  @opt_db_config   = nil
  @opt_no_edges    = false
  @opt_pre_mapper  = lambda { |t| t }
  @opt_type_mapper = lambda { |t| t }
  parse_opts
end

Instance Attribute Details

#opt_bindObject (readonly)

Returns the value of attribute opt_bind.



17
18
19
# File 'lib/neoscout/main.rb', line 17

def opt_bind
  @opt_bind
end

#opt_dbObject (readonly)

Returns the value of attribute opt_db.



13
14
15
# File 'lib/neoscout/main.rb', line 13

def opt_db
  @opt_db
end

#opt_db_configObject (readonly)

Returns the value of attribute opt_db_config.



20
21
22
# File 'lib/neoscout/main.rb', line 20

def opt_db_config
  @opt_db_config
end

#opt_no_edgesObject (readonly)

Returns the value of attribute opt_no_edges.



21
22
23
# File 'lib/neoscout/main.rb', line 21

def opt_no_edges
  @opt_no_edges
end

#opt_no_nodesObject (readonly)

Returns the value of attribute opt_no_nodes.



19
20
21
# File 'lib/neoscout/main.rb', line 19

def opt_no_nodes
  @opt_no_nodes
end

#opt_output_fileObject (readonly)

Returns the value of attribute opt_output_file.



24
25
26
# File 'lib/neoscout/main.rb', line 24

def opt_output_file
  @opt_output_file
end

#opt_portObject (readonly)

Returns the value of attribute opt_port.



15
16
17
# File 'lib/neoscout/main.rb', line 15

def opt_port
  @opt_port
end

#opt_pre_mapperObject (readonly)

Returns the value of attribute opt_pre_mapper.



23
24
25
# File 'lib/neoscout/main.rb', line 23

def opt_pre_mapper
  @opt_pre_mapper
end

#opt_reportObject (readonly)

Returns the value of attribute opt_report.



18
19
20
# File 'lib/neoscout/main.rb', line 18

def opt_report
  @opt_report
end

#opt_schemaObject (readonly)

Returns the value of attribute opt_schema.



14
15
16
# File 'lib/neoscout/main.rb', line 14

def opt_schema
  @opt_schema
end

#opt_type_mapperObject (readonly)

Returns the value of attribute opt_type_mapper.



22
23
24
# File 'lib/neoscout/main.rb', line 22

def opt_type_mapper
  @opt_type_mapper
end

#opt_webserviceObject (readonly)

Returns the value of attribute opt_webservice.



16
17
18
# File 'lib/neoscout/main.rb', line 16

def opt_webservice
  @opt_webservice
end

Instance Method Details

#parse_optsObject



39
40
41
42
43
44
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/neoscout/main.rb', line 39

def parse_opts
  optparse = OptionParser.new do |opts|
    opts.banner = "Usage: --db <neo4j:path> --schema <url> [--port <port>]"
    opts.on('-d', '--db DB', 'Path to database in the form neo4j:<path>)') do |db|
      @opt_db     = db
    end
    opts.on('-u', '--schema-url URL', 'URL to database schema') do |url|
      @opt_schema = lambda { || ::JSON.parse(HTTParty.get(url)) }
    end
    opts.on('-s', '--schema-file FILE', 'schema file') do |file|
      @opt_schema = lambda { || ::JSON.parse(IO::read(file)) }
    end
    opts.on('-o', '--output-file FILE', 'output file in standalone mode') do |f|
      @opt_output_file = f
    end
    opts.on('-w', '--webservice', 'Run inside sinatra') do
      @opt_webservice = true
    end
    opts.on('-p', '--port PORT', 'Port to be used') do |port|
      @opt_port   = port.to_i
    end
    opts.on('-b', '--bind ITF', 'Interface to be used') do |itf|
      @port       = itf
    end
    opts.on('-r', '--report NUM', 'Report progress every NUM graph elements') do |num|
      @opt_report = num.to_i
    end
    opts.on('--no-nodes', 'Do not iterate over nodes') do
      @opt_no_nodes = true
    end
    opts.on('--no-edges', 'Do not iterate over edges') do
      @opt_no_edges = true
    end
    opts.on('-P', '--pluralize-types', 'Pluralize type names') do
      @opt_pre_mapper = lambda { |t| t.pluralize }
    end
    opts.on('-S', '--singularize-types', 'Singularize type names') do
      @opt_pre_mapper = lambda { |t| t.singularize }
    end
    opts.on('-M', '--type-mapper MAPPER',
            'Set the type mapper (underscore, downcase, upcase)') do |mapper|
      @opt_type_mapper = case mapper
                           when 'underscore'
                             lambda { |t| t.underscore }
                           when 'downcase'
                             lambda { |t| t.downcase }
                           when 'upcase'
                             lambda { |t| t.upcase }
                           else
                             raise ArgumentException('Unsupported mapper')
                         end
    end
    opts.on('-C', '--db-config FILE', 'Set config file for db driver') do |file|
      @opt_db_config = file
    end
    opts.on('-h', '--help', 'Display this screen') do
      puts opts
      exit 1
    end
  end
  optparse.parse!
  @opt_output_file = nil if @opt_webservice
end

#runObject



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/neoscout/main.rb', line 137

def run
  ### Load schema at least once to know that we're safe
  self.opt_schema.call()
  ### Run as service if requested
  return run_webservice(self.opt_schema, self.start_db) if self.opt_webservice

  json = run_standalone(self.opt_schema, self.start_db, SimpleConsoleLogger.new)
  if self.opt_output_file
    then File.open(self.opt_output_file, 'w') { |f| f.write(json) }
    else puts(json) end
  shutdown_db
end

#run_standalone(schema_maker, scout_maker, logger) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/neoscout/main.rb', line 150

def run_standalone(schema_maker, scout_maker, logger)
  schema   = schema_maker.call()
  scout    = scout_maker.call()
  scout.verifier.init_from_json schema
  counts   = scout.new_counts
  logger   = SimpleConsoleLogger.new unless logger
  progress = lambda do |mode, what, num|
    if ((num % self.opt_report) == 0) || (mode == :finish)
      logger.info("#{DateTime.now}: #{what} ITERATOR PROGRESS (#{mode} / #{num})")
    end
  end
  scout.count_edges counts: counts, report_progress: progress unless self.opt_no_edges
  scout.count_nodes counts: counts, report_progress: progress unless self.opt_no_nodes
  counts.add_to_json schema
  schema.to_json
end

#run_webservice(schema_maker, scout_maker) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/neoscout/main.rb', line 167

def run_webservice(schema_maker, scout_maker)
  ### Run sinatra
  require 'sinatra'

  set :port, @opt_port if @opt_port
  set :bind, @opt_bind if @opt_bind
  set :show_exceptions, true
  set :sessions, false
  set :logging, true
  set :dump_errors, true
  set :lock, true # -- really?
  set :root, File.expand_path("../../root", __FILE__)
  set :run, true
  # set :public_folder

  ### Keep self around for calling helpers in sinatra handlers
  main = self

  ### Return schema
  get '/schema' do
    content_type :json
    schema_maker.call().to_json
  end

  ### Run verify over database and report results
  get '/verify' do
    content_type :json
    main.run_standalone(schema_maker, scout_maker, self.logger)
  end

  ### Shutdown server, the hard way
  get '/shutdown' do
    main.shutdown_db
    java.lang.System.exit(0)
  end
end

#shutdown_dbObject



120
121
122
123
124
125
126
127
# File 'lib/neoscout/main.rb', line 120

def shutdown_db
  @opt_db.match(/(neo4j:)(.*)/) do |m|
    Neo4j.shutdown
    return
  end

  raise ArgumentError("Unsupported database type")
end

#start_dbObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/neoscout/main.rb', line 103

def start_db
  @opt_db.match(/(neo4j:)(.*)/) do |m|
    Neo4j.config[:storage_path] = m[2] unless (m[2].length == 0)
    YAML.load_file(@opt_db_config).each_pair { |k,v| Neo4j.config[k] = v } if @opt_db_config
    Neo4j.start
    return lambda do
      scout = ::NeoScout::GDB_Neo4j::Scout.new
      pre_mapper = self.opt_pre_mapper
      scout.typer.node_mapper = lambda { |t| self.opt_type_mapper.call(pre_mapper.call(t)) }
      scout.typer.edge_mapper = lambda { |t| self.opt_type_mapper.call(pre_mapper.call(t)) }
      scout
    end
  end

  raise ArgumentError("Unsupported database type")
end