Module: Octo::Baseline

Includes:
Counter::Helper
Included in:
CategoryBaseline, ProductBaseline, TagBaseline
Defined in:
lib/octocore-mongo/baseline.rb

Overview

The baseline module. This module has methods that support

a baseline structure.

Constant Summary collapse

MAX_DURATION =

Define the past duration to look for while calculating baseline. This must be in days

7

Constants included from Counter::Helper

Counter::Helper::METHOD_PREFIX

Instance Method Summary collapse

Methods included from Counter::Helper

counter_text, #generate_aggregators, #get_duration_for_counter_type, #get_fromtype_for_totype, #get_typecounters, #max_type, #method_names_type_counter, #string_to_const_val, #type_counters_method_names

Instance Method Details

#aggregate(type, ts) ⇒ Object

Does an aggregation of type for a timestamp

Parameters:

  • type (Fixnum)

    The counter type for which aggregation has to be done

  • ts (Time)

    The time at which aggregation should happen



63
64
65
66
67
# File 'lib/octocore-mongo/baseline.rb', line 63

def aggregate(type, ts)
  Octo::Enterprise.each do |enterprise|
    aggregate_baseline enterprise.id, type, ts
  end
end

#aggregate_baseline(enterprise_id, type, ts = Time.now.floor) ⇒ Object

Aggregates the baseline for a minute



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/octocore-mongo/baseline.rb', line 71

def aggregate_baseline(enterprise_id, type, ts = Time.now.floor)
  clazz = @baseline_for.constantize
  _ts = ts
  start_calc_time = (_ts.to_datetime - MAX_DURATION.day).to_time
  last_n_days_interval = start_calc_time.ceil.to(_ts, 24.hour)
  last_n_days_interval.each do |hist|
    args = {
        ts: hist,
        type: type,
        enterprise_id: enterprise_id
    }
    counters = @baseline_for.constantize.send(:where, args)
    baseline = baseline_from_counters(counters)
    store_baseline enterprise_id, type, hist, baseline
  end
end

#baseline_for(klass) ⇒ Object

Defines the class for whom the baseline is applicable



32
33
34
# File 'lib/octocore-mongo/baseline.rb', line 32

def baseline_for(klass)
  @baseline_for = klass
end

#baselineableObject

Defines the column needed for a baseline



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/octocore-mongo/baseline.rb', line 17

def baselineable
  key :type, Integer
  key :ts, Time
  key :uid, String

  key :val, Float

  # Generate the aggregator methods
  generate_aggregators { |ts, method|
    type = method_names_type_counter(method)
    aggregate type, ts
  }
end

#get_baseline_value(baseline_type, obj, ts = Time.now.ceil) ⇒ Object

Finds baseline value of an object

Parameters:

  • baseline_type (Fixnum)

    One of the valid Baseline Types defined

  • obj (Object)

    The object for whom baseline value is to be found

  • ts (Time) (defaults to: Time.now.ceil)

    The timestamp at which baseline is to be found



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/octocore-mongo/baseline.rb', line 40

def get_baseline_value(baseline_type, obj, ts = Time.now.ceil)
  unless Octo::Counter.constants.include?baseline_type
    raise ArgumentError, 'No such baseline defined'
  end

  args = {
      ts: ts,
      type: Octo::Counter.const_get(baseline_type),
      uid: obj.unique_id,
      enterprise_id: obj.enterprise.id
  }
  bl = get_cached(args)
  if bl
    bl.val
  else
    0.01
  end
end