Search in sources :

Example 1 with UnsampledReport

use of com.google.api.services.analytics.model.UnsampledReport in project incubator-gobblin by apache.

the class GoogleAnalyticsUnsampledExtractorTest method testPollForCompletionWithException.

public void testPollForCompletionWithException() throws IOException {
    wuState = new WorkUnitState();
    wuState.setProp(POLL_RETRY_PREFIX + RETRY_TIME_OUT_MS, TimeUnit.SECONDS.toMillis(30L));
    wuState.setProp(POLL_RETRY_PREFIX + RETRY_INTERVAL_MS, 1L);
    GoogleAnalyticsUnsampledExtractor extractor = setup(ReportCreationStatus.COMPLETED, wuState, true);
    UnsampledReport requestedReport = new UnsampledReport().setAccountId("testAccountId").setWebPropertyId("testWebPropertyId").setProfileId("testProfileId").setId("testId");
    String actualFileId = extractor.pollForCompletion(wuState, gaService, requestedReport).getDriveDownloadDetails().getDocumentId();
    Assert.assertEquals(actualFileId, EXPECTED_FILE_ID);
    verify(getReq, atLeast(5)).execute();
}
Also used : WorkUnitState(org.apache.gobblin.configuration.WorkUnitState) UnsampledReport(com.google.api.services.analytics.model.UnsampledReport)

Example 2 with UnsampledReport

use of com.google.api.services.analytics.model.UnsampledReport in project incubator-gobblin by apache.

the class GoogleAnalyticsUnsampledExtractorTest method testPollForCompletionFailure.

public void testPollForCompletionFailure() throws IOException {
    wuState = new WorkUnitState();
    wuState.setProp(POLL_RETRY_PREFIX + RETRY_TIME_OUT_MS, TimeUnit.SECONDS.toMillis(30L));
    wuState.setProp(POLL_RETRY_PREFIX + RETRY_INTERVAL_MS, 1L);
    GoogleAnalyticsUnsampledExtractor extractor = setup(ReportCreationStatus.FAILED, wuState, false);
    UnsampledReport requestedReport = new UnsampledReport().setAccountId("testAccountId").setWebPropertyId("testWebPropertyId").setProfileId("testProfileId").setId("testId");
    try {
        extractor.pollForCompletion(wuState, gaService, requestedReport);
        Assert.fail("Should have failed with failed status");
    } catch (Exception e) {
        Assert.assertTrue(e.getCause().getCause() instanceof NonTransientException);
    }
    verify(getReq, atLeast(5)).execute();
}
Also used : NonTransientException(org.apache.gobblin.exception.NonTransientException) WorkUnitState(org.apache.gobblin.configuration.WorkUnitState) NonTransientException(org.apache.gobblin.exception.NonTransientException) IOException(java.io.IOException) UnsampledReport(com.google.api.services.analytics.model.UnsampledReport)

Example 3 with UnsampledReport

use of com.google.api.services.analytics.model.UnsampledReport in project incubator-gobblin by apache.

the class GoogleAnalyticsUnsampledExtractor method requestUnsampledReport.

