class WSDispatcher():

    _structures = None
    
    """
    """
    def __init__(self, source='espol'):
        self._source = source
        if source == 'espol':
            self._structures = get_structures_espol()
            C_f = 5
            C_k = 3
            m = 1.25
        elif source == 'kuleuven':
            self._structures = get_structures_kuleuven()
            C_f = 4
            C_k = 2
            m = 2
        self.academic_clusterer = AcademicClusterer( self._structures['core_courses'],
                                                     self._structures['conval_dict'],
                                                     self._structures['factors_dict'], 
                                                     self._structures['_programs'],
                                                     source=source,
                                                     C_f=C_f,
                                                     C_k=C_k,
                                                     m=m )
        self._start_year = -1 
        self._end_year = 1
        
        self.init_estimator()
        
    def init_estimator(self):
        self.academic_estimator = AcademicFailureEstimator(self.academic_clusterer)
        self.academic_estimator.init_semesters_classifier_fn()
        self.academic_estimator.init_students_classifier_fn()
        if self._source == 'kuleuven':
            self.academic_estimator.init_classifier_fn()
        
        
    @property
    def structures(self):
        return self._structures
    
    """
    """
    @property
    def students(self):
        return json_dumps( list( self.structures['population_IDs'] ) )
    
    """
    """
    def risk(self, json_input):
        try:
            _root = json_input
            semester = [course['id']  for course in _root['courses']]
            student_ID = int( _root['student'][0]['id'] )
            
            start_year = _root['data'][0]['from']
            end_year = _root['data'][0]['to']
            if self._start_year != start_year or self._end_year != end_year:
                self._start_year = start_year
                self._end_year = end_year
                self.academic_clusterer.set_ha_df(start_year=start_year, end_year=end_year)
                self.init_estimator()
                #print(self.academic_clusterer.rates)
                
            
            risk, quality = self.academic_estimator.predict( student_ID=student_ID,
                                                             semester=semester
                                                             )
        except:
            import traceback
            traceback.print_exc()
            risk, quality = (0,0)
        return json_dumps( {'risk':risk,'quality':quality} )
 def init_estimator(self):
     self.academic_estimator = AcademicFailureEstimator(self.academic_clusterer)
     self.academic_estimator.init_semesters_classifier_fn()
     self.academic_estimator.init_students_classifier_fn()
     if self._source == 'kuleuven':
         self.academic_estimator.init_classifier_fn()