from flask import Flask, jsonify, request
import pyodbc
import pandas as pd
from scipy import spatial
import geopy.distance
import numpy as np
import math

from pytictoc import TicToc
from scipy import stats
import datetime
now = datetime.datetime.now()

app = Flask (__name__)

class originDestinationEquipment:
    def  __init__(self,origin,destination,equipment):
        self.origin=origin
        self.destination=destination
        self.equipment=equipment

class carrier_ode_loads_kpi_std:   # ode here is a pd.df with 4 features, o, d, corridor and equip.
    def  __init__(self,carrier,ode,loads,kpi,std):
        self.carrier=carrier
        self.ode=ode
        self.loads=loads
        self.kpi=kpi
        self.std=std

def Get_truckinsurance(carrierID):
    cn = pyodbc.connect('DRIVER={SQL Server};SERVER=ANALYTICSPROD;DATABASE=Bazooka;trusted_connection=true')
    query= """
       select
        case when cargolimit= 0 then 500000 else cargolimit end 'cargolimit'
		 from
        bazooka.dbo.Carrier Car
        where Car.ID=?
        """
    truck=pd.read_sql(query,cn,params= [carrierID])
    return truck.cargolimit.tolist()[0]

def Get_truck(carrierID):
    cn = pyodbc.connect('DRIVER={SQL Server};SERVER=ANALYTICSPROD;DATABASE=Bazooka;trusted_connection=true')
    query= """
    select
    carrierID,car.name,EmptyDate,EquipmentType,EquipmentLength,
    OriginLatitude 'originLat',
    OriginLongitude    'originLon',
    DestinationLatitude   'destLat',
    DestinationLongitude  'destLon',
    originDeadHead 'originDeadHead_radius',
    destinationDeadHead 'destinationDeadHead_radius',
    car.cargolimit
    from bazooka.dbo.Truck Tru
     inner join bazooka.dbo.Carrier Car on Car.ID=Tru.CarrierID and Name not like 'UPS%'
    where convert (date,EmptyDate) between convert(date,getdate ()) and convert(date,dateadd (day,1,GETDATE()))
     and OriginLongitude<0 and DestinationLongitude<0
    and carrierID=?
    """
    trucks=pd.read_sql(query,cn,params= [carrierID])
    return trucks

