import logging

from c2corg_api.caching import cache_document_detail
from c2corg_api.models import DBSession, es_sync
from c2corg_api.search import elasticsearch_config
from c2corg_api.views import cors_policy
from cornice.resource import resource, view

log = logging.getLogger(__name__)


@resource(path='/health', cors_policy=cors_policy)
class HealthRest(object):

    def __init__(self, request):
        self.request = request

    @view()
    def get(self):
        """ Returns information about the version of the API and the status
        of its components:

            - Git revision
            - PostgreSQL status
            - Last run of the ES syncer script
            - ES status
            - Number of documents indexed in ES
            - Redis status
            - Number of keys in Redis

        """
        status = {
            'version': self.request.registry.settings.get('cache_version')
        }

        self._add_database_status(status)
        self._add_es_status(status)
        self._add_redis_status(status)

        return status

    def _add_database_status(self, status):
        last_es_syncer_run = None
        success = False

        try:
            last_es_syncer_run, _ = es_sync.get_status(DBSession)
            success = True
        except:
            log.exception('Getting last es syncer run failed')
            self.request.response.status_code = 500

        status['pg'] = 'ok' if success else 'error'
        status['last_es_syncer_run'] = last_es_syncer_run.isoformat() \
            if last_es_syncer_run else ''

    def _add_es_status(self, status):
        es_docs = None
        success = False

        try:
            client = elasticsearch_config['client']
            index = elasticsearch_config['index']
            stats = client.indices.stats(index, metric='docs')
            es_docs = stats['indices'][index]['total']['docs']['count']
            success = True
        except:
            log.exception('Getting indexed docs count failed')
            self.request.response.status_code = 500

        status['es'] = 'ok' if success else 'error'
        status['es_indexed_docs'] = es_docs

    def _add_redis_status(self, status):
        redis_keys = None
        success = False

        try:
            client = cache_document_detail.backend.client
            redis_keys = client.dbsize()
            success = True
        except:
            log.exception('Getting redis keys failed')

        status['redis'] = 'ok' if success else 'error'
        status['redis_keys'] = redis_keys

@@ -246,16 +246,43 @@
 ce, view
+%0Afrom os.path import isfile
 %0A%0Alog = 
@@ -831,16 +831,54 @@
 in Redis
+%0A            - Maintenance mode status
 %0A%0A      
@@ -1104,16 +1104,66 @@
 (status)
+%0A        self._add_maintenance_mode_status(status)
 %0A%0A      
@@ -2679,8 +2679,452 @@
 is_keys%0A
+%0A    def _add_maintenance_mode_status(self, status):%0A        maintenance_mode = False%0A        maintenance_file = 'maintenance_mode.txt'%0A%0A        if isfile(maintenance_file):%0A            maintenance_mode = True%0A            log.warn(%0A              'service is in maintenance mode, remove %25s to reenable.' %25%0A              maintenance_file)%0A            self.request.response.status_code = 404%0A%0A        status%5B'maintenance_mode'%5D = maintenance_mode%0A

add maintenance mode