# Path: website/get_db_engine.py
# def get_db_engine(db_name='write_to'):
#     """
#     Emits a SQL alchemy engine from the settings file
#     """
#     db = settings.CUSTOM_DATABASES[db_name]
#     engine_string = '%s://%s:%s@%s:%s/%s' % (
#         db['ENGINE'].split('.')[-1].lower(),
#         db['USER'],
#         urlquote(db['PASSWORD']),
#         db['HOST'],
#         db['PORT'],
#         db['NAME'])
#     return sqlalchemy.create_engine(engine_string,)


import models
import website.models
import website.query
import uuid
import os
import logging
from django.core.mail import send_mail
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from email.MIMEImage import MIMEImage
from django.utils import timezone
from dateutil.relativedelta import relativedelta
from website.get_db_engine import get_db_engine
from haystack.management.commands import update_index
        # to_mail = self.owner.email
        send_mail(
            'JOB FAILURE : %s' % (self.id),
            trace_string,
            self.owner.email,
            [self.owner.email],
            fail_silently=False
        )


def cache_buster(days_back=2):
    """
    When run will remove all tables that are for old cache
    """
    # Get all dead tables
    qc = website.models.QueryCache.objects.filter(
        run_time__lt=timezone.now() - relativedelta(days=days_back)
    ).order_by('run_time').all()[:100]
    for q in qc:
        if q.is_expired:
            # Drop them like they are hot!
            logging.warning('Dropping table %s' % (q.table_name))
            drop_table(q.table_name)
            q.delete()


def drop_table(table_name):
    """
    Drops given table from the scratch disk
    """

# Path: website/get_db_engine.py
# def get_db_engine(db_name='write_to'):
#     """
#     Emits a SQL alchemy engine from the settings file
#     """
#     db = settings.CUSTOM_DATABASES[db_name]
#     engine_string = '%s://%s:%s@%s:%s/%s' % (
#         db['ENGINE'].split('.')[-1].lower(),
#         db['USER'],
#         urlquote(db['PASSWORD']),
#         db['HOST'],
#         db['PORT'],
#         db['NAME'])
#     return sqlalchemy.create_engine(engine_string,)


# Path: cron/cron.py
import models
import website.models
import website.query
import uuid
import os
import logging
from django.core.mail import send_mail
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from email.MIMEImage import MIMEImage
from django.utils import timezone
from dateutil.relativedelta import relativedelta
from website.get_db_engine import get_db_engine
from haystack.management.commands import update_index

        # to_mail = self.owner.email
        send_mail(
            'JOB FAILURE : %s' % (self.id),
            trace_string,
            self.owner.email,
            [self.owner.email],
            fail_silently=False
        )


def cache_buster(days_back=2):
    """
    When run will remove all tables that are for old cache
    """
    # Get all dead tables
    qc = website.models.QueryCache.objects.filter(
        run_time__lt=timezone.now() - relativedelta(days=days_back)
    ).order_by('run_time').all()[:100]
    for q in qc:
        if q.is_expired:
            # Drop them like they are hot!
            logging.warning('Dropping table %s' % (q.table_name))
            drop_table(q.table_name)
            q.delete()


def drop_table(table_name):
    """
    Drops given table from the scratch disk
    """

    engine = get_db_engine()