#Give CarrierID
def Get_Carrier_histLoad (CarrierID,date1,date2):
    cn = pyodbc.connect('DRIVER={SQL Server};SERVER=ANALYTICSPROD;DATABASE=Bazooka;trusted_connection=true')

    query= """
        set nocount on
        declare @CarrierID as int =?

	declare @CarrierDate1 as date = ?
	declare @CarrierDate2 as date = ?

	--declare @HistDate1 as date = '2017-06-01'
	--declare @HistDate2 as date = '2018-06-01'

	If(OBJECT_ID('tempdb..#Bounce_Reason') Is Not Null)
	Begin
	Drop Table #Bounce_Reason
	End
	Create Table #Bounce_Reason (FaultType int, ReasonType int, Reason varchar(30))
	Insert into #Bounce_Reason Values(0,0,	'Carrier')
	Insert into #Bounce_Reason Values(1,1,	'Carrier')
	Insert into #Bounce_Reason Values(1,2,	'Carrier')
	Insert into #Bounce_Reason Values(1,3,	'Carrier')
	Insert into #Bounce_Reason Values(1,4,	'Carrier')
	Insert into #Bounce_Reason Values(1,6,	'Carrier')
	Insert into #Bounce_Reason Values(1,12,	'Carrier')
	Insert into #Bounce_Reason Values(1,13,	'Carrier')
	Insert into #Bounce_Reason Values(1,7,	'Carrier_Reps')
	Insert into #Bounce_Reason Values(1,8,	'Cust_Reps')
	Insert into #Bounce_Reason Values(2,1,	'Carrier')
	Insert into #Bounce_Reason Values(2,2,	'Carrier')
	Insert into #Bounce_Reason Values(2,3,	'Carrier')
	Insert into #Bounce_Reason Values(2,4,	'Carrier')
	Insert into #Bounce_Reason Values(2,5,	'Customer')
	Insert into #Bounce_Reason Values(2,7,	'Carrier_Reps')
	Insert into #Bounce_Reason Values(2,8,	'Cust_Reps')
	Insert into #Bounce_Reason Values(2,9,	'Carrier')
	Insert into #Bounce_Reason Values(2,10,	'Facility')
	Insert into #Bounce_Reason Values(2,13,	'Carrier_Reps')
	Insert into #Bounce_Reason Values(3,10,	'Facility')
	Insert into #Bounce_Reason Values(3,11,	'Facility')
	Insert into #Bounce_Reason Values(3,12,	'Customer')
	Insert into #Bounce_Reason Values(3,13,	'Customer')

	If(OBJECT_ID('tempdb..#Service') Is Not Null)
	Begin
	Drop Table #Service
	End
	Create Table #Service ( LoadID int, CarrierID int, PUScore int, DelScore int)
	Insert into #Service

	select LoadID,
	Carrierid,
	case when datediff(minute,PU_Appt,PU_Arrive)<=60 then 25
	when datediff(minute,PU_Appt,PU_Arrive)<= 120 then 20
	when datediff(day,PU_Appt,PU_Arrive)=0 then 10
	else 5 end 'PU',
	case when datediff(minute,DO_Appt,DO_Arrive)<=60 then 25
	when datediff(minute,DO_Appt,DO_Arrive)<= 120 then 20
	when datediff(day,DO_Appt,DO_Arrive)=0 then 10
	else 5 end 'Del'
	from (
	select  L.id 'LoadID',
	LCAR.CarrierID,
	(case when LSP.[ScheduleCloseTime] = '1753-01-01' then
	convert(datetime, CONVERT(date, LSP.LoadByDate)) + convert(datetime, CONVERT(time, LSP.CloseTime))
	else LSP.[ScheduleCloseTime] end) 'PU_Appt',
	LSP.[ArriveDateTime] 'PU_Arrive'
	, case when LSD.[ScheduleCloseTime] = '1753-01-01' then
	convert(datetime, CONVERT(date, LSD.DeliverByDate)) + convert(datetime, CONVERT(time, LSD.CloseTime))
	else LSD.[ScheduleCloseTime] end 'DO_Appt',
	LSD.[ArriveDateTime] 'DO_Arrive'
	FROM Bazooka.dbo.[Load] L
	INNER JOIN Bazooka.dbo.LoadCarrier LCAR ON LCAR.LoadID = L.ID and LCAR.Main = 1 and LCAR.IsBounced = 0
	inner join Bazooka.dbo.loadstop LSP on  LSP.ID=L.OriginLoadStopID
	inner join Bazooka.dbo.loadstop LSD on  LSD.ID=L.DestinationLoadStopID
	WHERE L.Mode = 1 AND    L.LoadDate between @CarrierDate1 and @CarrierDate2 and L.Miles>0 and LCAR.CarrierID= @CarrierID
	) X

	If(OBJECT_ID('tempdb..#Bounce') Is Not Null)
	Begin
	Drop Table #Bounce
	End
	Create Table #Bounce ( LoadID int, CarrierID int,  Offer int, Accept int, Bounce int, OriginDH decimal(8,2), EmptyTime datetime)
	Insert into #Bounce

	select
	L.ID, LCAR.CarrierID, 1 'Offer',1 'Accepted Offers',
	sum (case when BR.Reason like 'Carrier' then 1 else 0 end) 'Bounce',
	min(case when LCAR.ActualDistance<1 then 1 else LCAR.ActualDistance end)   'OriginDH',
	case when convert (date, max(LCAR.ActualDateTime))='1753-01-01' then getdate() else  max(LCAR.ActualDateTime) end 'EmptyTime'
	FROM Bazooka.dbo.[Load] L
	INNER JOIN Bazooka.dbo.LoadCarrier LCAR ON LCAR.LoadID = L.ID
	left join Bazooka.dbo.LoadChangeLog Log_B on  Log_B.ChangeType=4 and Log_B.EntityID=LCAR.ID and LCAR.IsBounced=1
	left join #Bounce_Reason BR on BR.FaultType=Log_B.FaultType and BR.ReasonType=Log_B.ReasonType
	WHERE L.Mode = 1 AND    L.LoadDate between @CarrierDate1 and @CarrierDate2 and L.Miles>0 and LCAR.CarrierID=@CarrierID   and L.ProgressType>=7
	group by L.id, LCAR.CarrierID
	order by Bounce

	If(OBJECT_ID('tempdb..#Offer') Is Not Null)
	Begin
	Drop Table #Offer
	End
	Create Table #Offer( LoadID int, CarrierID int, Offer int, Cost money, Ask money,  BadOffer int,OriginDH int, AvailableTime datetime, Rnk int)
	Insert into #Offer
	select
	O.LoadID, CarrierID, 1 'Offer',L.totalrate, --LRD.Cost,
	 Ask,
	case when  Ask >L.totalrate*0.9 then 1 else 0 end  'Badoffer',
	case when O.MilesToOrigin<1 then 1 else O.MilesToOrigin end 'OriginDH',
	convert(datetime, CONVERT(date,O.CanLoadDate))+convert(datetime, CONVERT(time,O.CanLoadTime)) 'AvailableTime',
	RANK() over (partition by O.LoadID, O.CarrierID order by O.CreateDate desc) 'rnk'
	from bazooka.dbo.Offer O
	inner join Bazooka.dbo.[Load] L on O.LoadID = L.ID
	inner join Bazooka.dbo.LoadCustomer LCUS on LCUS.LoadID = L.ID and LCUS.Main = 1
	--inner join (select  loadid, SUM(amount) 'Cost' from Bazooka.dbo.LoadRateDetail
	--				where EntityType = 12 and EDIDataElementCode IN  ('405','FR',  'PM' ,'MN','SCL','OT','EXP') --and CreateDate > '2018-01-01'
	--				Group by loadid) LRD on LRD.loadid = L.ID
	--inner join #Cost C on C.LoadID=O.LoadID
	where O.Carrierid=@CarrierID   and O.LoadDate between @CarrierDate1 and @CarrierDate2 and
	Ask>0 and L.totalrate > 150 and  L.Mode = 1  and L.ProgressType>=7  and L.Miles>0

	---End of Load-Carrier KPI Score

	---Start of Carrier Features

	If(OBJECT_ID('tempdb..#Carrier_HistLoad') Is Not Null)
	Begin
	Drop Table #Carrier_HistLoad
	End
	Create Table #Carrier_HistLoad (LoadID int,   Origin varchar (50), Destination varchar(50),  OriginCluster varchar (50), DestinationCluster varchar (50), Corridor varchar (100))
	Insert into #Carrier_HistLoad

	select L.id 'LoadID',
        L.OriginCityName + ', ' + L.OriginStateCode  'Origin'
	,L.DestinationCityName + ', ' + L.DestinationStateCode  'Destination'
	--,L.TotalValue
	,RCO.ClusterNAME 'OriginCluster'
	,RCD.ClusterName 'DestinationCluster'
	,RCO.ClusterNAME+'-'+RCD.ClusterName  'Corridor'
	FROM #Service S
	inner join 	Bazooka.dbo.[Load] L on L.id=S.LoadID
	LEFT JOIN Analytics.CTM.RateClusters RCO ON RCO.Location = L.OriginCityName + ', ' + L.OriginStateCode
	LEFT JOIN Analytics.CTM.RateClusters RCD ON RCD.Location = L.DestinationCityName + ', ' + L.DestinationStateCode
	order by Origin,Destination

	If(OBJECT_ID('tempdb..#Carrier_Origin') Is Not Null)
	Begin
	Drop Table #Carrier_Origin
	End
	Create Table #Carrier_Origin (OriginCluster varchar (50), Count_Origin int)
	Insert into #Carrier_Origin
	select distinct OriginCluster,
	count(loadid) 'Count_Origin'
	from #Carrier_HistLoad
	group by OriginCluster
	order by 2 desc

	If(OBJECT_ID('tempdb..#Carrier_Dest') Is Not Null)
	Begin
	Drop Table #Carrier_Dest
	End
	Create Table #Carrier_Dest (DestinationCluster varchar (50), Count_Dest int)
	Insert into #Carrier_Dest
	select distinct DestinationCluster,
	count(loadid) 'Count_Dest'
	from #Carrier_HistLoad
	group by DestinationCluster
	order by 2 desc
	---End of Carrier Features

	select * from (
	select  COALESCE(B.LoadID,O.LoadID)   'loadID',
	COALESCE(B.CarrierID,O.CarrierID)    'carrierID', L.hot 'hot',
	O.cost 'customer_rate',
	case when  B.Accept=1 then l.totalcost else o.Ask  end 'carrier_cost',
	(O.cost-(case when  B.Accept=1 then l.totalcost else o.Ask  end ) )/O.cost*100 'margin_perc',
	L.miles, (case when  B.Accept=1 then l.totalcost else o.Ask end)/(L.miles+COALESCE(O.OriginDH,B.OriginDH) )  'rpm',
	--COALESCE(S.PUScore,0)          'puScore',
	--COALESCE(S.DelScore,0)            'delScore',
	--Coalesce(O.Offer, B.Offer)*40    'offer',
	--COALESCE(B.Accept,0)*10    'offerAccept' ,
	--COALESCE(B.Bounce,0)*(-20)     'bounce'  ,
	--COALESCE(O.BadOffer,0)*-20     'badOffer',
	COALESCE(S.PUScore,0) +       COALESCE(S.DelScore,0)  +
	Coalesce(O.Offer, B.Offer)*40  +
	COALESCE(B.Accept,0)*10   +
	COALESCE(B.Bounce,0)*(-20)     +
	COALESCE(O.BadOffer,0)*-20     'kpiScore',
	COALESCE(O.OriginDH,B.OriginDH )   'originDH',
	--case when COALESCE(O.OriginDH,B.OriginDH )<=10 then 10
	--when COALESCE(O.OriginDH,B.OriginDH )<=50 then 50
	--when COALESCE(O.OriginDH,B.OriginDH )<=100 then 100
	--else 200 end 'originDH-levels',
	--COALESCE(O.AvailableTime,B.EmptyTime) 'Available',
	--case when LSP.[ScheduleCloseTime] = '1753-01-01' then
	--convert(datetime, CONVERT(date, LSP.LoadByDate)) + convert(datetime, CONVERT(time, LSP.CloseTime))
	--else LSP.[ScheduleCloseTime] end  'PU_Appt',
	datediff(hour,COALESCE(O.AvailableTime,B.EmptyTime),case when LSP.[ScheduleCloseTime] = '1753-01-01' then
	convert(datetime, CONVERT(date, LSP.LoadByDate)) + convert(datetime, CONVERT(time, LSP.CloseTime))
	else LSP.[ScheduleCloseTime] end) 'pu_GAP',
	--datediff(minute,COALESCE(O.AvailableTime,S.EmptyTime),S.PU_Appt) 'PU_GAP',
	--CUS.name 'CustomerName'
	RCO.ClusterNAME 'originCluster'
	,RCD.ClusterName 'destinationCluster'
	,RCO.ClusterNAME+'-'+RCD.ClusterName 'corridor'
	, case when  l.equipmenttype like '%V%' then 'V' when  l.equipmenttype like 'R' then 'R' else 'other' end 'equipment'
	--,COALESCE(Cor.Count_Corridor,0)  'corridor_count'
	,COALESCE(Ori.Count_Origin,0)  'origin_count'
	,COALESCE(Dest.Count_Dest,0)  'dest_count'
	--,COALESCE(CC.Count_Cus,0)  'cus_Count'
	--,COALESCE(CC.Count_ALL,0)   'cus_All'
	--,case when COALESCE(CC.Count_ALL,0)<3000 then 'Small'
	--when COALESCE(CC.Count_ALL,0)<10000 then 'Small-Med'
	--when COALESCE(CC.Count_ALL,0)< 25000 then   'Med'
	--when COALESCE(CC.Count_ALL,0)<50000 then  'Med-Large'
	--else 'Large' end 'cus_Size'
	,C.DandBIndustryId  'industryID',
	D.Code 'industry'
	,	CityO.Latitude 'originLat',CityO.Longitude 'originLon',
	CityD.Latitude 'destinationLat',CityD.Longitude 'destinationLon'
	--,case when CC.Count_ALL>0 then CC.Count_Cus*1.0/CC.Count_ALL  else 0 end 'Cus_Ratio',
	--,L.Miles,
	-- Case
	--when L.Miles <250 then'Short'
	--when L.Miles between 250 and 500 then 'Medium-Short'
	--when L.Miles between 500 and 1000 then 'Medium'
	--when L.Miles between 1000 and 2000 then 'Medium-Long'
	--when L.Miles >2000 then 'Long' end 'Haul-Length'
	from #Service S
	full join #Bounce B on B.LoadID=S.LoadID and B.CarrierID=S.CarrierID
	full join #Offer O on S.LoadID=O.LoadID and S.CarrierID=O.CarrierID
	inner join bazooka.dbo.LoadCustomer LCUS on LCUS.LoadID = COALESCE(B.LoadID,O.LoadID)
	--inner join bazooka.dbo.Customer CUS on CUS.id=LCUS.CustomerID
	inner join bazooka.dbo.load L on L.id=LCUS.LoadID AND LCUS.Main = 1
	inner join bazooka.dbo.loadstop LSP on LSP.id=L.OriginLoadStopID
	inner join bazooka.dbo.loadstop LSD on LSD.id=L.DestinationLoadStopID
	inner join bazooka.dbo.City CityO on CityO.id=LSP.CityID
	inner join bazooka.dbo.City CityD on CityD.id=LSD.CityID
	LEFT JOIN Analytics.CTM.RateClusters RCO ON RCO.Location = L.OriginCityName + ', ' + L.OriginStateCode
	LEFT JOIN Analytics.CTM.RateClusters RCD ON RCD.Location = L.DestinationCityName + ', ' + L.DestinationStateCode
	--left join #Carrier_Corridor Cor on Cor.Corridor=RCO.ClusterNAME +'-'+RCD.ClusterName
	left join #Carrier_Origin Ori on Ori.OriginCluster=RCO.ClusterNAME
	left join #Carrier_Dest Dest on Dest.DestinationCluster=RCD.ClusterNAME
	--left join #Carrier_Cust CC on CC.CustID = LCUS.CustomerID
	inner join bazooka.dbo.CustomerRelationshipManagement  C on C.CustomerID=LCUS.CustomerID
	inner join
	bazooka.dbo.DandBIndustry D  on C.DandBIndustryId=D.DandBIndustryId
	where   rnk=1  and L.Miles>0
)X
	where pu_Gap>=0 and margin_perc between -65 and 65
	----65 and 65 are 1% to 99% of the margin_perc
	 order by corridor
	"""

    histload=pd.read_sql(query,cn,params= [CarrierID,date1,date2])
    if (len(histload)==0):
        return {'flag':0,'histload':0}
    #histload['corridor_max']=max(histload.corridor_count)
    histload['origin_max']=max(histload.origin_count)
    histload['dest_max']=max(histload.dest_count)
    return {'flag':1,'histload':histload}

