Module: Deforest

Defined in:
lib/deforest.rb,
lib/deforest/engine.rb,
lib/deforest/version.rb,
app/models/deforest/log.rb,
app/helpers/deforest/logs_helper.rb,
app/helpers/deforest/application_helper.rb,
app/controllers/deforest/files_controller.rb,
app/controllers/deforest/application_controller.rb

Defined Under Namespace

Modules: ApplicationHelper, LogsHelper Classes: ApplicationController, Engine, FilesController, Log

Constant Summary collapse

VERSION =
"1.1.1"

Class Method Summary collapse

Class Method Details

.get_app_classes(dir) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/deforest.rb', line 9

def self.get_app_classes(dir)
  Dir["#{Rails.root}#{dir}/**/*.rb"].each do |f|
    idx = f.index(dir)
    models_heirarchy = f[idx..-1].gsub(dir,"")
    exec_str = models_heirarchy.split("/").map(&:camelize).join("::").chomp(".rb")
    begin
      model = exec_str.constantize
      yield model
    rescue
      puts "Deforest warning: could not track #{exec_str}"
    end
  end
end

.initialize!Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/deforest.rb', line 57

def self.initialize!
  if block_given?
    yield self
  end
  self.initialize_db_sync_file()
  Deforest.track_dirs.each do |dir|
    self.get_app_classes(dir) do |klass|
      if klass.present?
        klass.prepend(Deforest.inject_tracking_module(klass, dir, 'instance'))
        klass.singleton_class.prepend(Deforest.inject_tracking_module(klass, dir, 'class'))
      end
    end
  end
end

.initialize_db_sync_fileObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/deforest.rb', line 72

def self.initialize_db_sync_file
  File.open("deforest.#{Process.pid}.log", "w") unless File.exist?("deforest.#{Process.pid}.log")
  if File.exist?("deforest_db_sync.#{Process.pid}.txt")
    Deforest.last_saved_log_file_at = Time.at(File.open("deforest_db_sync.#{Process.pid}.txt").read.to_i)
  else
    File.open("deforest_db_sync.#{Process.pid}.txt", "w") do |f|
      current_time = Time.zone.now
      Deforest.last_saved_log_file_at = current_time
      f.write(current_time.to_i)
    end
  end
end

.inject_tracking_module(klass, dir, method_type) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/deforest.rb', line 23

def self.inject_tracking_module(klass, dir, method_type)
  track_methods = if method_type == 'instance'
    klass.instance_methods(false)
  elsif method_type == 'class'
    klass.singleton_methods(false)
  else
    raise "Unknown method type: #{method_type}"
  end
  Module.new do
    track_methods.each do |mname|
      method_location = if method_type == 'instance'
        klass.instance_method(mname).source_location
      else
        klass.singleton_method(mname).source_location
      end
      if method_location.first.ends_with?("#{klass.to_s.underscore}.rb")
        define_method mname do |*args, &block|
          file_name, line_no = method_location
          if file_name.include?(dir)
            Deforest.insert_into_logs(mname, file_name, line_no)
          end
          if Deforest.last_saved_log_file_at < Deforest.write_logs_to_db_every.ago && !Deforest.saving_log_file
            Deforest.parse_and_save_log_file()
            t = Time.zone.now
            Deforest.last_saved_log_file_at = t
            File.open("deforest_db_sync.#{Process.pid}.txt", "w") { |fl| fl.write(t.to_i) }
          end
          super(*args, &block)
        end
      end
    end
  end
end

.insert_into_logs(method_name, file_name, line_no) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/deforest.rb', line 85

def self.insert_into_logs(method_name, file_name, line_no)
  key = "#{file_name}|#{line_no}|#{method_name}\n"
  log_file_name = Deforest.saving_log_file ? "deforest_tmp.#{Process.pid}.log" : "deforest.#{Process.pid}.log"
  File.open(log_file_name, "a") do |f|
    f.write(key)
  end
end

.least_used_methods(dir, size = 1) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/deforest.rb', line 138

def self.least_used_methods(dir, size=1)
  if size == nil
    self.most_used_methods(dir, nil).reverse
  else
    self.most_used_methods(dir, nil).reverse.take(size)
  end
end

.most_used_methods(dir, size = 1) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/deforest.rb', line 123

def self.most_used_methods(dir, size=1)
  query = Deforest::Log.group(:file_name, :line_no, :method_name)
  if dir.present?
    query = query.where("file_name like '%#{dir}/%'")
  end
  res = query.pluck("file_name, line_no, method_name, SUM(count)")
            .sort_by { |fname, lno, method, cnt| cnt }
            .reverse
  if size == nil
    return res
  else
    return res.take(size)
  end
end

.parse_and_save_log_fileObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/deforest.rb', line 93

def self.parse_and_save_log_file
  Deforest.saving_log_file = true
  sql_stmt = "INSERT INTO deforest_logs (file_name, line_no, method_name, count, created_at, updated_at) VALUES "
  hash = {}
  File.foreach("deforest.#{Process.pid}.log") do |line|
    line = line.chomp("\n")
    if hash.has_key?(line)
      hash[line] += 1
    else
      hash[line] = 1
    end
  end
  hash.each do |loc, count|
    t = Time.zone.now
    sql_stmt += "(#{loc.split("|").map { |s| "'#{s}'" }.join(",")}, #{count}, '#{t}', '#{t}'),"
  end
  sql_stmt.chomp!(",")
  sql_stmt += ";"
  if hash.present?
    ActiveRecord::Base.connection.execute(sql_stmt)
    if File.exist?("deforest_tmp.#{Process.pid}.log")
      File.delete("deforest.#{Process.pid}.log")
      File.rename("deforest_tmp.#{Process.pid}.log", "deforest.#{Process.pid}.log")
    else
      File.delete("deforest.#{Process.pid}.log")
    end
  end
  Deforest.saving_log_file = false
end