# 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."""

from zoe_api.api_endpoint import APIEndpoint  # pylint: disable=unused-import
from zoe_api.web.utils import get_auth, catch_exceptions
from zoe_api.web.custom_request_handler import ZoeRequestHandler
from zoe_api.exceptions import ZoeException


class StatusEndpointWeb(ZoeRequestHandler):
    """Handler class"""
    def initialize(self, **kwargs):
        """Initializes the request handler."""
        super().initialize(**kwargs)
        self.api_endpoint = kwargs['api_endpoint']  # type: APIEndpoint

    @catch_exceptions
    def get(self):
        """Status and statistics page."""
        uid, role = get_auth(self)
        if uid is None or role != 'admin':
            return self.redirect(self.get_argument('next', u'/login'))

        stats = self.api_endpoint.statistics_scheduler(uid, role)
        if stats is None:
            raise ZoeException('Cannot retrieve statistics from the Zoe master')

        executions_in_queue = {}
        for exec_id in stats['queue']:
            executions_in_queue[exec_id] = self.api_endpoint.execution_by_id(uid, role, exec_id)
        for exec_id in stats['running_queue']:
            executions_in_queue[exec_id] = self.api_endpoint.execution_by_id(uid, role, exec_id)

        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')

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

        template_vars = {
            "uid": uid,
            "role": role,
            "stats": stats,
            "executions_in_queue": executions_in_queue,
            "services_per_node": services_per_node,
            "max_service_count": max_service_count
        }

        self.render('status.html', **template_vars)

@@ -2083,16 +2083,296 @@
 tarted')
+%0A            for service in services_per_node%5Bnode%5B'name'%5D%5D:%0A                if service.id not in node%5B'service_stats'%5D:%0A                    node%5B'service_stats'%5D%5Bservice.id%5D = %7B%0A                        'mem_limit': 0,%0A                        'core_limit': 0%0A                    %7D
 %0A%0A      

Fix status page crash when service is in transient state being terminated