1
2
3
4
5
6
7
8
9
10
11
12
13
14 import re
15
16 from Products.Five.browser import BrowserView
17 from Products.ZenUtils.json import json
18
21 """
22 Gets event pill for worst severity.
23
24 @return: HTML that will render the event pill.
25 @rtype: str
26 """
27 zem = self.context.dmd.ZenEventManager
28 pill = getEventPillME(zem, self.context)
29 if type(pill)==type([]) and len(pill)==1: return pill[0]
30 return pill
31
32
34 """
35 Return an HTML link and event pill for each object passed as a JSON
36 object ready for inclusion in a YUI data table.
37
38 @param objects: The objects for which to create links and pills.
39 @type objects: list
40 @return: A JSON-formatted string representation of the columns and rows
41 of the table
42 @rtype: string
43 """
44 @json
49
51 raise NotImplementedError
52
53
57
58
62
63
67
68
70 """
71 Return an HTML link and event pill for each object passed as a JSON
72 object ready for inclusion in a YUI data table.
73
74 @param objects: The objects for which to create links and pills.
75 @type objects: list
76 @return: A JSON-formatted string representation of the columns and rows
77 of the table
78 @rtype: string
79 """
80 mydict = {'columns':[], 'data':[]}
81 mydict['columns'] = ['Object', 'Events']
82 getcolor = re.compile(r'class=\"evpill-(.*?)\"', re.S|re.I|re.M).search
83 colors = "red orange yellow blue grey green".split()
84 def pillcompare(a,b):
85 a, b = map(lambda x:getcolor(x[1]), (a, b))
86 def getindex(x):
87 try:
88 color = x.groups()[0]
89 smallcolor = x.groups()[0].replace('-acked','')
90 isacked = 'acked' in color
91 index = colors.index(x.groups()[0].replace('-acked',''))
92 if isacked: index += .5
93 return index
94 except: return 5
95 a, b = map(getindex, (a, b))
96 return cmp(a, b)
97 devdata = []
98 for obj in objects:
99 alink = obj.getPrettyLink()
100 pill = getEventPillME(zem, obj, showGreen=True, prodState=prodState)
101 if type(pill)==type([]): pill = pill[0]
102 devdata.append([alink, pill])
103 devdata.sort(pillcompare)
104 mydict['data'] = [{'Object':x[0],'Events':x[1]} for x in devdata]
105 return mydict
106
107
114
115
116 -def getEventPillME(zem, me, number=1, minSeverity=0, showGreen=True,
117 prodState=None):
118 """
119 Get HTML code displaying the maximum event severity and the number of
120 events of that severity on a particular L{ManagedEntity} in a pleasing
121 pill-shaped container. Optionally return pills for lesser severities as
122 well. Optionally return a green pill if there are no events (normally no
123 events in a severity will not yield a result).
124
125 @param zem: A reference to ZenEventManager
126 @type zem: L{Products.ZenEvents.EventManagerBase.EventManagerBase}
127 @param me: The object regarding which event data should be queried.
128 @type me: L{ManagedEntity}
129 @param number: The number of pills to return
130 @type number: int
131 @param showGreen: Whether to return an empty green pill if all is well
132 @type showGreen: bool
133 @return: HTML strings ready for template inclusion
134 @rtype: list
135 """
136 try:
137 evsum = zem.getEventSummaryME(me, minSeverity, prodState=prodState)
138 summary =[x[2] for x in evsum]
139 colors = "red orange yellow blue grey".split()
140 info = ["%s out of %s acknowledged" % (x[1],x[2])
141 for x in evsum]
142 results = zip(colors, [me.getPrimaryUrlPath()]*5, info, summary)
143 template = ('<div class="evpill-%s" onclick="location.href='
144 '\'%s/viewEvents\'" title="%s">%s</div>')
145 pills = []
146 for i, result in enumerate(results):
147 color, path, info, summary = result
148 if evsum[i][1]>=evsum[i][2]:
149 if color!='green': color += '-acked'
150 result = color, path, info, summary
151 if (result[3]):
152 pills.append(template % result)
153 if len(pills)==number: return pills
154 if (showGreen):
155 color = 'green'
156 summary = ' '
157 info = 'No events'
158 result = (color, path, info, summary)
159 pills.append(template % result)
160 return pills
161 except:
162 return None
163