Skip to content

Commit

Permalink
Helper functions to render notes and actions
Browse files Browse the repository at this point in the history
  • Loading branch information
mohierf committed Jan 3, 2019
1 parent 0a614a6 commit a6081ca
Showing 1 changed file with 141 additions and 0 deletions.
141 changes: 141 additions & 0 deletions module/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

from shinken.misc.sorter import hst_srv_sort
from shinken.misc.perfdata import PerfDatas
from shinken.macroresolver import MacroResolver


# pylint: disable=no-self-use
Expand Down Expand Up @@ -905,4 +906,144 @@ def get_event_icon(self, event, disabled=False, label='', use_title=True):
</span>
''' % (color, icon_text, label)

def render_url(self, obj, items, popover=False, css=''):
"""Returns formatted HTML for an element URL
If popover is true, a bootstrap popover is built, else a standard link ...
"""
result = []
for (icon, title, description, url) in items:
if not url and not description:
# Nothing to do here!
continue

# Replace MACROS in url, title and description
if hasattr(obj, 'get_data_for_checks'):
if url:
url = MacroResolver().resolve_simple_macros_in_string(url,
obj.get_data_for_checks())
if title:
title = MacroResolver().resolve_simple_macros_in_string(title,
obj.get_data_for_checks())
if description:
description = MacroResolver().resolve_simple_macros_in_string(description,
obj.get_data_for_checks())

if not description:
description = url

if not title:
popover = False

if icon:
icon = '<i class="fa fa-%s"></i>' % icon
else:
icon = ''

content = '&nbsp;%s' % description
if popover:
content = '&nbsp;%s' % title

if popover:
for code in [['&', '&amp;'], ['<', '&lt;'], ['>', '&gt;'], ['"', '&quot;']]:
description = description.replace(code[0], code[1])

popover = 'data-toggle="popover medium" data-html="true" data-content="%s" ' \
'data-trigger="hover focus" data-placement="bottom"' % description
else:
popover = ''

if not url:
url = 'href="#" '
else:
url = 'href="%s" target="_blank" ' % url

result.append('<a %s %s %s>%s%s</a>' % (url, css, popover, icon, content))

return result

def get_element_urls(self, obj, property, default_title=None, default_icon=None, popover=False, css=''):
""""Return list of element notes urls
The notes field is a string in which individual notes are separated with a | character.
Each note is composed of a left part and a right part: description::url where url is
never present in the Nagios legacy syntax :)
notes_url contain a list of | separated URLs that would be rendered
as navigable links. If a left part exist left::url, the left part is composed as an
individual note
Indeed notes, notes_url and actions_url are the same encoding: description::url where
description and url are optional! If url is present, it will be rendered as a navigable
link in the UI. If description is present, it may be title,,icon
As a sump-up, a notes declaration with all kind of notes:
notes simple note... only text but <em>may be formated</em>\
|Title::note with only title...\
|Title,,file::note with a title and an icon...\
|Title,,file::note with a title and an icon and an url...,,http://my-url.fr\
|KB1023,,tag::<strong>Lorem ipsum dolor sit amet</strong>, consectetur adipiscing elit. \
Proin et leo gravida, lobortis nunc nec, imperdiet odio. Vivamus quam velit, scelerisque \
nec egestas et, semper ut massa. Vestibulum id tincidunt lacus. <em>Ut in arcu at ex egestas \
vestibulum eu non sapien</em>. <span style="color:blue">Nulla facilisi</span>. \
Aliquam non blandit tellus, non luctus tortor. \
Mauris tortor libero, egestas quis rhoncus in, sollicitudin et tortor.\
notesèurl and actionsèurl may be declared with the same information!
"""
if not obj or not hasattr(obj, property):
return []

# We build a list of: title, icon, description, url
notes = []

# Several notes are defined in the notes attribute with | character
for item in getattr(obj, property).split('|'):
# A note is: [[title][,,icon]::]description[,,url]
try:
(decoration, description) = item.split('::')
except:
decoration = "%s,,%s" % (default_title, default_icon)
description = item

try:
(title, icon) = decoration.split(',,')
except:
icon = default_icon
title = decoration

if popover and not title:
title = description[5:]

try:
(description, url) = description.split(',,')
except:
# description = 'No description provided'
url = None

notes.append((icon, title, description, url))

return self.render_url(obj, notes, popover=popover, css=css)

def get_element_notes(self, obj, default_title=None, default_icon=None, popover=False, css=''):
""""See the comment of get_element_urls"""
return self.get_element_urls(obj, 'notes',
default_title=default_title, default_icon=default_icon,
popover=popover, css=css)

def get_element_notes_url(self, obj, default_title=None, default_icon=None, popover=False, css=''):
""""See the comment of get_element_urls"""
return self.get_element_urls(obj, 'notes_url',
default_title=default_title, default_icon=default_icon,
popover=popover, css=css)

def get_element_actions_url(self, obj, default_title=None, default_icon=None, popover=False, css=''):
""""See the comment of get_element_urls"""
return self.get_element_urls(obj, 'action_url',
default_title=default_title, default_icon=default_icon,
popover=popover, css=css)


helper = Helper()

0 comments on commit a6081ca

Please sign in to comment.