summaryrefslogtreecommitdiff
blob: a66457f1947b21bd2153a7b398a2a3d466060ac6 (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
include Nanoc::Helpers::Blogging
include Nanoc::Helpers::Rendering
require 'date'

# Defining state constants
module State
	UP=1
	DOWN=2
	WARNING=3
end

def service_icons(service)
	content = ""

	if service_has_notices?(service)
		content << notice_icon + " "
	end

	unless (forced_state = get_forced_state(service)) == nil
		content << status_icon(forced_state)
		return content
	end

	case ServiceRegistry.instance.services[service]
	when State::UP
		content << status_icon('up')
	when State::WARNING
		content << status_icon('warning')
	when State::DOWN
		content << status_icon('down')
	else
		content << status_icon('na')
	end

	content
end

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

def notice_icon
	return '<img src="/img/notice.png" alt="There are notices regarding this service posted below." title="There are notices regarding this service posted below." />'
end

def item_icon(type)
	case type.to_s
	when 'maintenance'
		return '<img src="' + base_url + 'img/maintenance.png" alt="Scheduled maintenance" title="Scheduled maintenance" />'
	when 'outage'
		return '<img src="' + base_url + '/img/outage.png" alt="Unplanned outage" title="Unplanned outage" />'
	when 'information'
		return '<img src="' + base_url + '/img/information.png" alt="General information" title="General information" />'
	end
end

# Compiles all active notices into one piece of HTML
def notices
	content=""

	sorted_articles.each do |notice|
		next if is_expired? notice

		content += notice.compiled_content(:snapshot => :single_post)
	end

	content
end

# Are there any notices for the service?
def service_has_notices?(service)
	articles.each do |notice|
		next if is_expired? notice

		if notice.attributes.has_key? :affects and notice[:affects].include? service
			return true
		end
	end
	
	false
end

def get_forced_state(service)
	return nil unless service_has_notices?(service)

	sorted_articles.each do |notice|
		next if is_expired? notice

		if notice.attributes.has_key? :affects and notice[:affects].include? service
			return notice[:force_state] if notice.attributes.has_key? :force_state
		end
	end
end

# Filters expired items and items w/o expiration date that are older than a week.
def is_expired?(notice)
	if notice.attributes.has_key? :active
		return true if notice[:active] != true
	end

	if notice.attributes.has_key? :expire
		expire_date = DateTime.parse(notice[:expire])

		DateTime.now > expire_date
	else
		creation_date = DateTime.parse(notice[:created_at])
		date_diff = DateTime.now - creation_date

		date_diff.to_i > 7
	end
end

def feed_proc
	lambda do |notice|
		notice.compiled_content(:snapshot => :single_post)
	end
end

def feed_articles
	articles.select {|n| not is_expired? n}
end

def base_url
	@site.config[:base_url] + '/'
end