Class: SimpleMetrics::DataPointRepository

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

Constant Summary collapse

@@collection =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ DataPointRepository

Returns a new instance of DataPointRepository.



61
62
63
# File 'lib/simple_metrics/data_point_repository.rb', line 61

def initialize(collection)
  @collection = collection
end

Class Method Details

.collection(name) ⇒ Object

Raises:

  • (ArgumentError)


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

def collection(name)
  raise ArgumentError, "Unknown retention: #{name}" unless retention_names.include?(name)
  @@collection[name] ||= db.collection(name)
end

.ensure_collections_existObject



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

def ensure_collections_exist
  SimpleMetrics.logger.debug "SERVER: MongoDB - found following collections: #{db.collection_names.inspect}"
  buckets.each do |retention|
    unless db.collection_names.include?(retention.fetch(:name))
      db.create_collection(retention.fetch(:name), :capped => retention.fetch(:capped), :size => retention.fetch(:size)) 
      SimpleMetrics.logger.debug "SERVER: MongoDB - created collection #{retention.fetch(:name)}, capped: #{retention.fetch(:capped)}, size: #{retention.fetch(:size)}"
    end
    
    db.collection(retention.fetch(:name)).ensure_index([['ts', ::Mongo::ASCENDING]])
    db.collection(retention.fetch(:name)).ensure_index([['_id', ::Mongo::ASCENDING]])
    SimpleMetrics.logger.debug "SERVER: MongoDB - ensure index on column ts for collection #{retention.fetch(:name)}"
  end 
end

.for_retention(name) ⇒ Object



10
11
12
# File 'lib/simple_metrics/data_point_repository.rb', line 10

def for_retention(name)
  self.new(collection(name))
end

.truncate_collectionsObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/simple_metrics/data_point_repository.rb', line 33

def truncate_collections
  buckets.each do |retention|
    if db.collection_names.include?(retention.fetch(:name))
      if retention.fetch(:capped)
        collection(retention.fetch(:name)).drop # capped collections can't remove elements, drop it instead
      else
        collection(retention.fetch(:name)).remove
      end
      SimpleMetrics.logger.debug "SERVER: MongoDB - truncated collection #{retention.fetch(:name)}"
    end
  end
end

Instance Method Details

#find_all_at_ts(ts) ⇒ Object



73
74
75
76
# File 'lib/simple_metrics/data_point_repository.rb', line 73

def find_all_at_ts(ts)
  results = @collection.find({ :ts => ts }).to_a
  data_points(results)
end

#find_all_in_ts_range_by_name(from, to, name) ⇒ Object



83
84
85
86
# File 'lib/simple_metrics/data_point_repository.rb', line 83

def find_all_in_ts_range_by_name(from, to, name)
  results = @collection.find({ :name => name }.merge(range_query(from, to))).to_a
  data_points(results)
end

#find_all_in_ts_range_by_wildcard(from, to, target) ⇒ Object



88
89
90
91
# File 'lib/simple_metrics/data_point_repository.rb', line 88

def find_all_in_ts_range_by_wildcard(from, to, target)
  results = @collection.find({ :name => regexp(target) }.merge(range_query(from, to))).to_a
  data_points(results)
end

#find_data_point_at_ts(ts, name) ⇒ Object



78
79
80
81
# File 'lib/simple_metrics/data_point_repository.rb', line 78

def find_data_point_at_ts(ts, name)
  result = @collection.find_one({ :ts => ts, :name => name })
  data_point(result) if result
end

#save(result) ⇒ Object



65
66
67
# File 'lib/simple_metrics/data_point_repository.rb', line 65

def save(result)
  @collection.insert(result.attributes.reject { |k, v| k == 'id' })
end

#update(dp, ts) ⇒ Object



69
70
71
# File 'lib/simple_metrics/data_point_repository.rb', line 69

def update(dp, ts)
  @collection.update({ "_id" => dp.id }, { "$set" => { :value => dp.value, :sum => dp.sum, :total => dp.total }})  
end