Class: Copperegg::Revealmetrics::CustomDashboard

Inherits:
Object
  • Object
show all
Includes:
Mixins::Persistence
Defined in:
lib/copperegg/revealmetrics/custom_dashboard.rb

Constant Summary collapse

WIDGET_TYPES =
%w(metric metric_list timeline)
WIDGET_STYLES =
%w(value timeline both list values)
WIDGET_MATCHES =
%w(select multi tag all)

Instance Attribute Summary collapse

Attributes included from Mixins::Persistence

#error, #id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Persistence

#delete, included, #initialize, #persisted?, #save

Instance Attribute Details

#dataObject

Returns the value of attribute data.



13
14
15
# File 'lib/copperegg/revealmetrics/custom_dashboard.rb', line 13

def data
  @data
end

#labelObject

Returns the value of attribute label.



13
14
15
# File 'lib/copperegg/revealmetrics/custom_dashboard.rb', line 13

def label
  @label
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/copperegg/revealmetrics/custom_dashboard.rb', line 13

def name
  @name
end

Class Method Details

.create(*args) ⇒ Object

Raises:

  • (ArgumentError)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/copperegg/revealmetrics/custom_dashboard.rb', line 93

def create(*args)
  options = args.last.class == Hash ? args.pop : {}

  return super(args.first) if args.first.is_a?(Hash)

  metric_group = args.first
  raise ArgumentError.new("Copperegg::Revealmetrics::MetricGroup object expected") if !metric_group.is_a?(MetricGroup)
  raise ArgumentError.new("Invalid metric group") if !metric_group.valid?

  metrics = filter_metrics(metric_group, options[:metrics]).map { |name| metric_group.metrics.find { |metric| metric.name == name } }
  identifiers = options[:identifiers].is_a?(Array) ? (options[:identifiers].empty? ? nil : options[:identifiers]) : (options[:identifier] ? [options[:identifiers]] : nil)
  widget_match = identifiers.nil? ? "all" : (identifiers.size == 1 ? "select" : "multi")
  widget_type = widget_match == "select" ? "metric" : "timeline"
  widget_style = widget_type == "metric" ? "both" : "values"
  name = options[:name] || "#{metric_group.label} Dashboard"

  dashboard = new(:name => name)
  metrics.each.with_index do |metric, i|
    metric_data = [metric.position, metric.name]
    metric_data.push("rate") if metric.type == "ce_counter" || metric.type == "ce_counter_f"
    widget = {:type => widget_type, :style => widget_style, :match => widget_match, :metric => {metric_group.name => [metric_data]}}
    widget[:match_param] = identifiers if identifiers
    dashboard.data["widgets"][i.to_s] = widget
  end
  dashboard.save
  dashboard
end

.find_by_name(name) ⇒ Object



121
122
123
# File 'lib/copperegg/revealmetrics/custom_dashboard.rb', line 121

def find_by_name(name)
  find.detect { |dashboard| dashboard.name == name }
end

Instance Method Details

#load_attributes(attributes) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/copperegg/revealmetrics/custom_dashboard.rb', line 15

def load_attributes(attributes)
  @data = {"widgets" => {}, "order" => []}
  attributes.each do |name, value|
    if name.to_s == "id"
      @id = value
    elsif name.to_s == "data"
      attributes[name].each do |data_name, data_value|
        if data_name.to_s == "order"
          data["order"] = data_value
        else
          data["widgets"] = data_value
        end
      end
    elsif !respond_to?("#{name}=")
      next
    else
      send "#{name}=", value
    end
  end
end

#to_hashObject



81
82
83
84
85
86
87
88
89
90
# File 'lib/copperegg/revealmetrics/custom_dashboard.rb', line 81

def to_hash
  set_data_order
  self.instance_variables.reduce({}) do |memo, variable|
    unless variable.to_s == "@error"
      value = instance_variable_get(variable)
      memo[variable.to_s.sub("@", "")] = value
    end
    memo
  end
end

#valid?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/copperegg/revealmetrics/custom_dashboard.rb', line 36

def valid?
  @error = nil
  if self.name.nil? || self.name.to_s.strip.empty?
    @error = "Name can't be blank."
  else
    self.data["widgets"].values.each do |widget|
      widget.each do |key, value|
        if key.to_s == "type" && !WIDGET_TYPES.include?(value)
          @error = "Invalid widget type #{value}."
        elsif key.to_s == "style" && !WIDGET_STYLES.include?(value)
          @error = "Invalid widget style #{value}."
        elsif key.to_s == "match" && !WIDGET_MATCHES.include?(value)
          @error = "Invalid widget match #{value}."
        elsif key.to_s == "metric" && (!value.is_a?(Hash) || value.keys.size == 0)
          @error = "Invalid widget metric. #{value}"
        else
          (widget["metric"] || widget[:metric]).each do |metric_group_name, metric_group_value|
            if !metric_group_value.is_a?(Array)
              @error = "Invalid widget metric. #{metric_group_value}"
            elsif metric_group_value.length == 0
              @error = "Invalid widget metric. #{metric_group_value}"
            else
              metric_group_value.each do |metric_data|
                if !metric_data.is_a?(Array)
                  @error = "Invalid widget metric. #{metric_group_value}"
                elsif metric_data.length < 2
                  @error = "Invalid widget metric. #{metric_group_value}"
                elsif (/^\d+$/ =~ metric_data.first.to_s).nil?
                  @error = "Invalid widget metric. #{metric_group_value}"
                end
              end
            end
          end
        end
      end
      match_param = widget["match_param"] || widget[:match_param]
      if (widget["match"] || widget[:match]) != "all" && (match_param.nil? || match_param.to_s.strip.empty?)
        @error = "Missing match parameter."
      end
      break if !@error.nil?
    end
  end
  @error.nil?
end