Class: MongodbLogger::ServerModel::Analytic

Inherits:
Base
  • Object
show all
Defined in:
lib/mongodb_logger/server/model/analytic.rb

Constant Summary

FIXED_PARAMS_ON_FORM =
['type', 'unit', 'start_date', 'end_date']
ANALYTIC_TYPES =
[[0, "Count of requests"], [1, "Count of errors"]]
ANALYTIC_UNITS =
[[0, "Month"], [1, "Day"], [2, "Hour"]]
FORM_NAME =
"analytic"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#create_variable, #set_params_to_methods

Constructor Details

#initialize(mongo_adapter, params) ⇒ Analytic



12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#mongo_adapterObject (readonly)

Returns the value of attribute mongo_adapter



9
10
11
# File 'lib/mongodb_logger/server/model/analytic.rb', line 9

def mongo_adapter
  @mongo_adapter
end

#paramsObject (readonly)

Returns the value of attribute params



9
10
11
# File 'lib/mongodb_logger/server/model/analytic.rb', line 9

def params
  @params
end

Instance Method Details

#calculate_default_map_reduce(params = {}) ⇒ Object



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
56
57
58
59
60
61
62
63
64
# 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_nameObject



24
25
26
# File 'lib/mongodb_logger/server/model/analytic.rb', line 24

def form_name
  FORM_NAME
end

#get_dataObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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