# -*- coding: utf-8 -*-
from flask_restful import Resource, reqparse

from ..models.feed import FeedItem
from ..managers.backends import current_backend


health_req = reqparse.RequestParser()
health_req.add_argument('show_sources', type=int, default=0, ignore=True)


class Health(Resource):
    """
    Return information about the Health of the service.

    ``Health`` returns information about the manager health and,
    optionally, the number of feeds stored from each source.

    The 'status' can be one of the following values:
    * green: Everything is ok.
    * yellow: Something is not ok, but Distribution can live with this
      problem. i.e. ElasticSearch cannot allocate a replica shard.
    * red: Something is not ok and must be fixed. This can be a problem
      with a mis-configured alias or a problematic primary shard in
      ElasticSearch. You should check the rest of the report for more
      information.

    To include the object counts for each source pass ?show_sources=1

    """
    @staticmethod
    def get():
        backend_health = current_backend.health()
        health = dict(manager=backend_health, status=backend_health['status'])

        if health_req.parse_args()['show_sources']:
            health['sources'] = FeedItem.manager.count_sources()

        return health