def Get_newload(date1,date2):
    cn = pyodbc.connect('DRIVER={SQL Server};SERVER=reportingdatabases;DATABASE=Bazooka;trusted_connection=true')
    query="""
    declare @date1 as date = ?
    declare @date2 as date = ?

    select L.Id  'loadID', convert (date,L.loaddate) 'loaddate',l.TotalValue 'value',
    --LRD.Cost 'customer_rate',
    L.totalrate 'customer_rate', L.EquipmentType 'equipment',
    L.equipmentlength,
    L.miles,
	(case when LSP.[ScheduleCloseTime] = '1753-01-01' then
	convert(datetime, CONVERT(date, LSP.LoadByDate)) + convert(datetime, CONVERT(time, LSP.CloseTime))
	else LSP.[ScheduleCloseTime] end) 'pu_appt',
    --LCUS.customerID 'customerID',
    --COALESCE(O.OriginDH,B.OriginDH )   'originDH',
	--datediff(hour,COALESCE(O.AvailableTime,B.EmptyTime),case when LSP.[ScheduleCloseTime] = '1753-01-01' then
	--convert(datetime, CONVERT(date, LSP.LoadByDate)) + convert(datetime, CONVERT(time, LSP.CloseTime))
	--else LSP.[ScheduleCloseTime] end) 'pu_GAP',
    --CUS.name 'CustomerName'
    L.OriginCityName + '-'+L.OriginStateCode 'origin',
	L.DestinationCityName + '-'+L.DestinationStateCode 'destination',
    CityO.Longitude 'originLon',CityO.Latitude 'originLat',
	CityD.Longitude 'destinationLon',CityD.Latitude 'destinationLat',
	 RCO.ClusterNAME 'originCluster'
	,RCD.ClusterName 'destinationCluster'
    ,RCO.ClusterNAME+'-'+RCD.ClusterName 'corridor'
	--, case when  l.equipmenttype like '%V%' then 'V' when  l.equipmenttype like 'R' then 'R' else 'other' end 'equipment'
	,COALESCE(C.DandBIndustryId,0)  'industryID',
	COALESCE(D.Code,'unknown') 'industry'
    from bazooka.dbo.load L
    inner join bazooka.dbo.LoadCustomer LCUS on L.id=LCUS.LoadID AND LCUS.Main = 1
	INNER JOIN Bazooka.dbo.Customer CUS ON LCUS.CustomerID = CUS.ID
	LEFT JOIN Bazooka.dbo.Customer PCUS ON CUS.ParentCustomerID = PCUS.ID
    inner join bazooka.dbo.loadstop LSP on LSP.id=L.OriginLoadStopID
    inner join bazooka.dbo.loadstop LSD on LSD.id=L.DestinationLoadStopID
    inner join bazooka.dbo.City CityO on CityO.id=LSP.CityID
    inner join bazooka.dbo.City CityD on CityD.id=LSD.CityID
    LEFT JOIN Analytics.CTM.RateClusters RCO ON RCO.Location = L.OriginCityName + ', ' + L.OriginStateCode
    LEFT JOIN Analytics.CTM.RateClusters RCD ON RCD.Location = L.DestinationCityName + ', ' + L.DestinationStateCode
	left join bazooka.dbo.CustomerRelationshipManagement  C on C.CustomerID=LCUS.CustomerID
	left join
	Analytics.bazooka.dbo.DandBIndustry D  on C.DandBIndustryId=D.DandBIndustryId
	--inner join (select  loadid, SUM(amount) 'Cost' from Bazooka.dbo.LoadRateDetail
	--				where EntityType = 12 and EDIDataElementCode IN  ('405','FR',  'PM' ,'MN','SCL','OT','EXP') --and CreateDate > '2018-01-01'
	--				Group by loadid) LRD on LRD.loadid = L.Id
   where
   L.StateType = 1 and L.progresstype=1 and L.totalrate>150
    and  L.LoadDate between @Date1 and @Date2  and L.Miles>0 and L.division between 1 and 2
    AND L.Mode = 1
    --AND LCAR.CarrierID=@CarrierID
    AND L.ShipmentType not in (3,4,6,7)
   -- AND (CASE WHEN L.EquipmentType LIKE '%V%' THEN 'V' ELSE L.EquipmentType END) IN ('V', 'R')
    --AND CAR.ContractVersion NOT IN ('TMS FILE', 'UPSDS CTM', 'UPSCD CTM') --Exclude Managed Loads
    --AND COALESCE(PCUS.CODE,CUS.CODE) NOT IN ('UPSAMZGA','UPSRAILPEA')
    --AND L.TotalRAte >= 150 AND L.TotalCost >= 150
    AND  L.[OriginStateCode] in (select [Code]  FROM [Bazooka].[dbo].[State] where [ID]<=51)
    AND  L.[DestinationStateCode] in (select [Code]  FROM [Bazooka].[dbo].[State] where [ID]<=51)
	and CUS.Name not like 'UPS%'
	AND COALESCE(PCUS.CODE,CUS.CODE) NOT IN ('UPSAMZGA','UPSRAILPEA')
    """
    newload=pd.read_sql(query,cn,params=[date1,date2] )
    return (newload)

def makeMatrix(x,y,z=[]):  #x is the hist load list, y is the unique ode list; x and y are pd.df structure
    kpiMatrix = []
    odlist=[]
    for i in z:
        for j in y.itertuples():
            loads=[]
            std1=[]
            selectedloads=x[(x['carrierID'] == i) & (x['corridor']==j.corridor)]

            for k in  selectedloads.itertuples():
                loads.append(k)
                std1.append(k.kpiScore)
            #for k in x.itertuples():
                 #if (k.corridor == j.corridor):
                  #  loads.append(k)
                  #  std1.append(k.kpiScore)
            # no need to loop x for i times, as x is ordered by carrierid; needs to find the blocks for carrier[i]
            if (len(selectedloads)>0):
                odlist.append(j.corridor)
                kpiMatrix.append(carrier_ode_loads_kpi_std(i,j,loads,np.mean(np.asarray(std1)),np.std(np.asarray(std1))))
    return  kpiMatrix, odlist

def get_odelist_hist(loadlist):
    odelist = []
    for x in loadlist.itertuples():
        # odelist.append({'origin':x.originCluster,'destination':x.destinationCluster,'corridor':x.corridor,'equipment':x.equipment,'corridor_count':x.corridor_count,'corridor_max':x.corridor_max,'origin_count':x.origin_count,'origin_max':x.origin_count,'dest_count':x.dest_count,'dest_max':x.dest_max
        #                 })
        odelist.append({'origin':x.originCluster,'destination':x.destinationCluster,'corridor':x.corridor,'equipment':x.equipment,'origin_count':x.origin_count,'origin_max':x.origin_count,'dest_count':x.dest_count,'dest_max':x.dest_max
                        })
    odelist_df=pd.DataFrame(odelist)
    return odelist_df

def get_odelist_new(loadlist):
    odelist = []
    for x in loadlist.itertuples():
        odelist.append({'origin':x.originCluster,'destination':x.destinationCluster,'corridor':x.corridor,'equipment':x.equipment})
    odelist_df=pd.DataFrame(odelist)
    return odelist_df

def find_ode(kpilist, load, odlist ):
    matchlist=[]
    #matchindex=[]
    perc=[]
    #carriers=[]
    vol=0

    if load.corridor in odlist:
        loc=odlist.index(load.corridor)
        x=kpilist[loc]
        if  x.ode.equipment ==load.equipment :
            weight=1.0
        else:
            weight=0.9
        matchlist.append(x.loads)
        #matchindex.append(kpilist.index(x))
        perc.append(weight)
        vol=len(matchlist)

    else:
        for x in kpilist:
        #if x.carrier not in carriers and (x.ode.origin == load.origin or x.ode.destination==load.destination) and x.ode.equipment ==load.equipment:
            if x.ode.origin == load.origin or x.ode.destination == load.destination:
                 matchlist.append(x.loads)
                 #matchindex.append(kpilist.index(x))

                 if x.ode.origin_max>0:
                     origin_weight= x.ode.origin_count/x.ode.origin_max
                 else:
                     origin_weight=0
                 if x.ode.dest_max>0:
                     dest_weight=x.ode.dest_count/x.ode.dest_max
                 else:
                     dest_weight=0
                 weight= 0.9*max(origin_weight,dest_weight)
                 perc.append(weight)

    if len(matchlist)>0:   #merge match list, if we find mutiple matches for either origin cluster or destination cluster
        matchlist_merge=[]
        perc_merge=[]
        for i in range(0,len(matchlist)):
            for j in range(0,len(matchlist[i])):
                matchlist_merge.append(matchlist[i][j])
                perc_merge.append(perc[i])
        return matchlist_merge,perc_merge, vol
    return  matchlist, perc, vol

def similarity(loadlist, newload, weight):
    carrier_scores = []
    for i in range (0,len(loadlist)):
        load = loadlist[i]
        ori_dist = geopy.distance.vincenty((newload.originLat, newload.originLon),
                                           (load.originLat, load.originLon)).miles
        destination_dist = geopy.distance.vincenty((newload.destinationLat, newload.destinationLon),
                                                   (load.destinationLat, load.destinationLon)).miles
        histload_feature = [ori_dist, destination_dist, load.industryID,load.miles/10]
        newload_feature = [0.01, 0.01, newload.industryID, newload.miles/10]
        sim = 1 - spatial.distance.cosine(histload_feature, newload_feature)

        # other feature could be 'pu_GAP','DH' --- need to verify later

        carrier_scores.append(
        {'carrierID': load.carrierID, 'loadID': newload.loadID, 'similarity': sim, 'kpi': load.kpiScore,
         'rpm': load.rpm, 'miles': load.miles, 'customer_rate': load.customer_rate, 'weight': weight[i],
         'margin_perc':load.margin_perc,
         # 'origin': newload.originCluster, 'dest': newload.destinationCluster, 'loaddate': newload.loaddate
         })
    carrier_scores_df = pd.DataFrame(carrier_scores)

    score_df = hist_scoring(carrier_scores_df, load.carrierID, newload.loadID)

    score_df['estimated_margin'] = newload.customer_rate - score_df['rpm'] * (newload.miles)
    score_df['estimated_margin%'] = score_df['estimated_margin'] / newload.customer_rate*100
    return score_df

