Skip to content

Commit

Permalink
stateengine plugin: improve graphviz issue handling and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
onkelandy committed Aug 16, 2023
1 parent ff2b48a commit bce5bfc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
6 changes: 5 additions & 1 deletion stateengine/StateEngineWebif.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,12 @@ def drawgraph(self, filename):
self.__conditionset_count = len(self.__states[state].get('conditionsets'))
if self.__conditionset_count == 0:
self.__states[state]['conditionsets'][''] = ''
try:
list_index = list(self.__states.keys()).index(self.__active_state)
except Exception:
list_index = 0
color = "chartreuse3" if state == self.__active_state \
else "gray" if i > list(self.__states.keys()).index(self.__active_state) else "indianred2"
else "gray" if i > list_index else "indianred2"

new_y -= 1 * self.__scalefactor
position = '{},{}!'.format(0, new_y)
Expand Down
37 changes: 33 additions & 4 deletions stateengine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
from lib.item import Items
from .webif import WebInterface

try:
import pydotplus
VIS_ENABLED = True
except Exception:
VIS_ENABLED = False


logging.addLevelName(StateEngineDefaults.VERBOSE, 'DEVELOP')


Expand All @@ -52,6 +59,9 @@ def __init__(self, sh):
self.__sh = sh
self.alive = False
self.__cli = None
self.vis_enabled = self._test_visualization()
if not self.vis_enabled:
self.logger.warning(f'StateEngine is missing the PyDotPlus package, WebIf visualization is disabled')
self.init_webinterface(WebInterface)
self.__log_directory = self.get_parameter_value("log_directory")
try:
Expand Down Expand Up @@ -206,9 +216,9 @@ def get_graph(self, abitem, graphtype='link'):
<img src="static/img/visualisations/{0}.svg"\
style="max-width: 100%; height: auto; width: auto\9; ">\
</iframe></object>'.format(abitem)
except ImportError as ex:
self.logger.error("Problem getting graph for {}. ImportError: {}".format(abitem, ex))
return '<h4>Can not show visualization.</h4> ' \
except pydotplus.graphviz.InvocationException as ex:
self.logger.error("Problem getting graph for {}. Error: {}".format(abitem, ex))
return '<h4>Can not show visualization. Most likely GraphViz is not installed.</h4> ' \
'Current issue: ' + str(ex) + '<br/>'\
'Please make sure <a href="https://graphviz.org/download/" target="_new">' \
'graphviz</a> is installed.<br/>' \
Expand All @@ -217,4 +227,23 @@ def get_graph(self, abitem, graphtype='link'):
except Exception as ex:
self.logger.error("Problem getting graph for {}. Unspecified Error: {}".format(abitem, ex))
return '<h4>Can not show visualization.</h4> ' \
'Current unspecified issue: ' + str(ex) + '<br/>'
'Current issue: ' + str(ex) + '<br/>'


def _test_visualization(self):
if not VIS_ENABLED:
return False

img_path = self.path_join(self.get_plugin_dir(), 'webif/static/img/visualisations/se_test')
graph = pydotplus.Dot('StateEngine', graph_type='digraph', splines='false',
overlap='scale', compound='false', imagepath=img_path)
graph.set_node_defaults(color='lightgray', style='filled', shape='box',
fontname='Helvetica', fontsize='10')
graph.set_edge_defaults(color='darkgray', style='filled', shape='box',
fontname='Helvetica', fontsize='10')

try:
result = graph.write_svg(img_path, prog='fdp')
except pydotplus.graphviz.InvocationException:
return False
return True
1 change: 1 addition & 0 deletions stateengine/locale.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ plugin_translations:
'Items': {'de': '=', 'en': '='}
'SE Item': {'de': '=', 'en': '='}
'Detailvisualisierung': {'de': '=', 'en': 'Detailed Visualization'}
'KeineVisualisierung': {'de': 'Visualisierung nicht verfügbar', 'en': 'Visualization not available'}
11 changes: 9 additions & 2 deletions stateengine/webif/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@
<div class="container-fluid m-2 table-resize">
<div class="mb-2">
{{ _('Die folgenden Items verfügen über ein StateEngine Item') }}.
<strong>{{ _('Klick auf das Lupensymbol öffnet die Visualisierung!') }}</strong>
{% if p.vis_enabled %}
<strong>{{ _('Klick auf das Lupensymbol öffnet die Visualisierung!') }}</strong>
{% endif %}
</div>
<table id="maintable">
<thead>
Expand All @@ -128,7 +130,12 @@
<td class="py-1" id="{{ item }}_lastconditionset">
{% if item.lastconditionset_name == "" %}-{% else %}{{ item.lastconditionset_name }}{% endif %}</td>
<td>
<button type="button" class="btn btn-shng btn-sm" title="{{ _('Detailvisualisierung') }}" onclick="location.href='?action=get_graph&abitem={{ item }}';"><i class="fas fa-search"></i></button></td>
{% if p.vis_enabled %}
<button type="button" class="btn btn-shng btn-sm" title="{{ _('Detailvisualisierung') }}" onclick="location.href='?action=get_graph&abitem={{ item }}';"><i class="fas fa-search"></i></button>
{% else %}
<span title="{{ _('KeineVisualisierung') }}">X</span>
{% endif %}
</td>
<td class="py-1">{{ item.log_level }}</td>
<td class="py-1">{% for cond in item.webif_infos.keys() %}{% if not p.itemsApi.return_item(cond) == None %}{% if loop.index > 1 %},{% endif %}{{ p.itemsApi.return_item(cond)._name.split('.')[-1] }}{% endif %}{% endfor %}</td>

Expand Down

0 comments on commit bce5bfc

Please sign in to comment.