Class: MongoProfiler::Profile

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
lib/mongo_profiler/models/profile.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.register(started_at, database, collection, selector, options = {}) ⇒ Object



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
# File 'lib/mongo_profiler/models/profile.rb', line 47

def register(started_at, database, collection, selector, options = {})
  return if collection =~ /mongo_profiler/ || collection =~ /system/
  return if selector['$explain']

  _caller = MongoProfiler::Caller.new(caller)

  group = ProfileGroup.find_or_create_by(name: MongoProfiler.current_group_name)

  group.touch

  profile_md5 = generate_profile_md5(database, collection, selector, _caller)

  return if Profile.where(profile_md5: profile_md5, profile_group_id: group.id).any?

  result = {}
  result[:profile_md5]        = profile_md5
  result[:profile_group_id]   = group.id

  result[:total_time]         = elapsed(started_at)
  result[:command_database]   = database
  result[:command_collection] = collection
  result[:command]            = JSON.dump(selector)
  result[:file]               = _caller.file
  result[:line]               = _caller.line
  result[:method]             = _caller.method

  # TODO do it in background
  result[:explain] = JSON.dump(generate_explain(collection, selector))

  self.create(result)
end

Instance Method Details

#scoreObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mongo_profiler/models/profile.rb', line 22

def score
  explain = JSON.parse(self.explain)

  n              = explain['n']
  ns_scanned     = explain['nscanned']
  cursor         = explain['cursor']
  scan_and_order = explain['scanAndOrder']

  case
  when cursor == 'BasicCursor'
    :no_index
  when n == 0
    :no_docs_found
  when ns_scanned == n
    :perfect
  when ns_scanned > n
    :scanned_more_than_returned
  when scan_and_order
    :had_to_order
  end
rescue => e
  e.message
end