
    (^i?                    x   d dl mZ d dlmZ d dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ erd dlZd dlmZ d dlmZ n e
d      Z e
d      Z e
d      Z ej:                  e      ZdZ  G d dejB                        Z"ddZ#ddZ$ddZ%ddZ&y)    )annotations)datetimeN)TYPE_CHECKING)logging)_LazyImport)distribution_to_json)json_to_distribution)DuplicatedStudyError)UpdateFinishedTrialError)BaseStorage)StudyDirection)FrozenTrial)
TrialState)api_pb2)api_pb2_grpcz,optuna.storages._grpc.auto_generated.api_pb2z1optuna.storages._grpc.auto_generated.api_pb2_grpcgrpcz%Y-%m-%d %H:%M:%S.%fc                     e Zd ZddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZ		 	 	 	 	 	 ddZ
	 	 	 	 	 	 dd	Z	 	 	 	 	 	 dd
Z	 	 	 	 	 	 d dZ	 	 	 	 	 	 d!dZ	 	 	 	 	 	 d"dZ	 	 	 	 	 	 d#dZ	 	 	 	 	 	 d$dZ	 	 	 	 	 	 d%dZ	 	 	 	 	 	 d&dZ	 	 	 	 	 	 d'dZ	 	 	 	 	 	 d(dZ	 	 	 	 	 	 d)dZy)*OptunaStorageProxyServicec                D    || _         t        j                         | _        y )N)_backend	threadingLock_lock)selfstorages     _/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/optuna/storages/_grpc/servicer.py__init__z"OptunaStorageProxyService.__init__$   s    ^^%
    c                   |j                   D cg c]5  }|t        j                  k(  rt        j                  nt        j                  7 }}|j
                  }	 | j                  j                  ||      }t        j                        S c c}w # t        $ r>}|j                  t        j                  j                  t        |             Y d }~]d }~ww xY w)N)
directions
study_namecodedetailsstudy_id)r    r   MINIMIZEr   MAXIMIZEr!   r   create_new_studyr
   abortr   
StatusCodeALREADY_EXISTSstrCreateNewStudyReply)r   requestcontextdr    r!   r&   es           r   CreateNewStudyz(OptunaStorageProxyService.CreateNewStudy(   s     ''
 ()G,<,<'<N##.BYBYY

 
 ''
	O}}55Xb5cH **H==
 $ 	OMMt==s1vMNN	Os   :BB 	C4CCc                4   |j                   }	 | j                  j                  |       t        j                         S # t        $ rQ}|j	                  t
        j                  j                  t        |             Y d }~t        j                         S d }~ww xY wNr"   )r&   r   delete_studyKeyErrorr*   r   r+   	NOT_FOUNDr-   r   DeleteStudyReplyr   r/   r0   r&   r2   s        r   DeleteStudyz%OptunaStorageProxyService.DeleteStudy9   s|    
 ##	JMM&&x0 ''))  	JMMt88#a&MII''))	Js   = 	B4BBc                   	 | j                   j                  |j                  |j                  t	        j
                  |j                               t        j                         S # t        $ rQ}|j                  t        j                  j                  t        |             Y d }~t        j                         S d }~ww xY wr5   )r   set_study_user_attrr&   keyjsonloadsvaluer7   r*   r   r+   r8   r-   r   SetStudyUserAttributeReplyr   r/   r0   r2   s       r   SetStudyUserAttributez/OptunaStorageProxyService.SetStudyUserAttributeE   s    
	JMM--  '++tzz'--/H
 1133  	JMMt88#a&MII1133	J   AA$ $	B>-4B99B>c                   	 | j                   j                  |j                  |j                  t	        j
                  |j                               t        j                         S # t        $ rQ}|j                  t        j                  j                  t        |             Y d }~t        j                         S d }~ww xY wr5   )r   set_study_system_attrr&   r>   r?   r@   rA   r7   r*   r   r+   r8   r-   r   SetStudySystemAttributeReplyrC   s       r   SetStudySystemAttributez1OptunaStorageProxyService.SetStudySystemAttributeR   s    
	JMM//  '++tzz'--/H
 3355  	JMMt88#a&MII3355	JrE   c                   	 | j                   j                  |j                        }t        j                        S # t        $ r>}|j	                  t
        j                  j                  t        |             Y d }~Xd }~ww xY w)Nr"   r%   )r   get_study_id_from_namer!   r7   r*   r   r+   r8   r-   r   GetStudyIdFromNameReplyr:   s        r   GetStudyIdFromNamez,OptunaStorageProxyService.GetStudyIdFromName_   sj    
	J}};;G<N<NOH ..AA  	JMMt88#a&MII	Js   %= 	B4A??Bc                   |j                   }	 | j                  j                  |      }J t        j                  |      S # t        $ r>}|j	                  t
        j                  j                  t        |             Y d }~\d }~ww xY w)Nr"   )r!   )r&   r   get_study_name_from_idr7   r*   r   r+   r8   r-   r   GetStudyNameFromIdReply)r   r/   r0   r&   namer2   s         r   GetStudyNameFromIdz,OptunaStorageProxyService.GetStudyNameFromIdj   s}    
 ##	J==77AD ..$??  	JMMt88#a&MII	Js   A 	B
