
    jJhaC                     D    d dl mZ d dlmZ d dlmZmZmZ  G d d      Zy)    )
SortedDict)
app_logger)CustomizedProgramSeq
ProgramseqExperimentConditionsc                   Z    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zy)ProgramSequenceServicec           	         t         j                  j                         }g }|D ]_  }t        |j                  |j
                  |j                  |j                  |j                  |j                        }|j                  |       a |S N)r   query!get_all_sequence_by_sequence_typer   id
content_id
program_idsequence_indexdayweekappend)selfprogram_sequencesprogram_seq_listprogram_seqpseq_objs        R/var/www/dostadmin/dostadmin/services/program_sequence/program_sequence_service.pyget_program_type_sequencesz1ProgramSequenceService.get_program_type_sequences   s{    &,,NNP,K+&&&&**  H ##H- -      c                    t         j                  j                         }i }i }t        j                  j	                         }|D ]  }|||j
                  <    |D ]g  }|j                  t         j                  j                  k(  }d|j                   d}d }	|rH|j                  |j                        }	|	s y d|j                   d|	j                   d|	j                   }|j                  |j                        ;|t        |j                   |gg      | d|	d n|	j"                  i||j                  <   ||j                     j                  |      	 Mi ||j                     |t        |j                   |gg      | d|	d n|	j"                  i||j                  <   I|||j                     |   |j                   <   j |S )NM_Prog_Exp__module_length)r   r   get_all_sequencer   get_experiment_logr   sequence_typeSequenceType
EXPERIMENTr   getexperiment_condition_idexperiment_idexperiment_group_namer   r   r   value)
r   r   formatted_program_sequenceexperiment_conditions_dictexperiment_conditions	conditionsequenceis_experiment_sequenceformatted_module_key_nameexperiment_conditions
             r   *get_module_wise_formatted_program_sequencezAProgramSequenceService.get_module_wise_formatted_program_sequence   s2   &,,==?%'"%'" 4 : : M M O.I7@&y||4 / *H&&**A*A*L*LL # +,HMM?%(@%#' %'A'E'E44($ ,./dCWCeCeBffgh|  iS  iS  hT  -U))--h.A.ABJ-z"118<=0 11@+3 CG-33C*8+>+>? +8+>+>?CC- 

C01D1DE
C 2:&55x@A4 55^D/7 GK177
C*8+>+>? .6 +8+>+>?-))+U *\ *)r   c                    t         j                  j                         }i }|D ]  }|j                  |j                        2|j
                  t        |j                  |gg      i||j                  <   P||j                     j                  |j
                        	 Ci ||j                     |j
                  t        |j                  |gg      i||j                  <   |||j                     |j
                     |j                  <    |S r   )	r   r   r#   r(   r   r   r   r   r   )r   r   r-   r1   s       r   +get_content_wise_formatted_program_sequencezBProgramSequenceService.get_content_wise_formatted_program_sequenceP   s   &,,==?%'")H)--h.A.ABJ''hkk85L4M)NC*8+>+>? +8+>+>?CCHDWDWXC01D1DEC**Jh8O7P,QRC*8+>+>?  +8+>+>?@S@ST++ *$ *)r   c                 &   |j                   d u}|j                   }|r|j                  nd }|j                  d      }||j                     j                  |      }	d }
d| d|j                   }|	2|t        j                  |       y ||vrt        j                  |       y |	j                         D ]  }|
)|j                  t        j                  j                  k(  r|}
|j                  |rt        j                  j                  nt        j                  j                  k7  rrt        j                  j!                  |j"                        }|s|j                   |k(  s|j                  |k(  s|}
 n |
