# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from jobs import constants, functions
from django.contrib.auth.models import User
import datetime
from ckeditor.fields import RichTextField

# Create your models here.

#supporting models
class Region(models.Model):
    name = models.CharField(max_length=100)

    def __unicode__(self):
        return self.name

    class Meta:
        ordering='name',

class JobLevel(models.Model):
    description = models.CharField(max_length=100)

    def __unicode__(self):
        return self.description

class EmployementType(models.Model):
    name = models.CharField(max_length=50)
    color = models.CharField(max_length=20, default='#55a747')

    def __unicode__(self):
        return self.name

class Degree(models.Model):

    name = models.CharField(max_length=100)
    def __unicode__(self):
        return self.name

class SchoolLevel(models.Model):
    name = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name

#Main models
class Category(models.Model):
    name = models.CharField(max_length=100)
    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name_plural = 'Sectors'
        verbose_name = 'Sector'

class Entity(models.Model):
    user =  models.OneToOneField(User, on_delete=models.CASCADE)
    class Meta:
        abstract = True

class Company(Entity):
    name = models.CharField(max_length=100)
    profile_pic = models.FileField(upload_to=functions.getFileName, null=True, blank=True)
    brief_description = models.TextField(null=True, blank=True)
    region = models.ForeignKey('Region', null=True, blank=True, related_name='companies')
    city = models.CharField(max_length=100, null=True, blank=True, default="Addis Ababa")
    website = models.URLField(blank=True, null=True)
    facebook = models.URLField(blank=True, null=True)
    twitter = models.URLField(blank=True, null=True)
    linkedin = models.URLField(blank=True, null=True)
    phone = models.CharField(max_length=25, blank=True, null=True)
    joined_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name_plural = 'Employers'

    def __unicode__(self):
        return self.name

class AccessToken(models.Model):
    value = models.CharField(max_length=200, primary_key=True)
    paramFilter = models.CharField(max_length=100)
    paramQuery = models.CharField(max_length=300)
    company = models.ForeignKey('company', related_name='accestokens')

    @staticmethod
    def generateValue():
        import hashlib
        import random
        m = hashlib.md5()
        random_data = []
        for i in range(15):
            random_data.append( str(random.randint(1,1000)) )

        random_data = ''.join(random_data)
        m.update(random_data)
        return m.hexdigest()

class Contact(models.Model):
    full_name = models.CharField(max_length=200)
    email = models.EmailField()
    subject = models.CharField(max_length=200)
    message = models.TextField()
    status = models.CharField(max_length = 10, default='unread', choices=(constants.SEEN_UNSEEN_STATUS))
    messaged_on = models.DateTimeField(auto_now_add=True)

    @classmethod
    def todays(cls):
        today_min = datetime.datetime.combine(datetime.date.today(), datetime.time.min)
        today_max = datetime.datetime.combine(datetime.date.today(), datetime.time.max)
        return cls.objects.filter(messaged_on__range=(today_min, today_max))

    @classmethod
    def yesterdays(cls):
        yesterday = datetime.date.today() - datetime.timedelta(days=1)
        yesterday_min = datetime.datetime.combine(yesterday, datetime.time.min)
        yesterday_max = datetime.datetime.combine(yesterday, datetime.time.max)
        return cls.objects.filter(messaged_on__range=(yesterday_min, yesterday_max))

    @classmethod
    def last7days(cls):
        last7day = datetime.date.today() - datetime.timedelta(days=7)
        last7day_min = datetime.datetime.combine(last7day, datetime.time.min)
        today_max = datetime.datetime.combine(datetime.date.today(), datetime.time.max)
        return cls.objects.filter(messaged_on__range=(last7day_min, today_max))

    class Meta:
        ordering = ('-status', '-messaged_on' )

    def __unicode__(self):
        return self.subject

class Message(models.Model):
    subject = models.CharField(max_length=200)
    content = models.TextField()
    company = models.ForeignKey('Company', related_name='messages')
    sender = models.ForeignKey(User, related_name='messages')
    status = models.CharField(max_length = 10, default='unread', choices=(constants.SEEN_UNSEEN_STATUS))
    messaged_on = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ('-status', '-messaged_on' )
    @classmethod
    def todays(cls):
        today_min = datetime.datetime.combine(datetime.date.today(), datetime.time.min)
        today_max = datetime.datetime.combine(datetime.date.today(), datetime.time.max)
        return cls.objects.filter(messaged_on__range=(today_min, today_max))

    @classmethod
    def yesterdays(cls):
        yesterday = datetime.date.today() - datetime.timedelta(days=1)
        yesterday_min = datetime.datetime.combine(yesterday, datetime.time.min)
        yesterday_max = datetime.datetime.combine(yesterday, datetime.time.max)
        return cls.objects.filter(messaged_on__range=(yesterday_min, yesterday_max))

    @classmethod
    def last7days(cls):
        last7day = datetime.date.today() - datetime.timedelta(days=7)
        last7day_min = datetime.datetime.combine(last7day, datetime.time.min)
        today_max = datetime.datetime.combine(datetime.date.today(), datetime.time.max)
        return cls.objects.filter(messaged_on__range=(last7day_min, today_max))

class UserBackGround(models.Model):
    city = models.CharField(max_length=100)
    start_month = models.CharField( max_length=10, choices=constants.MONTHS)
    start_year = models.IntegerField()
    end_month = models.CharField(max_length=10, choices=constants.MONTHS)
    end_year = models.IntegerField()
    description = models.TextField()

    class Meta:
        abstract = True

class Experience(UserBackGround):
    job_title = models.CharField(max_length=100)
    company_name = models.CharField(max_length=100)
    employee = models.ForeignKey('Employee', related_name='experiences')
    region = models.ForeignKey('Region')

class Education(UserBackGround):
    school = models.CharField(max_length=100)
    degree = models.ForeignKey('Degree')
    field_of_study = models.CharField(max_length=100)
    employee = models.ForeignKey('Employee', related_name='educations')
    region = models.ForeignKey('Region')

class Skill(models.Model):
    name = models.CharField(max_length=100)
    employee = models.ForeignKey('Employee', related_name='skills')

class WebsiteInfo(models.Model):
    adress = models.URLField()
    employee = models.ForeignKey('Employee', related_name='websites')

class JobAlert(models.Model):
    full_name = models.CharField(max_length=100)
    email = models.EmailField(unique=True)

    def __unicode__(self):
        return self.full_name

class AgeRange(models.Model):
    startAge = models.IntegerField(verbose_name='Start Age')
    endAge = models.CharField(max_length=5, verbose_name='Enda Age')

    def __unicode__(self):
        if self.endAge.isnumeric():
            return "%d - %s" %(self.startAge, self.endAge)
        return "%d%s" %(self.startAge, self.endAge)

    class Meta:
        ordering = ('startAge',)

class JobSeekerService(models.Model):
    name = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name

class EmployerService(models.Model):
    name = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name

class JobType(models.Model):
    name = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name

