Class: Dash::Models::Dashboard

Inherits:
Base
  • Object
show all
Defined in:
lib/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, each, find, #match, #multi_match, #to_s, #update_params

Constructor Details

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

Returns a new instance of Dashboard.



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

def initialize(name, params={})
  super

  @graphs = []
  @graph_opts = {}
  params["graphs"].each do |name|
    # graphs map to option hashes
    if name.instance_of?(Hash) # could be YAML::Omap
      g = Graph.find(name.keys.first) # should only be one key
      @graph_opts[g] = name[name.keys.first]||{}
    else
      raise "Bad format for graph (must be a hash)"
    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.



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

def graph_opts
  @graph_opts
end

#graphsObject

Returns the value of attribute graphs.



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

def graphs
  @graphs
end

Class Method Details

.find_by_graph(graph) ⇒ Object



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

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



31
32
33
34
35
# File 'lib/models/dashboard.rb', line 31

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

#get_all_hosts(cluster = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/models/dashboard.rb', line 37

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



84
85
86
# File 'lib/models/dashboard.rb', line 84

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

#get_valid_hosts(graph, cluster = nil) ⇒ Object



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

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 by what matches the graph definition
  hosts = hosts.select { |h| h.multi_match(graph["hosts"]) }

  # filter if we have a dashboard-level 'hosts' filter
  if @params["hosts"]
    hosts = hosts.select { |h| h.multi_match(@params["hosts"]) }
  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



74
75
76
77
78
79
80
81
82
# File 'lib/models/dashboard.rb', line 74

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)
  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



88
89
90
91
92
93
94
95
96
97
# File 'lib/models/dashboard.rb', line 88

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

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