status.py 3.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Copyright (c) 2017, Daniele Venzano
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Main points of entry for the Zoe web interface."""

18
from zoe_api.web.request_handler import ZoeWebRequestHandler
Daniele Venzano's avatar
Daniele Venzano committed
19
from zoe_api.exceptions import ZoeException
20
from zoe_lib.config import get_conf
21 22


23
class StatusEndpointWeb(ZoeWebRequestHandler):
24 25
    """Handler class"""

26 27 28 29 30 31 32 33 34 35 36 37
    def _calculate_load(self, sched):
        core_total = sched['platform_stats']['cores_total']
        memory_total = sched['platform_stats']['memory_total']
        core_reserved = 0
        memory_reserved = 0
        for node in sched['platform_stats']['nodes']:
            core_reserved += node['cores_reserved']
            memory_reserved += node['memory_reserved']
        core_usage = core_reserved / core_total
        memory_usage = memory_reserved / memory_total
        return core_usage, memory_usage

38 39
    def get(self):
        """Status and statistics page."""
40
        if self.current_user is None or not self.current_user.role.can_see_status:
41
            return
42

43
        stats = self.api_endpoint.statistics_scheduler()
44 45
        if stats is None:
            raise ZoeException('Cannot retrieve statistics from the Zoe master')
46 47 48

        executions_in_queue = {}
        for exec_id in stats['queue']:
49
            executions_in_queue[exec_id] = self.api_endpoint.execution_by_id(None, exec_id)
50
        for exec_id in stats['running_queue']:
51
            executions_in_queue[exec_id] = self.api_endpoint.execution_by_id(None, exec_id)
52 53
        for exec_id in stats['termination_queue']:
            executions_in_queue[exec_id] = self.api_endpoint.execution_by_id(None, exec_id)
54

55 56 57
        services_per_node = {}
        for node in stats['platform_stats']['nodes']:
            services_per_node[node['name']] = self.api_endpoint.sql.services.select(backend_host=node['name'], backend_status='started')
58 59 60 61 62 63
            for service in services_per_node[node['name']]:
                if service.id not in node['service_stats']:
                    node['service_stats'][service.id] = {
                        'mem_limit': 0,
                        'core_limit': 0
                    }
64 65

        max_service_count = max([len(services_per_node[name]) for name in services_per_node])
66

67 68
        template_vars = {
            "stats": stats,
69
            "executions_in_queue": executions_in_queue,
70
            "services_per_node": services_per_node,
71
            "max_service_count": max_service_count,
72
            'eurecom': get_conf().eurecom,
Daniele Venzano's avatar
Daniele Venzano committed
73
            'platform_load': self._calculate_load(stats)
74 75
        }

Daniele Venzano's avatar
Daniele Venzano committed
76
        self.render('status.jinja2', **template_vars)