class Employee(Entity):
    profile_pic = models.FileField(upload_to=functions.getFileName,  blank=True)
    gender = models.CharField(blank=True, max_length=8, null=True, choices=constants.GENDER_CHOICES)
    age = models.ForeignKey('AgeRange', blank=True, null=True, related_name='employees')
    highest_education_level = models.IntegerField( choices=constants.EDUCATION_LEVELS, null=True, blank=True)
    employement_status = models.IntegerField(choices=constants.EMPLOYEMENT_STATUS, null=True, blank=True)
    job_types = models.ManyToManyField('JobType', related_name='jobseekers', blank=True)
    services_intersted_in = models.ManyToManyField('JobSeekerService', related_name='jobseekers', blank=True)

    #location information
    city = models.CharField(max_length=100, null=True, blank=True)
    region = models.ForeignKey('Region', blank=True, null=True)

    about_me = models.TextField(blank=True)
    volunteer_experience = models.TextField(blank=True)
    phone = models.CharField(max_length=15, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    joined_at = models.DateTimeField(auto_now_add=True)
    facebook_url = models.URLField(blank=True, null=True)
    linkedin_url = models.URLField(blank=True, null=True)
    twitter_url = models.URLField(blank=True, null=True)

    applications = models.ManyToManyField('Job',
                                        blank=True,
                                        related_name='applicants',
                                        through='JobApplication',
                                        through_fields=('applicant', 'job'))

    class Meta:
        verbose_name_plural = 'Job Seekers'

    def __unicode__(self):
        return self.user.first_name

    def apply(self, job):
        #check for necessary informatino before applying

        if self.cvs.count() > 0:
            pass
        elif not self.experiences.count() > 0 or not self.educations.count() > 0 or not self.skills.count() > 0:
            return False

        if not self.applications.filter(pk = job.pk):
            application = JobApplication.objects.create(applicant=self, job=job)
            return application

        return False

    @property
    def first_name(self):
        return self.user.first_name

    @property
    def last_name(self):
        return self.user.last_name

    def jobMatches(self):
        pass
class EmployeeJobInterest(models.Model):
    job_level = models.ForeignKey('JobLevel', blank=True, null=True)
    job_category = models.ForeignKey( 'Category', blank=True, null=True)
    job_region = models.ForeignKey('Region', blank=True, null=True)
    employement_type = models.ForeignKey( 'EmployementType', blank=True, null=True)
    salary_start = models.IntegerField(blank=True, null=True)
    salary_end = models.IntegerField(blank=True, null=True)
    pay_period = models.CharField(max_length = 10, blank=True, null=True, choices=constants.PAY_PERIOD)
    employee = models.OneToOneField(Employee, related_name='preference')

class PublishedJobs(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(is_draft=False)

class Job(models.Model):

    title = models.CharField(max_length=100)
    summary = RichTextField(blank=True)
    requirements =RichTextField(blank=True)
    knowledge_skills =RichTextField(blank=True)
    education_experience =RichTextField(blank=True)
    how_to_apply =RichTextField(blank=True)
    responsibilities =RichTextField(blank=True)
    department = models.CharField(blank=True, max_length=100)
    level = models.ForeignKey('JobLevel', null=True, blank=True)
    company = models.ForeignKey('Company', related_name='jobs')
    salary = models.CharField(max_length=200, blank=True, null=True) #0 means not determined
    number_of_candidates = models.IntegerField(blank=True, null=True) #0 means not determined
    deadline = models.DateField(blank=True, null=True)
    status = models.IntegerField(choices=constants.JOB_STATUS, default=constants.JOB_STATUS_OPEN)
    employement_type = models.ForeignKey('EmployementType', blank=True, null=True)
    report_to = models.CharField(blank=True, max_length=100)
    region = models.ForeignKey('Region', null=True, blank=True, related_name='jobs')
    city = models.CharField(max_length=100, default="Addis Ababa")
    created_at = models.DateTimeField(auto_now_add=True)
    categories = models.ManyToManyField('Category', blank=True, related_name='jobs')
    views = models.IntegerField(default=0, blank=True)
    apply_through_portal = models.BooleanField(default=True)
    application_link = models.URLField(blank=True, null=True)
    is_draft = models.BooleanField(default=False)

    class Meta:
        ordering = ('-created_at', )

    def getuniquelink(self):
        return '-'.join(self.title.replace('/', '').replace('.', '').replace('(', '').replace(')', '').replace('!', '').replace('>', '').replace('<', '').replace('?', '').replace(':', '').replace('$', '').replace('+', '').replace('-', '').replace('\\','').replace('=','').replace('*','').lower().split(" ")) + '-' + str(self.pk)
class JobApplication(models.Model):
    applicant = models.ForeignKey('Employee')
    job = models.ForeignKey('Job', related_name='applications')
    status = models.CharField(max_length = 10, default='unread', choices=(constants.SEEN_UNSEEN_STATUS))
    applied_on = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ('-status', '-applied_on')

    @classmethod
    def todays(cls, jobs):
        today_min = datetime.datetime.combine(datetime.date.today(), datetime.time.min)
        today_max = datetime.datetime.combine(datetime.date.today(), datetime.time.max)
        return cls.objects.filter(applied_on__range=(today_min, today_max), job__in=jobs)

    @classmethod
    def yesterdays(cls, jobs):
        yesterday = datetime.date.today() - datetime.timedelta(days=1)
        yesterday_min = datetime.datetime.combine(yesterday, datetime.time.min)
        yesterday_max = datetime.datetime.combine(yesterday, datetime.time.max)
        return cls.objects.filter(applied_on__range=(yesterday_min, yesterday_max), job__in=jobs)

    @classmethod
    def last7days(cls, jobs):
        last7day = datetime.date.today() - datetime.timedelta(days=7)
        last7day_min = datetime.datetime.combine(last7day, datetime.time.min)
        today_max = datetime.datetime.combine(datetime.date.today(), datetime.time.max)
        return cls.objects.filter(applied_on__range=(last7day_min, today_max), job__in=jobs)

class CV(models.Model):
    name = models.CharField(max_length=200)
    document = models.FileField(upload_to=functions.getFileName)
    employee = models.ForeignKey('Employee', related_name='cvs')

class References(models.Model):
    first_name = models.CharField(max_length=200)
    last_name = models.CharField(max_length=200)
    organization = models.CharField(max_length=200)
    title = models.CharField(max_length=200)
    phone = models.CharField(max_length=200)
    email = models.EmailField()
    employee = models.ForeignKey('Employee', related_name='references')

class Association(models.Model):
    name = models.CharField(max_length=200)
    employee = models.ForeignKey('Employee', related_name='associations')

class WorkSample(models.Model):
    name = models.CharField(max_length=200)
    description = models.TextField()
    date = models.DateField()
    document = models.FileField(upload_to=functions.getFileName)
    employee = models.ForeignKey('Employee', related_name='worksamples')

class WorkLink(models.Model):
    name = models.CharField(max_length=200)
    url = models.URLField()
    description = models.TextField()
    employee = models.ForeignKey('Employee', related_name='worklinks')

class VisitCount(models.Model):

    uniqueHitCount = models.IntegerField()
    totalHitCount = models.IntegerField()
    visitDate = models.DateField(auto_now_add=True)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""

Main (v)AMM simulator

This simulates the price discovery position of an AMM.

Created on Wed Apr  6 14:16:58 2022

@author: juan
"""
import numpy as np
import matplotlib.pyplot as plt
from gbm import genPsi as generate_oracle
import amm as amm
import trading_logics
import inner_simmulator as mc
import distributions as dists

#defines some initial parameters
SECONDS_IN_A_YEAR=3600*24*356
#defines rate in seconds
RATE_COSTUMERS_IN_SECONDS=14

# rate in $$ for each transaction
RATE_AMOUNT=2000
#defines rate in years. this is the one we will need
RATE_COSTUMERS=RATE_COSTUMERS_IN_SECONDS/(SECONDS_IN_A_YEAR)
# number of native coins in the initial reserve
INITIAL_RESERVE=1000
# time in years for which we run the simulation
FINAL_TIME=0.5
# defines how often we look at the oracle.
HOURS_IN_A_YEAR=24*365
# by default, we check the oracle every hour
dt=FINAL_TIME/HOURS_IN_A_YEAR
# Here we define some hyper-parameters for the GBM model of for the price
R=0.2 #risk-free interest rate
SIGMA=0.6 #volatility
#price at time 0.
INITIAL_PRICE=3200
# nominal fee for the AMM
AMM_FEE=1/100 #

gamma=1-AMM_FEE
market_logic=trading_logics.only_arb_zone
#generates a list of prices that we will look at
oracle_price=generate_oracle(T=FINAL_TIME,
                             N=HOURS_IN_A_YEAR,
                             r=R,
                             sigma=SIGMA,
                             S0=INITIAL_PRICE)

time_in_hours=np.linspace(0,1,HOURS_IN_A_YEAR)
# Plots to check that everything makes sense
plt.plot(time_in_hours,oracle_price, label='simulated oracle price')
plt.plot(time_in_hours,oracle_price/gamma, label='UB simulated oracle price')
plt.plot(time_in_hours,oracle_price*gamma, label='LB simulated oracle price')

plt.legend()
plt.xlabel('Time (years)')
plt.ylabel('Price, USD')
plt.show()
#%%
#instantiates AMM
#the initial reserve
LAMBDA=np.random.random()
W_BASE=np.random.random()
W_QUOTE=1-W_BASE
initial_quote_reserve=INITIAL_PRICE**(1/LAMBDA)*INITIAL_RESERVE*W_QUOTE/W_BASE
# initializes the AMM object
AMM=amm.lambdaAMM(fee=AMM_FEE,
             reserves_base=INITIAL_RESERVE,
             reserves_quote=initial_quote_reserve,
             w_quote=W_QUOTE,w_base=W_BASE,l=LAMBDA) # trading function=None uses a CFMM, just like UNIV2

dx_dist=dists.exponential(rate=RATE_AMOUNT)
dt_dist=dists.exponential(rate=RATE_COSTUMERS)

params={
        'amm':AMM,
        'final_time':FINAL_TIME,
        'dt_dist':dt_dist,
        'dx_dist':dx_dist,
        'initial_price':INITIAL_PRICE,
        'oracle_price':oracle_price,
        'market_logic':market_logic,
        'amm_fee':AMM_FEE
        }

res=mc.inner_loop(params)

#%%
time=res['time']
plt.plot(time,res['price_amm'])
plt.plot(time,res['price_oracle'])
plt.show()
cols=res.columns

for i in cols:
    if i!='time':
        plt.plot(time,res[i])
        plt.title(i)
        plt.show()

# price_amm=res['price_amm']
# price_oracle=res['price_oracle']
# slippage=res['slippage']
# reserves_base=res['reserves_base']
# reserves_quote=res['reserves_quote']
# k=res['k']


import curses
from curses import wrapper

def start_screen(stdscr):
    stdscr.clear()
    stdscr.addstr("Welcome to typing test!\n")
    stdscr.addstr("\nPress any key to begin")
    stdscr.refresh()
    stdscr.getkey()

def wpm_test(stdscr):
    target_text = "This is the test for typing test! please feel free to review the app"
    current_text = []

    stdscr.clear()
    stdscr.addstr(target_text)

    stdscr.refresh()
    stdscr.getkey()

def main(stdscr):
    curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
    curses.init_pair(2, curses.COLOR_RED, curses.COLOR_RED)
    curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_RED)

    start_screen(stdscr)
    wpm_test(stdscr)
wrapper(main)


import requests
from bs4 import BeautifulSoup

def getText(a):
	Text = ""
	for each in a:
		Text = Text + " " + each.text.encode("utf-8").strip()
	return Text

a = BeautifulSoup(requests.get("https://www.google.co.in/search?q=MSFT&hl=en&gl=in&as_drrb=b&authuser=0&source=lnt&tbs=cdr%3A1%2Ccd_min%3A10%2F10%2F2011%2Ccd_max%3A10%2F10%2F2011&tbm=nws&&start=0").content, "lxml").select('.r a')
href = []
for each in a:
	href.append(each['href'][7:])

data = [['p', 'span', 'strong', 'b', 'pre', 'summary']]
for each in href:
	tags = []
	soup = BeautifulSoup(requests.get(each).content, "lxml")
	for tag in data[0]:
		tags.append(getText(soup.select(tag)))
	data.append(tags)
print(data)


from selenium import webdriver
from time import sleep

driver=webdriver.Chrome()
driver.get("https://www.dcard.tw/f")
#將滾動條拖到最底部
js="var action=document.documentElement.scrollTop=10000"
driver.execute_script(js)
sleep(3)
#將滾動條拖到最頂部
js="var action=document.documentElement.scrollTop=0"
driver.execute_script(js)
sleep(3)
driver.quit()
print("滾動成功")


from google.appengine.ext import db
from tools.tag_processor import tag_processor

class Comments():

    def getCommentsCount(self, entities):
        comments_count = 0
        for entity in entities:
            comments = db.GqlQuery("SELECT __key__ FROM DBComments WHERE obj = :entity",
                                     entity = entity)
            comments_count += comments.count()
        return comments_count

    def fixCommentsForTopics(self):
        topics = db.GqlQuery("SELECT * FROM DBForumTopic")
        for topic in topics:
            topic.last_comment = self.getLastCommentInTopicForFix(topic.key())
            topic.comments_count = self.getCommentsCount([topic.key()]) - 1
            topic.put()

    def fixCommentsForCategories(self):
        categories = db.GqlQuery("SELECT * FROM DBForumCategory")
        for cat in categories:
            topics = db.GqlQuery("SELECT * FROM DBForumTopic where category = :cat", cat = cat.key())
            formalized_topics = []
            comments_count = 0
            for topic in topics:
                comments_count = comments_count + topic.comments_count
                formalized_topics.append(topic.key())
            topics_count = len(formalized_topics)
            last_comment = self.getLastCommentForCategoryFix(formalized_topics)
            cat.last_comment = last_comment
            cat.topics_count = topics_count
            cat.comments_count = comments_count
            cat.put()

    def getLastCommentInTopicForFix(self, key):
        comments = db.GqlQuery("SELECT __key__ FROM DBComments WHERE obj = :obj ORDER BY date DESC", obj = key)
        comment = comments.fetch(1)[0]
        return comment

    def getLastCommentForCategoryFix(self, entities):
        comments = db.GqlQuery("SELECT * FROM DBComments ORDER BY date DESC")
        if(comments.count()>0):
            for comment in comments:
                try:
                    if comment.obj.key() in entities:
                        return comment
                except: pass
            return False
        else:
            return False
    def getLastComment(self, entities):
        comments = db.GqlQuery("SELECT * FROM DBComments ORDER BY date DESC")
        if(comments.count()>0):
            for comment in comments:
                try:
                    if comment.obj.key() in entities:
                        return { 'date' : comment.date,
                                 'user' : { 'login': comment.user.login.encode("utf8") },
                                 'objid': comment.obj.key().id() }
                except: pass
            return False
        else:
            return False

    def getComments(self, obj):
            formalized_comments = []
            comments = db.GqlQuery("SELECT * FROM DBComments WHERE obj = :obj ORDER BY date",
                                     obj = obj)
            for comment in comments:
                #try:
                if not comment.parent_comment and comment.content != "":
                    formalized_comments.append({
                                                 'login' : comment.user.login.encode("utf8"),
                                                 'content' : tag_processor().prepare(comment.content),
                                                 'date' : comment.date,
                                                 'userid' : comment.user.key().id(),
                                                 'key' : comment.key(),
                                              })
                    formalized_comments += self.getChildComments(comment)
                #except: pass
            return formalized_comments

    def getChildComments(self, comment, level = 50):
        formilized_child_comments = []
        child_comments = db.GqlQuery("SELECT * FROM DBComments WHERE parent_comment = :key ORDER BY date", key = comment.key())
        for child_comment in child_comments:
            formilized_child_comments.append({
                                    'login' : child_comment.user.login.encode("utf8"),
                                    'content' : tag_processor().prepare(child_comment.content),
                                    'date' : child_comment.date,
                                    'userid' : child_comment.user.key().id(),
                                    'key' : child_comment.key(),
                                    'level' : level,
                                 })

            formilized_child_comments += self.getChildComments(child_comment, level + 50)
            child_comment = None
        return formilized_child_comments


import cv2
import numpy as np

img = cv2.imread('task2.png', 0)

kernel = np.ones((5, 5), np.uint8)
img_erosion = cv2.erode(img, kernel, iterations=1)
img_dilation = cv2.dilate(img, kernel, iterations=1)
cv2.imshow('Input', img)
cv2.imshow('Dilatation', img_dilation)
cv2.imshow('Erosion',img_erosion)
cv2.imwrite('dimadilation.jpg', img_dilation)
cv2.waitKey(0)


import datetime
import glob
import pandas as pd
import pandas_datareader.data as web
import numpy as np
#from textblob import TextBlob
from tqdm import tqdm
from sklearn import preprocessing
from sklearn.decomposition import TruncatedSVD
from sklearn.feature_extraction.text import TfidfVectorizer
import matplotlib.pyplot as plt
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer

class MasterData(object):

    tmpFiles ='../tmp/'

    srcDir = '../data/scrape_data/*.csv'
    rawDataFile = '../data/data_evaluation_set.csv'

    dataSetFile="../data/index_sentiment_labels_complete.csv"

    ## Contstuctor
    def __init__(self):
        self.masterDF = pd.DataFrame()
        self.rawData=pd.DataFrame()
        self.startDate= datetime.datetime(2012,12,20)
        self.endDate = datetime.datetime(2017,12,1)
        self.indexRawData={}
        self.indexNames = ['^GSPC', '^DJI', 'MSCI']
        pass

    # prepData - reads in the source directory files and consolidates the data
    # Into a single .csv file with the data/data pulled together.
    # This will prevent the evaluation from having to pull all the data together every time it is run

    # Input : srcDir - directory containing all of the source file. FIles in format of index/cdx/data
    # Output : destFile - writes out the list as a .csv dataframe to sourcefile. (rawDataFile)
    def prepRawData(self,srcDir,destFile):

        # Step 1 Read in files from srcDir
        sourceFiles = glob.glob(srcDir)

        #sourceFiles = sourceFiles[:5

        masterDF = pd.DataFrame()
        list = []
        for f in sourceFiles:
            df = pd.read_csv(f,index_col=0)
            s = str(df.iloc[0]['cdx'])[:8]
            d = pd.datetime.strptime(s,'%Y%m%d')
            rows = [[d, data] for data in df['data']]

            list.extend(rows)

        # Createa a dataframe from the list
        df = pd.DataFrame(list,columns=['date','data'])
        df.to_csv(destFile)

        self.rawdata = df

        return self.rawdata

    # Remove all items in the group list from the data object that have a count greater than count
    # input [idx=date][hdr] 1-(n) rows
    # output [idx=date][hdr] 1-(n-outlier) rows
    def removeOutliers(self,data,count=100):

        data['counter']=1
        group = pd.pivot_table(data, index= ['data'], values= "counter", aggfunc= np.sum,fill_value=0)
        group = group.sort_values('counter',ascending=False)

        # Remember the group dataframe is indexed with the headings, so i== heading, row[0] = count
        for i,row in group.iterrows():
            if(row[0]>count):
                data = data[data['data']!=i].copy()
        return data

    # Add in the sentiment information for every row
    # Input 1 to (n) [date][hdr] --> Output 1 to (n) [date][data][neg][neu][pos][compound]
    # where sentiment is a number between 0 and 1
    def createSentimentData(self,df):

        sid = SentimentIntensityAnalyzer()

        df['neg'] = df.apply(lambda x: sid.polarity_scores(x['data'])['neg'], axis=1)
        df['neu'] = df.apply(lambda x: sid.polarity_scores(x['data'])['neu'], axis=1)
        df['pos'] = df.apply(lambda x: sid.polarity_scores(x['data'])['pos'], axis=1)
        df['compound'] = df.apply(lambda x: sid.polarity_scores(x['data'])['compound'], axis=1)

        return df

    # Take in the data that is multiple lines per day and consolidate it
    # Input 1 to (n) [date][data][neg][neu][pos][compound]
    # Output 1 to (days) [date][data][neg][neu][pos][compound]
    # Where the sentiment date for each column is the mean of the sentiment for any given day
    # Where the data line is the contatenation of ALL hdrs for the day into one string
    def consolidateData(self,df):

        # Pull out the unique days in the set
        indexes = df.index.unique()

        # for each day pull out all the rows and consolidate into a single row
        list =[]
        for i in indexes:
            dfr = df.loc[i]
            hdrs=[]
            for n,j in dfr.iterrows():
                hdrs.append(j.data)

            hdr = " ".join(hdrs)

            negMean = np.mean(dfr['neg'])
            neuMean = np.mean(dfr['neu'])
            posMean = np.mean(dfr['pos'])
            compMean = np.mean(dfr['compound'])

            row=[i,hdr,negMean,neuMean,posMean,compMean]
            list.append(row)

        # Add back into the list
        df = pd.DataFrame(list,columns=['date','data','neg','neu','pos','compound'])
        df.set_index('date',inplace=True,drop=True)

        return df

    # pull information for each index in the list of indexNames
    # input List indexNames
    # Output dict{indexName:dataframe, indexName2:dataframe2}
    # Where dataframe is a data set with the values
    # Date Open, Low, High, Close, Volume, Adj Close
    #
    def getIndexData(self,indexNames):

        indexData={}
        for indexName in indexNames:
            df = web.DataReader(indexName, "yahoo", self.startDate, self.endDate)
            indexData.update({indexName:df.copy()})

        return indexData

    # Take a dict of indexes and their data and flatten it into single rows
    # each index becomes a set of columns [date] [<indxname>_Open][<indxname>_Low] .. etc for open,low,high,close,volume,adj close
    def flattenIndexData(self,d,indexNames):

        # Rename the columns for the dataframes based on index name
        list = []
        for i in indexNames:
            d[i].rename(columns={"Open":"{}_Open".format(i),
                                   "Low":"{}_Low".format(i),
                                   "High":"{}_High".format(i),
                                   "Close":"{}_Close".format(i),
                                   "Volume":"{}_Volume".format(i),
                                   "Adj Close":"{}_Adj Close".format(i)},
                                   inplace=True)
            list.append(d[i])

        # merge all of the dataframes together based on index info
        merged = pd.concat(list,axis=1)

        return merged

    # merge together the index trading information and consolidated data
    # input
    # index [date][indx_Open][indx_Low][indx_High][indx_Close][indx_Volume][indx_Adj Close]...[all indexes]
    # consData [date][data][data][neg][neu][pos][compound]
    # Output index+consData
    def mergeIndexAndConsolidated(self,indexDF,consDF):
        merged = pd.concat([consDF,indexDF],axis=1,join="inner")
        return merged

    # addLabels takes the merged data set and adds apprpriate labels for each index
    # each index will get 2 new columns, <index>_y1, <index>_y2
    # where y1 is the current days market close being up or down (0 or 1)
    # and y2 is the next trading day market close being up or down (0 or 1)
    # those labels can be used by a final ML algorithm for supervised learning
    def addLabels(self,df,indexNames):

        # for each index pull out the column for it's adj close
        # for each day add a column for today's day vs. prev day close as 0 or 1
        # and next day vs today adj close as 0 or 1
        df = df.reset_index() # Reset the index so that it's not using the dates
        df = df.rename(columns={"index":"Date"})
        for i in indexNames:
            df[i+"_y1"]=0
            df[i+"_y2"]=0
            #adjCloseDF.reset_index()    # reindex so we can index in by number+1,-1 etc.
            for n in range(1,len(df)-1):
                if df.loc[n,i+"_Adj Close"] >= df.loc[n-1,i+"_Adj Close"]:
                    df.loc[n,i+"_y1"]=1
                else:
                    df.loc[n,i+"_y1"]=0

                if df.loc[n+1,i+"_Adj Close"] >= df.loc[n,i+"_Adj Close"]:
                    df.loc[n,i+"_y2"]=1
                else:
                    df.loc[n,i+"_y2"]=0

        df = df.set_index('Date')

        return df

    # Create Data creates the large data-set from scratch and pulls it in from
    # all of the primary sources
    def createData(self):

        # First try and load the raw data file
        try:
            self.rawData = pd.read_csv(self.rawDataFile,index_col=0,parse_dates=[1],dtype={"data":"str"})  # Parse the second column as dates
            self.rawData.set_index('date',inplace=True,drop=True)
        except :
            print("Could not find Raw Data File")

        if len(self.rawData)<1:
            self.prepRawData(self.srcDir,self.rawDataFile)

        ### Raw Data is now 1--(n) rows [Date][Data (hdr)] where Date is the date of the header and data is a single header
        # Make a local copy here for use without affecting Class scoped
        rawData=self.rawData[:].copy()  #when testing limit this to 5000

        # Remove all of the outliers, any entry that shows up over 200 times
        noOutliersDF = self.removeOutliers(rawData,200)

        # Add on the sentiment information for every header
        ### sentimentDF is 1--(n) rows [Date][Data(hdr)][neg][neu][pos][compound]
        sentimentDF = self.createSentimentData(noOutliersDF)
        sentimentDF.to_csv(self.tmpFiles+"tmp1_sentiment.csv")

        # Consolidate the data into 1 row per day, taking the average neg/neu/pow/consolidated per day
        # Also consolidate the data/header information so that it is ALL the headers in the string for that
        # given day.
        consolidatedDF = self.consolidateData(sentimentDF)
        consolidatedDF.to_csv(self.tmpFiles+"tmp2_consolidated.csv")

        # Pull the index information for the indexes.  This data is ffilled for the full
        # date range
        self.indexRawData = self.getIndexData(self.indexNames)

        # Flatten IndexData
        self.indexData = self.flattenIndexData(self.indexRawData,self.indexNames)
        self.indexData.to_csv(self.tmpFiles+"tmp3_indexdata.csv")

        # Resample the IndexData so that it covers all days
        self.indexData = self.indexData.resample("D").last().ffill()

        # Add the stock information to the data.  This will add the open-low-high-adj close for each index for each day
        # format is <index>_open, <index>_low, etc...
        mergedDF = self.mergeIndexAndConsolidated(consolidatedDF,self.indexData)
        mergedDF.to_csv(self.tmpFiles+"tmp4_mergeddata.csv")

        # Finally add in the current day and next day classification binaries.
        # 0 if the current or next ay close is lower than the previous days
        # 1 if the current or next day close is higher or equal than the previous days
        # note that there are entries for EACH index <index>_currenday_class,<index>_nextday_class
        finalDF = self.addLabels(mergedDF,self.indexNames)

        # Remove the first and last entries as they don't have labels
        masterDF = finalDF[0:-1]

        masterDF.to_csv(self.dataSetFile)

    # Return the NP's for each index and the y1 and y2 values
    # the NP is simply the % of TP/total for y1 and y2 it represents what would happen
    # if you always chose UP for the day or next day's market
    # Return a dataframe with index,y1_perc,y2_perc for each row
    def getNaivePredictors(self):

        list=[]
        for i in self.indexNames:
            tp_y1 = float(sum(self.masterDF[i+"_y1"]))
            tp_y2 = float(sum(self.masterDF[i+"_y2"]))
            total = float(len(self.masterDF))
            list.append([i,tp_y1/total,tp_y2/total])

        return pd.DataFrame(list,columns=['index','y1_perc','y2_perc'])

    # Read in the data from the file, set it up with proper colnames and use Date as index
    # Other classes only need to call loadData to get it and then use the
    # data accessor methods to pull out slices.
    def loadData(self):
        self.masterDF=pd.read_csv(self.dataSetFile,index_col=0,parse_dates=[0])
        return self.masterDF

    # getIndexInfo returns just the information for a specific index as a dataframe
    # input: indexName
    # Output: DataFrame with [Date][Open][Low][High][Close][Volume][Adj Close]
    #
    def getIndexInfo(self,indexName):

        # Create the column names
        cols = [indexName+"_Open",indexName+"_High",indexName+"_Low",indexName+"_Close",indexName+"_Volume",indexName+"_Adj Close"]
        df = self.masterDF[cols]
        return df

    # Return a DF with the y1 and y2 labels for a given index
    # Note the labels are renamed so that the index name is stripped out
    def getLabelsForIndex(self,indexName):
        cols = [indexName+"_y1",indexName+"_y2"]
        df = self.masterDF[cols]
        df = df.rename(columns={indexName+"_y1": 'y1', indexName+"_y2": 'y2'})
        return df

    # Return a DF for the semantic information for a specific index.  Index name is stripped out
    # Returns [neg][neu][pos][compount] for all rows
    def getSemanticInfo(self):
        cols = ["neg","neu","pos","compound"]
        df = self.masterDF[cols]
        return df

if __name__ == "__main__":

    md = MasterData()
    md.createData()
    md.loadData()
    iDF = md.getIndexInfo(md.indexNames[0])
    pass


import networkx as nx
import matplotlib.pyplot as plt
from networkx.classes import graph

from readInstance import getInstance
from classes import Graph,Node,Edge

#myInstance = getInstance('inst4linearperturbacaoA')
#print(myInstance.weights)

def drawGraph():
    rgb_color = ['gray','r','g','b','y','m','brown']
    H = nx.Graph()
    for node in graph.nodes:
        H.add_node(node.name)
    for edge in graph.edges:
        u = edge.nodes[0].name
        v = edge.nodes[1].name
        color = edge.color
        H.add_edge(u,v,color=rgb_color[color])
    edges = H.edges()
    colors = [H[u][v]['color'] for u,v in edges]
    nx.draw(H, pos=nx.spring_layout(H),edge_color=colors, with_labels=True)
    plt.show()

def chain(v0,w):
    # Escolho uma cor para beta
    for beta_color in w.free_color:
        for vizinho in v0.neighbour:
            pass

    print(v0.free_color,w.free_color)

n = 5
G = nx.complete_graph(n)
graph = Graph()

# n cores disponiveis para cada no
all_colors = [x for x in range(1,n+1)]

for i in range (n):
    u = Node(i,set(all_colors))
    graph.nodes.append(u)

for edge in G.edges():
    u = graph.nodes[edge[0]]
    v = graph.nodes[edge[1]]
    edge = Edge(u,v)
    graph.edges.append(edge)
    u.neighbour.append(v)
    v.neighbour.append(u)

for edge in graph.edges:
    v0,w = edge.nodes
    common_color = v0.free_color.intersection(w.free_color)
    if common_color:
        color = common_color.pop()
        edge.color = color
        v0.free_color.remove(color)
        w.free_color.remove(color)
        #print(u.name,v.name)
    else:
        chain(v0,w)

# print(all_colors)
# for e in graph.edges:
#     print(e.nodes[0].name,e.nodes[1].name,'color:', e.color)
# for node in graph.nodes:
#     print('node:',node.name, node.free_color)
drawGraph()


from django.db.models import Count, Q
from django.http import HttpResponse
from django.shortcuts import redirect
from django.views.generic import TemplateView

from orders.models import Basket
from .models import SlideShow
from products.models import Category, Product
from django.db.models import Q

# Create your views here.

class MainPage(TemplateView):
    template_name = 'main/index.html'
    extra_context = {'slides': SlideShow.objects.all(),
                     'categories': Category.objects.filter(~Q(parent__name=None)).annotate(id_count=Count('id')).order_by(
                         '-id')[:6],
                     'products': Product.objects.annotate(id_count=Count('id')).order_by('-id')[:6],
                     'cloth': Category.objects.filter(Q(parent__name='clothes')),
                     'accessories': Category.objects.filter(Q(parent__name='accessory'))
                     }

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        user = self.request.user
        basket_items = []
        if user.is_authenticated:
            try:
                basket = Basket.objects.get(user=user)
                basket_items = basket.basket_items.all()
            except Basket.DoesNotExist:
                basket_items = None
        else:
            redirect('login')
        context['basket_items'] = basket_items
        products = Product.objects.filter(Q(category__name__contains='clothes') |
                                          Q(category__parent__name__contains='clothes'))
        context['clothes_product'] = products
        accessory_product = Product.objects.filter(Q(category__name__contains='accessory') |
                                                   Q(category__parent__name__contains='accessory'))
        context['accessory_product'] = accessory_product
        return context


import torch
import numpy as np

def normalize(input_t):
    """
    Normalize every input channel to mean 0 and variance 1 for each input

    :param input_t: (torch.tensor or np.array) of shape (.., W, H)
    :return: input_normalized (torch.tensor)
    """
    input_as_array = np.asarray(input_t)

    means = np.nanmean(input_as_array, axis=(-2, -1), keepdims=True)
    stds = np.nanstd(input_as_array, axis=(-2, -1), keepdims=True)

    input_normalized = (input_as_array - means) / stds
    input_normalized = torch.from_numpy(input_normalized)

    return input_normalized

def target_to_crack_tip_position(target):
    """
    Extracts crack tip positions from segmentation ground truth targets

    :param target: (torch.tensor) of shape (W, H)
    :return: crack_tip_positions (torch.tensor) of size 2
    """
    pixel_position = torch.nonzero((target == 2), as_tuple=False)
    crack_tip_position = pixel_position / 1.
    return crack_tip_position


import asyncio
import logging
from decimal import Decimal
from statistics import mean
from typing import Dict, List, Set, Union

import numpy as np
import pandas as pd

from hummingbot.connector.exchange_base import ExchangeBase
from hummingbot.connector.parrot import get_campaign_summary
from hummingbot.core.clock import Clock
from hummingbot.core.data_type.common import OrderType, TradeType
from hummingbot.core.data_type.limit_order import LimitOrder
from hummingbot.core.rate_oracle.rate_oracle import RateOracle
from hummingbot.core.utils.estimate_fee import build_trade_fee
from hummingbot.logger import HummingbotLogger
from hummingbot.strategy.market_trading_pair_tuple import MarketTradingPairTuple
from hummingbot.strategy.pure_market_making.inventory_skew_calculator import (
    calculate_bid_ask_ratios_from_base_asset_ratio,
)
from hummingbot.strategy.strategy_py_base import StrategyPyBase
from hummingbot.strategy.utils import order_age

from ...client.config.client_config_map import ClientConfigMap
from ...client.config.config_helpers import ClientConfigAdapter
from .data_types import PriceSize, Proposal

NaN = float("nan")
s_decimal_zero = Decimal(0)
s_decimal_nan = Decimal("NaN")
lms_logger = None

class LiquidityMiningStrategy(StrategyPyBase):

    @classmethod
    def logger(cls) -> HummingbotLogger:
        global lms_logger
        if lms_logger is None:
            lms_logger = logging.getLogger(__name__)
        return lms_logger

    def init_params(self,
                    client_config_map: Union[ClientConfigAdapter, ClientConfigMap],
                    exchange: ExchangeBase,
                    market_infos: Dict[str, MarketTradingPairTuple],
                    token: str,
                    order_amount: Decimal,
                    spread: Decimal,
                    inventory_skew_enabled: bool,
                    target_base_pct: Decimal,
                    order_refresh_time: float,
                    order_refresh_tolerance_pct: Decimal,
                    inventory_range_multiplier: Decimal = Decimal("1"),
                    volatility_interval: int = 60 * 5,
                    avg_volatility_period: int = 10,
                    volatility_to_spread_multiplier: Decimal = Decimal("1"),
                    max_spread: Decimal = Decimal("-1"),
                    max_order_age: float = 60. * 60.,
                    status_report_interval: float = 900,
                    hb_app_notification: bool = False):
        self._client_config_map = client_config_map
        self._exchange = exchange
        self._market_infos = market_infos
        self._empty_ob_market_infos = {}
        self._token = token
        self._order_amount = order_amount
        self._spread = spread
        self._order_refresh_time = order_refresh_time
        self._order_refresh_tolerance_pct = order_refresh_tolerance_pct
        self._inventory_skew_enabled = inventory_skew_enabled
        self._target_base_pct = target_base_pct
        self._inventory_range_multiplier = inventory_range_multiplier
        self._volatility_interval = volatility_interval
        self._avg_volatility_period = avg_volatility_period
        self._volatility_to_spread_multiplier = volatility_to_spread_multiplier
        self._max_spread = max_spread
        self._max_order_age = max_order_age
        self._ev_loop = asyncio.get_event_loop()
        self._last_timestamp = 0
        self._status_report_interval = status_report_interval
        self._ready_to_trade = False
        self._refresh_times = {market: 0 for market in market_infos}
        self._token_balances = {}
        self._sell_budgets = {}
        self._buy_budgets = {}
        self._mid_prices = {market: [] for market in market_infos}
        self._volatility = {market: s_decimal_nan for market in self._market_infos}
        self._last_vol_reported = 0.
        self._hb_app_notification = hb_app_notification

        self.add_markets([exchange])

    @property
    def active_orders(self):
        """
        List active orders (they have been sent to the market and have not been canceled yet)
        """
        limit_orders = self.order_tracker.active_limit_orders
        return [o[1] for o in limit_orders]

    @property
    def sell_budgets(self):
        return self._sell_budgets

    @property
    def buy_budgets(self):
        return self._buy_budgets

    def tick(self, timestamp: float):
        """
        Clock tick entry point, is run every second (on normal tick setting).
        :param timestamp: current tick timestamp
        """
        if not self._ready_to_trade:
            # Check if there are restored orders, they should be canceled before strategy starts.
            self._ready_to_trade = self._exchange.ready and len(self._exchange.limit_orders) == 0
            if not self._exchange.ready:
                self.logger().warning(f"{self._exchange.name} is not ready. Please wait...")
                return
            else:
                self.logger().info(f"{self._exchange.name} is ready. Trading started.")
                if self._validate_order_book_for_markets() >= 1:
                    self.create_budget_allocation()
                else:
                    self.logger().warning(f"{self._exchange.name} has no pairs with order book. Consider redefining your strategy.")
                    return

        self.update_mid_prices()
        self.update_volatility()
        proposals = self.create_base_proposals()
        self._token_balances = self.adjusted_available_balances()
        if self._inventory_skew_enabled:
            self.apply_inventory_skew(proposals)
        self.apply_budget_constraint(proposals)
        self.cancel_active_orders(proposals)
        self.execute_orders_proposal(proposals)

        self._last_timestamp = timestamp

    async def active_orders_df(self) -> pd.DataFrame:
        """
        Return the active orders in a DataFrame.
        """
        size_q_col = f"Amt({self._token})" if self.is_token_a_quote_token() else "Amt(Quote)"
        columns = ["Market", "Side", "Price", "Spread", "Amount", size_q_col, "Age"]
        data = []
        for order in self.active_orders:
            mid_price = self._market_infos[order.trading_pair].get_mid_price()
            spread = 0 if mid_price == 0 else abs(order.price - mid_price) / mid_price
            size_q = order.quantity * mid_price
            age = order_age(order, self.current_timestamp)
            # // indicates order is a paper order so 'n/a'. For real orders, calculate age.
            age_txt = "n/a" if age <= 0. else pd.Timestamp(age, unit='s').strftime('%H:%M:%S')
            data.append([
                order.trading_pair,
                "buy" if order.is_buy else "sell",
                float(order.price),
                f"{spread:.2%}",
                float(order.quantity),
                float(size_q),
                age_txt
            ])
        df = pd.DataFrame(data=data, columns=columns)
        df.sort_values(by=["Market", "Side"], inplace=True)
        return df

    def budget_status_df(self) -> pd.DataFrame:
        """
        Return the trader's budget in a DataFrame
        """
        data = []
        columns = ["Market", f"Budget({self._token})", "Base bal", "Quote bal", "Base/Quote"]
        for market, market_info in self._market_infos.items():
            mid_price = market_info.get_mid_price()
            base_bal = self._sell_budgets[market]
            quote_bal = self._buy_budgets[market]
            total_bal_in_quote = (base_bal * mid_price) + quote_bal
            total_bal_in_token = total_bal_in_quote
            if not self.is_token_a_quote_token():
                total_bal_in_token = base_bal + (quote_bal / mid_price)
            base_pct = (base_bal * mid_price) / total_bal_in_quote if total_bal_in_quote > 0 else s_decimal_zero
            quote_pct = quote_bal / total_bal_in_quote if total_bal_in_quote > 0 else s_decimal_zero
            data.append([
                market,
                float(total_bal_in_token),
                float(base_bal),
                float(quote_bal),
                f"{base_pct:.0%} / {quote_pct:.0%}"
            ])
        df = pd.DataFrame(data=data, columns=columns).replace(np.nan, '', regex=True)
        df.sort_values(by=["Market"], inplace=True)
        return df

    def market_status_df(self) -> pd.DataFrame:
        """
        Return the market status (prices, volatility) in a DataFrame
        """
        data = []
        columns = ["Market", "Mid price", "Best bid", "Best ask", "Volatility"]
        for market, market_info in self._market_infos.items():
            mid_price = market_info.get_mid_price()
            best_bid = self._exchange.get_price(market, False)
            best_ask = self._exchange.get_price(market, True)
            best_bid_pct = abs(best_bid - mid_price) / mid_price
            best_ask_pct = (best_ask - mid_price) / mid_price
            data.append([
                market,
                float(mid_price),
                f"{best_bid_pct:.2%}",
                f"{best_ask_pct:.2%}",
                "" if self._volatility[market].is_nan() else f"{self._volatility[market]:.2%}",
            ])
        df = pd.DataFrame(data=data, columns=columns).replace(np.nan, '', regex=True)
        df.sort_values(by=["Market"], inplace=True)
        return df

    async def miner_status_df(self) -> pd.DataFrame:
        """
        Return the miner status (payouts, rewards, liquidity, etc.) in a DataFrame
        """
        data = []
        g_sym = self._client_config_map.global_token.global_token_symbol
        columns = ["Market", "Payout", "Reward/wk", "Liquidity", "Yield/yr", "Max spread"]
        campaigns = await get_campaign_summary(self._exchange.display_name, list(self._market_infos.keys()))
        for market, campaign in campaigns.items():
            reward = await RateOracle.get_instance().get_value(
                amount=campaign.reward_per_wk, base_token=campaign.payout_asset
            )
            data.append([
                market,
                campaign.payout_asset,
                f"{g_sym}{reward:.0f}",
                f"{g_sym}{campaign.liquidity_usd:.0f}",
                f"{campaign.apy:.2%}",
                f"{campaign.spread_max:.2%}%"
            ])
        df = pd.DataFrame(data=data, columns=columns).replace(np.nan, '', regex=True)
        df.sort_values(by=["Market"], inplace=True)
        return df

    async def format_status(self) -> str:
        """
        Return the budget, market, miner and order statuses.
        """
        if not self._ready_to_trade:
            return "Market connectors are not ready."
        lines = []
        warning_lines = []
        warning_lines.extend(self.network_warning(list(self._market_infos.values())))

        budget_df = self.budget_status_df()
        lines.extend(["", "  Budget:"] + ["    " + line for line in budget_df.to_string(index=False).split("\n")])

        market_df = self.market_status_df()
        lines.extend(["", "  Markets:"] + ["    " + line for line in market_df.to_string(index=False).split("\n")])

        miner_df = await self.miner_status_df()
        if not miner_df.empty:
            lines.extend(["", "  Miner:"] + ["    " + line for line in miner_df.to_string(index=False).split("\n")])

        # See if there are any open orders.
        if len(self.active_orders) > 0:
            df = await self.active_orders_df()
            lines.extend(["", "  Orders:"] + ["    " + line for line in df.to_string(index=False).split("\n")])
        else:
            lines.extend(["", "  No active maker orders."])

        warning_lines.extend(self.balance_warning(list(self._market_infos.values())))
        if len(warning_lines) > 0:
            lines.extend(["", "*** WARNINGS ***"] + warning_lines)
        return "\n".join(lines)

    def start(self, clock: Clock, timestamp: float):
        restored_orders = self._exchange.limit_orders
        for order in restored_orders:
            self._exchange.cancel(order.trading_pair, order.client_order_id)

    def stop(self, clock: Clock):
        pass

    def _validate_order_book_for_markets(self):
        """
        Verify that the active markets have non-empty order books. Put the trading on hold
        for pairs with empty order books, in case it is a glitch?
        """
        markets = self._market_infos.copy()
        markets.update(self._empty_ob_market_infos)
        for market, market_info in markets.items():
            if markets[market].get_price(False).is_nan():
                self._empty_ob_market_infos[market] = market_info
                if market in self._market_infos:
                    self.logger().warning(f"{market} has an empty order book. Trading is paused")
                    self._market_infos.pop(market)
            else:
                self._market_infos[market] = market_info
                if market in self._empty_ob_market_infos:
                    self.logger().warning(f"{market} is being reactivated")
                    self._empty_ob_market_infos.pop(market)
        return len(self._market_infos)

    def create_base_proposals(self):
        """
        Each tick this strategy creates a set of proposals based on the market_info and the parameters from the
        constructor.
        """
        proposals = []
        for market, market_info in self._market_infos.items():
            spread = self._spread
            if not self._volatility[market].is_nan():
                # volatility applies only when it is higher than the spread setting.
                spread = max(spread, self._volatility[market] * self._volatility_to_spread_multiplier)
            if self._max_spread > s_decimal_zero:
                spread = min(spread, self._max_spread)
            mid_price = market_info.get_mid_price()
            buy_price = mid_price * (Decimal("1") - spread)
            buy_price = self._exchange.quantize_order_price(market, buy_price)
            buy_size = self.base_order_size(market, buy_price)
            sell_price = mid_price * (Decimal("1") + spread)
            sell_price = self._exchange.quantize_order_price(market, sell_price)
            sell_size = self.base_order_size(market, sell_price)
            proposals.append(Proposal(market, PriceSize(buy_price, buy_size), PriceSize(sell_price, sell_size)))
        return proposals

    def total_port_value_in_token(self) -> Decimal:
        """
        Total portfolio value in self._token amount
        """
        all_bals = self.adjusted_available_balances()
        port_value = all_bals.get(self._token, s_decimal_zero)
        for market, market_info in self._market_infos.items():
            base, quote = market.split("-")
            if self.is_token_a_quote_token():
                port_value += all_bals[base] * market_info.get_mid_price()
            else:
                port_value += all_bals[quote] / market_info.get_mid_price()
        return port_value

    def create_budget_allocation(self):
        """
        Create buy and sell budgets for every market
        """
        self._sell_budgets = {m: s_decimal_zero for m in self._market_infos}
        self._buy_budgets = {m: s_decimal_zero for m in self._market_infos}
        portfolio_value = self.total_port_value_in_token()
        market_portion = portfolio_value / len(self._market_infos)
        balances = self.adjusted_available_balances()
        for market, market_info in self._market_infos.items():
            base, quote = market.split("-")
            if self.is_token_a_quote_token():
                self._sell_budgets[market] = balances[base]
                buy_budget = market_portion - (balances[base] * market_info.get_mid_price())
                if buy_budget > s_decimal_zero:
                    self._buy_budgets[market] = buy_budget
            else:
                self._buy_budgets[market] = balances[quote]
                sell_budget = market_portion - (balances[quote] / market_info.get_mid_price())
                if sell_budget > s_decimal_zero:
                    self._sell_budgets[market] = sell_budget

    def base_order_size(self, trading_pair: str, price: Decimal = s_decimal_zero):
        base, quote = trading_pair.split("-")
        if self._token == base:
            return self._order_amount
        if price == s_decimal_zero:
            price = self._market_infos[trading_pair].get_mid_price()
        return self._order_amount / price

    def apply_budget_constraint(self, proposals: List[Proposal]):
        balances = self._token_balances.copy()
        for proposal in proposals:
            if balances[proposal.base()] < proposal.sell.size:
                proposal.sell.size = balances[proposal.base()]
            proposal.sell.size = self._exchange.quantize_order_amount(proposal.market, proposal.sell.size)
            balances[proposal.base()] -= proposal.sell.size

            quote_size = proposal.buy.size * proposal.buy.price
            quote_size = balances[proposal.quote()] if balances[proposal.quote()] < quote_size else quote_size
            buy_fee = build_trade_fee(self._exchange.name, True, proposal.base(), proposal.quote(),
                                      OrderType.LIMIT, TradeType.BUY, proposal.buy.size, proposal.buy.price)
            buy_size = quote_size / (proposal.buy.price * (Decimal("1") + buy_fee.percent))
            proposal.buy.size = self._exchange.quantize_order_amount(proposal.market, buy_size)
            balances[proposal.quote()] -= quote_size

    def is_within_tolerance(self, cur_orders: List[LimitOrder], proposal: Proposal):
        """
        False if there are no buys or sells or if the difference between the proposed price and current price is less
        than the tolerance. The tolerance value is strict max, cannot be equal.
        """
        cur_buy = [o for o in cur_orders if o.is_buy]
        cur_sell = [o for o in cur_orders if not o.is_buy]
        if (cur_buy and proposal.buy.size <= 0) or (cur_sell and proposal.sell.size <= 0):
            return False
        if cur_buy and \
                abs(proposal.buy.price - cur_buy[0].price) / cur_buy[0].price > self._order_refresh_tolerance_pct:
            return False
        if cur_sell and \
                abs(proposal.sell.price - cur_sell[0].price) / cur_sell[0].price > self._order_refresh_tolerance_pct:
            return False
        return True

    def cancel_active_orders(self, proposals: List[Proposal]):
        """
        Cancel any orders that have an order age greater than self._max_order_age or if orders are not within tolerance
        """
        for proposal in proposals:
            to_cancel = False
            cur_orders = [o for o in self.active_orders if o.trading_pair == proposal.market]
            if cur_orders and any(order_age(o, self.current_timestamp) > self._max_order_age for o in cur_orders):
                to_cancel = True
            elif self._refresh_times[proposal.market] <= self.current_timestamp and \
                    cur_orders and not self.is_within_tolerance(cur_orders, proposal):
                to_cancel = True
            if to_cancel:
                for order in cur_orders:
                    self.cancel_order(self._market_infos[proposal.market], order.client_order_id)
                    # To place new order on the next tick
                    self._refresh_times[order.trading_pair] = self.current_timestamp + 0.1

    def execute_orders_proposal(self, proposals: List[Proposal]):
        """
        Execute a list of proposals if the current timestamp is less than its refresh timestamp.
        Update the refresh timestamp.
        """
        for proposal in proposals:
            maker_order_type: OrderType = self._exchange.get_maker_order_type()
            cur_orders = [o for o in self.active_orders if o.trading_pair == proposal.market]
            if cur_orders or self._refresh_times[proposal.market] > self.current_timestamp:
                continue
            mid_price = self._market_infos[proposal.market].get_mid_price()
            spread = s_decimal_zero
            if proposal.buy.size > 0:
                spread = abs(proposal.buy.price - mid_price) / mid_price
                self.logger().info(f"({proposal.market}) Creating a bid order {proposal.buy} value: "
                                   f"{proposal.buy.size * proposal.buy.price:.2f} {proposal.quote()} spread: "
                                   f"{spread:.2%}")
                self.buy_with_specific_market(
                    self._market_infos[proposal.market],
                    proposal.buy.size,
                    order_type=maker_order_type,
                    price=proposal.buy.price
                )
            if proposal.sell.size > 0:
                spread = abs(proposal.sell.price - mid_price) / mid_price
                self.logger().info(f"({proposal.market}) Creating an ask order at {proposal.sell} value: "
                                   f"{proposal.sell.size * proposal.sell.price:.2f} {proposal.quote()} spread: "
                                   f"{spread:.2%}")
                self.sell_with_specific_market(
                    self._market_infos[proposal.market],
                    proposal.sell.size,
                    order_type=maker_order_type,
                    price=proposal.sell.price
                )
            if proposal.buy.size > 0 or proposal.sell.size > 0:
                if not self._volatility[proposal.market].is_nan() and spread > self._spread:
                    adjusted_vol = self._volatility[proposal.market] * self._volatility_to_spread_multiplier
                    if adjusted_vol > self._spread:
                        self.logger().info(f"({proposal.market}) Spread is widened to {spread:.2%} due to high "
                                           f"market volatility")

                self._refresh_times[proposal.market] = self.current_timestamp + self._order_refresh_time

    def is_token_a_quote_token(self):
        """
        Check if self._token is a quote token
        """
        quotes = self.all_quote_tokens()
        if len(quotes) == 1 and self._token in quotes:
            return True
        return False

    def all_base_tokens(self) -> Set[str]:
        """
        Get the base token (left-hand side) from all markets in this strategy
        """
        tokens = set()
        for market in self._market_infos:
            tokens.add(market.split("-")[0])
        return tokens

    def all_quote_tokens(self) -> Set[str]:
        """
        Get the quote token (right-hand side) from all markets in this strategy
        """
        tokens = set()
        for market in self._market_infos:
            tokens.add(market.split("-")[1])
        return tokens

    def all_tokens(self) -> Set[str]:
        """
        Return a list of all tokens involved in this strategy (base and quote)
        """
        tokens = set()
        for market in self._market_infos:
            tokens.update(market.split("-"))
        return tokens

    def adjusted_available_balances(self) -> Dict[str, Decimal]:
        """
        Calculates all available balances, account for amount attributed to orders and reserved balance.
        :return: a dictionary of token and its available balance
        """
        tokens = self.all_tokens()
        adjusted_bals = {t: s_decimal_zero for t in tokens}
        total_bals = {t: s_decimal_zero for t in tokens}
        total_bals.update(self._exchange.get_all_balances())
        for token in tokens:
            adjusted_bals[token] = self._exchange.get_available_balance(token)
        for order in self.active_orders:
            base, quote = order.trading_pair.split("-")
            if order.is_buy:
                adjusted_bals[quote] += order.quantity * order.price
            else:
                adjusted_bals[base] += order.quantity
        return adjusted_bals

    def apply_inventory_skew(self, proposals: List[Proposal]):
        """
        Apply an inventory split between the quote and base asset
        """
        for proposal in proposals:
            buy_budget = self._buy_budgets[proposal.market]
            sell_budget = self._sell_budgets[proposal.market]
            mid_price = self._market_infos[proposal.market].get_mid_price()
            total_order_size = proposal.sell.size + proposal.buy.size
            bid_ask_ratios = calculate_bid_ask_ratios_from_base_asset_ratio(
                float(sell_budget),
                float(buy_budget),
                float(mid_price),
                float(self._target_base_pct),
                float(total_order_size * self._inventory_range_multiplier)
            )
            proposal.buy.size *= Decimal(bid_ask_ratios.bid_ratio)
            proposal.sell.size *= Decimal(bid_ask_ratios.ask_ratio)

    def did_fill_order(self, event):
        """
        Check if order has been completed, log it, notify the hummingbot application, and update budgets.
        """
        order_id = event.order_id
        market_info = self.order_tracker.get_shadow_market_pair_from_order_id(order_id)
        if market_info is not None:
            if event.trade_type is TradeType.BUY:
                msg = f"({market_info.trading_pair}) Maker BUY order (price: {event.price}) of {event.amount} " \
                      f"{market_info.base_asset} is filled."
                self.log_with_clock(logging.INFO, msg)
                self.notify_hb_app_with_timestamp(msg)
                self._buy_budgets[market_info.trading_pair] -= (event.amount * event.price)
                self._sell_budgets[market_info.trading_pair] += event.amount
            else:
                msg = f"({market_info.trading_pair}) Maker SELL order (price: {event.price}) of {event.amount} " \
                      f"{market_info.base_asset} is filled."
                self.log_with_clock(logging.INFO, msg)
                self.notify_hb_app_with_timestamp(msg)
                self._sell_budgets[market_info.trading_pair] -= event.amount
                self._buy_budgets[market_info.trading_pair] += (event.amount * event.price)

    def update_mid_prices(self):
        """
        Query asset markets for mid price
        """
        for market in self._market_infos:
            mid_price = self._market_infos[market].get_mid_price()
            self._mid_prices[market].append(mid_price)
            # To avoid memory leak, we store only the last part of the list needed for volatility calculation
            max_len = self._volatility_interval * self._avg_volatility_period
            self._mid_prices[market] = self._mid_prices[market][-1 * max_len:]

    def update_volatility(self):
        """
        Update volatility data from the market
        """
        self._volatility = {market: s_decimal_nan for market in self._market_infos}
        for market, mid_prices in self._mid_prices.items():
            last_index = len(mid_prices) - 1
            atr = []
            first_index = last_index - (self._volatility_interval * self._avg_volatility_period)
            first_index = max(first_index, 0)
            for i in range(last_index, first_index, self._volatility_interval * -1):
                prices = mid_prices[i - self._volatility_interval + 1: i + 1]
                if not prices:
                    break
                atr.append((max(prices) - min(prices)) / min(prices))
            if atr:
                self._volatility[market] = mean(atr)
        if self._last_vol_reported < self.current_timestamp - self._volatility_interval:
            for market, vol in self._volatility.items():
                if not vol.is_nan():
                    self.logger().info(f"{market} volatility: {vol:.2%}")
            self._last_vol_reported = self.current_timestamp

    def notify_hb_app(self, msg: str):
        """
        Send a message to the hummingbot application
        """
        if self._hb_app_notification:
            super().notify_hb_app(msg)


from django.views import View
from django.http import HttpResponse,HttpResponseNotFound
from django.shortcuts import render
from django.urls import reverse
from django.http import JsonResponse

from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt,csrf_protect

from django.contrib import messages

from django.db.models import Count

from django.shortcuts import redirect

import json
from django.core import serializers

#Models
from mission.models import Mission
from device.models import Device
from route.models import Location

import json

def get_dashboard(request):

    '''
    Display dashboard
    '''

    response=render(request, "index.html")
    return response

def get_mission_form(request):

    '''
    Return form to create missions
    '''

    response=render(request, "mission.html")
    return response

def get_recent_missions(request):

    '''
    Return the recent missions creatd by user
    '''
    try:
        missions=Mission.objects.all().reverse()[:10]
        mission_obj=[]

        for mission in missions:

            start_loc=[0,0]
            end_loc=[0,0]

            location=mission.locations.values()
            if location.exists():
                print(mission.locations.values())
                start_loc[0]=mission.locations.values()[0]['lat']
                start_loc[1]=mission.locations.values()[0]['long']
                end_loc[0]=mission.locations.values()[1]['lat'],
                end_loc[1]=mission.locations.values()[1]['long']
                name=mission.mission_name
                mission_obj.append({'name':name,'start':start_loc,'end':end_loc})

        context = {'missions': mission_obj }
        #response=render(request, "default.html",context)
        response=render(request, "route.html",context)
        return response

    except Mission.DoesNotExist():
        print('Not enough missions created')
        return JsonResponse({"mission_error":"Not enough missions"},status=200)

def get(request):

    '''
    Return the recent missions creatd by user
    '''

    try:
        mission=Mission.objects.values().reverse()[:10]
        return JsonResponse(mission,safe=False)

    except :
        print('Not enough missions created')
        return JsonResponse({"mission_error":"Not enough missions"},status=200)

def create_mission(request):

    '''
    Create new mission
    '''
    #Getting mission data from form details
    data = json.loads(request.body)
    print(data)

    mission_name = data['mission_name']
    #device_id    = data['device_id']
    date         = data['date']
    time         = data['time']

    mission=Mission(mission_name=mission_name,date=date,time=time)

    mission.save()
    print(mission)

        #Creating the locations, and adding to device
    location_list      =json.loads(data['locations'])
    print(location_list)
    for location in location_list:
        location_lat   = location['lat']
        location_lon   = location['long']

        location_obj=Location.objects.create(lat=location_lat,long=location_lon)
        print(location_obj)
        mission.locations.add(location_obj)

    return JsonResponse({"Mission created":mission.id},status=200)

def update_mission(request):

    '''
    Update mission given mission_id
    '''
    data = json.loads(request.body)

    mission_name            =   data['mission_name']
    mission_id              =   data['mission_id']
    device                  =   data['device_id']
    date                    =   data['date']
    time                    =   data['time']

    try:
        #Check if mission exists
        mission = Mission.objects.get(mission_id=mission_id)

        mission.mission_name=mission_name
        mission.device      =device
        mission.date        =date
        mission.time        =time

        print(mission)

        mission.save()

    except Mission.DoesNotExist:
        print('Mission not found')
        return jsonResponse({"Mission not found":mission_id},status=200)

def delete(request):

    '''
    Delete mission given mission_id
    '''

    mission_id=json.loads(request.body)['id']

    mission = Mission.objects.get(mission_id=mission_id)
    mission.delete()
    return JsonResponse({mission_id:" The mission is deleted "},status=200)


from unittest import mock

import pytest
from ml_models.permissions import IsModelPublic, IsOwnerOrReadOnly
from projects.permissions import HasUserAPIKey
from rest_framework.permissions import IsAuthenticated

@pytest.fixture(autouse=True, scope="session")
def _shut_logger(session_mocker):
    with session_mocker.patch("ml_models.tasks.logger"):
        yield

@pytest.fixture(scope="function")
def _mock_views_permissions():

    patch_perm = lambda perm: mock.patch.multiple(
        perm,
        has_permission=mock.Mock(return_value=True),
        has_object_permission=mock.Mock(return_value=True),
    )
    with (
        patch_perm(IsAuthenticated),
        patch_perm(HasUserAPIKey),
        patch_perm(IsOwnerOrReadOnly),
        patch_perm(IsModelPublic),
    ):
        yield


import logging

from pcapi.core.categories import subcategories
from pcapi.model_creators.specific_creators import create_stock_from_event_occurrence
from pcapi.repository import repository
from pcapi.sandboxes.scripts.utils.select import remove_every

logger = logging.getLogger(__name__)

from .utils import get_occurrence_short_name
from .utils import get_price_by_short_name

EVENT_OCCURRENCES_WITH_STOCKS_REMOVE_MODULO = 4

def create_industrial_event_stocks(event_occurrences_by_name):
    logger.info("create_industrial_event_stocks")

    event_stocks_by_name = {}
    short_names_to_increase_price = []

    event_occurrence_items = list(event_occurrences_by_name.items())

    event_occurrence_items_with_stocks = remove_every(
        event_occurrence_items, EVENT_OCCURRENCES_WITH_STOCKS_REMOVE_MODULO
    )

    for event_occurrence_item_with_stocks in event_occurrence_items_with_stocks:
        (event_occurrence_with_stocks_name, event_occurrence_with_stocks) = event_occurrence_item_with_stocks
        available = 20

        short_name = get_occurrence_short_name(event_occurrence_with_stocks_name)
        price = get_price_by_short_name(short_name)
        price_counter = short_names_to_increase_price.count(short_name)
        if price_counter > 2:
            price = price + price_counter
        short_names_to_increase_price.append(short_name)

        if event_occurrence_with_stocks["offer"].product.subcategoryId in subcategories.ACTIVATION_SUBCATEGORIES:
            price = 0

        name = event_occurrence_with_stocks_name + " / " + str(available) + " / " + str(price)

        event_stocks_by_name[name] = create_stock_from_event_occurrence(
            event_occurrence_with_stocks, price=price, quantity=available
        )

    repository.save(*event_stocks_by_name.values())

    logger.info("created %d event_stocks", len(event_stocks_by_name))


import logging
import os
import tarfile

import filmykeeda.utils.modelDownload as connection
import gpt_2_simple as gpt2

logging.getLogger().setLevel(logging.INFO)

class GPT2:
    def __init__(self):
        """Initalize the GPT2 model
        """
        self.checkpointFiles = [
            "vocab.bpe",
            "model-1000.meta",
            "model-1000.index",
            "model-1000.data-00000-of-00001",
            "hparams.json",
            "events.out.tfevents.1565109747.6bad2fa838cd",
            "encoder.json",
            "counter",
            "checkpoint",
        ]
        self.checkModelFile()
        logging.info("Model file present!")
        logging.info("Loading model ...")
        self.sess = gpt2.start_tf_sess()
        gpt2.load_gpt2(
            self.sess, run_name=self.runFolder, checkpoint_dir=self.modelFolder
        )

    def checkModelFile(self):
        """Check if every necessary file is present in the models
        folder and if not then download and save.
        """
        logging.info("Checking GPT2 Model File ...")
        if not os.path.exists(os.path.join(os.getcwd(), "models")):
            os.mkdir(os.path.join(os.getcwd(), "models"))
        self.modelFolder = os.path.join(os.getcwd(), "models")
        downloadFlag = False
        for checkpointFile in self.checkpointFiles:
            if not os.path.exists(
                os.path.join(self.modelFolder, "GPT2", checkpointFile)
            ):
                downloadFlag = True
                break
        if downloadFlag:
            if os.path.exists(os.path.join(self.modelFolder, "GPT2")):
                os.remove(os.path.join(self.modelFolder, "GPT2"))
            self.downloadCompressedFile()
        self.runFolder = os.path.join(self.modelFolder, "GPT2")

    def downloadCompressedFile(self):
        """Download the compressed model from Google Drive
        """
        logging.warning("File Missing...")
        logging.info("Downloading File...")
        connection.downloadModelFile(
            id="1AVjqBj7xaB2_GrSJUdKqWD2UeTpo8BnS",
            destination=os.path.join(self.modelFolder, "GPT2.tar.gz"),
        )
        self.decompressFile()

    def decompressFile(self):
        """Decompress the model files into the folder.
        """
        logging.info("Extracting File...")
        with tarfile.open(
            os.path.join(self.modelFolder, "GPT2.tar.gz"), "r:gz"
        ) as reader:
            reader.extractall(self.modelFolder)

    def generate(self, text, n_words=1000):
        """Generate GPT2 text from a trained model

        Arguments:
            text {str} -- Prompt for the model

        Keyword Arguments:
            n_words {int} -- Length of the generated text (default: {1000})

        Returns:
            str -- Generated Text
        """
        return gpt2.generate(
            self.sess,
            run_name=self.runFolder,
            checkpoint_dir=self.modelFolder,
            prefix=text,
            length=n_words,
        )