def hist_scoring(carrier_scores_df, carrierID, loadID):
    k = 0.3
    # we can choose different condition: maybe top 5, top 10%, sim> 0.8 etc.

    select_k = max(math.ceil(len(carrier_scores_df) * k), min(10, len(carrier_scores_df)))

    carrier_scores_select = carrier_scores_df.sort_values(by=['similarity', 'kpi'], ascending=False)[0:select_k]
    #if len(carrier_scores_select) == 0:
        #print(carrier_info.carrierID, carrier_info.loadID)
        #print(carrierID, loadID)
    sim_score = sum(carrier_scores_select.kpi * carrier_scores_select.similarity * carrier_scores_select.weight) / len(carrier_scores_select)  # top n loads
    sim_margin = sum(carrier_scores_select.margin_perc) / len(carrier_scores_select)
    sim_rpm = sum(carrier_scores_select.rpm) / len(carrier_scores_select)

    score = sim_score

    score_df = {'carrierID': carrierID, 'loadID': loadID,
                'hist_perf': score, 'rpm': sim_rpm, 'margin_perc': sim_margin}

    return score_df

def check(carrier_load,newloads,carrier,corridor_info):
    if carrier_load['flag']==1:
        loadList=carrier_load['histload']
        loadList.to_csv('carrier' + str(carrier.carrierID) + 'histload.csv',index=False)
        # loadList=  Carrier_Load_loading(1000)
        carrier_load_score=indiv_recommender(carrier, newloads, loadList)
    else:
        carrier_load_score=general_recommender(carrier,newloads,corridor_info)

    return (carrier_load_score)

def general_recommender(carrier,newloads,corridor_info):
##for new carriers, which has no hist data
# margin and rpm and margin perc, needs to use all data from this corridor, no need to grab only from this carrier if this is a new carrier
    carrier_load_score=[]
    carrierID = int(carrier.carrierID)
    for i in range(0, len(newloads)):
        newload = newloads.iloc[i]
        if (any(corridor_info.corridor==newload.corridor)):
            rpm= corridor_info[corridor_info.corridor==newload.corridor].rpm.values[0]
            estimate_margin_p = corridor_info[corridor_info.corridor == newload.corridor].corrdor_margin_perc.values[0]
        elif (any(corridor_info.OriginCluster==newload.originCluster)):
            rpm = pd.DataFrame.mean(corridor_info[corridor_info.OriginCluster == newload.originCluster].rpm)
            estimate_margin_p= pd.DataFrame.mean(corridor_info[corridor_info.OriginCluster == newload.originCluster].corrdor_margin_perc)
        else:
            rpm=pd.DataFrame.mean(corridor_info.rpm)
            estimate_margin_p = pd.DataFrame.mean(corridor_info.corrdor_margin_perc)
        score = {'carrierID': carrierID,
             'loadID': newload.loadID,
             # 'origin': newload.originCluster, 'destination': newload.destinationCluster,
             # 'loaddate': newload.loaddate,
                 'hist_perf': 0, 'rpm': rpm,
                 #'estimated_margin': newload.customer_rate - rpm * (newload.miles + newload.originDH),
                 'estimated_margin': newload.customer_rate - rpm * (newload.miles),
                 'estimated_margin%': estimate_margin_p,
                 'margin_perc': estimate_margin_p,
                 'desired_OD': 0
             }
        carrier_load_score.append(score)
    return (carrier_load_score)

def indiv_recommender(carrier,newloads,loadList):
    """once there is any historical information for given carrier, use historical info to calculate the scores(hist preference)"""

    carrierID = int(carrier.carrierID)
    newload_ode = get_odelist_new(newloads)

    carriers = sorted(set(loadList.carrierID.tolist()))
    histode = get_odelist_hist(loadList)
    # odelist = set(histode)   # set is not useful for the object list
    odelist = histode.drop_duplicates(subset=['origin', 'destination', 'equipment'])

    kpiMatrix,kpi_odlist = makeMatrix(loadList, odelist, carriers)

    carrier_load_score = []

    for i in range(0, len(newloads)):
        newload=newloads.iloc[i]
        new_ode=newload_ode.iloc[i]
        time_carrier = pd.Timestamp(carrier.EmptyDate)
        time_load = pd.Timestamp(newload.pu_appt)
        time_gap = time_load - time_carrier

        matchlist,   weight, corridor_vol = find_ode(kpiMatrix,new_ode,kpi_odlist )
        ## updated, match list will be a list of all matched loads. i.e. one new load can only have one matched list
        ##weight is also a list of load coresponding weight
        if corridor_vol>min(len(loadList)*0.1,10):
            desired_OD = 100
        else:
            desired_OD = 0
        if len(matchlist) > 0:
            score = similarity(matchlist, newload, weight)
            score['desired_OD'] = desired_OD
            carrier_load_score.append(score)
        else:
            score = {'carrierID': carrierID,
                     'loadID': int(newload.loadID),

                     'hist_perf': 0, 'rpm': pd.DataFrame.mean(loadList.rpm),
                     #'estimated_margin': newload.customer_rate - pd.DataFrame.mean(loadList.rpm) * (newload.miles+newload.originDH),
                     'estimated_margin': newload.customer_rate - pd.DataFrame.mean(loadList.rpm) * (newload.miles),
                     'estimated_margin%': 100 - pd.DataFrame.mean(loadList.rpm) * (newload.miles+newload.originDH)/newload.customer_rate*100,
                     'margin_perc':pd.DataFrame.mean(loadList.margin_perc),
                     'desired_OD': 0}

            carrier_load_score.append(score)

    return (carrier_load_score)

def score_DH(DH,radius ):
    score=(radius-np.array(DH))/radius*100
    score_check=[min(max(0,a),100) for a in score]
    return  score_check

def pu_Gap(pu_appt,EmptyDate,traveltime):
    time_gap=pu_appt-EmptyDate
    return time_gap.days * 24-traveltime + time_gap.seconds / 3600

def dynamic_input(newloads_df,carrier):
    ##This part is for new api input
    # newloads_df['originDH'] = originDH
    # newloads_df['destDH'] = destDH
    # newloads_df['puGap'] = gap
    # newloads_df['totalDH'] = originDH+destDH
    if  carrier.originLat is not None and carrier.originLon is not None:
         newloads_ODH= {'originDH': newloads_df.apply(lambda row: geopy.distance.vincenty((row.originLat, row.originLon), (
             float(carrier.originLat), float(carrier.originLon))).miles, axis=1)}

         newloads_df.update(pd.DataFrame(newloads_ODH))
    if  carrier.destLat is not None and carrier.destLon is not None:
         newloads_DDH= {'destDH': newloads_df.apply(lambda row: geopy.distance.vincenty((row.originLat, row.originLon), (
             float(carrier.destLat), float(carrier.destLon))).miles, axis=1)}
         newloads_df.update(pd.DataFrame(newloads_DDH))
    if carrier.EmptyDate  is not None:
        if carrier.originLat is not None and carrier.originLon is not None:
            newloads_puGap={'puGap': newloads_df.apply(lambda row: pu_Gap(pd.Timestamp(row.pu_appt), pd.Timestamp(carrier.EmptyDate),row.originDH/40.0),
                                    axis=1)}
        else:
            newloads_puGap = {'puGap': newloads_df.apply(
                lambda row: pu_Gap(pd.Timestamp(row.pu_appt), pd.Timestamp(carrier.EmptyDate), 0),
                axis=1)}
        newloads_df.update(pd.DataFrame(newloads_puGap))

    newloads_df['totalDH'] = newloads_df.apply(lambda row: row.originDH + row.destDH, axis=1)

    return newloads_df

def reasoning(results_df):
    reasons=[]
    reason_label=['close to origin','short total deadhead','good historical performance on similar loads','estimated margin', 'close to pickup time','desired OD']
    for load in results_df.itertuples():
        scores=[load.ODH_Score * 0.35, load.totalDH_Score * 0.20, load.hist_perf * 0.30,
                load.margin_Score* 0.10, load.puGap_Score* 0.05, load.desired_OD * 0.1]
        reasons.append ( reason_label[scores.index(max(scores))])
    return reasons

def api_json_output(results_df,carrierID):
    results_df['Score'] = results_df['Score'].apply(np.int)
    api_resultes_df = results_df[['loadID', 'Reason', 'Score']]
    loads=[]
    #print (results_json)
##    results_df.to_csv(
##        'carrier' + str(carrierID) + '_load_recommender' + datetime.datetime.now().strftime(
##            "%Y%m%d-%H%M%S") + '.csv',
##        index=False,
##        columns=['carrierID', 'loadID', 'loaddate', 'origin', 'destination', 'originDH', 'destDH',
##                 'totalDH', 'margin_perc', 'estimated_margin', 'corrdor_margin_perc', 'estimated_margin%',
##                 'puGap', 'ODH_Score', 'totalDH_Score', 'puGap_Score',
##                 'margin_Score', 'hist_perf', 'Score', 'Reason'])
    # for i in api_resultes_df.index:
    #     load=api_resultes_df.loc[i]
    #     #api_resultes_df.loc[i].to_json("row{}.json".format(i))
    #     load_json=load.to_json()
    #     loads.append(load_json)

    for i in api_resultes_df.index:
        load = api_resultes_df.loc[i]

        _loadid = load["loadID"].item()
        _reason = load["Reason"]
        _score = load["Score"].item()
        loads.append({
            "loadid": _loadid,
            "Reason": _reason,
            "Score": _score
        })
    return loads

