
    php                         d Z ddlZddlmZ ddlmZ ddlmZ  ej                  e      Z	 e       Z
d Zd Zd Zd	 Zd
 ZddedefdZy)zH
Background cron jobs for analytics and performance score calculations.
    N)BackgroundScheduler)CronTrigger)datetimec                  l   	 t         j                  t        t        ddd      ddd       t         j                  t        t        dd	      d
dd       t         j                          t        j                  d       y# t        $ r+} t        j                  dt        |               Y d} ~ yd} ~ ww xY w)zStart the analytics cron jobs.      r   )dayhourminutemonthly_performance_scoresz%Monthly Performance Score CalculationT)functriggeridnamereplace_existing)r
   r   daily_twitter_metricszDaily Twitter Metrics Updatez(Analytics cron jobs started successfullyz$Error starting analytics cron jobs: N)	scheduleradd_job(calculate_monthly_performance_scores_jobr   daily_twitter_metrics_jobstartloggerinfo	Exceptionerrorstres    @/var/www/html/hubwallet-dev/src/marketing/apps/Analytics/cron.pystart_analytics_cronr       s    F9AAa8+8! 	 	
 	*Qq1&/! 	 	
 	>? F;CF8DEEFs   A<A? ?	B3!B..B3c                      	 t         j                          t        j                  d       y# t        $ r+} t        j                  dt        |               Y d} ~ yd} ~ ww xY w)zStop the analytics cron jobs.zAnalytics cron jobs stoppedz$Error stopping analytics cron jobs: N)r   shutdownr   r   r   r   r   r   s    r   stop_analytics_cronr#   )   sN    F12 F;CF8DEEFs   ), 	A !AA c                  0   	 t         j                  d       ddlm}   |        }|d   rt         j                  d|d           y	t         j	                  d|d           y	# t
        $ r+}t         j	                  dt        |              Y d	}~y	d	}~ww xY w)
z@Monthly cron job to calculate performance scores for all stores.z2Starting monthly performance score calculation jobr   )$calculate_monthly_performance_scoressuccessz1Monthly performance score calculation completed: messagez.Monthly performance score calculation failed: z4Error in monthly performance score calculation job: N)r   r   .src.marketing.apps.Analytics.twitter_analyticsr%   r   r   r   )r%   resultr   s      r   r   r   1   s    VHIg57)KKKFS\L]K^_`LLI&QZJ[I\]^ VKCPQF8TUUVs   AA! A! !	B*!BBc                      	 t         j                  d       ddlm}   |         t         j                  d       y# t        $ r+}t         j                  dt        |              Y d}~yd}~ww xY w)z)Daily cron job to update Twitter metrics.z)Starting daily Twitter metrics update jobr   )update_twitter_metrics_dailyz*Daily Twitter metrics update job completedz+Error in daily Twitter metrics update job: N)r   r   r(   r+   r   r   r   )r+   r   s     r   r   r   B   sX    
M?@_$&@A MB3q6(KLLMs   7: 	A.!A))A.c            	         	 g } t         j                         D ]e  }| j                  |j                  |j                  |j
                  r|j
                  j                         ndt        |j                        d       g t         j                  | dS # t        $ r8}t        j                  dt        |              dt        |      icY d}~S d}~ww xY w)z Get the status of all cron jobs.N)r   r   next_run_timer   )scheduler_runningjobszError getting cron status: r   )r   get_jobsappendr   r   r-   	isoformatr   r   runningr   r   r   )r/   jobr   s      r   get_cron_statusr5   P   s    !%%' 	CKKffBEBSBS!2!2!<!<!>Y]s{{+	 	 "+!2!2
 	
  !23q6(;<Q  !s   BB 	C-C	CCstore_id	branch_idc                    	 t         j                  d|         ddlm} ddlm}  |       } ||| |      }|j                          |d   r t         j                  d|  d|d           |S t         j                  d	|  d
|d           |S # t        $ r9}t         j                  dt        |              dt        |      dcY d}~S d}~ww xY w)zDManually trigger performance score calculation for a specific store.z<Manually triggering performance score calculation for store r   )!calculate_store_performance_score)get_db_sessionr&   z'Manual calculation completed for store z: Score overall_scorez$Manual calculation failed for store z: r'   z/Error in manual performance score calculation: F)r&   r'   N)
r   r   r(   r9   src.utils.dbr:   closer   r   r   )r6   r7   r9   r:   dbr)   r   s          r   trigger_manual_calculationr?   d   s    5RS[R\]^d/22xK

)KKA(8TZ[jTkSlmn  LL?zFS\L]K^_` 5Fs1vhOP SV445s$   A)B ,B 	C.C	C	C)N)__doc__logging!apscheduler.schedulers.backgroundr   apscheduler.triggers.cronr   r   	getLogger__name__r   r   r    r#   r   r   r5   intr?        r   <module>rI      sd     A 1 			8	$  !	F6FV"M!(5 5 5rH   