Class: RequestLog::Db

Inherits:
Object
  • Object
show all
Defined in:
lib/request_log/db.rb

Constant Summary collapse

@@mongo_db =
nil

Class Method Summary collapse

Class Method Details

.filtered_requests(start_time, end_time, conditions = {}) ⇒ Object



27
28
29
30
31
32
# File 'lib/request_log/db.rb', line 27

def self.filtered_requests(start_time, end_time, conditions = {})
  start_time = Time.parse(start_time).utc if start_time.is_a?(String)
  end_time = Time.parse(end_time).utc if end_time.is_a?(String)
  time_condition = {"time" => {"$gt" => start_time, "$lt" => end_time}}
  requests.find(time_condition.merge(parse_conditions(conditions))).sort([:time, :ascending])
end

.mongo_dbObject



9
10
11
# File 'lib/request_log/db.rb', line 9

def self.mongo_db
  @@mongo_db
end

.mongo_db=(mongo_db) ⇒ Object



5
6
7
# File 'lib/request_log/db.rb', line 5

def self.mongo_db=(mongo_db)
  @@mongo_db = mongo_db
end

.parse_conditions(conditions) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/request_log/db.rb', line 41

def self.parse_conditions(conditions)
  if conditions
    # We need to parse a Ruby hash here, let's not require braces
    conditions = "{#{conditions}}" unless conditions[0] == "{"
    eval(conditions)
  else
    {}
  end      
end


34
35
36
37
38
39
# File 'lib/request_log/db.rb', line 34

def self.print_requests(start_time, end_time, conditions = {})
  filtered_requests(start_time, end_time, conditions).each do |request|
    puts printable_request(request)
    puts
  end
end

.printable_request(request) ⇒ Object



21
22
23
24
25
# File 'lib/request_log/db.rb', line 21

def self.printable_request(request)
  request.keys.reject { |key| key == "_id" }.map do |key|
    "#{key}: #{request[key].inspect}"
  end.join("\n")
end

.profilingObject



17
18
19
# File 'lib/request_log/db.rb', line 17

def self.profiling
  mongo_db['request_log_profiling']
end

.requestsObject



13
14
15
# File 'lib/request_log/db.rb', line 13

def self.requests
  mongo_db['requests']
end