<commit_before>import json
import logging
import uuid

import django
from django.conf import settings
from django.contrib.auth import get_user_model, login, authenticate, REDIRECT_FIELD_NAME
from django.db import connection, DatabaseError
from django.http import HttpResponse, Http404
from django.shortcuts import redirect
from django.views.generic import View, TemplateView
from django.core.urlresolvers import reverse_lazy
from analyticsclient.client import Client
from analyticsclient.exceptions import ClientError

from analytics_dashboard.courses import permissions


logger = logging.getLogger(__name__)
User = get_user_model()


def status(_request):
    return HttpResponse()


def health(_request):
    OK = 'OK'
    UNAVAILABLE = 'UNAVAILABLE'

    overall_status = analytics_api_status = database_status = UNAVAILABLE

    try:
        cursor = connection.cursor()
        cursor.execute("SELECT 1")
        cursor.fetchone()
        cursor.close()
        database_status = OK
    except DatabaseError:  # pylint: disable=catching-non-exception
        database_status = UNAVAILABLE

    try:
        client = Client(base_url=settings.DATA_API_URL, auth_token=settings.DATA_API_AUTH_TOKEN)
        if client.status.healthy:
            analytics_api_status = OK
    except ClientError as e:<commit_msg>Log when the insights database is unavailable<commit_after>import json
import logging
import uuid

import django
from django.conf import settings
from django.contrib.auth import get_user_model, login, authenticate, REDIRECT_FIELD_NAME
from django.db import connection, DatabaseError
from django.http import HttpResponse, Http404
from django.shortcuts import redirect
from django.views.generic import View, TemplateView
from django.core.urlresolvers import reverse_lazy
from analyticsclient.client import Client
from analyticsclient.exceptions import ClientError

from analytics_dashboard.courses import permissions


logger = logging.getLogger(__name__)
User = get_user_model()


def status(_request):
    return HttpResponse()


def health(_request):
    OK = 'OK'
    UNAVAILABLE = 'UNAVAILABLE'

    overall_status = analytics_api_status = database_status = UNAVAILABLE

    try:
        cursor = connection.cursor()
        cursor.execute("SELECT 1")
        cursor.fetchone()
        cursor.close()
        database_status = OK
    except DatabaseError as e:  # pylint: disable=catching-non-exception
        logger.exception('Database is not reachable: %s', e)
        database_status = UNAVAILABLE

    try:
        client = Client(base_url=settings.DATA_API_URL, auth_token=settings.DATA_API_AUTH_TOKEN)
        if client.status.healthy:
            analytics_api_status = OK
    except ClientError as e:
{'fraction_non_alphanumeric': 0.06456346294937637, 'fraction_numerical': 0.00293470286133529, 'mean_word_length': 4.106741573033708, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5428713', 'n_tokens_mistral': 761, 'n_tokens_neox': 743, 'n_words': 211}