summaryrefslogtreecommitdiff
blob: 129f9d1dfbdba89e7479c1ee77fac00bcd9163c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
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
80
81
82
83
84
85
86
87
88
89
90
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
helpers do
  def h(text)
    Rack::Utils.escape_html(text)
  end

  def markdown(text)
    markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true)
    markdown.render(text)
  end

  def get_forced_state(notices)
    notices.each do |notice|
      next unless notice.has_key? 'force_state'

      return notice['force_state']
    end

    nil
  end

  def services_info(services)
    content = '<div class="list-group">'

    services.each do |service|
      _service_status = service_status(service)
      _status_text = status_text(_service_status)
      _service_info = service_info(service)
      service_name = ServiceRegistry.instance.services[service][:name]
      data_service_name = service_name.gsub('"', "'")
      content << "
        <a class=\"list-group-item has-tooltip notice-link\" href=\"#notices\" title=\"#{_status_text}\"
           data-toggle=\"tooltip\" data-placement=\"top\" data-container=\"body\"
           data-service=\"#{service}\" data-service-name=\"#{data_service_name}\">
        #{_service_info}
        #{service_name}
        </a>"
    end

    content << '</div>'
  end

  def service_status(service)
    return 'na' unless ServiceRegistry.instance.services.has_key? service
    active_notices = NoticeStore.instance.active_notices_for(service)

    unless (forced_state = get_forced_state(active_notices)) == nil
      return forced_state
    else
      case ServiceRegistry.instance.services[service][:status]
        when State::UP
          return 'up'
        when State::WARNING
          return 'warning'
        when State::DOWN
          return 'down'
        else
          return 'na'
      end
    end
  end

  def service_info(service)
    visible_notices = NoticeStore.instance.visible_notices_for(service)

    content = status_icon(service_status(service))
    content << '<span class="badge" style="margin-right: 1em;" title="There are notices (%s) below regarding this service.">%s</span>' % [visible_notices.count, visible_notices.count] if visible_notices.count > 0
    content
  end

  def panel_class(notice)
    if notice['type'] == 'outage'
      'panel-danger'
    elsif notice['type'] == 'information'
      'panel-info'
    elsif notice['type'] == 'maintenance'
      'panel-warning'
    else
      'panel-default'
    end
  end

  def status_icon(status)
    case status.to_s
      when 'up'
        return '<img src="/icons/status_up.png" alt="The service is up and running." title="The service is up and running." class="pull-right" />'
      when 'down'
        return '<img src="/icons/status_down.png" alt="There are indications the service is down." title="There are indications the service is down." class="pull-right" />'
      when 'warning'
        return '<img src="/icons/status_warning.png" alt="There are issues with the service." title="There are issues with the service." class="pull-right" />'
      when 'maintenance'
        return '<img src="/icons/maintenance.png" alt="The service is undergoing scheduled maintenance." title="The service is undergoing scheduled maintenance." class="pull-right" />'
      else
        return '<img src="/icons/na.png" alt="No data available." title="No data available." class="pull-right" />'
    end
  end

  def status_text(status)
    case status.to_s
      when 'up'
        return 'The service is up and running.'
      when 'down'
        return 'There are indications the service is down.'
      when 'warning'
        return 'There are issues with the service.'
      when 'maintenance'
        return 'The service is undergoing scheduled maintenance.'
      else
        return 'No data available.'
    end
  end

  def item_icon(type)
    case type.to_s
      when 'maintenance'
        return '<img src="/icons/maintenance.png" alt="Scheduled maintenance" title="Scheduled maintenance" style="vertical-align: text-top;" />'
      when 'outage'
        return '<img src="/icons/outage.png" alt="Unplanned outage" title="Unplanned outage" style="vertical-align: text-top;" />'
      when 'information'
        return '<img src="/icons/information.png" alt="General information" title="General information" style="vertical-align: text-top;" />'
    end
  end

  def date_format(date)
    if date.nil?
      'n/a'
    else
      date.rfc2822
    end
  end

  def humanize(secs)
    [[60, :seconds], [60, :minutes], [24, :hours], [1000, :days]].map{ |count, name|
      if secs > 0
        secs, n = secs.divmod(count)
        "#{n.to_i} #{name}" unless name == :seconds
      end
    }.compact.reverse.join(' ')
  end
end