Class: Rearview::MetricsValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/rearview/metrics_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#messageObject

Returns the value of attribute message.



3
4
5
# File 'lib/rearview/metrics_validator.rb', line 3

def message
  @message
end

Instance Method Details

#cacheObject



42
43
44
# File 'lib/rearview/metrics_validator.rb', line 42

def cache
  @cache ||= {}
end

#cache?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/rearview/metrics_validator.rb', line 39

def cache?
  options[:cache]
end

#clientObject



45
46
47
# File 'lib/rearview/metrics_validator.rb', line 45

def client
  @client ||= Graphite::Client.new(Rearview.config.graphite_connection)
end

#metric_valid?(metric) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/rearview/metrics_validator.rb', line 14

def metric_valid?(metric)
  valid = false
  metric_key = nil
  target_parser.parse(metric)
  if target_parser.error?
    message = "contains an unparseable metric: #{metric} (#{target_parser.error})"  
  else
    if target_parser.tree.comment? 
      valid = true
    else
      metric_key = target_parser.tree.metric
      if metric_key.present?
        if cache?
          unless cache.has_key?(metric_key)
            cache[metric_key] = client.metric_exists?(metric_key)
          end
          valid = cache[metric_key]
        else
          valid = client.metric_exists?(metric_key)
        end
      end
    end
  end
  valid
end

#target_parserObject



48
49
50
# File 'lib/rearview/metrics_validator.rb', line 48

def target_parser
  @target_parser ||= Graphite::TargetParser.new
end

#validate_each(record, attribute, value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/rearview/metrics_validator.rb', line 4

def validate_each(record, attribute, value)
  if value.present? && value.respond_to?(:each)
    value.each do |metric|
      unless metric_valid?(metric)
        record.errors.add(attribute,(message || "contains an invalid metric: #{metric}"))
        message = nil
      end
    end
  end
end