Class: Pencil::Models::Dashboard

Inherits:
Base
  • Object
show all
Defined in:
lib/pencil/models/dashboard.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#<=>, #[], all, #compose_metric, #compose_metric2, each, find, #match, #multi_match, #to_s, #update_params

Constructor Details

#initialize(name, params = {}) ⇒ Dashboard

Returns a new instance of Dashboard.



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

def initialize(name, params={})
  super

  @graphs = []
  @graph_opts = {}
  params["graphs"].each do |n|
    if n.respond_to?(:keys)
      key = n.keys.first # should only be one key
      val = n.values.first
      g = Graph.find(key)
      @graph_opts[g] = val||{}
    else
      raise "Bad format for graph (must be a hash-y; #{n.class}:#{n.inspect} is not)"
    end

    @graphs << g if g
  end

  @valid_hosts_table = {} # cache calls to get_valid_hosts
end

Instance Attribute Details

#graph_optsObject

Returns the value of attribute graph_opts.



10
11
12
# File 'lib/pencil/models/dashboard.rb', line 10

def graph_opts
  @graph_opts
end

#graphsObject

Returns the value of attribute graphs.



9
10
11
# File 'lib/pencil/models/dashboard.rb', line 9

def graphs
  @graphs
end

Class Method Details

.find_by_graph(graph) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/pencil/models/dashboard.rb', line 111

def self.find_by_graph(graph)
  ret = []
  Dashboard.each do |name, dash|

    if dash["graphs"].map { |x| x.keys.first }.member?(graph.name)
      ret << dash
    end
  end

  return ret
end

Instance Method Details

#clustersObject



33
34
35
36
37
# File 'lib/pencil/models/dashboard.rb', line 33

def clusters
  clusters = Set.new
  @graphs.each { |g| clusters += get_valid_hosts(g)[1] }
  clusters.sort
end

#get_all_hosts(cluster = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/pencil/models/dashboard.rb', line 39

def get_all_hosts(cluster=nil)
  hosts = Set.new
  clusters = Set.new
  @graphs.each do |g|
    h, c = get_valid_hosts(g, cluster)
    hosts += h
    clusters += c
  end
  return hosts, clusters
end

#get_host_wildcards(graph) ⇒ Object



97
98
99
# File 'lib/pencil/models/dashboard.rb', line 97

def get_host_wildcards(graph)
  return graph_opts[graph]["hosts"] || @params["hosts"] || graph["hosts"]
end

#get_valid_hosts(graph, cluster = nil) ⇒ Object



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
# File 'lib/pencil/models/dashboard.rb', line 50

def get_valid_hosts(graph, cluster=nil)
  if @valid_hosts_table[[graph, cluster]]
    return @valid_hosts_table[[graph, cluster]]
  end

  clusters = Set.new
  if cluster
    hosts = Host.find_by_cluster(cluster)
  else
    hosts = Host.all
  end

  # filter as:
  #   - the dashboard graph hosts definition
  #   - the dashboard hosts definition
  #   - the graph hosts definition
  # this is new behavior: before the filters were additive
  filter = graph_opts[graph]["hosts"] || @params["hosts"] || graph["hosts"]
  if filter
    hosts = hosts.select { |h| h.multi_match(filter) }
  end

  hosts.each { |h| clusters << h.cluster }

  @valid_hosts_table[[graph, cluster]] = [hosts, clusters]
  return hosts, clusters
end

#render_cluster_graph(graph, clusters, opts = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pencil/models/dashboard.rb', line 78

def render_cluster_graph(graph, clusters, opts={})
  # FIXME: edge case where the dash filter does not filter to a subset of
  # the hosts filter

  hosts = get_host_wildcards(graph)

  # graphite doesn't support strict matching (as /\d+/), so we need to
  # enumerate the hosts if a "#" wildcard is found
  if ! (filter = hosts.select { |h| h =~ /#/ }).empty?
    hosts_new = hosts - filter
    hosts2 = Host.all.select { |h| h.multi_match(filter) }
    hosts = (hosts2.map {|h| h.name } + hosts_new).sort.uniq.join(',')
  end

  opts[:sum] = :cluster unless opts[:zoom]
  graph_url = graph.render_url(hosts.to_a, clusters, opts)
  return graph_url
end

#render_global_graph(graph, opts = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/pencil/models/dashboard.rb', line 101

def render_global_graph(graph, opts={})
  hosts = get_host_wildcards(graph)
  _, clusters = get_valid_hosts(graph)

  type = opts[:zoom] ? :cluster : :global
  options = opts.merge({:sum => type})
  graph_url = graph.render_url(hosts, clusters, options)
  return graph_url
end