
    b                        d Z ddlmZ ddlmZ ddlm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 ddlmZ ddlmZ ddlmZ ddlZd Zd Z G d de       Z!y)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                     |j                         d   }t        j                  j                  | |t        j
                  j                  j                  j                  dd      S )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                     |j                         d   }t        j                  j                  | |t        j
                  j                  j                  j                  dd      S )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                   h    e Zd ZdZddZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd ZddZd Zd Zd Zy)ModelMonitoringJobsClientzOHigh-level client for the AI Platform model deployment monitoring jobs surface.Nc                    |xs5 t        j                  t        j                  t        j                  |         | _        |xs | j
                  j                  | _        | j
                  j                  | _	        || _
        y N)r
   GetClientInstancer   AI_PLATFORM_API_NAMEAI_PLATFORM_API_VERSIONclientMESSAGES_MODULEr   0projects_locations_modelDeploymentMonitoringJobs_service_version)selfr3   r   versions       r&   __init__z"ModelMonitoringJobsClient.__init__C   sb     4D22&&))'24DK ; ; ;DMKKPPDMDMr(   c                     t        j                  d| j                               }g }g }|r|j                         D ]i  \  }}|sdn
t	        |      }|j                  |j                         j                  | t        j                  d| j                        |                   k |j                  |      |_        |r|j                         D ]i  \  }}|sdn
t	        |      }|j                  |j                         j                  | t        j                  d| j                        |                   k |j                  |      |_
        |S )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keyr@   additionalProperties)api_util
GetMessager7   itemsfloatappendDriftThresholdsValueAdditionalPropertydriftThresholds$AttributionScoreDriftThresholdsValueattributionScoreDriftThresholds)	r8   feature_thresholdsfeature_attribution_thresholdsprediction_drift_detectionadditional_properties!attribution_additional_propertiesrB   r@   	thresholds	            r&   _ConstructDriftThresholdsz3ModelMonitoringJobsClient._ConstructDriftThresholdsK   s   "!4!4F" " (*%*002*#u$C%,	$$%?&:&:&<=O=O-00<x/B/B->-1]]0<BK0M >P >N	O 3 4N3b3b4 4c 46 0%6<<>*#u$C%,	)00&KK   9h))*;*.--9?HJ ! K	L ? D^  DC  DC@ DC DB @ &%r(   c                     t        j                  d| j                               }g }g }|r|j                         D ]i  \  }}|sdn
t	        |      }|j                  |j                         j                  | t        j                  d| j                        |                   k |j                  |      |_        |r|j                         D ]i  \  }}|sdn
t	        |      }|j                  |j                         j                  | t        j                  d| j                        |                   k |j                  |      |_
        |S )a5  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>   r?   rA   rC   )rE   rF   r7   rG   rH   rI   SkewThresholdsValuerK   skewThresholds#AttributionScoreSkewThresholdsValueattributionScoreSkewThresholds)	r8   rO   rP   "training_prediction_skew_detectionrR   rS   rB   r@   rT   s	            r&   _ConstructSkewThresholdsz2ModelMonitoringJobsClient._ConstructSkewThresholdsu   s   *)<)<M* *& (*%*002*#u$C%,	$$%G&9&9&;<N<N-00<x/B/B->-1]]0<BK0M =O =N	O 3 ;]:p:p4 ;q ;6(7%6<<>*#u$C%,	)00.0023E3E9h))*;*.--9?HJ 4F 4K	L ? Km  KQ  KQ@ KQ KB(G .-r(   c                    | j                  ||      }| j                  ||      }g }|j                  D ]R  }|j                  j                  rb|j
                  r%|j
                  |j                  j                  _        |j                  r%|j                  |j                  j                  _        |j                  j                  rb|j                  r%|j                  |j                  j                  _        |j                  r%|j                  |j                  j                  _	        |j                  s|j                  r6 t        j                  d| j                        d      |j                  _        |j                  |       U |S )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)rU   r]   )modelDeploymentMonitoringObjectiveConfigsobjectiveConfig%trainingPredictionSkewDetectionConfigrY   r[   predictionDriftDetectionConfigrL   rN   rE   rF   r7   explanationConfigrI   )r8   existing_monitoring_jobrO   rP   rQ   r\   objective_configsobjective_configs           r&   "_ConstructObjectiveConfigForUpdatez<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>NX=P=P=t}}>N(,>.((: /0 ^  r(   c                     t        j                  d| j                               }|s|r	|s|s|s|r t        j                  d| j                               }|	t        j                  d      |	|_         t        j                  d| j                         t        j                  d| j                        |
            |_        |r t        ||      j                         |_	        n|r- t        j                  d| j                        |	      |_
        ne|s|ra|t        j                  d
      |t        j                  d      ||_         t        j                  d| j                        |      |_        | j                  ||      } t        j                  d| j                        ||      |_        n>| j                  ||      } t        j                  d| j                        |      |_        |r6 t        j                  d| j                        d      |j                  _        | j"                  j%                  |      }| j&                  j(                  j+                  |      }g }|j,                  D ]9  }t/        j0                  |      }|j2                  |_        |j7                  |       ; |S )a  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)trainingDatasetrd   )re   r_   Tr`   name)rE   rF   r7   r   ArgumentErrortargetFieldloggingSamplingStrategyr+   RelativeNamedatasetbigquerySource
