from __future__ import absolute_import

import itertools

from rest_framework.response import Response

from sentry import status_checks
from sentry.api.base import Endpoint
from sentry.api.permissions import SuperuserPermission
from sentry.utils.hashlib import md5_text


class SystemHealthEndpoint(Endpoint):
    permission_classes = (SuperuserPermission,)

    def get(self, request):
        results = status_checks.check_all()
        return Response({
            'problems': [
                {
                    'id': md5_text(problem.message).hexdigest(),
                    'message': problem.message,
                    'severity': problem.severity,
                    'url': problem.url,
                }
                for problem in sorted(itertools.chain.from_iterable(results.values()),
                                     reverse=True)
            ],
            'healthy': {type(check).__name__: not problems for check, problems in results.items()},
        })