4BB
c                   |j                   }	 | j                  j                  |      }J t        j                  |D cg c]5  }|t        j                  k(  rt        j                  nt        j                  7 c}      S # t        $ r>}|j	                  t
        j                  j                  t        |             Y d }~d }~ww xY wc c}w )Nr"   )r    )r&   r   get_study_directionsr7   r*   r   r+   r8   r-   r   GetStudyDirectionsReplyr   r'   r(   )r   r/   r0   r&   r    r2   r1   s          r   GetStudyDirectionsz,OptunaStorageProxyService.GetStudyDirectionsx   s    
 ##	J;;HEJ %%%.. $ %&)@)@$@  gFVFVV
 	
	  	JMMt88#a&MII	J
s   B  :C	C	4CC	c           
        	 | j                   j                  |j                        }t        j                  j                         D ci c]  \  }}|t        j                  |       c}}      S # t        $ r>}|j	                  t
        j                  j                  t        |             Y d }~d }~ww xY wc c}}w )Nr"   )user_attributes)r   get_study_user_attrsr&   r7   r*   r   r+   r8   r-   r   GetStudyUserAttributesReplyitemsr?   dumpsr   r/   r0   
attributesr2   r>   rA   s          r   GetStudyUserAttributesz0OptunaStorageProxyService.GetStudyUserAttributes   s    
	J;;G<L<LMJ 22FPFVFVFXY
US$**U"33Y
 	
  	JMMt88#a&MII	J Z   %A2 	 B<
2	B9;4B44B9c           
        	 | j                   j                  |j                        }t        j                  j                         D ci c]  \  }}|t        j                  |       c}}      S # t        $ r>}|j	                  t
        j                  j                  t        |             Y d }~d }~ww xY wc c}}w )Nr"   )system_attributes)r   get_study_system_attrsr&   r7   r*   r   r+   r8   r-   r   GetStudySystemAttributesReplyr[   r?   r\   r]   s          r   GetStudySystemAttributesz2OptunaStorageProxyService.GetStudySystemAttributes   s    
	J==g>N>NOJ 44HRHXHXHZ[*#usDJJu$55[
 	
  	JMMt88#a&MII	J \r`   c                   | j                   j                         }t        j                  |D cg c]  }t        j                  |j
                  |j                  |j                  D cg c]5  }|t        j                  k(  rt        j                  nt        j                  7 c}|j                  j                         D ci c]  \  }}|t        j                  |       c}}|j                  j                         D ci c]  \  }}|t        j                  |       c}}       c}}}}      S c c}w c c}}w c c}}w c c}}}}w )N)r&   r!   r    rX   rb   )studies)r   get_all_studiesr   GetAllStudiesReplyStudy	_study_idr!   r    r   r'   r(   
