
    b                        S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKJr  SSKJ	r	  SS	K
Jr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKrS rS r " S S\ 5      r!g)zAUtilities for dealing with AI Platform model monitoring jobs API.    )absolute_import)division)unicode_literalsN)encoding)extra_types)
list_pager)util)apis)messages)	constants)errors)model_monitoring_jobs_util)
validation)labels_util)
properties)	resources)yamlc                     UR                  5       S   n[        R                  R                  U U[        R
                  R                  R                  R                  S.SS9$ )z5Parses a endpoint ID into a endpoint resource object.locationsIdr   
projectsIdz'aiplatform.projects.locations.endpointsparams
collection	AsDictr   REGISTRYParser   VALUEScoreproject	GetOrFail)endpoint_id
region_refregions      =lib/googlecloudsdk/api_lib/ai/model_monitoring_jobs/client.py_ParseEndpointr'   (   s^    }-&				!	!"))..66@@ ; 
" 
< <    c                     UR                  5       S   n[        R                  R                  U U[        R
                  R                  R                  R                  S.SS9$ )z3Parses a dataset ID into a dataset resource object.r   r   z&aiplatform.projects.locations.datasetsr   r   )
dataset_idr$   r%   s      r&   _ParseDatasetr+   4   s^    }-&				!	!"))..66@@ : 
" 
; ;r(   c                   t    \ rS rSrSrSS jrS rS rS rS r	S	 r
S
 rS rS rS rSS jrS rS rS rSrg)ModelMonitoringJobsClient@   zOHigh-level client for the AI Platform model deployment monitoring jobs surface.Nc                    U=(       d6    [         R                  " [        R                  [        R                  U   5      U l        U=(       d    U R
                  R                  U l        U R
                  R                  U l	        X0l
        g N)r
   GetClientInstancer   AI_PLATFORM_API_NAMEAI_PLATFORM_API_VERSIONclientMESSAGES_MODULEr   0projects_locations_modelDeploymentMonitoringJobs_service_version)selfr4   r   versions       r&   __init__"ModelMonitoringJobsClient.__init__C   s`     4D22&&))'24DK ; ; ;DMKKPPDMMr(   c           
         [         R                  " SU R                  5      " 5       n/ n/ nU(       a  UR                  5        Hi  u  pgU(       d  SO
[	        U5      nUR                  UR                  5       R                  U[         R                  " SU R                  5      " US9S95        Mk     UR                  US9Ul        U(       a  UR                  5        Hi  u  pgU(       d  SO
[	        U5      nUR                  UR                  5       R                  U[         R                  " SU R                  5      " US9S95        Mk     UR                  US9Ul
        U$ )a  Construct drift thresholds from user input.

Args:
  feature_thresholds: Dict or None, key: feature_name, value: thresholds.
  feature_attribution_thresholds: Dict or None, key:feature_name, value:
    attribution score thresholds.

Returns:
  PredictionDriftDetectionConfig
<ModelMonitoringObjectiveConfigPredictionDriftDetectionConfig333333?ThresholdConfigvaluekeyrB   additionalProperties)api_util
GetMessager8   itemsfloatappendDriftThresholdsValueAdditionalPropertydriftThresholds$AttributionScoreDriftThresholdsValueattributionScoreDriftThresholds)	r9   feature_thresholdsfeature_attribution_thresholdsprediction_drift_detectionadditional_properties!attribution_additional_propertiesrD   rB   	thresholds	            r&   _ConstructDriftThresholds3ModelMonitoringJobsClient._ConstructDriftThresholdsK   ss    "*!4!4F" " (*%*002*#$C%,	$$%?&:&:&<=O=O-0/7/B/B->-1]]0<BK0M >P >N	O 3 4N3b3b4 4c 46 0%6<<>*#$C%,	)00&KK   ))*;*.--9?HJ ! K	L ? D^  DC  DC@ DC DB @ &%r(   c           
         [         R                  " SU R                  5      " 5       n/ n/ nU(       a  UR                  5        Hi  u  pgU(       d  SO
[	        U5      nUR                  UR                  5       R                  U[         R                  " SU R                  5      " US9S95        Mk     UR                  US9Ul        U(       a  UR                  5        Hi  u  pgU(       d  SO
[	        U5      nUR                  UR                  5       R                  U[         R                  " SU R                  5      " US9S95        Mk     UR                  US9Ul
        U$ )a  Construct skew thresholds from user input.

Args:
  feature_thresholds: Dict or None, key: feature_name, value: thresholds.
  feature_attribution_thresholds: Dict or None, key:feature_name, value:
    attribution score thresholds.

Returns:
  TrainingPredictionSkewDetectionConfig
CModelMonitoringObjectiveConfigTrainingPredictionSkewDetectionConfigr?   r@   rA   rC   rE   )rG   rH   r8   rI   rJ   rK   SkewThresholdsValuerM   skewThresholds#AttributionScoreSkewThresholdsValueattributionScoreSkewThresholds)	r9   rQ   rR   "training_prediction_skew_detectionrT   rU   rD   rB   rV   s	            r&   _ConstructSkewThresholds2ModelMonitoringJobsClient._ConstructSkewThresholdsu   sr    *2)<)<M* *& (*%*002*#$C%,	$$%G&9&9&;<N<N-0/7/B/B->-1]]0<BK0M =O =N	O 3 ;]:p:p4 ;q ;6(7%6<<>*#$C%,	)00.0023E3E))*;*.--9?HJ 4F 4K	L ? Km  KQ  KQ@ KQ KB(G .-r(   c                 b   U R                  X#5      nU R                  X#5      n/ nUR                   GHy  nUR                  R                  (       al  UR
                  (       a%  UR
                  UR                  R                  l        UR                  (       a%  UR                  UR                  R                  l        UR                  R                  (       al  UR                  (       a%  UR                  UR                  R                  l        UR                  (       a%  UR                  UR                  R                  l	        UR                  (       d  UR                  (       a4  [        R                  " SU R                  5      " SS9UR                  l        UR                  U5        GM|     U$ )a  Construct monitoring objective config.

Update the feature thresholds for skew/drift detection to all the existing
deployed models under the job.
Args:
  existing_monitoring_job: Existing monitoring job.
  feature_thresholds: Dict or None, key: feature_name, value: thresholds.
  feature_attribution_thresholds: Dict or None, key: feature_name, value:
    attribution score thresholds.

Returns:
  A list of model monitoring objective config.
/ModelMonitoringObjectiveConfigExplanationConfigTenableFeatureAttributes)rW   r`   )modelDeploymentMonitoringObjectiveConfigsobjectiveConfig%trainingPredictionSkewDetectionConfigr\   r^   predictionDriftDetectionConfigrN   rP   rG   rH   r8   explanationConfigrK   )r9   existing_monitoring_jobrQ   rR   rS   r_   objective_configsobjective_configs           r&   "_ConstructObjectiveConfigForUpdate<ModelMonitoringJobsClient._ConstructObjectiveConfigForUpdate   s     "&!?!?"<)-)F)F*<& 3]]		)	)	O	O-<< cE  cT  cT

*
*
P
P
_-LL sU  st  st

*
*
P
P
o		)	)	H	H%55\v  ]G  ]G

*
*
I
I
Y%EE mG  mg  mg

*
*
I
I
i	+	J	JNh  OI  OI=E=P=P=t}}>N(,>.((: /0 ^  r(   c                    [         R                  " SU R                  5      " 5       nU(       d  U(       Ga+  U(       d  U(       d  U(       d  U(       Ga  [         R                  " SU R                  5      " 5       nU	c  [        R                  " S5      eXl        [         R                  " SU R                  5      " [         R                  " SU R                  5      " U
S9S9Ul        U(       a   [        UU5      R                  5       Ul	        OU(       a+  [         R                  " SU R                  5      " US	9Ul
        OpU(       d  U(       ab  Uc  [        R                  " S
5      eUc  [        R                  " S5      eX|l        [         R                  " SU R                  5      " US9Ul        U R                  X45      n[         R                  " SU R                  5      " UUS9Ul        O;U R                  X45      n[         R                  " SU R                  5      " US9Ul        U(       a4  [         R                  " SU R                  5      " SS9UR                  l        U R"                  R%                  US9nU R&                  R(                  R+                  U5      n/ nUR,                   H;  n[.        R0                  " U5      nUR2                  Ul        UR7                  U5        M=     U$ )ax  Construct monitoring objective config.

Apply the feature thresholds for skew or drift detection to all the deployed
models under the endpoint.
Args:
  location_ref: Location reference.
  endpoint_name: Endpoint resource name.
  feature_thresholds: Dict or None, key: feature_name, value: thresholds.
  feature_attribution_thresholds: Dict or None, key: feature_name, value:
    attribution score thresholds.
  dataset: Vertex AI Dataset Id.
  bigquery_uri: The BigQuery table of the unmanaged Dataset used to train
    this Model.
  data_format: Google Cloud Storage format, supported format: csv,
    tf-record.
  gcs_uris: The Google Cloud Storage uri of the unmanaged Dataset used to
    train this Model.
  target_field: The target field name the model is to predict.
  training_sampling_rate: Training Dataset sampling rate.

Returns:
  A list of model monitoring objective config.
(ModelDeploymentMonitoringObjectiveConfig-ModelMonitoringObjectiveConfigTrainingDatasetzUTarget field must be provided if you'd like to do training-prediction skew detection.SamplingStrategy"SamplingStrategyRandomSampleConfig
sampleRaterandomSampleConfigBigQuerySource)inputUrizyData format is defined but no Google Cloud Storage uris are provided. Please use --gcs-uris to provide training datasets.zxNo Data format is defined for Google Cloud Storage training dataset. Please use --data-format to define the Data format.	GcsSource)urisModelMonitoringObjectiveConfig)trainingDatasetrh   )ri   rc   Trd   name)rG   rH   r8   r   ArgumentErrortargetFieldloggingSamplingStrategyr+   RelativeNamedatasetbigquerySource
dataFormat	gcsSourcer`   rg   rW   rj   r   .AiplatformProjectsLocationsEndpointsGetRequestr4   projects_locations_endpointsGetdeployedModelscopydeepcopyiddeployedModelIdrK   )r9   location_refendpoint_namerQ   rR   r   bigquery_uridata_formatgcs_uristarget_fieldtraining_sampling_rateobjective_config_templatetraining_datasetr_   rS   get_endpoint_reqendpointrl   deployed_modelrm   s                       r&   "_ConstructObjectiveConfigForCreate<ModelMonitoringJobsClient._ConstructObjectiveConfigForCreate   s   : !) 3 32DMM!C !E;	LH#..;T]]L N$$e  (4$3;3F3F4/#+#6#68$--$I#9$;4<0
 %273?&AAM 
",4,?,?-/'-)

) && L   && K  )4
%'/':':4==(*(!

$ .2-J-J.@*4<4G4G,dmm5
*0R5!1 &*%C%C&@"4<4G4G,dmm5=/I5K!1 
(FNFYFY=t}}GN(,G.!11C }}SS T {{77;;<LMH"11'@A)7):):&/0 2 r(   c                     [         R                  " U[        R                  " SU R                  5      " 5       R
                  5      $ )zParses create labels.ModelDeploymentMonitoringJob)r   ParseCreateArgsrG   rH   r8   LabelsValue)r9   argss     r&   _ParseCreateLabels,ModelMonitoringJobsClient._ParseCreateLabels%  s:    &&: MM	+ 	--8[: :r(   c                    ^ ^ UU 4S jn[         R                  " U[        R                  " ST R                  5      " 5       R
                  U5      $ )zParses update labels.c                  :   > TR                  T 5      R                  $ r0   )r   labels)model_monitoring_job_refr9   s   r&   	GetLabels?ModelMonitoringJobsClient._ParseUpdateLabels.<locals>.GetLabels.  s    XX./666r(   r   )r   ProcessUpdateArgsLazyrG   rH   r8   r   )r9   r   r   r   s   ``  r&   _ParseUpdateLabels,ModelMonitoringJobsClient._ParseUpdateLabels,  sF    7 ,,: MM	+ 	--8[)E Er(   c                    [        UR                  U5      n[        R                  " SU R                  5      " 5       n[
        R                  " U5      nUb*  [        R                  " SU R                  5      " US9Ul        UR                  (       a^  [        R                  " UR                  5      nU(       a6  [        R                  " U[        R                  " SU R                  5      5      nO|U R                  XR                  5       UR                  UR                   UR"                  UR$                  UR&                  UR(                  UR*                  UR,                  5
      Ul        UR                  5       Ul        UR0                  Ul        U R5                  U5      Ul        UR8                  c  SOUR8                  n[        R                  " SU R                  5      " U[        R                  " SU R                  5      " UR:                  S9UR<                  S9Ul        [        R                  " S	U R                  5      " [        R                  " S
U R                  5      " UR@                  S9S9Ul!        [        R                  " SU R                  5      " SRE                  [F        RH                  " S[K        URL                  5      -  5      5      S9Ul'        URP                  (       a  URP                  Ul)        URT                  (       a  URT                  Ul+        URX                  (       a@  SRE                  [F        RH                  " S[K        URX                  5      -  5      5      Ul-        UR\                  (       aJ  [^        R`                  " UR\                  5      n[b        Rd                  " [f        Rh                  U5      Ul5        U R                  [l        Rn                  :X  aA  U Rp                  Rs                  U Rt                  Rw                  UR                  5       US95      $ U Rp                  Rs                  U Rt                  Rw                  UR                  5       US95      $ )z*Creates a model deployment monitoring job.r   EncryptionSpec)
kmsKeyNameFModelMonitoringAlertConfig*ModelMonitoringAlertConfigEmailAlertConfig
userEmails)enableLoggingemailAlertConfignotificationChannelsrs   rt   ru   rw   'ModelDeploymentMonitoringScheduleConfig{}s  monitorIntervalQ )parent8googleCloudAiplatformV1beta1ModelDeploymentMonitoringJob)r   3googleCloudAiplatformV1ModelDeploymentMonitoringJob)<r'   r   rG   rH   r8   common_validationGetAndValidateKmsKeyencryptionSpecmonitoring_config_from_filer   	load_pathmessages_utilDictToMessageWithErrorCheckr   r   rQ   rR   r   r   r   r   r   r   rf   display_namedisplayNamer   r   anomaly_cloud_loggingemailsnotification_channelsmodelMonitoringAlertConfigprediction_sampling_rater   formatsix	text_typeintmonitoring_frequency'modelDeploymentMonitoringScheduleConfigpredict_instance_schemapredictInstanceSchemaUrianalysis_instance_schemaanalysisInstanceSchemaUrilog_ttllogTtlsample_predict_requestr   ReadInstanceFromArgsr   PyValueToMessager   	JsonValuesamplePredictInstancer   BETA_VERSIONr7   Creater   EAiplatformProjectsLocationsModelDeploymentMonitoringJobsCreateRequest)	r9   r   r   endpoint_refjob_speckms_key_namedataenable_anomaly_cloud_logginginstance_jsons	            r&   r    ModelMonitoringJobsClient.Create6  s   !$-->L""#A#'==2 4H$99$?L ( 3 34D48MM!CCO!Qh ''^^D<<=d	 << >NP <@;b;b
113T5L5L

-
-t||T=N=N


DMM4+<+<

%
%	<'h8
 %113H,,H--d3HO,0,F,F,N5TXTnTn *2*=*=$dmm+56%00<*.++7 "&!;!;+=H' (0':':DMM(+'224dmm E#<< >(?H$ 8@7J7J14==8B!LLdS)B)B%CCDF8GH4
 ##*.*F*Fh'$$+/+H+Hh(||S]]53t||;L3L%MNho""0EE

%
%'m'/'@'@


(0h$ }}	...]]!!
--
O
O!..0GO P   ]]!!
--
O
O!..0BJ P LM Mr(   c           
         [         R                  " SU R                  5      " 5       n/ n[         R                  " SU R                  5      " 5       nUR                  (       a  [        R
                  " UR                  5      nU(       aX  [        R                  " U[         R                  " SU R                  5      5      nUR                  Ul        UR                  S5        UR                  (       d  UR                  (       a~  U R                  R                  UR                  5       S9nU R                  R!                  U5      nU R#                  XR                  UR                  5      Ul        UR                  S5        UR$                  (       a"  UR$                  Ul        UR                  S5        UR(                  (       ah  [         R                  " SU R                  5      " [         R                  " SU R                  5      " UR(                  S9S9Ul        UR                  S	5        UR,                  br  UR(                  (       a  UR,                  UR*                  l        O4[         R                  " SU R                  5      " UR,                  S
9Ul        UR                  S5        UR0                  (       a  UR(                  (       d  UR,                  b  UR0                  UR*                  l        O4[         R                  " SU R                  5      " UR0                  S9Ul        UR                  S5        UR4                  (       ah  [         R                  " SU R                  5      " [         R                  " SU R                  5      " UR4                  S9S9Ul        UR                  S5        UR8                  (       at  [         R                  " SU R                  5      " SR;                  [<        R>                  " S[A        UR8                  5      -  5      5      S9Ul!        UR                  S5        URD                  (       a"  URD                  Ul#        UR                  S5        URH                  (       aQ  SR;                  [<        R>                  " S[A        URH                  5      -  5      5      Ul%        UR                  S5        U RM                  X5      n	U	RN                  (       a"  U	RP                  Ul(        UR                  S5        U(       d  [R        RT                  " S5      eU R                  [V        RX                  :X  a9  U R                  R[                  UR                  5       USR]                  U5      S9n
O8U R                  R[                  UR                  5       USR]                  U5      S9n
U R                  R_                  U
5      $ ) z)Update a model deployment monitoring job.r   -model_deployment_monitoring_objective_configsr   r   r   r   r   )r   z0model_monitoring_alert_config.email_alert_config)r   z,model_monitoring_alert_config.enable_logging)r   z3model_monitoring_alert_config.notification_channelsrs   rt   ru   rw   logging_sampling_strategyr   r   r   r   +model_deployment_monitoring_schedule_configanalysis_instance_schema_urir   r   r   zNo updates requested.,)r   r   
updateMask)r   r   r   )0rG   rH   r8   r   r   r   r   r   rf   rK   rQ   rR   r   BAiplatformProjectsLocationsModelDeploymentMonitoringJobsGetRequestr   r7   r   rn   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   needs_updater   r   NoFieldsSpecifiedErrorr   r   DAiplatformProjectsLocationsModelDeploymentMonitoringJobsPatchRequestjoinPatch)r9   r   r   model_monitoring_job_to_updateupdate_maskr   r   get_monitoring_job_reqmodel_monitoring_joblabels_updatereqs              r&   r   ModelMonitoringJobsClient.Patch  s   %-%8%8&&7 &9"K""#A#'==2 4H''^^D<<=d	 << >NP T\  TF  TF&PJK$"E"E#}}oo'446  p  8!]]../EFQUQxQx
 7 7

-
-R/$N HI373D3D$0({{


:DMM
J'22> ;; ( %? KL!!-	&& 	'AAO
  <dmmL"88 	'A
 GH!!	22>&& 	'AAV
  <dmmL%)%?%? 	'A
 NO $$?G?R?R
dmm@-!)!4!46"G!%!>!>"@@A$<
 45   OWObOb
3T]]PD#ll--s4+D+D'E EFHPI$L FG$$AEA^A^$>78||.3ll
--DLL 11
2/4$+#++,DKM!!.;.B.B$+"))*ABB}}	...MM^^'446CaXXk* _ ,c
 MM^^'446>\XXk* _ ,c
 ==s##r(   c                     U R                   R                  UR                  5       S9nU R                  R	                  U5      $ Nr   )r   r   r   r7   r   r9   r   requests      r&   r   ModelMonitoringJobsClient.Get  s=    mm^^%224 _ 6G==W%%r(   c                     [         R                  " U R                  U R                  R	                  UR                  5       S9SSUS9$ )N)r   modelDeploymentMonitoringJobspageSize)fieldbatch_size_attributelimit)r   YieldFromListr7   r   CAiplatformProjectsLocationsModelDeploymentMonitoringJobsListRequestr   )r9   r	  r$   s      r&   ListModelMonitoringJobsClient.List  sI    ##	L	L**, 
M 
.-' r(   c                     U R                   R                  UR                  5       S9nU R                  R	                  U5      $ r   )r   EAiplatformProjectsLocationsModelDeploymentMonitoringJobsDeleteRequestr   r7   Deleter  s      r&   r   ModelMonitoringJobsClient.Delete  =    mmaa%224 b 6G==((r(   c                     U R                   R                  UR                  5       S9nU R                  R	                  U5      $ r   )r   DAiplatformProjectsLocationsModelDeploymentMonitoringJobsPauseRequestr   r7   Pauser  s      r&   r  ModelMonitoringJobsClient.Pause  s=    mm``%224 a 6G==w''r(   c                     U R                   R                  UR                  5       S9nU R                  R	                  U5      $ r   )r   EAiplatformProjectsLocationsModelDeploymentMonitoringJobsResumeRequestr   r7   Resumer  s      r&   r   ModelMonitoringJobsClient.Resume  r  r(   )r7   r8   r4   r   )NNN)NN)__name__
__module____qualname____firstlineno____doc__r;   rW   r`   rn   r   r   r   r   r   r   r  r  r  r  __static_attributes__ r(   r&   r-   r-   @   sV    W(&T(.T&P\|:EKMZo$b&
)
(
)r(   r-   )"r  
__future__r   r   r   r   apitools.base.pyr   r   r   googlecloudsdk.api_lib.air	   rG   googlecloudsdk.api_lib.utilr
   r   r   googlecloudsdk.command_lib.air   r   r   r   r   $googlecloudsdk.command_lib.util.argsr   googlecloudsdk.corer   r   r   r   r'   r+   objectr-   r!  r(   r&   <module>r*     s\    H &  '  % ( ' 6 , A 3 0 D I < * ) $ 
	<	;P) P)r(   