# Path: browser/models.py
# class SearchSet(models.Model):
# 	user_id = models.CharField(max_length=200)
# 	job_name = models.CharField(max_length=200)
# 	job_id = models.CharField(max_length=200)
# 	job_start = models.CharField(max_length=30)
# 	job_status = models.CharField(max_length=20)
# 	job_progress = models.IntegerField()
# 	ss_file = models.FileField(upload_to='/var/django/melodi/abstracts')
# 	ss_desc = models.CharField(max_length=5000)
# 	pTotal = models.IntegerField()
# 
# 	class Meta:
# 		unique_together = ('user_id', 'job_name',),
# 
# 	#def __str__(self):
# 	#	return "User: " + self.user_id
# 
# class SearchSetAnalysis(models.Model):
# 	ss_id = models.ForeignKey(SearchSet)
# 	job_type =  models.CharField(max_length=20)
# 	complete = models.BooleanField(default=False)
# 	year_range = models.CharField(max_length=12)
# 
# class Compare(models.Model):
# 	user_id = models.CharField(max_length=200)
# 	job_name = models.CharField(max_length=200)
# 	job_desc = models.CharField(max_length=400)
# 	job_start = models.CharField(max_length=30)
# 	job_status = models.CharField(max_length=20)
# 	job_progress = models.IntegerField()
# 	job_type = models.CharField(max_length=20)
# 	year_range = models.CharField(max_length=12)
# 	share = models.BooleanField(default=False)
# 	hash_id = models.UUIDField(db_index=True,default=uuid.uuid4, editable=False)
# 
# class Overlap(models.Model):
# 	mc_id = models.ForeignKey(Compare)
# 	name = models.CharField(max_length=500,blank=True,db_index=True)
# 	name1 = models.CharField(max_length=200,blank=True,db_index=True)
# 	name2 = models.CharField(max_length=50,blank=True,db_index=True)
# 	name3 = models.CharField(max_length=200,blank=True,db_index=True)
# 	name4 = models.CharField(max_length=50,blank=True,db_index=True)
# 	name5 = models.CharField(max_length=200,blank=True,db_index=True)
# 	mean_cp = models.FloatField()
# 	mean_odds = models.FloatField()
# 	uniq_a = models.IntegerField()
# 	uniq_b = models.IntegerField()
# 	shared = models.IntegerField()
# 	score = models.FloatField()
# 	treeLevel = models.FloatField()
# 
# 	class Meta:
# 		unique_together = ('name', 'mc_id',)


from celery import shared_task, task
from celery.task import periodic_task
from celery.schedules import crontab
from django.core.mail import send_mail
from datetime import timedelta
from random import randint
from scipy import stats
from browser.models import SearchSet,SearchSetAnalysis,Compare,Overlap
from celery.utils.log import get_task_logger
from collections import defaultdict
from retrying import retry
from django.conf import settings
from neo4j.v1 import GraphDatabase,basic_auth
import datetime
import sys,gzip,time,os,subprocess,re,json,random
import statsmodels.sandbox.stats.multicomp as sm
import logging
import zipfile
import requests
import random
import shutil
import config

		print "Finished FET"
	else:
		filter_fet_dict = {}
	session.close()
	#logger.debug(filter_fet_dict)
	logger.debug('Number of FET after cpval = '+str(len(filter_fet_dict)))
	return(filter_fet_dict)

def run_fet(c,ss1,ss2):
	userID = c.user_id
	jobType = c.job_type
	yearRange = c.year_range
	year2 = c.year_range.split("-")[1].strip()
	Compare.objects.filter(id=c.id).update(job_status='Running FET 1',job_progress=10)
	start = time.time()

	#d=""
	print "Time taken:", round(time.time() - start, 3), "seconds"
	start = time.time()
	s1=SearchSet.objects.get(id=ss1)
	ss1_name = s1.job_name
	s1Check = SearchSetAnalysis.objects.filter(ss_id=s1,job_type=jobType,complete=True,year_range=yearRange)

	print "s1Check = ",len(s1Check)

	if len(s1Check)==0:
		#if d=="":
		#	d=get_mesh_freqs()
		ss1_fet=setup_fet(c,ss1,ss1_name,1)

