def show(self) -> None: items = self._core_toggles() sites.update_site_states_from_dead_sites() site_status_info: Dict[sites.SiteId, List] = {} try: sites.live().set_prepend_site(True) for row in sites.live().query("GET status\nColumns: %s" % " ".join([i[0] for i in items])): site_id, values = row[0], row[1:] site_status_info[site_id] = values finally: sites.live().set_prepend_site(False) for site_id, site_alias in user_sites.sorted_sites(): container: ContextManager[bool] = (foldable_container( treename="master_control", id_=site_id, isopen=True, title=site_alias, icon="foldable_sidebar", ) if not site_config.is_single_local_site() else nullcontext(False)) with container: try: self._show_master_control_site(site_id, site_status_info, items) except Exception as e: logger.exception( "error rendering master control for site %s", site_id) write_snapin_exception(e)
def ajax_snapin(): """Renders and returns the contents of the requested sidebar snapin(s) in JSON format""" html.set_output_format("json") # Update online state of the user (if enabled) if config.user.id is None: raise Exception("no user ID") userdb.update_user_access_time(config.user.id) user_config = UserSidebarConfig(config.user, config.sidebar) snapin_id = html.request.var("name") snapin_ids = [snapin_id ] if snapin_id else html.request.get_str_input_mandatory( "names", "").split(",") snapin_code: List[str] = [] for snapin_id in snapin_ids: try: snapin_instance = user_config.get_snapin(snapin_id).snapin_type() except KeyError: continue # Skip not existing snapins if not config.user.may(snapin_instance.permission_name()): continue # When restart snapins are about to be refreshed, only render # them, when the core has been restarted after their initial # rendering if not snapin_instance.refresh_regularly( ) and snapin_instance.refresh_on_restart(): since = html.request.get_float_input_mandatory('since', 0) newest = since for site in sites.states().values(): prog_start = site.get("program_start", 0) if prog_start > newest: newest = prog_start if newest <= since: # no restart snapin_code.append(u'') continue with html.plugged(): try: snapin_instance.show() except Exception as e: write_snapin_exception(e) e_message = _("Exception during snapin refresh (snapin \'%s\')" ) % snapin_instance.type_name() logger.error("%s %s: %s", html.request.requested_url, e_message, traceback.format_exc()) finally: snapin_code.append(html.drain()) html.write(json.dumps(snapin_code))
def ajax_snapin(): """Renders and returns the contents of the requested sidebar snapin(s) in JSON format""" response.set_content_type("application/json") user_config = UserSidebarConfig(user, config.sidebar) snapin_id = request.var("name") snapin_ids = ([snapin_id] if snapin_id else request.get_str_input_mandatory("names", "").split(",")) snapin_code: List[str] = [] for snapin_id in snapin_ids: try: snapin_instance = user_config.get_snapin(snapin_id).snapin_type() except KeyError: continue # Skip not existing snapins if not snapin_instance.may_see(): continue # When restart snapins are about to be refreshed, only render # them, when the core has been restarted after their initial # rendering if not snapin_instance.refresh_regularly( ) and snapin_instance.refresh_on_restart(): since = request.get_float_input_mandatory("since", 0) newest = since for site in sites.states().values(): prog_start = site.get("program_start", 0) if prog_start > newest: newest = prog_start if newest <= since: # no restart snapin_code.append("") continue with output_funnel.plugged(): try: snapin_instance.show() except Exception as e: write_snapin_exception(e) e_message = ( _("Exception during element refresh (element '%s')") % snapin_instance.type_name()) logger.error("%s %s: %s", request.requested_url, e_message, traceback.format_exc()) finally: snapin_code.append(output_funnel.drain()) response.set_data(json.dumps(snapin_code))
def render_snapin(self, snapin: UserSidebarSnapin) -> str: snapin_class = snapin.snapin_type name = snapin_class.type_name() snapin_instance = snapin_class() html.open_div(id_="snapin_container_%s" % name, class_="snapin") self._render_snapin_styles(snapin_instance) # When not permitted to open/close snapins, the snapins are always opened if snapin.visible == SnapinVisibility.OPEN or not config.user.may( "general.configure_sidebar"): style = None else: style = "display:none" toggle_url = "sidebar_openclose.py?name=%s&state=" % name # If the user may modify the sidebar then add code for dragging the snapin head_actions: Dict[str, str] = {} if config.user.may("general.configure_sidebar"): head_actions = { "onmouseover": "document.body.style.cursor='move';", "onmouseout ": "document.body.style.cursor='';", "onmousedown": "cmk.sidebar.snapin_start_drag(event)", "onmouseup": "cmk.sidebar.snapin_stop_drag(event)" } html.open_div(class_=["head", snapin.visible.value], **head_actions) if config.user.may("general.configure_sidebar"): # Icon for mini/maximizing html.div("", class_="minisnapin", title=_("Toggle this snapin"), onclick="cmk.sidebar.toggle_sidebar_snapin(this, '%s')" % toggle_url) # Button for closing (removing) a snapin html.open_div(class_="closesnapin") close_url = "sidebar_openclose.py?name=%s&state=off" % name html.icon_button( url=None, title=_("Remove this snapin"), icon="closesnapin", onclick="cmk.sidebar.remove_sidebar_snapin(this, '%s')" % close_url) html.close_div() # The heading. A click on the heading mini/maximizes the snapin toggle_actions: Dict[str, str] = {} if config.user.may("general.configure_sidebar"): toggle_actions = { "onclick": "cmk.sidebar.toggle_sidebar_snapin(this,'%s')" % toggle_url, "onmouseover": "this.style.cursor='pointer'", "onmouseout": "this.style.cursor='auto'" } html.b(snapin_class.title(), class_=["heading"], **toggle_actions) # End of header html.close_div() # Now comes the content html.open_div(class_="content", id_="snapin_%s" % name, style=style) refresh_url = '' try: # TODO: Refactor this confusing special case. Add deddicated method or something # to let the snapins make the sidebar know that there is a URL to fetch. url = snapin_instance.show() if url is not None: # Fetch the contents from an external URL. Don't render it on our own. refresh_url = url html.javascript( "cmk.ajax.get_url(\"%s\", cmk.utils.update_contents, \"snapin_%s\")" % (refresh_url, name)) except Exception as e: logger.exception("error rendering snapin %s", name) write_snapin_exception(e) html.close_div() html.close_div() return refresh_url