def recommender( carrier_load,trucks_df):
    originDH_default = 250  # get radius
    destDH_default = 300
    gap_default=48
    date1_default = now.strftime("%Y-%m-%d")
    date2_default = (datetime.timedelta(1) + now).strftime("%Y-%m-%d")
    corridor_info = pd.read_csv("corridor_margin.csv")  # should be saved somewhere

    ##initialization of the final results
    #results_sort_df = pd.DataFrame(columns=['loadID', 'Reason', 'Score'])
    result_json = {'Loads': []}

    carrier = trucks_df.iloc[0]
    newloadsall_df = Get_newload(date1_default,date2_default)
     ### should deal with if equipmenttype is a string carrier['EquipmentType'].fillna('', inplace=True)
    ###This part is for new api input

    # if any date will be put in, change the variables.
    # if date1 is not None and date2 is not None:
    #     Get_newload(date1,date2)
    # elif date2 is None:
    #     date2=date1 + 1
    #     Get_newload(date1,date2)
    # else:
    #     Get_newload()

    newloads_df = newloadsall_df[(newloadsall_df.value <= float(carrier.cargolimit))
                                 & [carrier.EquipmentType in equip for equip in newloadsall_df.equipment]
                                 & (newloadsall_df.equipmentlength <= float(carrier.EquipmentLength))]
    # newloads_df = newloadsall_df[
    #     (newloadsall_df.value <= carrier.cargolimit) & (newloadsall_df.equipment == carrier.EquipmentType)]
    originRadius = originDH_default if carrier.originDeadHead_radius == 0 else float(carrier.originDeadHead_radius)
    destRadius = destDH_default if carrier.destinationDeadHead_radius == 0 else float(carrier.destinationDeadHead_radius)

    # initialize 3 column features. if carrier put any info related to DH or puGap,we can update
    newloads_df['originDH'] = originRadius
    newloads_df['destDH'] = destRadius
    newloads_df['puGap'] = gap_default
    newloads_df['totalDH'] = originRadius+destRadius

    # need dynamic check: if equipment type is an entry, etc.
    if len(newloads_df) > 0:
        newloads_df = dynamic_input(newloads_df, carrier)

        # need to change, if not null for origin, update origin; if not null for dest, update dest,
        # if not null for date, select date from to.
        #print(carrier.carrierID)

        newloads_select = newloads_df[
            (newloads_df.originDH <= originRadius) | (newloads_df.totalDH <= (originRadius+destRadius)) & (newloads_df.puGap <= gap_default)]

        if len(newloads_select) > 0:
            carrier_load_score = check(carrier_load, newloads_select,carrier,corridor_info)
            # if (len(carrier_load_score) > 0):
            results_df = pd.DataFrame(carrier_load_score).merge(newloads_select, left_on="loadID", right_on="loadID",
                                                                how='inner')
            results_df = results_df.merge(corridor_info, left_on='corridor', right_on='corridor', how='left')
            results_df['corrdor_margin_perc'].fillna(0, inplace=True)
            # results_df.merge(newloads_df,left_on="loadID",right_on="loadID",how='inner')
            results_df['ODH_Score'] = score_DH(results_df['originDH'].tolist(), originDH_default)
            results_df['totalDH'] = results_df['originDH'] + results_df['destDH']
            results_df['totalDH_Score'] = score_DH(results_df['totalDH'].tolist(), (originDH_default + destDH_default))
            results_df['puGap_Score'] = score_DH(abs(results_df['puGap']).tolist(),gap_default )
            results_df['margin_Score'] = results_df['estimated_margin%'] * 0.3 + results_df['margin_perc'] * 0.7 \
                                         - results_df['corrdor_margin_perc']
            # margin score needs to be verified
            results_df['Score'] = results_df['ODH_Score'] * 0.25 + results_df['totalDH_Score'] * 0.20 + \
                                  results_df['hist_perf'] * 0.30  + results_df['margin_Score'] * 0.10 + \
                                  results_df['puGap_Score'] * 0.05 + results_df['desired_OD'] * 0.1
            results_df['Reason'] = reasoning(results_df)
            results_sort_df = results_df[results_df.Score > 0].sort_values(by=['Score'], ascending= False)

            result_json=api_json_output(results_sort_df,carrier.carrierID)
            #print ('test')
    return result_json

@app.route('/search/',methods=['GET'])
def search():
    truck = {'carrierID':0,
                 'originLat': None,
                 'originLon': None,
                 'destLat': None,
                 'destLon': None,
                 'EmptyDate': now.strftime("%Y-%m-%d"),
                 'EquipmentType': '',
                 'EquipmentLength':53,
                 'cargolimit': 500000,
                 'originDeadHead_radius': 0,
                 'destinationDeadHead_radius': 0
                 }
    truck_input = request.args.to_dict()
    truck.update(truck_input)
    truck['cargolimit'] = Get_truckinsurance(truck['carrierID'])
    carrier_load = Get_Carrier_histLoad(truck['carrierID'],(datetime.timedelta(-90-7) + now).strftime("%Y-%m-%d"),
                                        (datetime.timedelta(-7) + now).strftime("%Y-%m-%d"))

    carriers = []
    carriers.append(truck)
    carrier_df = pd.DataFrame(carriers)
    results=recommender(carrier_load, carrier_df)

    return jsonify({'Loads':results, "ver": "TruckNorris.0.1.18208.04"} )

if __name__=='__main__':
    app.run(debug = True)


import math

def triangulation(c, angleValues):
    A = angleValues[0]
    B = angleValues[1]
    C = 180 - angleValues[0] - angleValues[1]
    b = c * math.sin(B) / math.sin(C)
    h = math.sin(A) * b
    m = math.sqrt(math.pow(b, 2) - math.pow(h, 2))
    return m, h

def angleOfArrival(angleValues):
    x1 = 0
    x2 = 460
    c = x2 - x1
    m, h = triangulation(c, angleValues)
    return m, h

def trilateration(x1, x2, x3, y1, y2, y3, rssiValues):
    A = 2 * x2 - 2 * x1
    B = 2 * y2 - 2 * y1
    C = math.pow(rssiValues[0], 2) - math.pow(rssiValues[1], 2) - math.pow(x1, 2) + math.pow(x2, 2) - math.pow(y1,
                                                                                2) + math.pow(
        y2, 2)
    D = 2 * x3 - 2 * x2
    E = 2 * y3 - 2 * y2
    F = math.pow(rssiValues[1], 2) - math.pow(rssiValues[2], 2) - math.pow(x2, 2) + math.pow(x3, 2) - math.pow(y2,
                                                                                2) + math.pow(
        y3, 2)
    x = (C * E - F * B) / (E * A - B * D)
    y = (C * D - A * F) / (B * D - A * E)
    return x, y

def rssiBased(rssiValues):
    x1 = 0
    y1 = 0
    x2 = 580
    y2 = 0
    x3 = 0
    y3 = 460
    x, y = trilateration(x1, x2, x3, y1, y2, y3, rssiValues)
    return x, y

# def resultOfAlgorithms(self, rssiPos, anglePos):
#     positonX = input("Enter x coordiante: ")
#     positonY = input("Enter y coordiante: ")
#     counter = 0
#     if (rssiPos.get_positionX - positonX > anglePos.get_positionX - positonX):
#         counter += 1
#     elif (rssiPos.get_positionX - positonX < anglePos.get_positionX - positonX):
#         counter -= 1
#     if (rssiPos.get_positionY - positonY > anglePos.get_positionY - positonY):
#         counter += 1
#     elif (rssiPos.get_positionY - positonY < anglePos.get_positionY - positonY):
#         counter -= 1
#     if (counter > 0):
#         return "Rssi Based Algorithm is better."
#     elif (counter < 0):
#         return "Angle of Arrival Algorithm is better."
#     else:
#         return "Both algorithms give results with the same accuracy."


import pickle
import os.path
import json
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.http import MediaFileUpload

__author__ = 'Mykhailo Klimchuk'

SCOPES = ['https://www.googleapis.com/auth/drive']  # If modifying these scopes, delete the file token.pickle.

with open('backup_data.json', 'r') as backup_data_json:
    backup_data = json.load(backup_data_json)

BACKUPS_FOLDERS = {
    'Confluence': [backup_data.get('google_drive_confluence_folder_id')],
    'Jira': [backup_data.get('google_drive_jira_folder_id')],
}

def main(folder_name, file_name, source):
    credentials = None

    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            credentials = pickle.load(token)

    if not credentials or not credentials.valid:
        if credentials and credentials.expired and credentials.refresh_token:
            credentials.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            credentials = flow.run_local_server(port=0)

        with open('token.pickle', 'wb') as token:
            pickle.dump(credentials, token)  # Save the credentials for the next run

    service = build('drive', 'v3', credentials=credentials)

    folders = BACKUPS_FOLDERS.get(source)
    os.chdir(folder_name)

    file_metadata = {'name': file_name,
                     'parents': folders}
    media = MediaFileUpload(file_name,
                            resumable=True)

    file = service.files().create(body=file_metadata,
                                  media_body=media,
                                  fields='id').execute()
    os.chdir('..')
    return file


#!/usr/bin/env python
"""
Using custom colors
===================

Using the recolor method and custom coloring functions.
"""

