# coding: utf-8
##################################################
# TEST 1 (Control/sanity check)
#############################################
"""In guardian interactive shell with ISI_ETMY_ST1_BLND,
check that we are actually losing memory where we think.
"""

dof_list = ['X', 'Y', 'Z', 'RX', 'RY', 'RZ']
while True:
    for dof in dof_list:
        lbm = LIGOBlendManager([dof], ezca=ezca).get_cur_blend_name(dof)

# Clear loss of memory as time progressed.

##################################################
# TEST 2
#############################################
"""Again in Guardian interactive shell with ISI_ETMY_ST1_BLND
Make sure that that removing the instantiation in the loop
removes the memory leak.
"""
dof_list = ['X', 'Y', 'Z', 'RX', 'RY', 'RZ']

test_lbm = LIGOBlendManager(['X', 'Y', 'Z', 'RX', 'RY', 'RZ'], ezca=ezca)

while True:
    for dof in dof_list:
        cur_fm = test_lbm.get_cur_blend_name(dof)

# No loss of memory. As suspected, the loss comes from the
# constant instantiation of the LIGOBlendManager class


##################################################
# TEST 3
#############################################
"""In a regular guardian shell, for convenience, make a
simpler version of LIGOBlendManger to determine where 
the leak is actually coming from.

Build it in steps and test each step.
"""
gen_chan = 'ISI-ETMY_ST1_BLND_{}_CPS_CUR'
dof_list = ['X', 'Y', 'Z', 'RX', 'RY', 'RZ']

class TestClass(object):
    def __init__(self, dof_list, ezca):
        self.ezca = ezca
        self.dof_list = dof_list
        self.filt_dict = {}
        for dof in self.dof_list:
            self.filt_dict['{}_CPS'.format(dof)] = LIGOFilter(gen_chan.format(dof), self.ezca)
    def get_cur_blend_name(self, dof):
        for i in range(10):
            if self.filt_dict['{}_CPS'.format(dof)].is_engaged(i+1):
                return i+1, self.get_names(dof)[i]
    def get_names(self, dof):
        names = []
        for i in range(10):
            j = '{:02d}'.format(i)
            names.append(self.ezca.read('{}_Name{}'.format(gen_chan.format(dof), j)))
        return names
    
tc = TestClass(dof_list, ezca)
tc.get_cur_blend_name('X')
while True:
    for dof in dof_list:
        testc = TestClass([dof], ezca).get_cur_blend_name(dof)
# No change in memory usage




##################################################
# TEST 4
#############################################
from ezca.ezcaPV import EzcaPV
class TestClass(object):
    def __init__(self, dof_list, ezca):
        self.ezca = ezca
        self.dof_list = dof_list
        self.filt_dict = {}
        for dof in self.dof_list:
            self.filt_dict['{}_CPS'.format(dof)] = LIGOFilter(gen_chan.format(dof), self.ezca)
            setattr(self, '{}_DESIRED_FM'.format(dof), EzcaPV('ISI-ETMY_ST1_BLND_{}_DESIRED_FM'.format(dof), ezca=self.ezca))
    def get_cur_blend_name(self, dof):
        for i in range(10):
            if self.filt_dict['{}_CPS'.format(dof)].is_engaged(i+1):
                return i+1, self.get_names(dof)[i]
    def get_names(self, dof):
        names = []
        for i in range(10):
            j = '{:02d}'.format(i)
            names.append(self.ezca.read('{}_Name{}'.format(gen_chan.format(dof), j)))
        return names
    
tc = TestClass(dof_list, ezca)
while True:
    for dof in dof_list:
        testc = TestClass([dof], ezca).get_cur_blend_name(dof)
        
# This test did not increase memory used
# Adding a seperate blend class to setattr in init




##################################################
# TEST 5
#############################################
from ezca.ramp import Ramp
class Blend(Ramp):
    start_blend_transition = Ramp.start_ramp
    is_mixing = Ramp.is_ramping
    
class TestClass(object):
    def __init__(self, dof_list, ezca):
        self.ezca = ezca
        self.dof_list = dof_list
        self.filt_dict = {}
        for dof in self.dof_list:
            self.filt_dict['{}_CPS'.format(dof)] = LIGOFilter(gen_chan.format(dof), self.ezca)
            setattr(self, '{}_DESIRED_FM'.format(dof), EzcaPV('ISI-ETMY_ST1_BLND_{}_DESIRED_FM'.format(dof), ezca=self.ezca))
            def start_transition(nxt_number):
                print(nxt_number)
            def is_mixing(value):
                return bool(value)
            setattr(self, '{}_blend'.format(dof),
                    Blend('ISI-ETMY_ST1_BLND_{}_MIXSTATE'.format(dof), is_mixing, start_transition, self.ezca))
    def get_cur_blend_name(self, dof):
        for i in range(10):
            if self.filt_dict['{}_CPS'.format(dof)].is_engaged(i+1):
                return i+1, self.get_names(dof)[i]
    def get_names(self, dof):
        names = []
        for i in range(10):
            j = '{:02d}'.format(i)
            names.append(self.ezca.read('{}_Name{}'.format(gen_chan.format(dof), j)))
        return names
    
# Test: w/ Blend(Ramp) attr
while True:
    for dof in dof_list:
        testc = TestClass([dof], ezca).get_cur_blend_name(dof)
        
# This is it! Clear memory loss and then it stopped on sucessful ctrl-C.
# Odd, ctrl-C didn't work first few times, related?


##################################################
# TEST 6
#############################################
"""Check that is actually the setting of ezca.ramp.Ramp
that has us lose mem.
"""

gen_chan = 'ISI-ETMY_ST1_BLND_{}_MIX_STATE'
dof_list = ['X', 'Y', 'Z', 'RX', 'RY', 'RZ']

def mixing():
    pass

def blending():
    pass

from ezca.ramp import Ramp

while True:
    for dof in dof_list:
        r = Ramp(gen_chan.format(dof), mixing, blending, ezca)

# Clear loss of memory


##################################################
# TEST 7
#############################################
"""In a regular ipython shell, see if it is a guardian
specific issue.
"""

#(((Same code as above)))

# Still leaky
