Module: Octo::Counter::Helper

Included in:
Baseline, Octo::Counter, Trends
Defined in:
lib/octocore-cassandra/counter/helpers.rb

Constant Summary collapse

METHOD_PREFIX =

The prefix to use when converting a type constant into an aggregator method name

'aggregate'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.counter_textHash

Returns the mapping for counters as text

Returns:



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/octocore-cassandra/counter/helpers.rb', line 133

def self.counter_text
  {
    TYPE_MINUTE => 'Near Real Time',
    TYPE_MINUTE_30 => '30 Minute',
    TYPE_HOUR => 'Hourly',
    TYPE_HOUR_3 => '3 Hourly',
    TYPE_HOUR_6 => '6 Hourly',
    TYPE_HOUR_12 => '12 Hourly',
    TYPE_DAY => 'Daily',
    TYPE_DAY_3 => '3 Days',
    TYPE_DAY_6 => '6 Days',
    TYPE_WEEK => 'Weekly'
  }
end

Instance Method Details

#generate_aggregators(&block) ⇒ Object

Generate aggregator methods for a class. You can pass your own block to generator for all custom needs. Check out the implementation at Octo::Counter#countables or Octo::Trends#trendable

Parameters:

  • block (Block)

    The block to be evaluated while executing the method



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/octocore-cassandra/counter/helpers.rb', line 154

def generate_aggregators(&block)
  @stored_block = block
  type_counters_method_names.each do |method_name|
    singleton_class.module_eval(<<-RUBY, __FILE__, __LINE__+1)
      def #{ method_name } (ts=Time.now.floor)
        bl = self.instance_variable_get(:@stored_block)
        instance_exec(ts, __method__, &bl) if bl
      end
    RUBY
  end
end

#get_duration_for_counter_type(type, ts = Time.now.ceil) ⇒ Time

Gets the duration for a particular counter type. This helps in

aggregation.

Parameters:

  • type (Fixnum)

    The counter type

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

    The time at wich duration needs to be passed

Returns:

  • (Time)

    The time or time range for the given specs



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
# File 'lib/octocore-cassandra/counter/helpers.rb', line 105

def get_duration_for_counter_type(type, ts=Time.now.ceil)
  start_time, step =  case type
                        when TYPE_MINUTE
                          [2.minute.ago, 1.minute]
                        when TYPE_MINUTE_30
                          [30.minute.ago, 1.minute]
                        when TYPE_HOUR
                          [1.hour.ago, 30.minute]
                        when TYPE_HOUR_3
                          [3.hour.ago, 1.hour]
                        when TYPE_HOUR_6
                          [6.hour.ago, 3.hour]
                        when TYPE_HOUR_12
                          [12.hour.ago, 6.hour]
                        when TYPE_DAY
                          [1.day.ago, 6.hour]
                        when TYPE_DAY_3
                          [3.day.ago, 1.day]
                        when TYPE_DAY_6
                          [6.day.ago, 3.day]
                        when TYPE_WEEK
                          [1.week.ago, 1.week]
                      end
  start_time.ceil.to(ts, step)
end

#get_fromtype_for_totype(totype) ⇒ Object

Generates a fromtype for a totype. This defines the relation

of aggregation needed for the counters.


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/octocore-cassandra/counter/helpers.rb', line 75

def get_fromtype_for_totype(totype)
  case totype
    when TYPE_MINUTE_30
      TYPE_MINUTE
    when TYPE_HOUR
      TYPE_MINUTE_30
    when TYPE_HOUR_3
      TYPE_HOUR
    when TYPE_HOUR_6
      TYPE_HOUR_3
    when TYPE_HOUR_12
      TYPE_HOUR_6
    when TYPE_DAY
      TYPE_HOUR_6
    when TYPE_DAY_3
      TYPE_DAY
    when TYPE_DAY_6
      TYPE_DAY_3
    when TYPE_WEEK
      TYPE_DAY_3
    else
      TYPE_WEEK
  end
end

#get_typecountersArray

Get all the type counters i.e. TYPE_MINUTE_30 etc from

Counter class.

Returns:

  • (Array)

    Array of all the constants that define a counter type



25
26
27
28
29
30
31
32
33
34
# File 'lib/octocore-cassandra/counter/helpers.rb', line 25

def get_typecounters
  max = max_type
  Counter.constants.select do |x|
    if x.to_s.start_with?('TYPE')
      Counter.const_get(x) <= max
    else
      false
    end
  end
end

#max_type(type = nil) ⇒ Object

Define the max granularity that should exist



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/octocore-cassandra/counter/helpers.rb', line 37

def max_type(type = nil)
  if @max_type
    @max_type
  else
    if type
      @max_type = type
    else
      @max_type = 9
    end
  end
  @max_type
end

#method_names_type_counter(method_name) ⇒ Symbol

Coverts the method name to the constant type

Parameters:

  • method_name (String)

    The method name to convert into constant

Returns:

  • (Symbol)

    The constant



53
54
55
56
57
58
59
# File 'lib/octocore-cassandra/counter/helpers.rb', line 53

def method_names_type_counter(method_name)
  prefix, *counterType = method_name.to_s.split('_')
  if prefix == METHOD_PREFIX
    cnst = counterType.join('_').upcase.to_sym
    string_to_const_val(cnst)
  end
end

#string_to_const_val(cnst) ⇒ Fixnum

Converts a string (which may represent a counter constant) into its

corresponding constant

Parameters:

  • cnst (String)

    The string which may represent a constant

Returns:

  • (Fixnum)

    The constant value; iff the string represent a constant. Nil otherwise



66
67
68
69
70
71
# File 'lib/octocore-cassandra/counter/helpers.rb', line 66

def string_to_const_val(cnst)
  index = Counter.constants.index(cnst)
  if index
    Counter.const_get(cnst)
  end
end

#type_counters_method_names(type = nil) ⇒ Object

Defined the method names for the type counters.



12
13
14
15
16
17
18
19
20
# File 'lib/octocore-cassandra/counter/helpers.rb', line 12

def type_counters_method_names(type = nil)
  if type.nil?
    get_typecounters.map do |typ|
      [METHOD_PREFIX, typ.to_s.downcase].join('_')
    end
  else
    [METHOD_PREFIX, type.to_s.downcase].join('_')
  end
end