import numpy as np
from PIL import Image
from os import path
import matplotlib.pyplot as plt
import random
import argparse
from wordcloud import WordCloud, STOPWORDS
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--txt', type=str, default='text.txt',
                    help="Name of text for reading")

parser.add_argument('-m', '--mask', type=str,
                    help="Name of mask picture")

parser.add_argument('-bg', '--background', type=str, default='black',
                    help="background color")

parser.add_argument('-w', '--maxword', type=int, default=1000,
                    help="Max word")

parser.add_argument('-sw', '--stopword', nargs='+', default='a',
                    help="Stop words")

parser.add_argument('-sf', '--stopfile', type=str,
                    help="File with stopword")

args = parser.parse_args()
text_file = args.txt
mask_file = args.mask
bg = args.background
max_word = args.maxword
stop = args.stopword
stop_file = args.stopfile
print(stop)
stops = []
d = path.dirname(__file__)
if stop_file:
    stops = open(path.join(d, stop_file), encoding="utf8").readlines()

def grey_color_func(word, font_size, position, orientation, random_state=None,
                    **kwargs):
    return "hsl(0, 0%%, %d%%)" % random.randint(60, 100)

# read the mask image
# taken from
# http://www.stencilry.org/stencils/movies/star%20wars/storm-trooper.gif
# movie script of "a new hope"
# http://www.imsdb.com/scripts/Star-Wars-A-New-Hope.html
# May the lawyers deem this fair use.
text = open(path.join(d, text_file), encoding="utf8").read()
#text = open(path.join(d, 'a_new_hope.txt')).read()

# preprocessing the text a little bit
#text = text.replace("HAN", "Han")
#text = text.replace("LUKE'S", "Luke")

# adding movie script specific stopwords
stopwords = set(STOPWORDS)
for s in stops:
    s=s.strip('\n')
    stopwords.add(s)

for i in stop:
    # print(i)
    stopwords.add(i)
if mask_file:
    print(mask_file)
    mask = np.array(Image.open(path.join(d, mask_file)))
#mask = np.array(Image.open(path.join(d, "stormtrooper_mask.png")))
    wc = WordCloud(background_color=bg, mask=mask, max_words=max_word, stopwords=stopwords, margin=10,
               random_state=1).generate(text)
else:
    print('No mask')
    wc = WordCloud(width=2000, height=2100,background_color=bg, max_words=max_word, stopwords=stopwords, margin=10,
               random_state=1).generate(text)

# store default colored image
default_colors = wc.to_array()
wc.to_file(text_file.rsplit('.')[0]+'-1.png')

plt.figure( figsize=(20,10),facecolor='k')
plt.title("Custom colors")
plt.imshow(wc.recolor(color_func=grey_color_func, random_state=3), interpolation="bilinear")
wc.to_file(text_file.rsplit('.')[0]+'-2.png')
plt.axis("off")
plt.tight_layout(pad=0)
plt.title("Default colors")
plt.imshow(default_colors, interpolation="bilinear")
plt.axis("off")
#plt.savefig('tmp.png', dpi=1000)
plt.show()


A = int(input("A: "))
B = int(input("B: "))

while A < B:
    A = A + B
    print('Значение A:', A)
    if A < B:
         print('Пока что нет')
    else:
        print('Дождались')
        print('Финальный А:', A)


import win32com.client
import os
import re
import pythoncom
import sys
from collections import Counter
from win32com.client import constants
from docx import Document
from docx.opc.constants import RELATIONSHIP_TYPE as RT
app=[]
count=0
l=[]
import sys
from datetime import datetime
iter=sys.argv[1]
start=datetime.now()
print("--------------------------------------------------------------------------------------------------------")
print("Document Name:", iter)
print("CheckList Rule - 10: Table name should not be bold.")
print("Document Review Start Time:", start,"HH:MM:SS")
print("--------------------------------------------------------------------------------------------------------")
print("\n")
##Open the Document
if iter.endswith('.doc'): #or iter.endswith('.docx'):
 word1 = win32com.client.gencache.EnsureDispatch ("Word.Application")
 word1.Visible = True
 p = os.path.abspath(iter)
 word1.Documents.Open(p)
 sheet_1 = word1.ActiveDocument
 para=sheet_1.Paragraphs
 for p in para:

  k=p.Range.Text.encode('ascii','ignore').decode()
  #print(p.Range.Style,k)
  #if p.Range.Hyperlinks and re.search("^Table",str(p)) and p.Range.Font.Bold:
  # print("Bold Table name::", k)
  # count=count+1
  # print("Page number:",p.Range.Information(constants.wdActiveEndAdjustedPageNumber))
  # print("Line On Page:",p.Range.Information(constants.wdFirstCharacterLineNumber))
  if p.Range.Font.Bold and re.search("^Table",str(p)) :
   if str(p.Range.Style)=='Caption':  # and p.Range.Font.Bold and p.Range.Style=='Caption'
    print("Bold Table name:",k)
    print("Page number:",p.Range.Information(constants.wdActiveEndAdjustedPageNumber))
    print("Line On Page:",p.Range.Information(constants.wdFirstCharacterLineNumber))
    count=count+1
 if count==0:
  print("Status:Pass")
 else:
  print("Status:Fail")
 sheet_1.Close()
 word1.Quit()

elif iter.endswith('.docx'):
 doc = Document(iter)
 for para in doc.paragraphs:
  f=para.style.font.bold
  if f==True and re.search("^Table",para.text.encode('ascii','ignore').decode()):
   print(para.text.encode('ascii','ignore').decode())
   count=count+1
 if count==0:
  print("Status:Pass")
 else:
  print("Above are the Table Names with bold font.")
  print("Status:Fail")
end=datetime.now()
print("\nDocument Review End Time:", end)
print("\nTime taken For Document Review:", end-start,"HH:MM:SS")


from pathlib import Path
import os, shutil

pathway = Path()
# folders = [file.name for file in pathway.glob('Compound 1/*')]
folders = [file.name for file in pathway.glob('test/*')]

def rewrite(path):
    split_path = os.path.split(path)
#     print(split_path[1])
    return split_path[1]
    print('\n')

for folder in pathway.glob('test/*'):
#     print(folder)
    new_name = rewrite(folder)
    for files in pathway.glob("{target}/*".format(target=folder)):
        print(files.name)
        old_resname = files.stem[0:3]
        print(old_resname)

        if files.name.__contains__(".itp"):
            os.system(f"sed 's/{old_resname}/{new_name}/g' {files.name} > {new_name}.itp")
        elif files.name.__contains__(".gro"):
            os.system(f"sed 's/{old_resname}/{new_name}/g' {files.name} > {new_name}.gro")


import os
import io
import json
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
import traceback
import time
import sys
import requests
import traceback
import gc
from joblib import dump as joblib_dump, load as joblib_load
import pickle
#from sklearn.externals.joblib import dump as joblib_dump
#from sklearn.externals.joblib import load as joblib_load
import redis as saver_c
import base64

c_mambaml_load_model = 'mambaml_load_model('
# workarround to predict
# predict try to get model from redis memory
#   if not exist then download from s3 and update redis memory again for future processing
# workarround serialize
# binaryze the object and convert to b64 string
# save object to s3
# update redis memory

def mambaml_load_model(key):
    # this is a special function
    # all mambaml functions start with mambaml
    # this function locate the parent object
    # and get the model from memory instead of inputport
    # this function is usefule for predict endpoint
    # works in the same workarround
    # this method return a object getted from redis
    model_type = key.split('_')[0]
    print("MODEL_TYPE", model_type)
    if model_type == 'sklearn' or model_type == 'sklean':
        b64_object = saver_x.get(key)
        stream = b64_to_stream(b64_object)
        ret = joblib_load(stream)
        return joblib_load(stream)
    elif model_type == 'sklearnbestestimator':
        b64_object = saver_x.get(key)
        stream = b64_to_stream(b64_object)
        return joblib_load(stream)
    elif model_type == 'pickle':
        b64_object = saver_x.get(key)
        stream = b64_to_stream(b64_object)
        return pickle.load(stream)
    pass

# joblib.load(io.BytesIO(new_file.read()))
def b64_to_stream(b64data):
    stream = io.BytesIO(base64.b64decode(b64data))
    return stream

def file_to_b64(filename):
    with open(filename, "rb") as f:
        binfile=f.read()
    b64_file = base64.b64encode(binfile)
    return b64_file

def save_c_file(filename, key):
    b64_file = file_to_b64(filename)
    # one day
    # TODO  guardar en s3
    # save in cache
    saver_x.set(key, b64_file, ex=60*60*24)
    pass

def pickle_serialize_object_and_uplodad_to_s3(model, project_id, box_id, port_index):
    filename = 'pickle_{}_{}_{}.pickle'.format(str(project_id), box_id, port_index)
    with open(filename, 'wb') as pickle_file:
        pickle.dump(model, pickle_file)
    # TODO uplodad to s3 first
    save_c_file(filename, filename)
    print("Object saved correctly pickle")
    os.remove(filename)

def sk_learn_model_serialize_object_and_upload_to_s3(model, project_id, box_id, port_index):
    filename = 'sklearn_{}_{}_{}.joblib'.format(str(project_id), box_id, port_index)
    joblib_dump(model, filename, compress = False)
    # TODO upload to s3 first if we have problems with redis
    save_c_file(filename, filename)
    os.remove(filename)
    # upload direct to s3??

def sk_learn_best_estimator_model_serialize_object_and_upload_to_s3(model, project_id, box_id, port_index):
    filename = 'sklearnbestestimator_{}_{}_{}.joblib'.format(str(project_id), box_id, port_index)
    joblib_dump(model.best_estimator_, filename, compress = False)
    save_c_file(filename, filename)
    os.remove(filename)
    # TODO upload to s3 first if we have problems with redis
    # upload direct to s3??

