a ,"?gG@svddlmZddlZddlmZddlmZddlmZddlm Z ddl m Z ej Z ejZejZdd Zd d ZdS) )JSONDecodeErrorN)datetime)Config) relativedelta)build)ServiceAccountCredentialsc Csz8tr&ttt}tdd|d}|WStdWdSWnjtyn}ztdt |WYd}~dSd}~0t y}ztdt |WYd}~dSd}~00dS)NZanalyticsreportingZv4) credentialsz%No key path set for Google analytics.z6An error occurred while instantiating the GA reporter.) KEY_PATHrZfrom_json_keyfile_nameSCOPErlogginginforerrorstr TypeError)r analyticser=/home/cmiss/Jenkins/workspace/SPARC-API-DEV/app/metrics/ga.pyinit_ga_reportings   rcCsttdd}|d}zt|jdt|tddgddigdgid }t|d r|d d d d d dd }t |WSWnYdS0dS)N)yearsz%Y-%m-%dZreportRequests) startDateendDate expressionz ga:sessions)ZviewIdZ dateRangesmetrics)bodyreportsrdataZtotalsvalues) rnowrstrftimerZbatchGetVIEW_IDexecutelenint)r start_dateformatted_start_datereporttotalrrrget_ga_1year_sessions$s$    r))jsonrr r app.configrdateutil.relativedeltarZgoogleapiclient.discoveryrZoauth2client.service_accountrGOOGLE_API_GA_SCOPEr GOOGLE_API_GA_KEY_PATHr GOOGLE_API_GA_VIEW_IDr!rr)rrrrs