@VisibleForTesting
UnsampledReport requestUnsampledReport(UnsampledReport request) throws IOException {
    String accountId = request.getAccountId();
    String webPropertyId = request.getWebPropertyId();
    String profileId = request.getProfileId();
    // GA somehow does not allow these values in it.
    request.setAccountId(null).setWebPropertyId(null).setProfileId(null);
    final String endDate = request.getEndDate();
    final Insert insertRequest = gaService.management().unsampledReports().insert(accountId, webPropertyId, profileId, request);
    Config config = ConfigBuilder.create().loadProps(wuState.getProperties(), REQUEST_RETRY_PREFIX).build();
    Retryer<UnsampledReport> retryer = RetryerFactory.newInstance(config);
    LOG.info("Requesting to create unsampled report " + request);
    try {
        return retryer.call(new Callable<UnsampledReport>() {

            @Override
            public UnsampledReport call() throws Exception {
                UnsampledReport response = insertRequest.execute();
                if (ReportCreationStatus.FAILED.name().equals(response.getStatus())) {
                    // No retry if it's explicitly failed from server
                    throw new NonTransientException("Failed to create unsampled report " + response);
                }
                // response does not have end date where we need it later for next watermark calculation.
                response.setEndDate(endDate);
                return response;
            }
        });
    } catch (ExecutionException e) {
        throw new IOException(e);
    } catch (RetryException e) {
        throw new RuntimeException(e);
    }
}
Also used : NonTransientException(org.apache.gobblin.exception.NonTransientException) Config(com.typesafe.config.Config) IOException(java.io.IOException) Insert(com.google.api.services.analytics.Analytics.Management.UnsampledReports.Insert) RetryException(com.github.rholder.retry.RetryException) NonTransientException(org.apache.gobblin.exception.NonTransientException) RetryException(com.github.rholder.retry.RetryException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DataRecordException(org.apache.gobblin.source.extractor.DataRecordException) ExecutionException(java.util.concurrent.ExecutionException) UnsampledReport(com.google.api.services.analytics.model.UnsampledReport) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with UnsampledReport

use of com.google.api.services.analytics.model.UnsampledReport in project incubator-gobblin by apache.

the class GoogleAnalyticsUnsampledExtractor method createUnsampledReports.

@VisibleForTesting
UnsampledReport createUnsampledReports(UnsampledReport request) throws IOException {
    long startTimeInMillis = System.currentTimeMillis();
    try {
        UnsampledReport requestedReport = requestUnsampledReport(request);
        UnsampledReport createdReport = pollForCompletion(wuState, gaService, requestedReport);
        createdReport.setEndDate(requestedReport.getEndDate());
        return createdReport;
    } finally {
        long delta = System.currentTimeMillis() - startTimeInMillis;
        if (GobblinMetrics.isEnabled(wuState)) {
            Timer timer = Instrumented.getMetricContext(wuState, getClass()).timer(GA_UNSAMPLED_REPORT_CREATION_TIMER);
            Instrumented.updateTimer(Optional.of(timer), delta, TimeUnit.MILLISECONDS);
        }
    }
}
Also used : Timer(com.codahale.metrics.Timer) UnsampledReport(com.google.api.services.analytics.model.UnsampledReport) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 5 with UnsampledReport

use of com.google.api.services.analytics.model.UnsampledReport in project incubator-gobblin by apache.

the class GoogleAnalyticsUnsampledExtractorTest method setup.

private GoogleAnalyticsUnsampledExtractor setup(final ReportCreationStatus status, WorkUnitState wuState, final boolean throwException) throws IOException {
    Extractor actualExtractor = mock(Extractor.class);
    gaService = mock(Analytics.class);
    Management mgmt = mock(Management.class);
    when(gaService.management()).thenReturn(mgmt);
    UnsampledReports req = mock(UnsampledReports.class);
    when(mgmt.unsampledReports()).thenReturn(req);
    getReq = mock(Get.class);
    when(req.get(anyString(), anyString(), anyString(), anyString())).thenReturn(getReq);
    int pollCount = 10;
    final MutableInt countDown = new MutableInt(pollCount);
    when(getReq.execute()).then(new Answer<UnsampledReport>() {

        @Override
        public UnsampledReport answer(InvocationOnMock invocation) throws Throwable {
            countDown.decrement();
            if (countDown.intValue() == 0) {
                UnsampledReport response = new UnsampledReport();
                DriveDownloadDetails details = new DriveDownloadDetails();
                details.setDocumentId(EXPECTED_FILE_ID);
                response.setStatus(status.name()).setDownloadType(DOWNLOAD_TYPE_GOOGLE_DRIVE).setDriveDownloadDetails(details);
                return response;
            } else if (throwException) {
                throw new RuntimeException("Dummy exception.");
            }
            return new UnsampledReport();
        }
    });
    return new GoogleAnalyticsUnsampledExtractor<>(wuState, actualExtractor, gaService);
}
Also used : UnsampledReports(com.google.api.services.analytics.Analytics.Management.UnsampledReports) Management(com.google.api.services.analytics.Analytics.Management) Analytics(com.google.api.services.analytics.Analytics) DriveDownloadDetails(com.google.api.services.analytics.model.UnsampledReport.DriveDownloadDetails) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Get(com.google.api.services.analytics.Analytics.Management.UnsampledReports.Get) MutableInt(org.apache.commons.lang.mutable.MutableInt) Extractor(org.apache.gobblin.source.extractor.Extractor) UnsampledReport(com.google.api.services.analytics.model.UnsampledReport)

Aggregations

UnsampledReport (com.google.api.services.analytics.model.UnsampledReport)8 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 IOException (java.io.IOException)4 WorkUnitState (org.apache.gobblin.configuration.WorkUnitState)3 NonTransientException (org.apache.gobblin.exception.NonTransientException)3 RetryException (com.github.rholder.retry.RetryException)2 Config (com.typesafe.config.Config)2 ExecutionException (java.util.concurrent.ExecutionException)2 DataRecordException (org.apache.gobblin.source.extractor.DataRecordException)2 Timer (com.codahale.metrics.Timer)1 Analytics (com.google.api.services.analytics.Analytics)1 Management (com.google.api.services.analytics.Analytics.Management)1 UnsampledReports (com.google.api.services.analytics.Analytics.Management.UnsampledReports)1 Get (com.google.api.services.analytics.Analytics.Management.UnsampledReports.Get)1 Insert (com.google.api.services.analytics.Analytics.Management.UnsampledReports.Insert)1 DriveDownloadDetails (com.google.api.services.analytics.model.UnsampledReport.DriveDownloadDetails)1 Stopwatch (com.google.common.base.Stopwatch)1 Closeable (java.io.Closeable)1 Callable (java.util.concurrent.Callable)1 MutableInt (org.apache.commons.lang.mutable.MutableInt)1