class MongodbLogger::ServerModel::Analytic
Constants
- ANALYTIC_TYPES
- ANALYTIC_UNITS
- FIXED_PARAMS_ON_FORM
- FORM_NAME
Attributes
mongo_adapter[R]
params[R]
Public Class Methods
new(mongo_adapter, params)
click to toggle source
# File lib/mongodb_logger/server/model/analytic.rb, line 12 def initialize(mongo_adapter, params) FIXED_PARAMS_ON_FORM.each do |key| create_variable(key, nil) end @mongo_adapter = mongo_adapter @params = params set_params_to_methods # def values self.start_date ||= Time.now.strftime('%Y-%m-%d') self.end_date ||= Time.now.strftime('%Y-%m-%d') end
Public Instance Methods
calculate_default_map_reduce(params = {})
click to toggle source
# File lib/mongodb_logger/server/model/analytic.rb, line 28 def calculate_default_map_reduce(params = {}) addinional_params = case self.unit.to_i when 1 "day: this.request_time.getDate()" when 2 "day: this.request_time.getDate(), hour: this.request_time.getHours() + 1" else "" end map = <<EOF function() { var key = { year: this.request_time.getFullYear(), month: this.request_time.getMonth() + 1, #{addinional_params} }; emit(key, {count: 1}); } EOF reduce = <<EOF function(key, values) { var sum = 0; values.forEach(function(f) { sum += f.count; }); return {count: sum}; } EOF case self.type.to_i when 1 params[:conditions].merge!({:is_exception => true}) else # nothing end @mongo_adapter.calculate_mapreduce(map, reduce, {:conditions => params[:conditions]}) end
form_name()
click to toggle source
# File lib/mongodb_logger/server/model/analytic.rb, line 24 def form_name FORM_NAME end
get_data()
click to toggle source
# File lib/mongodb_logger/server/model/analytic.rb, line 66 def get_data m_start= Date.parse(self.start_date) rescue Date.today m_end = Date.parse(self.end_date) rescue Date.today conditions = { :request_time => { '$gte' => Time.utc(m_start.year, m_start.month, m_start.day, 0, 0, 0), '$lte' => Time.utc(m_end.year, m_end.month, m_end.day, 23, 59, 59) }} all_data = calculate_default_map_reduce( :conditions => conditions ) { :data => (all_data ? all_data.first.last : []), :headers => { :key => ["year", "month", "day", "hour"], :value => ["count"] }, unit: self.unit } end