user_attrsr[   r?   r\   system_attrs)r   r/   r0   rg   studyr1   r>   rA   s           r   GetAllStudiesz'OptunaStorageProxyService.GetAllStudies   s!   
 --//1))  %   "__$// "'!1!1  -.1H1H,H((gN^N^^ 
 BGAQAQAWAWAY%3=3TZZ..% BGASASAYAYA['3=3TZZ..'
 	

 %'s/   8E	):D8#E	 D= " E	 E""E	8E	c                Z   |j                   }d }|j                  st        |j                        }	 | j                  j                  ||      }t        j                        S # t        $ r>}|j                  t        j                  j                  t        |             Y d }~Xd }~ww xY wNr"   )trial_id)r&   template_trial_is_none_from_proto_trialtemplate_trialr   create_new_trialr7   r*   r   r+   r8   r-   r   CreateNewTrialReply)r   r/   r0   r&   ru   rr   r2   s          r   CreateNewTrialz(OptunaStorageProxyService.CreateNewTrial   s    
 ##--.w/E/EFN	J}}55hOH **H==  	JMMt88#a&MII	Js   A# #	B*,4B%%B*c                   |j                   }|j                  }|j                  }t        |j                        }	 | j
                  j                  ||||       t#        j$                         S # t        $ rQ}|j                  t        j                  j                  t        |             Y d }~t#        j$                         S d }~wt        $ rQ}|j                  t        j                  j                  t        |             Y d }~t#        j$                         S d }~wt        $ rQ}|j                  t        j                  j                   t        |             Y d }~t#        j$                         S d }~ww xY wr5   )rr   
param_nameparam_value_internalr	   distributionr   set_trial_paramr7   r*   r   r+   r8   r-   r   FAILED_PRECONDITION
ValueErrorINVALID_ARGUMENTr   SetTrialParameterReply)r   r/   r0   rr   rz   r{   r|   r2   s           r   SetTrialParameterz+OptunaStorageProxyService.SetTrialParameter   s(   
 ##''
&;;+G,@,@A	QMM))(J@TVbc --//  	JMMt88#a&MII
 --//	 ( 	TMMtBBCPQFMSS --//  	QMMt??QMPP--//	Qs/   A- -	E764CE74DE7&4E22E7c                ,   |j                   }|j                  }	 | j                  j                  ||      }t        j                        S # t        $ r>}|j                  t        j                  j                  t        |             Y d }~Xd }~ww xY wrq   )r&   trial_numberr   'get_trial_id_from_study_id_trial_numberr7   r*   r   r+   r8   r-   r   %GetTrialIdFromStudyIdTrialNumberReply)r   r/   r0   r&   r   rr   r2   s          r    GetTrialIdFromStudyIdTrialNumberz:OptunaStorageProxyService.GetTrialIdFromStudyIdTrialNumber   s    
 ##++	J}}LL,H
 <<hOO  	JMMt88#a&MII	Js   A 	B4BBc                   |j                   }|j                  }|j                  rt        |j                        nd }	 | j                  j                  |t        |      |      }t        j                         S # t        $ r>}|j                  t        j                  j                  t        |             Y d }~Xd }~wt        $ r>}|j                  t        j                  j                  t        |             Y d }~d }~ww xY w)Nr"   )trial_updated)rr   statevalueslistr   set_trial_state_values_from_proto_trial_stater7   r*   r   r+   r8   r-   r   r~   r   SetTrialStateValuesReply)r   r/   r0   rr   r   r   r   r2   s           r   SetTrialStateValuesz-OptunaStorageProxyService.SetTrialStateValues   s    
 ##)0gnn%T	T MM@@1%8&M //mLL	  	JMMt88#a&MII' 	TMMtBBCPQFMSS	Ts#   &A9 9	D4B;;D4D  Dc                   |j                   }|j                  }|j                  }	 | j                  j	                  |||       t        j                         S # t
        $ rQ}|j                  t        j                  j                  t        |             Y d }~t        j                         S d }~wt        $ rQ}|j                  t        j                  j                  t        |             Y d }~t        j                         S d }~ww xY wr5   )rr   stepintermediate_valuer   set_trial_intermediate_valuer7   r*   r   r+   r8   r-   r   r~   r   SetTrialIntermediateValueReply)r   r/   r0   rr   r   r   r2   s          r   SetTrialIntermediateValuez3OptunaStorageProxyService.SetTrialIntermediateValue  s    
 ##||$77	TMM66xGYZ
 5577	  	JMMt88#a&MII 5577 ( 	TMMtBBCPQFMSS5577	Ts#   A 	D	 4B,,D	84DD	c                >   |j                   }|j                  }t        j                  |j                        }	 | j
                  j                  |||       t        j                          S # t        $ rQ}|j                  t        j                  j                  t        |             Y d }~t        j                          S d }~wt        $ rQ}|j                  t        j                  j                  t        |             Y d }~t        j                          S d }~ww xY wr5   )rr   r>   r?   r@   rA   r   set_trial_user_attrr7   r*   r   r+   r8   r-   r   r~   r   SetTrialUserAttributeReplyr   r/   r0   rr   r>   rA   r2   s          r   SetTrialUserAttributez/OptunaStorageProxyService.SetTrialUserAttribute  s    
 ##kk

7==)	TMM--hUC
 1133	  	JMMt88#a&MII 1133 ( 	TMMtBBCPQFMSS1133	T#   A* *	D34B??D4DDc                >   |j                   }|j                  }t        j                  |j                        }	 | j
                  j                  |||       t        j                          S # t        $ rQ}|j                  t        j                  j                  t        |             Y d }~t        j                          S d }~wt        $ rQ}|j                  t        j                  j                  t        |             Y d }~t        j                          S d }~ww xY wr5   )rr   r>   r?   r@   rA   r   set_trial_system_attrr7   r*   r   r+   r8   r-   r   r~   r   SetTrialSystemAttributeReplyr   s          r   SetTrialSystemAttributez1OptunaStorageProxyService.SetTrialSystemAttribute'  s    
 ##kk

