Class: Blackbeard::MetricData::Base

Inherits:
Object
  • Object
show all
Includes:
ConfigurationMethods
Defined in:
lib/blackbeard/metric_data/base.rb

Direct Known Subclasses

Total, Unique

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConfigurationMethods

#config, #db, #guest_method, included, #tz

Constructor Details

#initialize(metric, group = nil) ⇒ Base

Returns a new instance of Base.



11
12
13
14
# File 'lib/blackbeard/metric_data/base.rb', line 11

def initialize(metric, group = nil)
  @metric = metric
  @group = group
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



9
10
11
# File 'lib/blackbeard/metric_data/base.rb', line 9

def group
  @group
end

#metricObject (readonly)

Returns the value of attribute metric.



9
10
11
# File 'lib/blackbeard/metric_data/base.rb', line 9

def metric
  @metric
end

Instance Method Details

#hour_keys_for_day(date) ⇒ Object



32
33
34
35
# File 'lib/blackbeard/metric_data/base.rb', line 32

def hour_keys_for_day(date)
  start_of_day = date.to_time
  Array(0..23).map{|x| start_of_day + (3600 * x) }.map{|t| key_for_hour(t) }
end

#keyObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/blackbeard/metric_data/base.rb', line 45

def key
  @key ||= begin
    lookup_hash = "metric_data_keys"
    lookup_field = "metric-#{metric.id}"
    lookup_field += "::group-#{group.id}" if group
    uid = db.hash_get(lookup_hash, lookup_field)
    if uid.nil?
      uid = db.increment("metric_data_next_uid")
      # write and read to avoid race conditional writes
      db.hash_key_set_if_not_exists(lookup_hash, lookup_field, uid)
      uid = db.hash_get(lookup_hash, lookup_field)
    end
    "data::#{uid}"
  end
end

#recent_days(count = 28, starting_on = tz.now.to_date) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/blackbeard/metric_data/base.rb', line 16

def recent_days(count=28, starting_on = tz.now.to_date)
  Array(0..count-1).map do |offset|
    date = starting_on - offset
    result = result_for_day(date)
    Blackbeard::MetricDate.new(date, result)
  end
end

#recent_hours(count = 24, starting_at = tz.now) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/blackbeard/metric_data/base.rb', line 24

def recent_hours(count = 24, starting_at = tz.now)
  Array(0..count-1).map do |offset|
    hour = starting_at - (offset * 3600)
    result = result_for_hour(hour)
    Blackbeard::MetricHour.new(hour, result)
  end
end

#result_for_day(date) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/blackbeard/metric_data/base.rb', line 37

def result_for_day(date)
  key = key_for_date(date)
  result = db.hash_get_all(key)
  result = generate_result_for_day(date) if result.empty?
  result.each { |k,v| result[k] = v.to_f }
  result
end

#segmentsObject



61
62
63
64
65
66
67
# File 'lib/blackbeard/metric_data/base.rb', line 61

def segments
  if group && group.segments.any?
    group.segments
  else
    [self.class::DEFAULT_SEGMENT]
  end
end