Class: SimpleMetrics::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_metrics/bucket.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Bucket

Returns a new instance of Bucket.



36
37
38
39
40
41
# File 'lib/simple_metrics/bucket.rb', line 36

def initialize(attributes)
  @name    = attributes.fetch(:name)
  @seconds = attributes.fetch(:seconds)
  @capped  = attributes.fetch(:capped)
  @size    = attributes.fetch(:size)
end

Instance Attribute Details

#cappedObject (readonly)

Returns the value of attribute capped.



34
35
36
# File 'lib/simple_metrics/bucket.rb', line 34

def capped
  @capped
end

#nameObject (readonly)

Returns the value of attribute name.



34
35
36
# File 'lib/simple_metrics/bucket.rb', line 34

def name
  @name
end

Class Method Details

.[](index) ⇒ Object



15
16
17
# File 'lib/simple_metrics/bucket.rb', line 15

def [](index)
  all[index]
end

.allObject



7
8
9
# File 'lib/simple_metrics/bucket.rb', line 7

def all
  @@all ||= SimpleMetrics.config.buckets.map { |r| Bucket.new(r) }
end

.firstObject



11
12
13
# File 'lib/simple_metrics/bucket.rb', line 11

def first
  all.first
end

.for_time(time) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/simple_metrics/bucket.rb', line 19

def for_time(time)
  case time
  when 'minute'
    self[0]
  when 'hour'
    self[1]
  when 'day'
    self[2]
  when 'week'
    self[3]
  end
end

Instance Method Details

#capped?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/simple_metrics/bucket.rb', line 92

def capped?
  @capped == true
end

#fill_gaps(from, to, query_result) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/simple_metrics/bucket.rb', line 96

def fill_gaps(from, to, query_result)
  return query_result if query_result.nil? || query_result.size == 0
  
  existing_ts_entries =  query_result.inject({}) { |result, dp| result[dp.ts] = dp; result }
  dp_template = query_result.first

  result = []
  each_ts(from, to) do |ts_bucket|
    dp = existing_ts_entries[ts_bucket] || DataPoint::Base.new(:name => dp_template.name, :ts => ts_bucket)
    result << dp
  end
  result
end

#find_all_at_ts(ts) ⇒ Object

TODO: only used in tests, do we need it?



64
65
66
# File 'lib/simple_metrics/bucket.rb', line 64

def find_all_at_ts(ts)
  repository.find_all_at_ts(ts_bucket(ts))
end

#find_all_in_ts_range_by_name(from, to, name) ⇒ Object



72
73
74
# File 'lib/simple_metrics/bucket.rb', line 72

def find_all_in_ts_range_by_name(from, to, name)
  repository.find_all_in_ts_range_by_name(from, to, name)
end

#find_all_in_ts_range_by_wildcard(from, to, target) ⇒ Object



76
77
78
# File 'lib/simple_metrics/bucket.rb', line 76

def find_all_in_ts_range_by_wildcard(from, to, target)
  repository.find_all_in_ts_range_by_wildcard(from, to, target)
end

#find_data_point_at_ts(ts, name) ⇒ Object



68
69
70
# File 'lib/simple_metrics/bucket.rb', line 68

def find_data_point_at_ts(ts, name)
  repository.find_data_point_at_ts(ts_bucket(ts), name)
end

#next_ts_bucket(ts) ⇒ Object



55
56
57
# File 'lib/simple_metrics/bucket.rb', line 55

def next_ts_bucket(ts)
  ts_bucket(ts) + seconds
end

#previous_ts_bucket(ts) ⇒ Object



59
60
61
# File 'lib/simple_metrics/bucket.rb', line 59

def previous_ts_bucket(ts)
  ts_bucket(ts) - seconds
end

#save(dp, ts) ⇒ Object



80
81
82
83
84
85
# File 'lib/simple_metrics/bucket.rb', line 80

def save(dp, ts)
  dp.ts    = ts_bucket(ts)
  dp.sum   = dp.value
  dp.total = 1
  repository.save(dp)
end

#secondsObject



43
44
45
# File 'lib/simple_metrics/bucket.rb', line 43

def seconds
  @seconds.to_i
end

#sizeObject



47
48
49
# File 'lib/simple_metrics/bucket.rb', line 47

def size
  @size.to_i
end

#ts_bucket(ts) ⇒ Object



51
52
53
# File 'lib/simple_metrics/bucket.rb', line 51

def ts_bucket(ts)
  (ts / seconds) * seconds
end

#update(dp, ts) ⇒ Object



87
88
89
90
# File 'lib/simple_metrics/bucket.rb', line 87

def update(dp, ts)
  dp.ts = ts_bucket(ts)
  repository.update(dp, ts)
end