Class: Bipbip::Storage::Copperegg

Inherits:
Bipbip::Storage show all
Defined in:
lib/bipbip/storage/copperegg.rb

Instance Attribute Summary

Attributes inherited from Bipbip::Storage

#config, #name

Instance Method Summary collapse

Methods inherited from Bipbip::Storage

factory

Constructor Details

#initialize(name, config) ⇒ Copperegg

Returns a new instance of Copperegg.



3
4
5
6
# File 'lib/bipbip/storage/copperegg.rb', line 3

def initialize(name, config)
  super(name, config)
  ::Copperegg::Revealmetrics::Api.apikey = config['api_key']
end

Instance Method Details

#_load_dashboardsObject



75
76
77
78
79
80
81
82
83
# File 'lib/bipbip/storage/copperegg.rb', line 75

def _load_dashboards
  log(Logger::INFO, 'Loading dashboards')
  dashboards = ::Copperegg::Revealmetrics::CustomDashboard.find
  if dashboards.nil?
    log(Logger::FATAL, 'Cannot load dashboards')
    exit 1
  end
  dashboards
end

#_load_metric_groupsObject



65
66
67
68
69
70
71
72
73
# File 'lib/bipbip/storage/copperegg.rb', line 65

def _load_metric_groups
  log(Logger::INFO, 'Loading metric groups')
  metric_groups = ::Copperegg::Revealmetrics::MetricGroup.find
  if metric_groups.nil?
    log(Logger::FATAL, 'Cannot load metric groups')
    exit 1
  end
  metric_groups
end

#_load_tagsCopperegg::Revealmetrics::Tag[]

Returns:

  • (Copperegg::Revealmetrics::Tag[])


86
87
88
89
90
91
92
93
94
# File 'lib/bipbip/storage/copperegg.rb', line 86

def _load_tags
  log(Logger::INFO, 'Loading tags')
  tags = ::Copperegg::Revealmetrics::Tag.find
  if tags.nil?
    log(Logger::FATAL, 'Cannot load tags')
    exit 1
  end
  tags
end

#setup_plugin(plugin) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bipbip/storage/copperegg.rb', line 8

def setup_plugin(plugin)
  @metric_groups ||= _load_metric_groups
  @dashboards ||= _load_dashboards
  @tags ||= _load_tags

  unless [5, 15, 60, 300, 900, 3600, 21_600].include?(plugin.frequency)
    log(Logger::FATAL, "Cannot use frequency #{plugin.frequency}")
    exit 1
  end

  metric_group = @metric_groups.detect { |m| m.name == plugin.metric_group }
  if metric_group.nil? || !metric_group.is_a?(::Copperegg::Revealmetrics::MetricGroup)
    log(Logger::INFO, "Creating metric group `#{plugin.metric_group}`")
    metric_group = ::Copperegg::Revealmetrics::MetricGroup.new(name: plugin.metric_group, label: plugin.metric_group, frequency: plugin.frequency)
  end
  metric_group.frequency = plugin.frequency
  metric_group.metrics = plugin.metrics_schema.map do |sample|
    {
      name: sample[:name],
      type: 'ce_' + sample[:type],
      unit: sample[:unit]
    }
  end
  log(Logger::INFO, "Updating metric group `#{plugin.metric_group}`")
  metric_group.save

  plugin.tags.each do |tag_name|
    tag = @tags.detect { |t| t.name == tag_name }
    if tag.nil?
      log(Logger::INFO, "Creating tag `#{tag_name}`")
      tag = ::Copperegg::Revealmetrics::Tag.new(name: tag_name)
    end
    object_identifier = plugin.source_identifier
    unless tag.objects.include?(object_identifier)
      log(Logger::INFO, "Attaching object to tag `#{tag_name}`")
      tag.objects << object_identifier
      store_sample(plugin, Time.now, {}) # Need to store a sample before we can tag a custom object
    end
    log(Logger::INFO, "Updating tag `#{tag_name}`")
    tag.save
  end

  dashboard = @dashboards.detect { |d| d.name == plugin.metric_group }
  if dashboard.nil?
    log(Logger::INFO, "Creating dashboard `#{plugin.metric_group}`")
    metrics = metric_group.metrics || []
    ::Copperegg::Revealmetrics::CustomDashboard.create(metric_group, name: plugin.metric_group, identifiers: nil, metrics: metrics)
  end
end

#store_sample(plugin, time, data) ⇒ Object



58
59
60
61
62
63
# File 'lib/bipbip/storage/copperegg.rb', line 58

def store_sample(plugin, time, data)
  response = ::Copperegg::Revealmetrics::MetricSample.save(plugin.metric_group, plugin.source_identifier, time.to_i, data)
  if response.code != '200'
    fail("Cannot store copperegg data `#{data}`. Response code `#{response.code}`, message `#{response.message}`, body `#{response.body}`")
  end
end