
                         ,   S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSK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r " S S\R4                  5      r " S S\5      rS rS rS rS rS r S r!g)z;Utils to send survey responses to clearcut's concord table.    )absolute_import)division)unicode_literalsN)question)config)
exceptions)log)metrics)
properties)requests)survey_check)	platforms)http_clientzhttps://play.googleapis.com/logc                       \ rS rSrSrSrg)Error)   z Base Error type for this module. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       1lib/googlecloudsdk/api_lib/survey/concord_util.pyr   r   )   s    (r   r   c                       \ rS rSrSrSrg)SurveyNotRecordedError-   z<Errors when the survey response cannot be logged to concord.r   Nr   r   r   r   r   r   -   s    Dr   r   c                     [         R                  " 5       R                  S5      (       a  SOSn U [        R                  " 5       [
        R                  " 5       [        R                  R                  [        R                  " 5       [        R                  " 5       [        R                  R
                  R                  R                  5       S.nUR!                  5        VVs/ s H  u  p#Uc  M
  X#S.PM     snn$ s  snnf )zGets user's environment.z.google.comGoogleExternal)install_typecid
user_agentrelease_channelpython_versionenvironmentenvironment_version)keyvalue)socketgethostnameendswithr   GetCIDr
   GetUserAgentINSTALLATION_CONFIGr&   platformr'   r   GetMetricsEnvironmentVALUESr)   Getitems)r#   envkvs       r   _SurveyEnvironmentr:   1   s     %002;;MJJ(  #]]_((*33CC //1557'..66JJNNP	# .1YY[	J[TQA
 !
 [	JJ	Js   	C8,C8c                      [         R                  " 5        n [        R                  " 5       U l        SSS5        g! , (       d  f       g= f)z%Records the time user answers survey.N)r   PromptRecordtimelast_answer_survey_time)prs    r   _UpdateSurveyCacher@   C   s*      "b!%B #""s	   :
Ac                 Z    [        5       [        R                  " 5       SS[        U 5      S.$ )NCloudSDK
hatsSurvey)event_metadataclient_install_idconsole_type
event_typehats_response)r:   r   r/   _HatsResponseFromSurvey)survey_instances    r   _ConcordEventForSurveyrK   I   s)    *,!==?  .?
 r   c                 (   SSU R                   S.n/ n/ n/ n[        U 5       GHF  u  pVUR                  5       (       d  M  [        U[        R
                  5      (       a  [        U5      S-   [        UR                  5      -
  nUR                  UU/U/UR                  [        UR                  5      5      /[        [        S[        U5      S-   5      5      S.5        M  [        U[        R                  5      (       a*  UR                  U[        UR                  5      S.5        GM  [        U[        R                  5      (       d  GM(  UR                  UUR                  S.5        GMI     SU0nU(       a  X(S	'   U(       a  X8S
'   U(       a  XHS'   U$ )zPuts survey response to a HatsResponse object.

Args:
  survey_instance: googlecloudsdk.command_lib.survey.survey.Survey, a survey
    object which contains user's response.

Returns:
  HatsResponse as a dictionary to send to concord.
rB   googlecloudsdk)site_id	site_name	survey_id   )question_numberorder_indexanswer_indexanswer_textorder)rR   rating)rR   rU   hats_metadatamultiple_choice_responserating_responseopen_text_response)name	enumerate
IsAnswered
isinstancer   MultiChoiceQuestionlenintanswerappendChoicelistrangeRatingQuestionFreeTextQuestion)	rJ   rX   multi_choice_questionsrating_questionsopen_text_questionsiq
answer_intresponses	            r   rI   rI   S   sg    #"''- (da<<>>!X1122
 q6A:AHH-j##$%,((3qxx=12aQ*+% 	 
Ax..	/	/M 	 
Ax00	1	1  " 	- )6 }-(+A'("2%8!"	/r   c                 6   [         R                  R                  5       n[        R                  " [        U 5      SS9[        R                  " 5       S./nSSUR                  R                  0S.S[        R                  " 5       [        R                  " 5       US.$ )	zPrepares clearcut LogRequest.

Args:
   survey_instance: googlecloudsdk.command_lib.survey.survey.Survey, a survey
     object which contains user's response.

Returns:
  A clearcut LogRequest object.
T	sort_keys)source_extension_jsonevent_time_msDESKTOPos)client_typedesktop_client_infoCONCORD)client_infolog_source_namezwieback_cookierequest_time_ms	log_event)r   PlatformCurrentjsondumpsrK   r
   GetTimeMillisoperating_systemidr   r/   )rJ   current_platformr   s      r   _ClearcutRequestr      s     ''//1 **+O<
M



!	 ) #$5588" # ..0
 r   c                 f   [         R                  " 5       nS[        R                  " 5       0n[        R
                  " [        U 5      SS9nUR                  S[        X2S9nUR                  [        R                  :w  a  [        S5      e[        5         [        R                  R!                  S5        g)	z(Sends survey response to clearcut table.z
user-agentTrr   POST)dataheaderszDWe cannot record your feedback at this time, please try again later.zYour response is submitted.N)r   
GetSessionr
   r0   r   r   r   request_CLEARCUT_ENDPOINTstatus_codehttplibOKr   r@   r	   errPrint)rJ   r   r   bodyrp   s        r   LogSurveyAnswersr      s    ##%+7//12'	$_5	F$   t ! >(WZZ'
 NP P''---.r   )"r   
__future__r   r   r   r   r2   r,   r=   !googlecloudsdk.command_lib.surveyr   googlecloudsdk.corer   r   r	   r
   r   r   googlecloudsdk.core.surveyr   googlecloudsdk.core.utilr   	six.movesr   r   r   r   r   r:   r@   rK   rI   r   r   r   r   r   <module>r      s    B &  '     6 & * # ' * ( 3 . ,6 )J )EU EK$-6rB/r   