def c_run_str_code(inputs, str_code, depend, params, input_boxes):
    global ret
    ret = []
    func_name = str_code.split('(')[0][4:].strip()

    sniped_code ="""
tmp_ret = FUNC_NAME(inputs)
for x in tmp_ret:
    ret.append(x)"""

    sniped_code = sniped_code.replace('FUNC_NAME',func_name,1)

    # input paramters
    parameters = None
    if not params is None and params != 'null':
        parameters = json.loads(params)

        for param in parameters:
            print("param-->", param)
            if param["type"] == "colselector":
                colsvalues = json.loads(param['value'])

                str_code = str_code.replace(param['name'], str(colsvalues["cols"]))
                print(str_code)
            else:
                str_code = str_code.replace(param['name'], param['value'])

    # if some saver information FOR NOW ONLY ONE mamab_load_model in box
    # cold be multiples but for now only 1 allowed
    # necessiamos parsear el codigo en busca de algo como mamba_load_model(
    line = str_code.find(c_mambaml_load_model)
    if line > 0:
        port_index = int(str_code[line+len(c_mambaml_load_model):line+len(c_mambaml_load_model)+1])
        save_key = input_boxes[port_index].parentBox.get_saver_key(input_boxes[port_index].numport-input_boxes[port_index].parentBox.n_inputs)
        print("RETRIVED KEY", port_index, save_key)
        str_code = str_code.replace(c_mambaml_load_model+str(port_index), c_mambaml_load_model+"'"+save_key+"'")
        print("RETRIVED KEY", port_index, save_key)

    LOC = ""

    if depend:
        LOC = str_code +("\r\n")+depend+("\r\n")+ sniped_code
    else:
        LOC = str_code+ "\r\n" + sniped_code

    print("NEWCODE", LOC)
    # debemos remplazar el mamba_load_model(index_port_id) por
    # mamba_load_model(BOX_PARENT_ID of index 0, index_port_id)
    exec(LOC)

    return ret

class InputPort():

    def __init__(self, name, numport, parentBox, input_port_num):
        self.name = name
        self.numport = numport
        self.parentBox = parentBox
        self.input_port_num = input_port_num

class BoxCode():

    def __init__(self, str_code, box_id, n_inputs,
                 n_outputs, json, depend, params, changed,
                 project_id, host, serialize_outputs, response=False):
        self.project_id = project_id
        self.host = host
        self.str_code = str_code
        self.depend  = depend
        self.n_inputs = n_inputs
        self.n_outputs = n_outputs
        self.inputs = []
        self.outputs = []
        self.box_id = box_id
        self.response = response
        self.setStatus('INIT')
        self.json = json
        self.params = params
        self.changed = changed
        self.serialize_outputs = serialize_outputs

    def get_saver_key(self, output_num):
        # necessitamos devolver algo como asi
        if self.serialize_outputs!=None and self.serialize_outputs!='null' and self.serialize_outputs != "[]":
            s_outputs = json.loads(self.serialize_outputs)
            for out in s_outputs:
                port_index = int(out['outputnum'])
                port_type = out['Outputtype']
                print("analize", port_index, output_num)
                if port_index == output_num and (port_type == 'sklearn_model' or port_type == 'sklean_model'):
                    return 'sklearn_{}_{}_{}.joblib'.format(str(self.project_id), self.box_id, str(port_index))
                elif port_index == output_num and port_type == 'GridSearch Model':
                    return 'sklearnbestestimator_{}_{}_{}.joblib'.format(str(self.project_id), self.box_id, str(port_index))
                elif (port_index == output_num and port_type == 'pickle'):
                    return 'pickle_{}_{}_{}.pickle'.format(str(self.project_id), self.box_id, str(port_index))
        return None

    def clear_inputs(self):
        for x in self.inputs:
            del x
        self.inputs = []

    def setChangedBox(self, str_code, box_id, n_inputs,
                      n_outputs, json, depend, params, changed, project_id, host, serialize_outputs):
        # to set trained false
        self.freespace()
        # if this box changed then result of preview run is empty
        self.json['nodes'][self.box_id]['properties']['payload']['result']=dict()
        # reset all parameter with new values
        self.__init__(str_code, box_id, n_inputs, n_outputs, json, depend,
                      params, changed, project_id, host, serialize_outputs, self.response)
        return None

    def isRunned(self):
        return self.status == 'RUNNED'

    def getStatus(self):
        return self.status

    def setStatus(self, status):
        # TODO update get project status
        self.status = status
        if self.response == False:
            requests.get(self.host+'/projects/set_status?id='+self.project_id+'&stat='+status+'&task='+self.box_id,)
        return self.status

    def freespace(self):
        for out in self.outputs:
            del out

        del self.outputs
        self.outputs = []

    def order_inputs(self):
        self.inputs.sort(key=lambda x: x.input_port_num, reverse=False)

    def run(self):
        try:
            if self.isRunned():
                return True

            self.setStatus('RUNNING')

            myinputs = []

            self.json['nodes'][self.box_id]['properties']['payload']['result']=dict()
            self.order_inputs()
            for i_input in self.inputs:
                print(i_input.parentBox.box_id, i_input.input_port_num)
                i_input.parentBox.run()
                myinputs.append(i_input.parentBox.outputs[i_input.numport-i_input.parentBox.n_inputs])

            out = c_run_str_code(myinputs, self.str_code, self.depend, self.params, self.inputs)

            for p_out in out:
                self.outputs.append(p_out)
        # after run
            index = 0
            # all box done ok
            self.json['nodes'][self.box_id]['properties']['payload']['result']['status']="DONE_OK"
            for out in self.outputs:
                self.json['nodes'][self.box_id]['properties']['payload']['result']['out'+str(index)]=dict()
                self.json['nodes'][self.box_id]['properties']['payload']['result']['out'+str(index)]['status'] = 'OK'
                if type(out) == type(pd.DataFrame()):
                    self.json['nodes'][self.box_id]['properties']['payload']['result']['out'+str(index)]['first100'] = out.head(1).to_json(force_ascii=True)
                    self.json['nodes'][self.box_id]['properties']['payload']['result']['out'+str(index)]['columns'] = pd.DataFrame(out.columns).to_json()
                index = index + 1
            # once trained ok then hasChange is False
            self.json['nodes'][self.box_id]['properties']['payload']['hasChange'] = False
            self.json['nodes'][self.box_id]['properties']['payload']['result']['error_message'] =''
            self.json['nodes'][self.box_id]['properties']['payload']['result']['error_args'] = ''
            self.json['nodes'][self.box_id]['properties']['payload']['result']['error_trace'] = ''

            if self.serialize_outputs!=None and self.serialize_outputs!='null' and self.serialize_outputs != "[]":
                # then they have a serialized outputs
                s_outputs = json.loads(self.serialize_outputs)

                for out in s_outputs:
                    print("OUT----->", out)
                    port_index = int(out['outputnum'])
                    port_type = out['Outputtype']
                    if port_type == 'sklearn_model' or port_type == 'sklean_model':
                       sk_learn_model_serialize_object_and_upload_to_s3(self.outputs[port_index],
                                                                        self.project_id,
                                                                        self.box_id,
                                                                        str(port_index))
                    elif port_type == 'GridSearch Model':
                       sk_learn_best_estimator_model_serialize_object_and_upload_to_s3(self.outputs[port_index],
                                                                                self.project_id,
                                                                                self.box_id,
                                                                                str(port_index))
                    elif port_type == 'pickle':
                        pickle_serialize_object_and_uplodad_to_s3(self.outputs[port_index],
                                                                  self.project_id,
                                                                  self.box_id,
                                                                  str(port_index))
                    else:
                        raise Exception(port_type + ' model type not allowed yet!')
            print("RUNNED")
            self.setStatus('RUNNED')
        except Exception as e:
            print("SOME ERROR")
            self.json['nodes'][self.box_id]['properties']['payload']['hasChange'] = True
            self.json['nodes'][self.box_id]['properties']['payload']['result']['status']="DONE_ERROR"
            self.json['nodes'][self.box_id]['properties']['payload']['result']['error_message'] = str(e)
            self.json['nodes'][self.box_id]['properties']['payload']['result']['error_args'] = ''
            self.json['nodes'][self.box_id]['properties']['payload']['result']['error_trace'] = traceback.format_exc()
            self.setStatus('ERROR')
            raise Exception('Run error check error message results!')
            return False
        return self.isRunned()

