
                         ,   d Z ddlmZ ddlmZ ddlmZ ddlZddl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Z G d dej4                        Z G d de      Zd Zd Zd Zd Zd Z d Z!y)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                       e Zd ZdZy)Errorz Base Error type for this module.N__name__
__module____qualname____doc__     1lib/googlecloudsdk/api_lib/survey/concord_util.pyr   r   )   s    (r   r   c                       e Zd ZdZy)SurveyNotRecordedErrorz<Errors when the survey response cannot be logged to concord.Nr   r   r   r   r   r   -   s    Dr   r   c                     t        j                         j                  d      rdnd} | t        j                         t        j                         t        j                  j                  t        j                         t        j                         t        j                  j
                  j                  j                         d}|j!                         D cg c]  \  }}|	||d c}}S c c}}w )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   _SurveyEnvironmentr6   1   s     %002;;MJ(  #]]_((*33CC //1557'..66JJNNP	# .1YY[	J[TQAM!a
 [	JJ	Js   
C+ C+c                      t        j                         5 } t        j                         | _        ddd       y# 1 sw Y   yxY w)z%Records the time user answers survey.N)r   PromptRecordtimelast_answer_survey_time)prs    r   _UpdateSurveyCacher<   C   s*      "b!%B #""s	   8Ac                 X    t               t        j                         ddt        |       dS )NCloudSDK
hatsSurvey)event_metadataclient_install_idconsole_type
event_typehats_response)r6   r   r+   _HatsResponseFromSurvey)survey_instances    r   _ConcordEventForSurveyrG   I   s)    *,!==?  .?
 r   c                    dd| j                   d}g }g }g }t        |       D ]-  \  }}|j                         st        |t        j
                        rt        |      dz   t        |j                        z
  }|j                  ||g|g|j                  t        |j                              gt        t        dt        |      dz               d       t        |t        j                        r(|j                  |t        |j                        d       t        |t        j                        s|j                  ||j                  d       0 d|i}|r||d	<   |r||d
<   |r||d<   |S )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.
  r>   googlecloudsdk)site_id	site_name	survey_id   )question_numberorder_indexanswer_indexanswer_textorder)rN   rating)rN   rQ   hats_metadatamultiple_choice_responserating_responseopen_text_response)name	enumerate
IsAnswered
isinstancer   MultiChoiceQuestionlenintanswerappendChoicelistrangeRatingQuestionFreeTextQuestion)	rF   rT   multi_choice_questionsrating_questionsopen_text_questionsiq
answer_intresponses	            r   rE   rE   S   sd    #"''- (da<<>!X112
 q6A:AHH-j##$%,((3qxx=12aQ*+% 	 
Ax..	/M 	 
Ax00	1  " 	- )6 }-(+AH'("2H%8H!"	/r   c                 4   t         j                  j                         }t        j                  t        |       d      t        j                         dg}dd|j                  j                  iddt        j                         t        j                         |d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dumpsrG   r
   GetTimeMillisoperating_systemidr   r+   )rF   current_platformr{   s      r   _ClearcutRequestr      s     ''//1 **+O<
M



!	 ) #$5588" # ..0
 r   c                 l   t        j                         }dt        j                         i}t	        j
                  t        |       d      }|j                  dt        ||      }|j                  t        j                  k7  rt        d      t                t        j                  j!                  d       y)	z(Sends survey response to clearcut table.z
user-agentTrn   POST)dataheaderszDWe cannot record your feedback at this time, please try again later.zYour response is submitted.N)r   
GetSessionr
   r,   r~   r   r   request_CLEARCUT_ENDPOINTstatus_codehttplibOKr   r<   r	   errPrint)rF   r   r   bodyrl   s        r   LogSurveyAnswersr      s    ##%+7//12'	$_5	F$   tW ! >(WZZ'
 NP P''---.r   )"r   
__future__r   r   r   r~   r.   r(   r9   !googlecloudsdk.command_lib.surveyr   googlecloudsdk.corer   r   r	   r
   r   r   googlecloudsdk.core.surveyr   googlecloudsdk.core.utilr   	six.movesr   r   r   r   r   r6   r<   rG   rE   r   r   r   r   r   <module>r      s    B &  '     6 & * # ' * ( 3 . ,6 )J )EU EK$-6rB/r   