s||vrt        j                  |       y |
S )N,content_wise_formatted_program_sequence_dictzDProgram Sequence Service: Program Sequence not found for content id  for user number )r*   r+   r(   r   phoner   warningerrorvaluesr%   r   r&   PROGRAMr'   r   r   	get_by_idr)   )r   formatted_program_sequencesr   
experiencespecial_content_id_listis_enrolled_in_experimentr*   r+   r9   r   program_sequence_for_contenterror_messager1   r4   s                 r   get_current_program_sequencez3ProgramSequenceService.get_current_program_sequenceh   s    %/$<$<D$H!"000IJ,,t 	 8S7V7V:8
4 I!!

#j/ 	 (,$^_i^jj{  }G  }M  }M  |N  O$!""=1  #::  /)002H,4**j.E.E.M.MM/7,%%, ''22,,44
 #7#=#=#G#G00$ 
 %(66-G(>>BWW/7,1 34 ,!88  /++r   c                    d}|j                  d      }| j                  ||||      }|| j                  |d|      }|y||fS ||j                     | j	                  ||         }	|j
                  dz   }
|	j                  |
      }|F| j                  ||j                  dz   |      }| | j                  ||j                  dz   |      }|rd}||fS )NF+module_wise_formatted_program_sequence_dict   )NN   T)r(   rG   get_first_sequence_of_a_weekr   .get_key_name_for_module_based_program_sequencer   r   )r   rA   previous_content_idrB   rC   is_module_getting_changedrI   #previous_delivered_program_sequencenext_sequencecurrent_module_sequencenext_sequence_indexs              r   get_next_program_sequencez0ProgramSequenceService.get_next_program_sequence   s7    %*!6Q6U6U97
3
 /3.O.O'#	/
+ /6 ==+Q
M $!!#<== #N!!#
 ??3Z#
 BPPSTT/334GH  ==+3881<M $ $ A A/7<<q@! ,0)899r   c                     d|j                    d}|j                  t        j                  j                  k(  r)d|j                    d|j
                   d|j                   }|S )Nr   r   r    r!   )r   r%   r   r&   r'   r*   r+   )r   r1   rB   key_names       r   rM   zEProgramSequenceService.get_key_name_for_module_based_program_sequence   s]    x}}oU+!!Z%<%<%G%GG8==/j.F.F-GqIiIiHjkHr   c                 h    |j                  d      }||j                     j                  |      }|yy)Nr9   FT)r(   r   )r   rA   r   rB   r9   sequences_for_contents         r   $is_content_exist_in_program_sequencez;ProgramSequenceService.is_content_exist_in_program_sequence   sI     8S7V7V:8
4 !M!!!

#j/ 	 !(r   c                 h   	 | j                  |||      sy |j                  d u}|j                  d      }||j                     j                  d| d      }|r?||j                     j                  d| d|j                   d|j                         }|r|}|4t        j                  d| d|j                   d|j                   d	       y t        t        |            }||   }	|	S # t        $ r?}
t        j                  d| d|j                   d|j                   d
|
        Y d }
~
y d }
~
ww xY w)NrI   r   r   r    r!   zJProgram Sequence Service: First program sequence cannot be found for week z and program id r:   .z. Error message: )is_module_valid_for_experimentr*   r(   r   r+   r   r<   r;   nextiter	Exceptionr=   )r   rA   week_numberrB   rD   rI   module_sequenceexperiment_sequencefirst_sequence_indexfirst_sequence_of_weekr=   s              r   rL   z3ProgramSequenceService.get_first_sequence_of_a_week   s   )	66+[* (2(@(@(L%+//A 8
 J%%cAk]%()  )&Q))'#}D)A)A(B!JDdDdCef $ '&9O&""`al`mm}  I  T  T  ~U  Uf  gq  gw  gw  fx  xy  z #'_(=#> %45I%J")) 	\]h\iiy  {E  {P  {P  zQ  Qb  cm  cs  cs  bt  tE  FK  EL  M 		s#   C) B7C) C) )	D125D,,D1c                     d| d|j                    d|j                   d}|j                  d      |j                     j                  |       }|r|t	        |      kD  ryy)Nr   r    r!   r"   rI   FT)r*   r+   r(   r   int)r   rA   r`   rB   r3   max_module_for_experiments         r   r\   z5ProgramSequenceService.is_module_valid_for_experiment+  s     '(}D9Q9Q8RRST^TtTtSu  vD  %E!$?$C$C9%