def run_celery_project(allboxes, project_id, task, host, saver_host, response):

    if saver_host != None:
        saver_x = saver_c.Redis(host=saver_host, port=6379, db=0)

    # TODO do it better
    def getboxby_name(box_name, boxes):
        for x in boxes:
            if x.box_id == box_name:
                return x
        return None

    # TODO do it better
    def getportid_to_index(portid):
        return portid.split('port')[1]

    # free space fot all boxes we retrain all model
    def freespace(boxes):
        for box in boxes:
            box.freespace()
        gc.collect()

    # clear all inputs ports
    def clear_all_input_ports(boxes):
        for x in boxes:
            x.clear_inputs()

        gc.collect()

    d_json = ""
    try:

        valid_boxes = []
        if allboxes is None or task == 'ALL':
            if allboxes is not None:
                freespace(allboxes)
            allboxes=[]
            r = requests.get(host+'/projects/get_internal?id='+project_id)
            d_json = json.loads(r.json()['json'])
            for x in d_json['nodes']:
                box_type = d_json['nodes'][x]['type']
                if (box_type[0:7] == 'Dataset'):
                    print("DATASET BOX")
                    node_name = x
                    import numpy as np
                    import pandas as pd
                    X = pd.DataFrame(np.array([[1, 1], [1, 2], [2, 2], [2, 3]]))
                    box = BoxCode("", node_name, 0, 1, d_json,"", "", False, project_id, host, "", response)
                    box.setStatus('RUNNED')
                    box.outputs.append(X)
                    # for now dummy data on dataset
                    allboxes.append(box)
                elif (box_type == ('Python Script')):
                    node_name = x
                    python_code = d_json['nodes'][x]['properties']['payload']['python_code']
                    n_inputs = d_json['nodes'][x]['properties']['payload']['n_input_ports']
                    n_outputs = d_json['nodes'][x]['properties']['payload']['n_output_ports']
                    depend = d_json['nodes'][x]['properties']['payload']['depen_code']
                    params = '' # input parameters
                    serialize_outputs = '' # output parameters to serialized
                    if 'parameters' in d_json['nodes'][x]['properties']['payload']:
                        params = d_json['nodes'][x]['properties']['payload']['parameters']

                    if 'outputs' in d_json['nodes'][x]['properties']['payload']:
                        serialize_outputs = d_json['nodes'][x]['properties']['payload']['outputs']

                    box = BoxCode(python_code, node_name, n_inputs, n_outputs,
                                  d_json, depend, params, False, project_id, host, serialize_outputs, response)
                    allboxes.append(box)

            for x in d_json['links']:
                orig_box_id = d_json['links'][x]['from']['nodeId']
                orig_input_port = d_json['links'][x]['from']['portId']

                dest_box_id = d_json['links'][x]['to']['nodeId']
                dest_input_port = d_json['links'][x]['to']['portId']

            # need locate orig_box
                orig_box = getboxby_name(orig_box_id, allboxes)
                orig_id = getportid_to_index(orig_input_port)

            # need locate dest_box
                dest_box = getboxby_name(dest_box_id, allboxes)
                dest_id = getportid_to_index(dest_input_port)
                if dest_box is not None and orig_box is not None and  dest_box.box_id != orig_box.box_id:
                    valid_boxes.append(dest_box)
                    valid_boxes.append(orig_box)

                    input_port = InputPort(orig_input_port, int(orig_id)-1, orig_box, int(dest_id) - 1)

                    dest_box.inputs.append(input_port)

        else:
            if allboxes is None:
                allboxes = []

            r = requests.get(host+'/projects/get_internal?id='+project_id)
            d_json = json.loads(r.json()['json'])
            # DO it easy first we start with changed boxes
            # and existing boxes

            # at this point we can clear all input ports and recreate again
            # we trust in hasChange recibed from fronted
            clear_all_input_ports(allboxes)

            changedBox = []
            # this loop only analize changed boxes
            for x in d_json['nodes']:
                box = getboxby_name(x, allboxes)
                if box:
                    if ((d_json['nodes'][x]['properties']['payload']['hasChange'] == 'True') or
                        (d_json['nodes'][x]['properties']['payload']['hasChange'] == 'true') or
                        (d_json['nodes'][x]['properties']['payload']['hasChange'] == True)) :
                        node_name = x
                        python_code = d_json['nodes'][x]['properties']['payload']['python_code']
                        n_inputs = d_json['nodes'][x]['properties']['payload']['n_input_ports']
                        n_outputs = d_json['nodes'][x]['properties']['payload']['n_output_ports']
                        depend = d_json['nodes'][x]['properties']['payload']['depen_code']
                        params = ''
                        serialize_utputs = ''
                        if 'parameters' in d_json['nodes'][x]['properties']['payload']:
                            params = d_json['nodes'][x]['properties']['payload']['parameters']

                        if 'outputs' in d_json['nodes'][x]['properties']['payload']:
                            serialize_outputs = d_json['nodes'][x]['properties']['payload']['outputs']

                        box.setChangedBox(python_code, node_name, n_inputs, n_outputs,
                                          d_json, depend, params, False, project_id, host, serialize_outputs)

                        # save changed box and existing maybe need latter
                        changedBox.append(box)
                        print('Caja cambiada')

            # this loop analize new boxes
            # existing boxes and NOT changed then nothing to do
            newboxes = []
            for x in d_json['nodes']:
                box = getboxby_name(x, changedBox)
                box_exit = getboxby_name(x, allboxes)
                if box is None and box_exit is None:
                    # we can continue
                    node_name = x
                    python_code = d_json['nodes'][x]['properties']['payload']['python_code']
                    n_inputs = d_json['nodes'][x]['properties']['payload']['n_input_ports']
                    n_outputs = d_json['nodes'][x]['properties']['payload']['n_output_ports']
                    depend = d_json['nodes'][x]['properties']['payload']['depen_code']
                    params = ''
                    serialize_outputs = ''
                    if 'parameters' in d_json['nodes'][x]['properties']['payload']:
                        params = d_json['nodes'][x]['properties']['payload']['parameters']

                    if 'outputs' in d_json['nodes'][x]['properties']['payload']:
                        serialize_outputs = d_json['nodes'][x]['properties']['payload']['outputs']

                    box = BoxCode(python_code, node_name, n_inputs, n_outputs,
                                  d_json, depend, params, False, project_id, host, serialize_outputs, response)
                    allboxes.append(box)
                    newboxes.append(box)
                else:
                    print("Nothing to do")

            # at this point we can regenerate all links again
            for x in d_json['links']:
                orig_box_id = d_json['links'][x]['from']['nodeId']
                orig_input_port = d_json['links'][x]['from']['portId']

                dest_box_id = d_json['links'][x]['to']['nodeId']
                dest_input_port = d_json['links'][x]['to']['portId']

            # need locate orig_box
                orig_box = getboxby_name(orig_box_id, allboxes)
                orig_id = getportid_to_index(orig_input_port)

            # need locate dest_box
                dest_box = getboxby_name(dest_box_id, allboxes)
                dest_id = getportid_to_index(dest_input_port)
                if dest_box is not None and orig_box is not None and  dest_box.box_id != orig_box.box_id:
                    valid_boxes.append(dest_box)
                    valid_boxes.append(orig_box)

                    input_port = InputPort(orig_input_port, int(orig_id)-1, orig_box)
                    dest_box.inputs.append(input_port)

            # now need analize changedboxes and set run status as 'init'
            # this maybe can do better algoritm
            hasmore = True
            analizedbox = []
            while hasmore:
                hasmore = False
                for box in allboxes:
                    for inputlink in box.inputs:
                        if ((getboxby_name(inputlink.parentBox.box_id, changedBox) or
                             getboxby_name(inputlink.parentBox.box_id, newboxes)) and
                            (getboxby_name(box.box_id, analizedbox) is None)):
                           # then this box need to be re-run
                           # this box not changed but need to be retrained some parent change
                           # or some parent is new box
                               box.setStatus('INIT')
                               changedBox.append(box)
                               hasmore = True
                               analizedbox.append(box)
                               break

        # we can remove all empty boxes not conected with input
        # and outputs
        for x in allboxes:
            if getboxby_name(x.box_id, valid_boxes) is None:
                x.setStatus('STAND-BY')

        if task == 'ALL':
            # if we retrain ALL then clean an retrain
            pendingTrain = True
            while pendingTrain:
                pendingTrain=False
                for x in allboxes:
                    if ((x.isRunned()==False) and
                        (not getboxby_name(x.box_id, valid_boxes) is None)):
                        x.run()
                        pendingTrain=True
        else:
            print("TRAY TO TRAIN TASK -->", task, file=sys.stderr)
            to_trainbox = getboxby_name(task, allboxes)
            to_trainbox.run()

            # set in standby all boxes init
            # means user changed but not need to run becausethey want to run another circuit
            for x in allboxes:
                if x.getStatus() == 'INIT':
                    x.setStatus('STAND-BY')

        if response == False:
            url = host+'/projects/set_status?id='+project_id+'&stat=OK&error=NONE'+'&data='+json.dumps(d_json)
            requests.get(url)
    except Exception as e:
        print("ERROR", e, file=sys.stderr)
        traceback.print_exc()
        if response == False:
            for x in allboxes:
                if x.getStatus() == 'INIT':
                    print("SET STAND-BY")
                    x.setStatus('STAND-BY')
            url = host+'/projects/set_status?id='+project_id+'&stat=ERROR'+'&error='+str(e)+'&data='+json.dumps(d_json)

            requests.get(url)
            print("END WITH ERROR", e)
            traceback.print_exc()

    return allboxes

def get_key_from_redis(project_id):
    r = requests.get(host+'/projects/get_status?id='+project_id)

    ret = r.json()
    print(ret['status'])
    d_json = json.loads(ret['status'])
    print(d_json)
    if d_json['project_stat'] == 'PENDING':
        return ret['task']

    return False

def run_str_code(func):
    global ret
    ret = []
    LOC1 = """def """+func+"""(num):"""
    LOC = """
    num*2
    print("hola")
    return num
"""
    exec(LOC1+LOC)

    return ret

def getOutput(allboxes, box_id, port):
    return "ok"

if __name__ == '__main__':
    allproject = None
    project_id = sys.argv[1]
    host = sys.argv[2]
    print(sys.argv[3])
    saver_host = sys.argv[3]
    saver_x = saver_c.Redis(host=saver_host, port=6379, db=0)

    print("Starting project", project_id, host)
    while True:
        print("ASK")
        try:
            task=get_key_from_redis(project_id)
            print("aaa", task)
            if task:
                print("NEW TASK", host)
                allproject = run_celery_project(allproject, project_id, task, host, None, False)
            else:
                print("NOTHING TO DO", task)
            time.sleep(1)
        except Exception as e:
            print('Error', e)
            time.sleep(1)
            pass