summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJochen Topf <jochen@topf.org>2013-01-20 20:20:09 +0100
committerJochen Topf <jochen@topf.org>2013-01-20 20:20:09 +0100
commit3e52b3ae5a25261af4b888747a3c91a9b8056c9b (patch)
tree1fe985e8c6120c20f7e39a7b6db6e90805aaebff
parentefd17ad0d6200faa8586077d622359fb64c2ad22 (diff)
downloadtaginfo-3e52b3ae5a25261af4b888747a3c91a9b8056c9b.tar
taginfo-3e52b3ae5a25261af4b888747a3c91a9b8056c9b.tar.gz
Make SQL query logging configurable
-rw-r--r--taginfo-config-example.json4
-rw-r--r--web/lib/sql.rb41
2 files changed, 31 insertions, 14 deletions
diff --git a/taginfo-config-example.json b/taginfo-config-example.json
index 053426e..69d5893 100644
--- a/taginfo-config-example.json
+++ b/taginfo-config-example.json
@@ -57,6 +57,10 @@
"planetfile": "/osm/planet/var/current-planet.osm.pbf"
}
},
+ "logging": {
+ // SQL queries longer than this many seconds are logged
+ "min_duration": 0.01
+ },
// For compiling tagstats.
"tagstats": {
// Extra compilerflags, for instance to find Osmium.
diff --git a/web/lib/sql.rb b/web/lib/sql.rb
index 29fb1b7..3b5fe5e 100644
--- a/web/lib/sql.rb
+++ b/web/lib/sql.rb
@@ -46,16 +46,40 @@ module SQL
@db = nil
end
+ def wrap_query(query, *params)
+ t1 = Time.now
+ out = yield
+ duration = Time.now - t1
+
+ min_duration = TaginfoConfig.get('logging.min_duration', 0)
+ if duration > min_duration
+ if params.size > 0
+ p = ' params=[' + params.map{ |p| "'#{p}'" }.join(', ') + ']'
+ else
+ p = ''
+ end
+ puts %Q{SQL duration=#{ duration } query="#{ query };"} + p
+ end
+
+ out
+ end
+
def execute(*args, &block)
- @db.execute(*args, &block)
+ wrap_query(*args) do
+ @db.execute(*args, &block)
+ end
end
def get_first_row(*args)
- @db.get_first_row(*args)
+ wrap_query(*args) do
+ @db.get_first_row(*args)
+ end
end
def get_first_value(*args)
- @db.get_first_value(*args)
+ wrap_query(*args) do
+ @db.get_first_value(*args)
+ end
end
def select(query, *params)
@@ -164,29 +188,18 @@ module SQL
@query.compact.join(' ')
end
- def log_query(query, params)
- if params.size > 0
- puts "Query: #{query}; (with params: #{params.map{ |p| "'#{p}'" }.join(', ')})"
- else
- puts "Query: #{query};"
- end
- end
-
def execute(&block)
q = build_query()
- log_query(q, @params)
@db.execute(q, *@params, &block)
end
def get_first_value
q = build_query()
- log_query(q, @params)
@db.get_first_value(q, *@params)
end
def get_columns(*columns)
q = build_query()
- log_query(q, @params)
row = @db.get_first_row(q, *@params)
return [nil] * columns.size if row.nil?;
columns.map{ |column| row[column.to_s] }