dataFormat	gcsSourcer]   rc   rU   rf   r   .AiplatformProjectsLocationsEndpointsGetRequestr3   projects_locations_endpointsGetdeployedModelscopydeepcopyiddeployedModelIdrI   )r8   location_refendpoint_namerO   rP   r   bigquery_uridata_formatgcs_uristarget_fieldtraining_sampling_rateobjective_config_templatetraining_datasetr\   rQ   get_endpoint_reqendpointrh   deployed_modelri   s                       r&   "_ConstructObjectiveConfigForCreatez<ModelMonitoringJobsClient._ConstructObjectiveConfigForCreate   s   :!C 3 32DMM!C !E;	LHL8..;T]]L N$$e  (4$4/83F3F4/$I8#6#68$--$I#9$;4<0
 %273?&AAM 
"-/H,?,?-/'-)

) && L   && K  )4

%(*x':':4==(*(!

$ .2-J-J >.@*5
H4G4G,dmm5
*0R5!1 &*%C%C >&@"5=H4G4G,dmm5=/I5K!1 
(GNhFYFY=t}}GN(,G.!11C }}SS T {{77;;<LMH"11'@A)7):):&/0 2 r(   c                     t        j                  | t        j                  d| j                               j
                        S )zParses create labels.ModelDeploymentMonitoringJob)r   ParseCreateArgsrE   rF   r7   LabelsValue)r8   argss     r&   _ParseCreateLabelsz,ModelMonitoringJobsClient._ParseCreateLabels%  s>    &&	+: MM	+ 	--8[: :r(   c                       fd}t        j                  | t        j                  d j                               j
                  |      S )zParses update labels.c                  :    j                         j                  S r/   )r   labels)model_monitoring_job_refr8   s   r&   	GetLabelsz?ModelMonitoringJobsClient._ParseUpdateLabels.<locals>.GetLabels.  s    XX./666r(   r   )r   ProcessUpdateArgsLazyrE   rF   r7   r   )r8   r   r   r   s   ``  r&   _ParseUpdateLabelsz,ModelMonitoringJobsClient._ParseUpdateLabels,  sJ    7 ,,	+: MM	+ 	--8[)E Er(   c                    t        |j                  |      } t        j                  d| j                               }t        j                  |      }|, t        j                  d| j                        |      |_        |j                  rVt        j                  |j                        }|rt        j                  |t        j                  d| j                              }n}| j                  ||j                         |j                  |j                   |j"                  |j$                  |j&                  |j(                  |j*                  |j,                  
      |_        |j                         |_        |j0                  |_        | j5                  |      |_        |j8                  dn|j8                  } t        j                  d| j                        | t        j                  d| j                        |j:                        |j<                        |_         t        j                  d	| j                         t        j                  d
