summaryrefslogtreecommitdiff
blob: 55fffeac0de3b2384015e773ee8f217f4981735b (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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.key? 'force_state'

      return notice['force_state']
    end

    nil
  end

  def render_column(id)
    content = ''

    ServiceRegistry.instance.columns[id].each do |category|
      content << "<h3>%s</h3>\n" % category
      content << services_info(ServiceRegistry.instance.categories[category][:services])
    end

    content
  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]
      service_array = ServiceRegistry.instance.services[service]
      service_name = service_array.nil? ? 'UNKNOWN NAME' : service_array[:name]
      data_service_name = service_name.gsub('"', "'")
      content << "
        <a class=\"list-group-item has-tooltip notice-link status-#{_service_status}\" href=\"#notices\" title=\"#{_status_text}\"
           data-toggle=\"tooltip\" data-placement=\"top\" data-container=\"body\"
           data-service=\"#{service}\" data-service-name=\"#{data_service_name}\">

        #{service_name}
        #{_service_info}
        </a>"
    end

    content << '</div>'
  end

  def service_status(service)
    return 'na' unless ServiceRegistry.instance.services.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 '<i class="status-icon fa fa-fw fa-check-square" title="The service is up and running"></i>'
    when 'down'
      return '<i class="status-icon fa fa-fw fa-times-circle" title="There are indications the service is down."></i>'
    when 'warning'
      return '<i class="status-icon fa fa-fw fa-warning" title="There are issues with the service."></i>'
    when 'maintenance'
      return '<i class="status-icon fa fa-fw fa-wrench" title="The service is undergoing scheduled maintenance."></i>'
    else
      return '<i class="status-icon fa fa-fw fa-question" title="No data available."></i>'
    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 '<i class="fa fa-wrench"></i>'
    when 'outage'
      return '<i class="glyphicon glyphicon-fire"></i>'
    when 'information'
      return '<i class="fa fa-info-circle"></i>'
    end
  end

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

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