add API for statistic
This commit is contained in:
parent
31e86c5c6a
commit
f01973ce92
@ -34,9 +34,9 @@ module Devops
|
||||
end
|
||||
|
||||
# Save information about requests with methods POST, PUT, DELETE
|
||||
def statistic msg=nil
|
||||
def insert_statistic msg=nil
|
||||
unless request.get?
|
||||
settings.mongo.statistic request.env['REMOTE_USER'], request.path, request.request_method, @body_json, response.status
|
||||
settings.mongo.insert_statistic request.env['REMOTE_USER'], request.path, request.request_method, @body_json, response.status
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
18
devops-service/app/api2/routes/statistic.rb
Normal file
18
devops-service/app/api2/routes/statistic.rb
Normal file
@ -0,0 +1,18 @@
|
||||
module Devops
|
||||
module API2_0
|
||||
module Routes
|
||||
module StatisticRoutes
|
||||
|
||||
def self.registered(app)
|
||||
app.get_with_headers '/statistic', :headers => [:accept] do
|
||||
# check_privileges('statistic', 'r')
|
||||
JSON.pretty_generate Devops::Db.connector.search_statistic(@params)
|
||||
end
|
||||
|
||||
puts "Statistic routes initialized"
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -70,6 +70,7 @@ module Devops
|
||||
require_relative "api2/routes/stack"
|
||||
require_relative "api2/routes/stack_template"
|
||||
require_relative "api2/routes/stack_presets"
|
||||
require_relative "api2/routes/statistic"
|
||||
require_relative "api2/routes/report"
|
||||
|
||||
routes = Devops::API2_0::Routes.constants.collect{|s| Devops::API2_0::Routes.const_get(s)}.select {|const| const.class == Module}
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
require "wisper"
|
||||
require "lib/hash_ext"
|
||||
|
||||
=begin
|
||||
require "db/exceptions/invalid_record"
|
||||
|
||||
@ -5,10 +5,77 @@ module Connectors
|
||||
self.collection = db.collection('statistic')
|
||||
end
|
||||
|
||||
def statistic(user, path, method, body, response_code)
|
||||
def insert_statistic(user, path, method, body, response_code)
|
||||
collection.insert(user: user, path: path, method: method, body: body, response_code: response_code, date: Time.now)
|
||||
end
|
||||
|
||||
# supported options:
|
||||
# user
|
||||
# response_code (format: 200)
|
||||
# method
|
||||
# path_equal
|
||||
# path_contains
|
||||
# date_from (format: 2014-12-01 09:18:40 UTC)
|
||||
# date_till (format: 2014-12-01 09:18:40 UTC)
|
||||
# limit: default 20. Zero value is equal to no limit.
|
||||
# sort_field: default 'date'. Could also be in [user, path, method, body, response_code, date].
|
||||
# sort_order: default 'asc'. Could also be 'desc'
|
||||
def search_statistic(filters={})
|
||||
limit = filters[:limit] || 20
|
||||
sort_field = filters[:sort_field] || 'date'
|
||||
sort_order = mongo_sort_order(filters[:sort_order] || 'asc')
|
||||
query = {}
|
||||
|
||||
|
||||
query.deep_merge!( user_equal(filters[:user])) if filters[:user]
|
||||
query.deep_merge!( method_equal(filters[:method])) if filters[:method]
|
||||
query.deep_merge!( response_code_equal(filters[:response_code])) if filters[:response_code]
|
||||
query.deep_merge!( path_equal(filters[:path_equal])) if filters[:path_equal]
|
||||
query.deep_merge!( path_contains(filters[:path_contains])) if filters[:path_contains]
|
||||
query.deep_merge!( date_from(filters[:date_from])) if filters[:date_from]
|
||||
query.deep_merge!( date_till(filters[:date_till])) if filters[:date_till]
|
||||
|
||||
collection.
|
||||
find(query, fields: {"_id" => false}).
|
||||
limit(limit.to_i).
|
||||
sort(sort_field => sort_order).
|
||||
to_a
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def mongo_sort_order(order)
|
||||
raise "Wrong sort order" unless %w(asc desc).include?(order)
|
||||
order == 'asc' ? 1 : -1
|
||||
end
|
||||
|
||||
def user_equal(user)
|
||||
{user: user}
|
||||
end
|
||||
|
||||
def method_equal(method)
|
||||
{method: method}
|
||||
end
|
||||
|
||||
def path_contains(path)
|
||||
{path: Regexp.new(path, 'i')}
|
||||
end
|
||||
|
||||
def path_equal(path)
|
||||
{path: path}
|
||||
end
|
||||
|
||||
def response_code_equal(response_code)
|
||||
{response_code: response_code.to_i}
|
||||
end
|
||||
|
||||
def date_from(date)
|
||||
{date: {'$gte' => Time.parse(date)}}
|
||||
end
|
||||
|
||||
def date_till(date)
|
||||
{date: {'$lte' => Time.parse(date)}}
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@ -31,7 +31,7 @@ class MongoConnector
|
||||
:user_update, :create_root_user, :check_user_privileges] => :users_connector,
|
||||
[:keys, :key, :key_insert, :key_delete] => :keys_connector,
|
||||
[:save_report, :report, :reports, :set_report_status, :set_report_server_data] => :reports_connector,
|
||||
[:statistic] => :statistics_connector
|
||||
[:insert_statistic, :search_statistic] => :statistics_connector
|
||||
)
|
||||
|
||||
def initialize(db, host, port=27017, user=nil, password=nil)
|
||||
|
||||
23
devops-service/lib/hash_ext.rb
Normal file
23
devops-service/lib/hash_ext.rb
Normal file
@ -0,0 +1,23 @@
|
||||
class Hash
|
||||
def deep_merge(other_hash, &block)
|
||||
dup.deep_merge!(other_hash, &block)
|
||||
end
|
||||
|
||||
def deep_merge!(other_hash, &block)
|
||||
other_hash.each_pair do |current_key, other_value|
|
||||
this_value = self[current_key]
|
||||
|
||||
self[current_key] = if this_value.is_a?(Hash) && other_value.is_a?(Hash)
|
||||
this_value.deep_merge(other_value, &block)
|
||||
else
|
||||
if block_given? && key?(current_key)
|
||||
block.call(current_key, this_value, other_value)
|
||||
else
|
||||
other_value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
self
|
||||
end
|
||||
end
|
||||
@ -29,7 +29,7 @@ module Sinatra
|
||||
post path, opt, &block
|
||||
|
||||
after path do
|
||||
statistic
|
||||
insert_statistic
|
||||
end
|
||||
end
|
||||
|
||||
@ -43,7 +43,7 @@ module Sinatra
|
||||
put path, opt, &block
|
||||
|
||||
after path do
|
||||
statistic
|
||||
insert_statistic
|
||||
end
|
||||
end
|
||||
|
||||
@ -57,7 +57,7 @@ module Sinatra
|
||||
patch path, opt, &block
|
||||
|
||||
after path do
|
||||
statistic
|
||||
insert_statistic
|
||||
end
|
||||
end
|
||||
|
||||
@ -71,7 +71,7 @@ module Sinatra
|
||||
delete path, opt, &block
|
||||
|
||||
after path do
|
||||
statistic
|
||||
insert_statistic
|
||||
end
|
||||
end
|
||||
|
||||
@ -91,7 +91,7 @@ module Sinatra
|
||||
end
|
||||
|
||||
after path do
|
||||
statistic
|
||||
insert_statistic
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user