| j                        |j@                              |_!         t        j                  d| j                        djE                  tG        jH                  dtK        |jL                        z                    |_'        |jP                  r|jP                  |_)        |jT                  r|jT                  |_+        |jX                  r?djE                  tG        jH                  dtK        |jX                        z              |_-        |j\                  rHt_        j`                  |j\                        }tc        jd                  tf        jh                  |      |_5        | j                  tl        jn                  k(  rD| jp                  js                  | jt                  jw                  |j                         |            S | jp                  js                  | jt                  jw                  |j                         |            S )z*Creates a model deployment monitoring job.r   EncryptionSpec)
kmsKeyNameFModelMonitoringAlertConfig*ModelMonitoringAlertConfigEmailAlertConfig
userEmails)enableLoggingemailAlertConfignotificationChannelsrn   ro   rp   rr   'ModelDeploymentMonitoringScheduleConfig{}s  monitorIntervalQ )parent8googleCloudAiplatformV1beta1ModelDeploymentMonitoringJob)r   3googleCloudAiplatformV1ModelDeploymentMonitoringJob)<r'   r   rE   rF   r7   common_validationGetAndValidateKmsKeyencryptionSpecmonitoring_config_from_filer   	load_pathmessages_utilDictToMessageWithErrorCheckr   r   rO   rP   r   r   r   r   r   r   rb   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_VERSIONr6   Creater   EAiplatformProjectsLocationsModelDeploymentMonitoringJobsCreateRequest)	r8   r   r   endpoint_refjob_speckms_key_namedataenable_anomaly_cloud_logginginstance_jsons	            r&   r   z ModelMonitoringJobsClient.Create6  s   !$-->L2x""#A#'==2 4H$99$?L!C 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 +5(*=*=$dmm+56X00<*.++7 "&!;!;+=H'(+x':':DMM(+ Ex224dmm E#<< >(?H$8Bx7J7J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           
      6    t        j                  d| j                               }g } t        j                  d| j                               }|j                  rwt	        j
                  |j                        }|rVt        j                  |t        j                  d| j                              }|j                  |_        |j                  d       |j                  s|j                  r| j                  j                  |j                               }| j                  j!                  |      }| j#                  ||j                  |j                        |_        |j                  d       |j$                  r"|j$                  |_        |j                  d       |j(                  rl t        j                  d| j                         t        j                  d| j                        |j(                              |_        |j                  d	       |j,                  o|j(                  r|j,                  |j*                  _        n6 t        j                  d| j                        |j,                  
      |_        |j                  d       |j0                  r{|j(                  s|j,                  |j0                  |j*                  _        n6 t        j                  d| j                        |j0                        |_        |j                  d       |j4                  rl t        j                  d| j                         t        j                  d| j                        |j4                              |_        |j                  d       |j8                  ru t        j                  d| j                        dj;                  t=        j>                  dtA        |j8                        z                    |_!        |j                  d       |jD                  r"|jD                  |_#        |j                  d       |jH                  rPdj;                  t=        j>                  dtA        |jH                        z              |_%        |j                  d       | jM                  ||      }	|	jN                  r"|	jP                  |_(        |j                  d       |stS        jT                  d      | j                  tV        jX                  k(  r<| j                  j[                  |j                         |dj]                  |            }
n;| j                  j[                  |j                         |dj]                  |            }
| j                  j_                  |
      S ) z)Update a model deployment monitoring job.r   -model_deployment_monitoring_objective_configsrz   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_channelsrn   ro   rp   rr   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   )0rE   rF   r7   r   r   r   r   r   rb   rI   rO   rP   r   BAiplatformProjectsLocationsModelDeploymentMonitoringJobsGetRequestr   r6   r   rj   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)r8   r   r   model_monitoring_job_to_updateupdate_maskr   r   get_monitoring_job_reqmodel_monitoring_joblabels_updatereqs              r&   r   zModelMonitoringJobsClient.Patch  s   &7X%8%8&&7 &9"K2x""#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({{
J(

:DMM
J x22> ;; ( %? KL!!-	&& 	'AAO
 MH <dmmL"88 	'A
 GH!!	22>&& 	'AAV
 MH <dmmL%)%?%? 	'A
 NO $$@-x?R?R
dmm@-"G!4!46"G!%!>!>"@@A$<
 45   PDxObOb
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$+#++,DdKM!!.;.B.B$+"))*ABB}}	...MM^^'446CaXXk* _ ,c
 MM^^'446>\XXk* _ ,c
 ==s##r(   c                     | j                   j                  |j                               }| j                  j	                  |      S Nrz   )r   r   r   r6   r   r8   r   requests      r&   r   zModelMonitoringJobsClient.Get  s=    mm^^%224 _ 6G==W%%r(   c                     t        j                  | j                  | j                  j	                  |j                               dd|      S )N)r   modelDeploymentMonitoringJobspageSize)fieldbatch_size_attributelimit)r   YieldFromListr6   r   CAiplatformProjectsLocationsModelDeploymentMonitoringJobsListRequestr   )r8   r   r$   s      r&   ListzModelMonitoringJobsClient.List  sI    ##	L	L**, 
M 
.-' r(   c                     | j                   j                  |j                               }| j                  j	                  |      S r   )r   EAiplatformProjectsLocationsModelDeploymentMonitoringJobsDeleteRequestr   r6   Deleter   s      r&   r  z ModelMonitoringJobsClient.Delete  =    mmaa%224 b 6G==((r(   c                     | j                   j                  |j                               }| j                  j	                  |      S r   )r   DAiplatformProjectsLocationsModelDeploymentMonitoringJobsPauseRequestr   r6   Pauser   s      r&   r  zModelMonitoringJobsClient.Pause  s=    mm``%224 a 6G==w''r(   c                     | j                   j                  |j                               }| j                  j	                  |      S r   )r   EAiplatformProjectsLocationsModelDeploymentMonitoringJobsResumeRequestr   r6   Resumer   s      r&   r
  z ModelMonitoringJobsClient.Resume  r  r(   )NNN)NN)__name__
__module____qualname____doc__r:   rU   r]   rj   r   r   r   r   r   r   r   r  r  r
   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	   rE   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(   