diff --git a/README.md b/README.md index 9e67e15..573aa79 100644 --- a/README.md +++ b/README.md @@ -61,21 +61,22 @@ Configure Neography as follows: ```ruby # these are the default values: Neography.configure do |config| - config.protocol = "http://" - config.server = "localhost" - config.port = 7474 - config.directory = "" # prefix this path with '/' - config.cypher_path = "/cypher" - config.gremlin_path = "/ext/GremlinPlugin/graphdb/execute_script" - config.log_file = "neography.log" - config.log_enabled = false - config.max_threads = 20 - config.authentication = nil # 'basic' or 'digest' - config.username = nil - config.password = nil - config.parser = MultiJsonParser -end -``` + config.protocol = "http://" + config.server = "localhost" + config.port = 7474 + config.directory = "" # prefix this path with '/' + config.cypher_path = "/cypher" + config.gremlin_path = "/ext/GremlinPlugin/graphdb/execute_script" + config.log_file = "neography.log" + config.log_enabled = false + config.slow_log_threshold = 0 # time in ms for query logging + config.max_threads = 20 + config.authentication = nil # 'basic' or 'digest' + config.username = nil + config.password = nil + config.parser = MultiJsonParser + end + ``` Then initialize a `Rest` instance: diff --git a/lib/neography/config.rb b/lib/neography/config.rb index 4c4b055..f76fed9 100644 --- a/lib/neography/config.rb +++ b/lib/neography/config.rb @@ -3,7 +3,7 @@ class Config attr_accessor :protocol, :server, :port, :directory, :cypher_path, :gremlin_path, - :log_file, :log_enabled, + :log_file, :log_enabled, :slow_log_threshold, :max_threads, :authentication, :username, :password, :parser @@ -14,39 +14,41 @@ def initialize def to_hash { - :protocol => @protocol, - :server => @server, - :port => @port, - :directory => @directory, - :cypher_path => @cypher_path, - :gremlin_path => @gremlin_path, - :log_file => @log_file, - :log_enabled => @log_enabled, - :max_threads => @max_threads, - :authentication => @authentication, - :username => @username, - :password => @password, - :parser => @parser + :protocol => @protocol, + :server => @server, + :port => @port, + :directory => @directory, + :cypher_path => @cypher_path, + :gremlin_path => @gremlin_path, + :log_file => @log_file, + :log_enabled => @log_enabled, + :slow_log_threshold => @slow_log_threshold, + :max_threads => @max_threads, + :authentication => @authentication, + :username => @username, + :password => @password, + :parser => @parser } end private def set_defaults - @protocol = "http://" - @server = "localhost" - @port = 7474 - @directory = "" - @cypher_path = "/cypher" - @gremlin_path = "/ext/GremlinPlugin/graphdb/execute_script" - @log_file = "neography.log" - @log_enabled = false - @max_threads = 20 - @authentication = nil - @username = nil - @password = nil - @parser = MultiJsonParser - end + @protocol = "http://" + @server = "localhost" + @port = 7474 + @directory = "" + @cypher_path = "/cypher" + @gremlin_path = "/ext/GremlinPlugin/graphdb/execute_script" + @log_file = "neography.log" + @log_enabled = false + @slow_log_threshold = 0 + @max_threads = 20 + @authentication = nil + @username = nil + @password = nil + @parser = MultiJsonParser + end end end diff --git a/lib/neography/connection.rb b/lib/neography/connection.rb index 193118f..b7e4faa 100644 --- a/lib/neography/connection.rb +++ b/lib/neography/connection.rb @@ -7,7 +7,7 @@ class Connection attr_accessor :protocol, :server, :port, :directory, :cypher_path, :gremlin_path, - :log_file, :log_enabled, :logger, + :log_file, :log_enabled, :logger, :slow_log_threshold, :max_threads, :authentication, :username, :password, :parser, :client @@ -54,7 +54,7 @@ def log(path, body) start_time = Time.now response = yield time = ((Time.now - start_time) * 1000).round(2) - @logger.info "[Neography::Query] #{path} #{body} [#{time}ms]" + @logger.info "[Neography::Query] #{path} #{body} [#{time}ms]" if time >= slow_log_threshold response else yield @@ -76,16 +76,17 @@ def merge_configuration(options) end def save_local_configuration(config) - @protocol = config[:protocol] - @server = config[:server] - @port = config[:port] - @directory = config[:directory] - @cypher_path = config[:cypher_path] - @gremlin_path = config[:gremlin_path] - @log_file = config[:log_file] - @log_enabled = config[:log_enabled] - @max_threads = config[:max_threads] - @parser = config[:parser] + @protocol = config[:protocol] + @server = config[:server] + @port = config[:port] + @directory = config[:directory] + @cypher_path = config[:cypher_path] + @gremlin_path = config[:gremlin_path] + @log_file = config[:log_file] + @log_enabled = config[:log_enabled] + @slow_log_threshold = config[:slow_log_threshold] + @max_threads = config[:max_threads] + @parser = config[:parser] @user_agent = { "User-Agent" => USER_AGENT } diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb index 3b6b931..b8676fb 100644 --- a/spec/unit/config_spec.rb +++ b/spec/unit/config_spec.rb @@ -7,35 +7,37 @@ module Neography context "defaults" do - its(:protocol) { should == 'http://' } - its(:server) { should == 'localhost' } - its(:port) { should == 7474 } - its(:directory) { should == '' } - its(:cypher_path) { should == '/cypher' } - its(:gremlin_path) { should == '/ext/GremlinPlugin/graphdb/execute_script' } - its(:log_file) { should == 'neography.log' } - its(:log_enabled) { should == false } - its(:max_threads) { should == 20 } - its(:authentication) { should == nil } - its(:username) { should == nil } - its(:password) { should == nil } - its(:parser) { should == MultiJsonParser} + its(:protocol) { should == 'http://' } + its(:server) { should == 'localhost' } + its(:port) { should == 7474 } + its(:directory) { should == '' } + its(:cypher_path) { should == '/cypher' } + its(:gremlin_path) { should == '/ext/GremlinPlugin/graphdb/execute_script' } + its(:log_file) { should == 'neography.log' } + its(:log_enabled) { should == false } + its(:slow_log_threshold) { should == 0 } + its(:max_threads) { should == 20 } + its(:authentication) { should == nil } + its(:username) { should == nil } + its(:password) { should == nil } + its(:parser) { should == MultiJsonParser} it "has a hash representation" do expected_hash = { - :protocol => 'http://', - :server => 'localhost', - :port => 7474, - :directory => '', - :cypher_path => '/cypher', - :gremlin_path => '/ext/GremlinPlugin/graphdb/execute_script', - :log_file => 'neography.log', - :log_enabled => false, - :max_threads => 20, - :authentication => nil, - :username => nil, - :password => nil, - :parser => MultiJsonParser + :protocol => 'http://', + :server => 'localhost', + :port => 7474, + :directory => '', + :cypher_path => '/cypher', + :gremlin_path => '/ext/GremlinPlugin/graphdb/execute_script', + :log_file => 'neography.log', + :log_enabled => false, + :slow_log_threshold => 0, + :max_threads => 20, + :authentication => nil, + :username => nil, + :password => nil, + :parser => MultiJsonParser } config.to_hash.should == expected_hash end diff --git a/spec/unit/connection_spec.rb b/spec/unit/connection_spec.rb index 567a6e3..8ed281f 100644 --- a/spec/unit/connection_spec.rb +++ b/spec/unit/connection_spec.rb @@ -20,35 +20,37 @@ module Neography context "hash options" do let(:options) do { - :protocol => "https://", - :server => "foobar", - :port => 4242, - :directory => "/dir", - :cypher_path => "/cyph", - :gremlin_path => "/grem", - :log_file => "neo.log", - :log_enabled => false, - :max_threads => 10, - :parser => Foo, - :authentication => "foo", - :username => "bar", - :password => "baz" + :protocol => "https://", + :server => "foobar", + :port => 4242, + :directory => "/dir", + :cypher_path => "/cyph", + :gremlin_path => "/grem", + :log_file => "neo.log", + :log_enabled => false, + :slow_log_threshold => 0, + :max_threads => 10, + :parser => Foo, + :authentication => "foo", + :username => "bar", + :password => "baz" } end it "accepts all options in a hash" do connection.configuration.should == "https://foobar:4242/dir/db/data" - connection.protocol.should == "https://" - connection.server.should == "foobar" - connection.port.should == 4242 - connection.directory.should == "/dir" - connection.cypher_path.should == "/cyph" - connection.gremlin_path.should == "/grem" - connection.log_file.should == "neo.log" - connection.log_enabled.should == false - connection.max_threads.should == 10 - connection.parser.should == Foo + connection.protocol.should == "https://" + connection.server.should == "foobar" + connection.port.should == 4242 + connection.directory.should == "/dir" + connection.cypher_path.should == "/cyph" + connection.gremlin_path.should == "/grem" + connection.log_file.should == "neo.log" + connection.log_enabled.should == false + connection.slow_log_threshold.should == 0 + connection.max_threads.should == 10 + connection.parser.should == Foo connection.authentication.should == { :foo_auth => { @@ -207,17 +209,14 @@ module Neography context "query logging" do before do - connection.logger = Logger.new(nil) + @logger = Logger.new(nil) + connection.logger = @logger connection.log_enabled = true end - let :expected_response do - "expected_response" - end + let(:expected_response) {"expected_response"} - let :request_body do - {key1: :val1} - end + let(:request_body) { {key1: :val1} } it "should log query" do connection.should_receive(:log).with("/foo/bar", request_body).once @@ -228,6 +227,30 @@ module Neography connection.stub(:evaluate_response).and_return expected_response connection.get("/foo/bar").should eq expected_response end + + describe "slow_log_threshold" do + before do + connection.stub(:evaluate_response).and_return expected_response + end + + context "default value" do + it "should have output" do + @logger.should_receive(:info).once + end + end + + context "high value" do + before { connection.slow_log_threshold = 100_000 } + it "should not have output" do + @logger.should_not_receive(:info) + end + end + + after do + connection.get("/foo/bar", {body: request_body}) + end + end + end end end