Source code for hawat.blueprints.hosts

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# -------------------------------------------------------------------------------
# This file is part of Mentat system (https://mentat.cesnet.cz/).
#
# Copyright (C) since 2011 CESNET, z.s.p.o (http://www.ces.net/)
# Use of this source is governed by the MIT license, see LICENSE file.
# -------------------------------------------------------------------------------


"""
This file contains pluggable module for Hawat web interface containing features
related to real time dashboard calculations for `IDEA <https://idea.cesnet.cz/en/index>`__
events. This module is currently experimental, because the searching and statistical
calculations can be very performance demanding.
"""

__author__ = "Jan Mach <jan.mach@cesnet.cz>"
__credits__ = "Pavel Kácha <pavel.kacha@cesnet.cz>, Andrea Kropáčová <andrea.kropacova@cesnet.cz>"

import datetime
import pytz

import flask
from flask_babel import lazy_gettext

import mentat.stats.idea
import mentat.services.eventstorage

import hawat.events
import hawat.const
import hawat.acl
import hawat.menu
from hawat.base import HawatBlueprint
from hawat.view import BaseSearchView
from hawat.view.mixin import HTMLMixin, AJAXMixin
from hawat.base import PsycopgMixin
from hawat.blueprints.hosts.forms import SimpleHostSearchForm

BLUEPRINT_NAME = 'hosts'
"""Name of the blueprint as module global constant."""


[docs]class AbstractSearchView(PsycopgMixin, BaseSearchView): # pylint: disable=locally-disabled,abstract-method """ Base class for view responsible for searching... """ authentication = True authorization = [hawat.acl.PERMISSION_POWER]
[docs] @classmethod def get_menu_title(cls, **kwargs): return lazy_gettext('Hosts')
[docs] @classmethod def get_view_title(cls, **kwargs): return lazy_gettext('Search hosts')
[docs] @classmethod def get_view_icon(cls): return 'module-{}'.format(cls.module_name)
[docs] @staticmethod def get_search_form(request_args): return SimpleHostSearchForm( request_args, meta={'csrf': False} )
[docs] def do_before_response(self, **kwargs): self.response_context.update( quicksearch_list=self.get_quicksearch_by_time() )
[docs] @staticmethod def get_event_factory(): return mentat.services.eventstorage.record_to_idea_ghost
[docs] @staticmethod def get_event_columns(): columns = list(mentat.services.eventstorage.EVENTS_COLUMNS) columns.remove('event') return columns
[docs]class SearchView(HTMLMixin, AbstractSearchView): # pylint: disable=locally-disabled,too-many-ancestors """ View responsible for querying `IDEA <https://idea.cesnet.cz/en/index>`__ event database and presenting the results in the form of HTML page. """ methods = ['GET']
[docs] @classmethod def get_breadcrumbs_menu(cls): breadcrumbs_menu = hawat.menu.Menu() breadcrumbs_menu.add_entry( 'endpoint', 'home', endpoint=flask.current_app.config['ENDPOINT_HOME'] ) breadcrumbs_menu.add_entry( 'endpoint', 'search', endpoint='{}.search'.format(cls.module_name) ) return breadcrumbs_menu
[docs]class APISearchView(AJAXMixin, AbstractSearchView): # pylint: disable=locally-disabled,too-many-ancestors """ View responsible for querying `IDEA <https://idea.cesnet.cz/en/index>`__ event database and presenting the results in the form of JSON document. """ methods = ['GET', 'POST']
[docs] @classmethod def get_view_name(cls): return 'apisearch'
# -------------------------------------------------------------------------------
[docs]class HostsBlueprint(HawatBlueprint): """Pluggable module - Host overview (*hosts*)."""
[docs] @classmethod def get_module_title(cls): return lazy_gettext('Host overview')
[docs] def register_app(self, app): app.menu_main.add_entry( 'view', 'developer.{}'.format(BLUEPRINT_NAME), position=80, view=SearchView )
# -------------------------------------------------------------------------------
[docs]def get_blueprint(): """ Mandatory interface for :py:mod:`hawat.Hawat` and factory function. This function must return a valid instance of :py:class:`hawat.app.HawatBlueprint` or :py:class:`flask.Blueprint`. """ hbp = HostsBlueprint( BLUEPRINT_NAME, __name__, template_folder='templates' ) hbp.register_view_class(SearchView, '/{}/search'.format(BLUEPRINT_NAME)) hbp.register_view_class(APISearchView, '/api/{}/search'.format(BLUEPRINT_NAME)) return hbp