Class: DashAnalyzer::TimeView

Inherits:
AbstractAnalyzer::View show all
Defined in:
lib/dash_analyzer/view.rb

Instance Attribute Summary

Attributes inherited from AbstractAnalyzer::View

#collection

Instance Method Summary collapse

Methods inherited from AbstractAnalyzer::View

#call, #db, get, inherited, to_app

Constructor Details

#initializeTimeView

Returns a new instance of TimeView.



6
7
8
9
# File 'lib/dash_analyzer/view.rb', line 6

def initialize(*)
  super
  setup_show
end

Instance Method Details

#setup_showObject

Use the collection names to create views



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
78
79
80
81
82
83
84
85
86
# File 'lib/dash_analyzer/view.rb', line 47

def setup_show
  db.collection_names.each do |name|
    self.class.class_eval do
      get "/analytics/show/#{name}" do
        coll = db.collection(name)

        lead = "Listing #{coll.count} #{name.to_s.titlecase} Rollups in the Last Hour"

        table = Table(:column_names => ["Time", "Metric Name", "Number of Calls", "Total Time"])

        total_invocations = 0
        total_values = 0.0

        # TODO: Reverse this collection
        coll.find({:created_at => {:$gte => Time.now.advance(:hours => -1)}}, {:sort => {:created_at => Mongo::DESCENDING}}).each do |row|
          values = row["values"]
    
          # Why is this an array
          if values && !values.empty?
            value = values.first["value"]
            invocations = values.first["invocations"]
      
            total_values = value.to_f
            total_invocations += invocations.to_i
          end
    
          table << [row["created_at"], row["description"], invocations.to_i, value.to_f]
        end
  
        results = []
        results << "Total Calls: #{total_invocations}"
        results << "Total Time: #{total_values} seconds"
        results << "Avg Time per Call: #{total_values/total_invocations.to_f} seconds"
        results = results.join("\n")

        [lead, results, table.to_s].join("\n")
      end
    end
  end
end