Class: RequestResponseStats::ReqResStat

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document
Defined in:
lib/request_response_stats/req_res_stat.rb

Constant Summary collapse

DEFAULT_STATS_GRANULARITY =
1.hour
PERCISION =
2

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_avg(key, start_time, end_time, granularity = DEFAULT_STATS_GRANULARITY) ⇒ Object

wrapper around ‘get_stat` for :avg stat



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/request_response_stats/req_res_stat.rb', line 66

def get_avg(key, start_time, end_time, granularity = DEFAULT_STATS_GRANULARITY)
  data = get_stat("sum", key, start_time, end_time, granularity)
  data.each do |e|
    e[:stat_type] = "avg"
    if e[:count] != 0
      e[:data] = (e[:data] * 1.0 / e[:count]).try(:round, PERCISION)
    else
      e[:data] = 0
    end

  end
  data
end

.get_details(key, start_time, end_time, stat_type = nil, granularity = DEFAULT_STATS_GRANULARITY) ⇒ Object

set ‘stat_type` as `nil` to return grouped but uncompacted data otherwise, you can set `stat_type` as :sum, :max, :min, :avg to get grouped data



82
83
84
85
86
87
88
89
90
91
92
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
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/request_response_stats/req_res_stat.rb', line 82

def get_details(key, start_time, end_time, stat_type = nil, granularity = DEFAULT_STATS_GRANULARITY)
  # get ungrouped data
  stat_type = stat_type.to_s.to_sym if stat_type
  key = key.to_s.to_sym
  relevant_records = get_within(start_time, end_time)
  time_ranges = get_time_ranges(start_time, end_time, granularity)
  stats = time_ranges.map do |time_range|
    data_for_time_range = relevant_records.get_within(*time_range.values).map{ |r|
      {server_plus_api: r.server_plus_api, data: r[key], key_name: r.key_name}
    }
    {data: data_for_time_range, **time_range}
  end

  # grouping data by :server_plus_api
  stats.each do |r|
    data = r[:data]
    data = data.map{ |e| {server_plus_api: e[:server_plus_api], data: e[:data]} }
    data = data.group_by { |e| e[:server_plus_api] }
    r[:data] = data
  end

  # calculating grouped value based on stat_type
  if stat_type
    if [:sum, :min, :max].include? stat_type

      # calculate grouped value
      stats.each do |r|
        data = r[:data]
        data = data.map do |k, v|
          # {server_plus_api: k, data: v.map{|e| e[:data]}}
          element_data = v.map{|e| e[:data]}
          {server_plus_api: k, count: element_data.size, data: element_data.compact.public_send(stat_type).try(:round, PERCISION)}
        end
        r[:data] = data
      end

      stats
    elsif stat_type == :avg
      data = get_details(key, start_time, end_time, stat_type = :sum, granularity)
      data.each do |r|
        r[:data].each do |e|
          e[:data] = (e[:data] * 1.0 / e[:count]).try(:round, PERCISION)
        end
      end

      data
    else
      "This :stat_type is not supported"
    end
  else
    stats
  end
end

.get_max(key, start_time, end_time, granularity = DEFAULT_STATS_GRANULARITY) ⇒ Object

wrapper around ‘get_stat` for :max stat



61
62
63
# File 'lib/request_response_stats/req_res_stat.rb', line 61

def get_max(key, start_time, end_time, granularity = DEFAULT_STATS_GRANULARITY)
  get_stat("max", key, start_time, end_time, granularity)
end

.get_min(key, start_time, end_time, granularity = DEFAULT_STATS_GRANULARITY) ⇒ Object

wrapper around ‘get_stat` for :min stat



56
57
58
# File 'lib/request_response_stats/req_res_stat.rb', line 56

def get_min(key, start_time, end_time, granularity = DEFAULT_STATS_GRANULARITY)
  get_stat("min", key, start_time, end_time, granularity)
end

.get_sum(key, start_time, end_time, granularity = DEFAULT_STATS_GRANULARITY) ⇒ Object

wrapper around ‘get_stat` for :sum stat



51
52
53
# File 'lib/request_response_stats/req_res_stat.rb', line 51

def get_sum(key, start_time, end_time, granularity = DEFAULT_STATS_GRANULARITY)
  get_stat("sum", key, start_time, end_time, granularity)
end

.get_within(start_time, end_time) ⇒ Object

Note: ‘start_time` and `end_time` are Time objects `start_time` in inclusive but `end_time` is not



46
47
48
# File 'lib/request_response_stats/req_res_stat.rb', line 46

def get_within(start_time, end_time)
  where(:start_time.gte => start_time, :end_time.lt => end_time)
end

Instance Method Details

#server_plus_apiObject



38
39
40
# File 'lib/request_response_stats/req_res_stat.rb', line 38

def server_plus_api
  [server_name, api_name, api_verb].join("_")
end