# Path: browser/models.py
# class SearchSet(models.Model):
# 	user_id = models.CharField(max_length=200)
# 	job_name = models.CharField(max_length=200)
# 	job_id = models.CharField(max_length=200)
# 	job_start = models.CharField(max_length=30)
# 	job_status = models.CharField(max_length=20)
# 	job_progress = models.IntegerField()
# 	ss_file = models.FileField(upload_to='/var/django/melodi/abstracts')
# 	ss_desc = models.CharField(max_length=5000)
# 	pTotal = models.IntegerField()
# 
# 	class Meta:
# 		unique_together = ('user_id', 'job_name',),
# 
# 	#def __str__(self):
# 	#	return "User: " + self.user_id
# 
# class SearchSetAnalysis(models.Model):
# 	ss_id = models.ForeignKey(SearchSet)
# 	job_type =  models.CharField(max_length=20)
# 	complete = models.BooleanField(default=False)
# 	year_range = models.CharField(max_length=12)
# 
# class Compare(models.Model):
# 	user_id = models.CharField(max_length=200)
# 	job_name = models.CharField(max_length=200)
# 	job_desc = models.CharField(max_length=400)
# 	job_start = models.CharField(max_length=30)
# 	job_status = models.CharField(max_length=20)
# 	job_progress = models.IntegerField()
# 	job_type = models.CharField(max_length=20)
# 	year_range = models.CharField(max_length=12)
# 	share = models.BooleanField(default=False)
# 	hash_id = models.UUIDField(db_index=True,default=uuid.uuid4, editable=False)
# 
# class Overlap(models.Model):
# 	mc_id = models.ForeignKey(Compare)
# 	name = models.CharField(max_length=500,blank=True,db_index=True)
# 	name1 = models.CharField(max_length=200,blank=True,db_index=True)
# 	name2 = models.CharField(max_length=50,blank=True,db_index=True)
# 	name3 = models.CharField(max_length=200,blank=True,db_index=True)
# 	name4 = models.CharField(max_length=50,blank=True,db_index=True)
# 	name5 = models.CharField(max_length=200,blank=True,db_index=True)
# 	mean_cp = models.FloatField()
# 	mean_odds = models.FloatField()
# 	uniq_a = models.IntegerField()
# 	uniq_b = models.IntegerField()
# 	shared = models.IntegerField()
# 	score = models.FloatField()
# 	treeLevel = models.FloatField()
# 
# 	class Meta:
# 		unique_together = ('name', 'mc_id',)


# Path: browser/tasks.py
from celery import shared_task, task
from celery.task import periodic_task
from celery.schedules import crontab
from django.core.mail import send_mail
from datetime import timedelta
from random import randint
from scipy import stats
from browser.models import SearchSet,SearchSetAnalysis,Compare,Overlap
from celery.utils.log import get_task_logger
from collections import defaultdict
from retrying import retry
from django.conf import settings
from neo4j.v1 import GraphDatabase,basic_auth
import datetime
import sys,gzip,time,os,subprocess,re,json,random
import statsmodels.sandbox.stats.multicomp as sm
import logging
import zipfile
import requests
import random
import shutil
import config


		print "Finished FET"
	else:
		filter_fet_dict = {}
	session.close()
	#logger.debug(filter_fet_dict)
	logger.debug('Number of FET after cpval = '+str(len(filter_fet_dict)))
	return(filter_fet_dict)

def run_fet(c,ss1,ss2):
	userID = c.user_id
	jobType = c.job_type
	yearRange = c.year_range
	year2 = c.year_range.split("-")[1].strip()
	Compare.objects.filter(id=c.id).update(job_status='Running FET 1',job_progress=10)
	start = time.time()

	#d=""
	print "Time taken:", round(time.time() - start, 3), "seconds"
	start = time.time()
	s1=SearchSet.objects.get(id=ss1)
	ss1_name = s1.job_name
	s1Check = SearchSetAnalysis.objects.filter(ss_id=s1,job_type=jobType,complete=True,year_range=yearRange)

	print "s1Check = ",len(s1Check)

	if len(s1Check)==0:
		#if d=="":
		#	d=get_mesh_freqs()
		ss1_fet=setup_fet(c,ss1,ss1_name,1)

		s=SearchSetAnalysis(ss_id=s1,job_type=jobType,complete=True,year_range=yearRange)