
    i1                         S SK Jr  S SKJr  S SKJrJr  SSKJrJrJ	r	J
r
  SSKJr  S SKr\R                  " \5      r\S 5       rS	 r\S
 5       r\S 5       r\S\4S j5       r\S 5       r\S 5       rg)    )shared_tasktimezone)datetime	timedelta   )UserPatternScheduledNotificationBurnoutAnalysisBurnoutNotificationPushNotificationServiceNc                  $    [         R                  S5        [        R                  R	                  SSS9R                  5       n SnU  H  n[        U5      nX-  nM     [         R                  SU S[        U 5       S35        S	U[        U 5      [        R                  " 5       R                  5       S
.$ ! [         aR  n[         R                  SU 35        S[        U5      [        R                  " 5       R                  5       S.s SnA$ SnAff = f)z2Schedule all notifications for the day at midnightz&Starting daily notification schedulinguser_idT)flatr   z
Scheduled z notifications for z userssuccess)statusnotifications_scheduledusers_processed	timestampz'Error in schedule_daily_notifications: errorr   r   r   N)loggerinfor	   objectsvalues_listdistinct%schedule_user_notifications_for_todaylenr   now	isoformat	Exceptionr   str)user_idsscheduled_countr   countes        !D:\TimeStamps\ml_service\tasks.pyschedule_daily_notificationsr)   
   s    
<= &&22942HQQSG9'BE$O   	j 11DS]OSYZ[  '6"8}!113	
 	
  
>qcBCV!113
 	

s   B0B3 3
D=AD
D
Dc           	      J    [         R                  R                  U S9nSnU GHI  nUR                  n[	        5       R                  US5      n[        R                  " 5       R                  5       n[        R                  " Xe5      nU[        R                  " 5       :  d  M~  [        R                  R                  U UR                  USS9R                  5       nU(       a  M  [        R                  [!        U 5      UR                  /US9n	[        R                  R#                  U UR                  UU	R$                  S9  US-  n[&        R)                  S	U  S
UR                   SU 35        GML     U$ ! [*         a%  n
[&        R-                  SU  SU
 35         Sn
A
gSn
A
ff = f)z(Schedule notifications for specific userr   r      F)r   question_typescheduled_timeis_sent)argseta)r   r-   r.   celery_task_idr   z Scheduled notification for user z, z at z(Error scheduling notifications for user z: N)r	   r   filteraverage_timer   subtract_minutesr   r    dater   combiner
   r-   existssend_notification_at_exact_timeapply_asyncr#   createidr   r   r"   r   )r   patternsr%   patterntypical_timenotification_timetodaynotification_datetimeexistingtaskr'   s              r(   r   r   )   s   -&&--g->G"//L 7 9 J J<Y[ \ LLN'')E$,$4$4U$N! %x||~5088??#")"7"7#8!	 @ 
 &(   x:FF!'lG,A,AB1 G D *1188 '&-&;&;'<'+ww	 9  $q(OKK"B7)2gNcNcMddhi~h  !A  BG  J  ?y1#NOs%   BE3 !=E3 "BE3 3
F"=FF"c                     [        5       n[        R                  R                  U US9nUR	                  X5      (       d  UR
                  R                  S5      nUR                  XU5      n[        R                  R                  U U[        R                  " 5       R                  5       SS9R                  S[        R                  " 5       S9  U(       aC  [        R                  SU  SU 35        S	U U[        R                  " 5       R!                  5       S
.$ [        R#                  SU  SU 35        SU USS.$ [        R                  SU  SU 35        SSU US.$ ! [        R$                   a%    [        R#                  SU  SU 35        SSU US.s $ [&         a2  n[        R#                  SU 35        S[)        U5      U US.s SnA$ SnAff = f)z)Send notification at exact scheduled time)r   r-   z%H:%MF)r   r-   scheduled_time__dater/   T)r/   sent_atz'Successfully sent notification to user z for r   )r   r   r-   rG   z$Failed to send notification to user failedzPush notification failed)r   r   r-   r   zSkipped notification for user z - already completed skippedzUser already completed action)r   reasonr   r-   zPattern not found for user z, question_type r   zPattern not found)r   r   r   r-   z*Error in send_notification_at_exact_time: N)r   r	   r   getuser_already_did_actionr4   strftimesend_smart_reminderr
   r3   r   r    r6   updater   r   r!   r   DoesNotExistr"   r#   )r   r-   push_servicer>   typical_time_strr   r'   s          r(   r9   r9   Z   s   B
.0 %%))' * 
 33GKK&33<<WE"66wO_`G "))00+%-\\^%8%8%:	 1 
 f   
 EgYeTaSbcd'&%2'||~779	  CG9ER_Q`ab&&%27	  KK8	AVWdVefg#9"!.	  ## 
27);KM?[\(*	
 	
  
A!EFV*	
 	

s0   DE !E ;!E 6G	G'GGGc                      [         R                  " 5       [        SS9-
  n [        R                  R                  U S9nUR                  5       nUR                  5         [        R                  SU S35        SU[         R                  " 5       R                  5       S.$ ! [         aR  n[        R                  SU 35        S	[        U5      [         R                  " 5       R                  5       S
.s SnA$ SnAff = f)z$Clean up old scheduled notifications   days)created_at__ltzCleaned up z old scheduled notificationsr   )r   deleted_countr   z.Error in cleanup_old_scheduled_notifications: r   r   N)r   r    r   r
   r   r3   r&   deleter   r   r!   r"   r   r#   )cutoff_dateold_notificationsr&   r'   s       r(   #cleanup_old_scheduled_notificationsr\      s    
llnya'88199@@& A 
 "'')  "k%(DEF  "!113
 	
  
EaSIJV!113
 	

s   BB 
C:(AC5/C:5C:r   c                     SSK Jn  U" 5       nUR                  U 5      nSU [        UR	                  S0 5      5      [
        R                  " 5       R                  5       S.$ ! [         aS  n[        R                  SU 35        SU [        U5      [
        R                  " 5       R                  5       S.s S	nA$ S	nAff = f)
z-Legacy task - kept for backward compatibilityr   )	MLServicer   r=   )r   r   patterns_learnedr   z%Error in analyze_user_patterns_task: r   )r   r   r   r   N)servicesr^   analyze_user_patternsr   rK   r   r    r!   r"   r   r   r#   )r   r^   
ml_serviceresultsr'   s        r(   analyze_user_patterns_taskrd      s    
'[
227;   #GKK
B$? @!113	
 	
  
<QC@AV!113	
 	

s   AA! !
B>+AB93B>9B>c            
          SSK Jn   [        R                  S5        SSKJn  SSKJn  UR                  R                  SS9nSnU H  n UR                  R                  US	9nUR                  (       d  M0   U " [        UR                  5      5      nUR                  S
S9nU(       d  Md  [         R                  R#                  UR                  [$        R&                  " 5       R)                  5       US   US   US   US   S9  US   S;   a)  [*        R-                  [        UR                  5      U5        US-  nM     [        R                  SU S35        SU[$        R&                  " 5       R/                  5       S.$ ! UR                   a     GML  f = f! [0         aR  n	[        R3                  SU	 35        S[        U	5      [$        R&                  " 5       R/                  5       S.s Sn	A	$ Sn	A	ff = f)z(Analyze all users for burnout risk dailyr   )BurnoutDetectorzStarting daily burnout analysisr   )User)UserSettingsT)	is_active)user   rU   
risk_scoreseverityrisk_factorsrecommendations)r   r6   rl   rm   rn   ro   )mediumhighcriticalz	Analyzed z users for burnoutr   )r   users_analyzedr   z Error in analyze_burnout_daily: r   r   N)burnout_detectorrf   r   r   user.modelsrg   userSettings.modelsrh   r   r3   rK   burnout_notifications_enabledrP   r#   r<   analyze_userr   r;   r   r    r6   send_burnout_notificationdelayr!   r"   r   )
rf   rg   rh   usersanalyzed_countrj   settingsdetectoranalysisr'   s
             r(   analyze_burnout_dailyr      s   7
556 	%4##d#3D'//333>== > 's477|4H,,",5Hx''.. GG!,,.'5%j1!).!9$,->$? /  J'+II-33CL(K!#7 : 	i//ABC  ,!113
 	
5  ,, @  
7s;<V!113
 	

sJ   AF 
*E>42F *CF >FF FF 
G2 AG-'G2-G2c           
          SSK Jn  SSKJn  US   S:X  a  SnSUS	    S
3nOUS   S:X  a  SnSnOSnSUS	    S3nUR	                  S/ 5      nU(       a  USUS   S    3-  n[
        R                  R                  U SUUUS	   U(       a  US   O0 S9nU" 5       n[        R                  R                  U S9R                  5       n	U	(       ak  SSKJn
  U
R                  R                  U SS9R                  5       nU(       a6  UR                  U UUS[        US	   5      [        UR                  5      SS.S9  [         R#                  SU  35        SU US	   UR$                  " 5       R'                  5       S.$ ! [(         aN  n[         R+                  S U 35        S![        U5      WR$                  " 5       R'                  5       S".s S#nA$ S#nAff = f)$z!Send burnout notification to userr   r   r   r   rm   rr   u!   ⚠️ High Burnout Risk DetectedzYour work patterns show rl   z+/100 burnout risk. Take action immediately.rq   u   ⚠️ Consider Taking a BreakzWYour recent work patterns suggest high burnout risk. Consider using available balances.u   ℹ️ Burnout AlertzBurnout risk detected: z!/100. Monitor your work patterns.ro   u   

💡 Suggestion: actionburnout_alert)r   notification_typetitlemessagerl   recommendationr+   )UserFCMTokenT)r   ri   burnout)typerl   notification_idr   )r   bodydataz"Sent burnout notification to user r   )r   r   rl   r   z$Error in send_burnout_notification: r   r   N)django.utilsr   rQ   r   rK   r   r   r;   r	   r3   firstmodelsr   send_notificationr#   r<   r   r   r    r!   r"   r   )r   r   r   r   r   r   ro   notificationrQ   	fcm_tokenr   	token_objr'   s                r(   ry   ry     s   A
)9 J:-7E-h|.D-EEpqDj!V+4ElD*E,Xl-C,DDefD #,,'8"=+OA,>x,H+IJJD +2299--1@?1-b : 
 /0''..w.?EEG	,$,,33Gt3TZZ\I.. /&)(<*@&A+.|+?-6		 / 
 	8	BC  "<0!113	
 	
  
;A3?@V!113
 	

s   E4E7 7
GAG
G
G)celeryr   r   r   r   r   r   r	   r
   r   r   rQ   r   logging	getLogger__name__r   r)   r   r9   r\   r#   rd   r   ry        r(   <module>r      s     ! ( \ \ 1 			8	$
 
</b D
 D
L 
 
< 
 
 
. 9
 9
v C
 C
r   