# 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
	compare = Compare.objects.get(pk=c_id)
	ss1=compare.job_name.split("_")[0]
	ss2=compare.job_name.split("_")[1]
	userID=compare.user_id
	jobType=compare.job_type
	yearRange=compare.year_range
	logger.debug("Comparing search sets "+ss1+" and "+ss2+" : "+jobType)
	#get compare object
	year2 = int(yearRange.split("-")[1].strip())+1
	#run fet
	fet_1,fet_2=run_fet(compare,ss1,ss2)
	Compare.objects.filter(id=c_id).update(job_status='Looking for overlaps',job_progress=90)
	if len(fet_1) > 0 and len(fet_2) >0:
		#do overlap
		if jobType == "meshMain" or jobType == "notMeshMain":
			overlapper(compare,fet_1,fet_2)
		elif jobType == 'semmed_c':
			overlapper(compare,fet_1,fet_2)
		elif jobType == 'semmed_t':
			semmed_triple_process(compare,fet_1,fet_2)
		Compare.objects.filter(id=compare.id).update(job_status='View results',job_progress=100)
	else:
		Compare.objects.filter(id=compare.id).update(job_status='No results',job_progress=100)

@task
def single_ss_Wrapper(userID,ss1,jobType,yearRange):
	print "Running single ss analysis",ss1
	c=Compare.objects.get(year_range=yearRange,user_id=userID,job_name=str(ss1),job_type=jobType)
	fet_1 = run_fet(c,ss1,'')
	if len(fet_1)>0:

# 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

	compare = Compare.objects.get(pk=c_id)
	ss1=compare.job_name.split("_")[0]
	ss2=compare.job_name.split("_")[1]
	userID=compare.user_id
	jobType=compare.job_type
	yearRange=compare.year_range
	logger.debug("Comparing search sets "+ss1+" and "+ss2+" : "+jobType)
	#get compare object
	year2 = int(yearRange.split("-")[1].strip())+1
	#run fet
	fet_1,fet_2=run_fet(compare,ss1,ss2)
	Compare.objects.filter(id=c_id).update(job_status='Looking for overlaps',job_progress=90)
	if len(fet_1) > 0 and len(fet_2) >0:
		#do overlap
		if jobType == "meshMain" or jobType == "notMeshMain":
			overlapper(compare,fet_1,fet_2)
		elif jobType == 'semmed_c':
			overlapper(compare,fet_1,fet_2)
		elif jobType == 'semmed_t':
			semmed_triple_process(compare,fet_1,fet_2)
		Compare.objects.filter(id=compare.id).update(job_status='View results',job_progress=100)
	else:
		Compare.objects.filter(id=compare.id).update(job_status='No results',job_progress=100)

@task
def single_ss_Wrapper(userID,ss1,jobType,yearRange):
	print "Running single ss analysis",ss1
	c=Compare.objects.get(year_range=yearRange,user_id=userID,job_name=str(ss1),job_type=jobType)
	fet_1 = run_fet(c,ss1,'')
	if len(fet_1)>0:

		Compare.objects.filter(id=c.id).update(job_status='View results',job_progress=100)