%!!$(A'B!D 	" %s;T7U)Ur   c                     i }i }|D ]m  }|j                   |v r||j                      }|||j                  <   n%t        |j                  |gg      ||j                   <   |j                  ||j                  <   o ||fS r   )r   r   r   r   )r   r   program_sequencecontent_sequence_mapr   sorted_program_seqs         r   create_program_seq_dictsz/ProgramSequenceService.create_program_seq_dicts8  s    !(H""&66%5h6I6I%J">F"8#:#:;8B--x899 !4!45 190G0G - )  !555r   c                 `    t        |      }|D ]  }||   }|j                  |k(  s||k(  s y y)NTF)lenr   )r   program_sequence_listr   length_of_programprogram_sequence_keyri   s         r   is_last_content_of_programz1ProgramSequenceService.is_last_content_of_programH  sD     56$9 45IJ**j8'+<<	 %: r   c                     | j                  ||      ry|D ]@  }||   }|j                  |k(  s|t        |      k(  r y||dz      }|j                  dk(  s@ y y)NTrJ   F)rr   r   rn   r   )r   ro   r   rq   ri   next_program_sequences         r   is_last_content_of_modulez0ProgramSequenceService.is_last_content_of_moduleS  st    **+@*M$9 45IJ**j8'3/D+EE(=>RUV>V(W%(,,1 %: r   c                     d}|j                  |       |j                         }g }|D ]+  }|d   }|d   }|d   }|d   }	|j                  ||	||f       - |S )Na	  
            WITH max_module_for_program_type_sequences as (
                SELECT program_id, null::integer as experiment_id, null::integer as experiment_condition_id, null as experiment_group_name, max(week) as max_module FROM programseq
                WHERE programseq.sequence_type = 'program'
                GROUP BY 1, 2, 3, 4
            ), max_module_for_experiment_type_sequences as (
                SELECT programseq.program_id, experiment_conditions.experiment_id, programseq.experiment_condition_id, experiment_conditions.experiment_group_name, experiment_conditions.value::Integer as max_module FROM programseq
                JOIN experiment_conditions
                ON experiment_conditions.id = programseq.experiment_condition_id
                AND experiment_conditions.key = 'max_module_length'
                GROUP BY 1, 2, 3, 4, 5
            ), max_module as (
                SELECT * FROM max_module_for_program_type_sequences
                UNION ALL
                SELECT * FROM max_module_for_experiment_type_sequences
            ), max_sequence_index_module_wise as (
                SELECT
                max_module.program_id, max_module.experiment_id, max_module.experiment_group_name, max_module,
                max_module.experiment_condition_id, max(programseq.sequence_index) as max_sequence_index_for_week
                FROM max_module
                JOIN programseq
                ON programseq.program_id = max_module.program_id
                AND programseq.week = max_module
                AND CASE
                		WHEN max_module.experiment_condition_id IS NULL
                		THEN programseq.experiment_condition_id IS NULL
                		ELSE programseq.experiment_condition_id = max_module.experiment_condition_id
                	END
                GROUP BY 1, 2, 3, 4, 5
            )
            SELECT
                msimw.program_id,
                msimw.experiment_id,
                msimw.experiment_group_name,
				programseq.content_id
            FROM max_sequence_index_module_wise as msimw
            JOIN programseq
            on programseq.program_id = msimw.program_id
            	AND programseq.week = msimw.max_module
            	AND programseq.sequence_index = msimw.max_sequence_index_for_week
            	AND CASE
                		WHEN msimw.experiment_condition_id IS NULL
                		THEN programseq.experiment_condition_id IS NULL
                		ELSE programseq.experiment_condition_id = msimw.experiment_condition_id
                	END
        r   rJ   rK      )executefetchallr   )
r   cursorsql	sequenceslast_contentsr1   r   r*   
group_namer   s
             r   get_last_content_for_programsz4ProgramSequenceService.get_last_content_for_programsb  sv    -\ 	sOO%	!H!!J$QKM!!J!!J  *j-!TU " r   N)__name__
__module____qualname__r   r5   r7   rG   rT   rM   rY   rL   r\   rl   rr   ru   r    r   r   r	   r	      sG      7*r*0;,z>:@ ,\6 	;r   r	   N)	sortedcontainersr   	dostadminr   dostadmin.db_modelr   r   r   r	   r   r   r   <module>r      s    '   U UW Wr   