# Path: ml/models.py
# class machine_learning_model(models.Model):
#     title = models.CharField(unique=True, max_length=100)
#     type = models.CharField(max_length=10,
#                             choices=(
#                                 ('logistic', 'logistic'), ('linear', 'linear'),
#                                 ('tree', 'tree')),
#                             default='None')
#     query = models.ForeignKey('website.Query')
#     target_column = models.CharField(max_length=100)
# 
#     class Meta:
#         unique_together = ['query', 'target_column', 'type']
# 
#     def __str__(self):
#         return '%s %s %s' % (self.title, self.type, self.target_column)
# 
#     # TODO make a validation method to validate columns existance


from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.contrib.admin.views.decorators import staff_member_required
from django.template import RequestContext
from django.contrib.auth.models import User
from django.db.models import Q
from itertools import chain
from ml.models import machine_learning_model
from date_time_encoder import DateTimeEncoder
import json
import time
import logging
import traceback
import favit.models
import query
import models
import sql_manager
def query_api(request, query_id):
    try:
        start_time = time.time()
        lq = query.LoadQuery(
            query_id=query_id,
            user=request.user,
            parameters=request.GET.dict(),
            cacheable=request.GET.get('cacheable', None)
        )
        q = lq.prepare_query()
        q.run_query()
        q.run_manipulations()
        response_data = q.data_array
        time_elapsed = time.time() - start_time
        # logging.warning('Cache Status View %s' % (q.get_cache_status()))
        return_data = {
            "data":
                {"columns": response_data.pop(0), "data": response_data},
            "time_elapsed": round(time_elapsed, 2),
            "cached": q.get_cache_status(),
            "error": False}
    except Exception, e:
        # logging.warning(str(sys.exc_info()) + str(e))
        logging.warning(traceback.format_exc())
        return_data = {
            "data": str(e),
            "time_elapsed": 0,
            "cached": False,
            "error": True}
    return HttpResponse(

# Path: ml/models.py
# class machine_learning_model(models.Model):
#     title = models.CharField(unique=True, max_length=100)
#     type = models.CharField(max_length=10,
#                             choices=(
#                                 ('logistic', 'logistic'), ('linear', 'linear'),
#                                 ('tree', 'tree')),
#                             default='None')
#     query = models.ForeignKey('website.Query')
#     target_column = models.CharField(max_length=100)
# 
#     class Meta:
#         unique_together = ['query', 'target_column', 'type']
# 
#     def __str__(self):
#         return '%s %s %s' % (self.title, self.type, self.target_column)
# 
#     # TODO make a validation method to validate columns existance


# Path: website/views.py
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.contrib.admin.views.decorators import staff_member_required
from django.template import RequestContext
from django.contrib.auth.models import User
from django.db.models import Q
from itertools import chain
from ml.models import machine_learning_model
from date_time_encoder import DateTimeEncoder
import json
import time
import logging
import traceback
import favit.models
import query
import models
import sql_manager

def query_api(request, query_id):
    try:
        start_time = time.time()
        lq = query.LoadQuery(
            query_id=query_id,
            user=request.user,
            parameters=request.GET.dict(),
            cacheable=request.GET.get('cacheable', None)
        )
        q = lq.prepare_query()
        q.run_query()
        q.run_manipulations()
        response_data = q.data_array
        time_elapsed = time.time() - start_time
        # logging.warning('Cache Status View %s' % (q.get_cache_status()))
        return_data = {
            "data":
                {"columns": response_data.pop(0), "data": response_data},
            "time_elapsed": round(time_elapsed, 2),
            "cached": q.get_cache_status(),
            "error": False}
    except Exception, e:
        # logging.warning(str(sys.exc_info()) + str(e))
        logging.warning(traceback.format_exc())
        return_data = {
            "data": str(e),
            "time_elapsed": 0,
            "cached": False,
            "error": True}
    return HttpResponse(

        json.dumps(return_data, cls=DateTimeEncoder),