a
    3f                     @   s   d dl Z d dlmZ d dlmZmZ d dlmZmZ d dl	m
Z
 d dlmZ d dlmZ dd	lT dd	lT d d	lT d d	lT d d	lT d
d Zdd ZdddZdd Zdd Zdd Ze eZeejejdZdd Z dS )    N)BackgroundScheduler)ProcessPoolExecutorThreadPoolExecutor)register_eventsregister_job)DjangoJobExecution)CronTrigger)settings   )*c                  C   s(   t j } | D ]}|jrt|j qd S )N)Blocksobjectsallis_autonomousZautonomous_flight_time_planningid)blocksblock r   -/var/www/html/django/DPS/gardens/scheduler.pyautonomous_flights   s
    
r   c                  C   sR   t j  t jdd } tjjdd}|D ]$}|j| jt jj	dk r(t
|j q(d S )N   )minutesr
   )statustzinfo)datetimenow	timedeltaFlightsr   filter
start_datereplacetimezoneutcZreschedule_flightr   )Z	late_timeflightsZchecked_flightr   r   r   reshedule_missed_flights   s    r%   :	 c                 C   s   t j|  dS )aN  
  This job deletes APScheduler job execution entries older than `max_age` from the database.
  It helps to prevent the database from filling up with old historical records that are no
  longer useful.
  
  :param max_age: The maximum length of time to retain historical job execution records.
                  Defaults to 7 days.
  N)r   r   delete_old_job_executions)max_ager   r   r   r'   #   s    	r'   c                  C   s   t j D ]} | jdkrNtjj| jdD ]"}tjj|jdD ]}t| q<q(q
| jdkrtjj| jdD ]"}tjj|jdD ]}t	| q|qhq
| jdkr
q
d S )Nr
   block_id)tree_id      )
r   r   r   cultivar_idTreesr   r   PhotosZappleDetectionModelZpearDetectionModel)r   treeZphotosr   r   r   machine_learning.   s    


r2   c                  C   sv  t jjdd} | D ]\}tjj|jd}|D ]@}tjj|jjd r,tjj|jjdd}|D ]}t	jj|jdd}|D ]v}|j
tj jtjjd	k r|tj jtjjd	|_
d
|_|  td d|_tj jtjjd	|_|  q|d}tjj|jjd}	tjj|	jd}
t	jj|jdd t	jj|jd kr`d|_tj jtjjd	|_|  q`q,q,qd S )NT)is_simulated)enterprise_idr)   r   )r*   r   r
   )
mission_idr   r   r,   <   r-   zhttps://www.allrecipes.com/thmb/YusIoqehzkB6YjCPzd9hhFswddw=/750x0/filters:no_upscale():max_bytes(150000):strip_icc():format(webp)/GettyImages-144476015-3dcb5c523152449caf604e8bc551f5a8.jpg)r   )r5   )Enterprisesr   r   Devicesr   Dronesr   existsMissionsr   r    r   r   r!   r"   r#   r   savetimesleepend_dater   getr/   countclosed_date)Zenterprises_simulated
enterpriseZdevicesZdeviceZmissionsmissionr$   ZflightZexample_photo_urlr   Ztreesr   r   r   simulate_working_drones?   s4    




	*rE   c                  C   s   t j } | D ]n}|jtjtjjjtjjdtj	dd k r|j
}ddlm} d}d}tj}|g}||||| |  qdS )	zJ
  This job deletes invite codes older than 24 hours from the database.
  r   r
   )daysr   )	send_mailu*   Reģistrācijas koda termiņš ir beidziesu   Jūsu reģistrācijas koda termiņš ir beidzies. Lūdzu, sazinieties ar sava uzņēmuma īpašnieku, lai saņemtu jaunu saiti.N)TemporaryUserr   r   invite_timer   r   r"   r#   r!   r   emaildjango.core.mailrG   r	   EMAIL_HOST_USERdelete)ZinvitesZinviterJ   rG   subjectmessageZ
email_fromrecipient_listr   r   r   delete_invite_codesl   s    
.rQ   )r"   c                   C   s   t jr t  tdtj tjtt	dddddd t
d tjtt	d	d
d
ddddd t
d tjtt	dddddd tt t  d S )NZapschedulerz*/10)minuter%   r
   T)triggerr   max_instancesreplace_existingz%Added job 'reshedule_missed_flights'.mon00)Zday_of_weekhourrR   r'   z.Added weekly job: 'delete_old_job_executions'.z*/5)secondrQ   )r	   DEBUGloggingbasicConfig	getLoggersetLevel	scheduleradd_jobr%   r   loggerinfor'   rQ   r   startr   r   r   r   rc      sD    
	rc   )r&   )!r[   !apscheduler.schedulers.backgroundr   apscheduler.executors.poolr   r   django_apscheduler.jobstoresr   r   Zdjango_apscheduler.modelsr   Zapscheduler.triggers.cronr   django.confr	   modelsZflight_planning_algorithmZ/api.functionsNeuralNetworks.appleDetectionModelZ.api.functionsNeuralNetworks.pearDetectionModelZ.api.functionsNeuralNetworks.scabDetectionModelr   r%   r'   r2   rE   rQ   r]   __name__ra   SCHEDULER_CONFIG	TIME_ZONEr_   rc   r   r   r   r   <module>   s(   
-
