Class: CopperEgg::CustomDashboard

Inherits:
Object
  • Object
show all
Includes:
Mixins::Persistence
Defined in:
lib/copperegg/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.



11
12
13
# File 'lib/copperegg/custom_dashboard.rb', line 11

def data
  @data
end

#is_databaseObject

Returns the value of attribute is_database.



11
12
13
# File 'lib/copperegg/custom_dashboard.rb', line 11

def is_database
  @is_database
end

#labelObject

Returns the value of attribute label.



11
12
13
# File 'lib/copperegg/custom_dashboard.rb', line 11

def label
  @label
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/copperegg/custom_dashboard.rb', line 11

def name
  @name
end

Class Method Details

.create(*args) ⇒ Object

Raises:

  • (ArgumentError)


91
92
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
# File 'lib/copperegg/custom_dashboard.rb', line 91

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::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, is_database: (options[:is_database] || false))
	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



119
120
121
# File 'lib/copperegg/custom_dashboard.rb', line 119

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

Instance Method Details

#load_attributes(attributes) ⇒ Object



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

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



79
80
81
82
83
84
85
86
87
88
# File 'lib/copperegg/custom_dashboard.rb', line 79

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)


34
35
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
# File 'lib/copperegg/custom_dashboard.rb', line 34

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