7==)	TMM//#uE
 3355	  	JMMt88#a&MII 3355 ( 	TMMtBBCPQFMSS3355	Tr   c                $   |j                   }	 | j                  j                  |      }t        j                  t                    S # t        $ r>}|j	                  t
        j                  j                  t        |             Y d }~ad }~ww xY w)Nr"   )trial)rr   r   	get_trialr7   r*   r   r+   r8   r-   r   GetTrialReply_to_proto_trial)r   r/   r0   rr   r   r2   s         r   GetTrialz"OptunaStorageProxyService.GetTrial7  sv    
 ##	JMM++H5E $$?5+ABB  	JMMt88#a&MII	Js   A 	B4B

Bc                   |j                   }t        |j                        }|j                  }	 | j                  j                  |d      }D cg c]*  }|j                  |kD  s|j                  |v rt        |      , }	}t        j                  |	      S # t        $ r>}|j                  t        j                  j                  t        |             Y d }~d }~ww xY wc c}w )NF)deepcopyr"   )trials)r&   setincluded_trial_idstrial_id_greater_thanr   get_all_trialsr7   r*   r   r+   r8   r-   	_trial_idr   r   GetTrialsReply)
r   r/   r0   r&   r   r   r   r2   tfiltered_trialss
             r   	GetTrialsz#OptunaStorageProxyService.GetTrialsD  s    
 ## !;!;< ' = =	J]]11(U1KF 
{{22akkEW6W A
 

 %%_==  	JMMt88#a&MII	J
s   B /C!	C 4CCN)r   r   returnNone)r/   zapi_pb2.CreateNewStudyRequestr0   grpc.ServicerContextr   zapi_pb2.CreateNewStudyReply)r/   zapi_pb2.DeleteStudyRequestr0   r   r   zapi_pb2.DeleteStudyReply)r/   z$api_pb2.SetStudyUserAttributeRequestr0   r   r   z"api_pb2.SetStudyUserAttributeReply)r/   z&api_pb2.SetStudySystemAttributeRequestr0   r   r   z$api_pb2.SetStudySystemAttributeReply)r/   z!api_pb2.GetStudyIdFromNameRequestr0   r   r   zapi_pb2.GetStudyIdFromNameReply)r/   z!api_pb2.GetStudyNameFromIdRequestr0   r   r   zapi_pb2.GetStudyNameFromIdReply)r/   z!api_pb2.GetStudyDirectionsRequestr0   r   r   zapi_pb2.GetStudyDirectionsReply)r/   z%api_pb2.GetStudyUserAttributesRequestr0   r   r   z#api_pb2.GetStudyUserAttributesReply)r/   z'api_pb2.GetStudySystemAttributesRequestr0   r   r   z%api_pb2.GetStudySystemAttributesReply)r/   zapi_pb2.GetAllStudiesRequestr0   r   r   zapi_pb2.GetAllStudiesReply)r/   zapi_pb2.CreateNewTrialRequestr0   r   r   zapi_pb2.CreateNewTrialReply)r/   z api_pb2.SetTrialParameterRequestr0   r   r   zapi_pb2.SetTrialParameterReply)r/   z/api_pb2.GetTrialIdFromStudyIdTrialNumberRequestr0   r   r   z-api_pb2.GetTrialIdFromStudyIdTrialNumberReply)r/   z"api_pb2.SetTrialStateValuesRequestr0   r   r   z api_pb2.SetTrialStateValuesReply)r/   z(api_pb2.SetTrialIntermediateValueRequestr0   r   r   z&api_pb2.SetTrialIntermediateValueReply)r/   z$api_pb2.SetTrialUserAttributeRequestr0   r   r   z"api_pb2.SetTrialUserAttributeReply)r/   z&api_pb2.SetTrialSystemAttributeRequestr0   r   r   z$api_pb2.SetTrialSystemAttributeReply)r/   zapi_pb2.GetTrialRequestr0   r   r   zapi_pb2.GetTrialReply)r/   zapi_pb2.GetTrialsRequestr0   r   r   zapi_pb2.GetTrialsReply)__name__
__module____qualname__r   r3   r;   rD   rI   rM   rR   rV   r_   re   ro   rx   r   r   r   r   r   r   r   r    r   r   r   r   #   s   &>.> &> 
%	>"
*+
* &
* 
"	
*454 &4 
,	4676 &6 
.	6	B2	B &	B 
)		B@2@ &@ 
)	@
2
 &
 
)	
(
6
 &
 
-	

8
 &
 
/	

-
 &
 
$	
4>.> &> 
%	>$010 &0 
(	0&P@P &P 
7	P M3M &M 
*	M$898 &8 
0	8 454 &4 
,	4 676 &6 
.	6 C(C &C 
	C>)> &> 
 	>r   r   c                |   | t         j                  k(  rt        j                  S | t         j                  k(  rt        j                  S | t         j                  k(  rt        j                  S | t         j
                  k(  rt        j
                  S | t         j                  k(  rt        j                  S t        d|        )NzUnknown TrialState: )r   RUNNINGr   COMPLETEPRUNEDFAILWAITINGr   r   s    r   _to_proto_trial_stater   Y  s    
"""
###
!!!~~
||
"""
+E73
44r   c                |   | t         j                  k(  rt        j                  S | t         j                  k(  rt        j                  S | t         j                  k(  rt        j                  S | t         j
                  k(  rt        j
                  S | t         j                  k(  rt        j                  S t        d|        )NzUnknown api_pb2.TrialState: )r   r   r   r   r   r   r   r   r   s    r   r   r   g  s    !!!   """   !!!
3E7;
<<r   c                   i }| j                   j                         D ]&  \  }}| j                  |   j                  |      ||<   ( t	        j
                  | j                  | j                  t        | j                        | j                  | j                  r| j                  j                  t              nd| j                  r| j                  j                  t              nd| j                  j                         D ci c]  \  }}|t        |       c}}|| j                   j                         D ci c]  \  }}|t#        j$                  |       c}}| j&                  j                         D ci c]  \  }}|t#        j$                  |       c}}| j(                  j                         D ci c]  \  }}||
 c}}      S c c}}w c c}}w c c}}w c c}}w )N )rr   numberr   r   datetime_startdatetime_completedistributionsparamsrX   rb   intermediate_values)r   r[   r   to_internal_reprr   Trialr   r   r   r   r   r   strftimeDATETIME_FORMATr   r   rl   r?   r\   rm   r   )r   r   r>   rA   r|   r   s         r   r   r   u  s   Fll((* G
U))#.??FsG ==||#EKK0||>C>R>RE  ))/:XZ BGAXAXE##,,_=^` &+%8%8%>%>%@
!\ %l33
 BGBRBRBXBXBZ[JCdjj//[DIDVDVD\D\D^_jc53

5 11_<A<U<U<[<[<]^[T5T5[^% 

 \_^s   ?G6 G6 G6Gc                   | j                   r$t        j                  | j                   t              nd }| j                  r$t        j                  | j                  t              nd }| j
                  j                         D ci c]  \  }}|t        |       }}}i }| j                  j                         D ]  \  }}||   j                  |      ||<    t        | j                  | j                  t        | j                        d | j                  r| j                  nd ||||| j                   j                         D ci c]  \  }}|t#        j$                  |       c}}| j&                  j                         D ci c]  \  }}|t#        j$                  |       c}}| j(                  j                         D ci c]  \  }}||
 c}}      S c c}}w c c}}w c c}}w c c}}w )N)rr   r   r   rA   r   r   r   r   r   rl   rm   r   )r   r   strptimer   r   r   r[   r	   r   to_external_reprr   rr   r   r   r   r   rX   r?   r@   rb   r   )r   r   r   r>   rA   r   r   r   s           r   rt   rt     s   DIDXDX%..@^b 
 "" 	%11?C  <A;N;N;T;T;V-7S%!%((M  Fll((* A
U#C(99%@sA ||%ekk2$||u||%+#=B=R=R=X=X=Z[zsECE**[?D?V?V?\?\?^_ec4::e,,_<A<U<U<[<[<]^[T5T5[^ " \_^s   G  G  G" G( )r   r   r   api_pb2.TrialState.ValueType)r   r   r   r   )r   r   r   api_pb2.Trial)r   r   r   r   )'
__future__r   r   r?   r   typingr   optunar   optuna._importsr   optuna.distributionsr   r	   optuna.exceptionsr
   r   optuna.storagesr   optuna.study._study_directionr   optuna.trial._frozenr   optuna.trial._stater   r   $optuna.storages._grpc.auto_generatedr   r   
get_loggerr   _loggerr   StorageServiceServicerr   r   r   r   rt   r   r   r   <module>r      s    "       ' 5 5 2 6 ' 8 , * <AHIGRSLvD '

X
&(s> C C s>l	5=6r   