Search in sources :

Example 6 with UnsampledReport

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

the class GoogleAnalyticsUnsampledExtractorTest method testPollForCompletion.

public void testPollForCompletion() 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, false);
    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 7 with UnsampledReport

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

the class GoogleAnalyticsUnsampledExtractor method prepareUnsampledReport.

/**
 * Create unsampled report in Google drive and add google drive file id into state so that Google drive extractor
 * can extract record from it. Also, update the state to use CsvFileDownloader unless other downloader is defined.
 *
 * It also register closer to delete the file from Google Drive unless explicitly requested to not deleting it.
 * @return documentID of unsampled report in Google drive
 * @throws IOException
 */
@VisibleForTesting
UnsampledReport prepareUnsampledReport(UnsampledReport request, final GoogleDriveFsHelper fsHelper, boolean isDeleteTempReport) throws IOException {
    UnsampledReport createdReport = createUnsampledReports(request);
    final String fileId = createdReport.getDriveDownloadDetails().getDocumentId();
    LOG.info("Temporary unsampled report created in Google Drive: " + fileId);
    if (isDeleteTempReport) {
        closer.register(new Closeable() {

            @Override
            public void close() throws IOException {
                LOG.info("Deleting created temporary unsampled report from Google drive " + fileId);
                fsHelper.deleteFile(fileId);
            }
        });
    } else {
        LOG.warn("Temporary unsampled report will not be deleted as requested. File ID: " + fileId);
    }
    wuState.setProp(SOURCE_FILEBASED_FILES_TO_PULL, fileId);
    if (!wuState.contains(SOURCE_FILEBASED_OPTIONAL_DOWNLOADER_CLASS)) {
        wuState.setProp(SOURCE_FILEBASED_OPTIONAL_DOWNLOADER_CLASS, CsvFileDownloader.class.getName());
    }
    return createdReport;
}
Also used : Closeable(java.io.Closeable) IOException(java.io.IOException) CsvFileDownloader(org.apache.gobblin.source.extractor.filebased.CsvFileDownloader) UnsampledReport(com.google.api.services.analytics.model.UnsampledReport) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 8 with UnsampledReport

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

the class GoogleAnalyticsUnsampledExtractor method pollForCompletion.

@VisibleForTesting
UnsampledReport pollForCompletion(State state, final Analytics gaService, final UnsampledReport requestedReport) throws IOException {
    Config config = ConfigBuilder.create().loadProps(state.getProperties(), POLL_RETRY_PREFIX).build().withFallback(POLL_RETRY_DEFAULTS);
    Retryer<UnsampledReport> retryer = RetryerFactory.newInstance(config);
    LOG.info("Will poll for completion on unsampled report with retry config: " + config);
    final Stopwatch stopwatch = Stopwatch.createStarted();
    UnsampledReport result = null;
    try {
        result = retryer.call(new Callable<UnsampledReport>() {

            @Override
            public UnsampledReport call() throws Exception {
                UnsampledReport response = null;
                try {
                    response = gaService.management().unsampledReports().get(requestedReport.getAccountId(), requestedReport.getWebPropertyId(), requestedReport.getProfileId(), requestedReport.getId()).execute();
                } catch (Exception e) {
                    LOG.warn("Encountered exception while polling for unsampled report. Will keep polling. " + "Elasped so far: " + stopwatch.elapsed(TimeUnit.SECONDS) + " seconds", e);
                    throw e;
                }
                ReportCreationStatus status = ReportCreationStatus.valueOf(response.getStatus());
                switch(status) {
                    case FAILED:
                        // Stop retrying if it explicitly failed from server.
                        throw new NonTransientException("Unsampled report has failed to be generated. " + response);
                    case PENDING:
                        LOG.info("Waiting for report completion. Elasped so far: " + stopwatch.elapsed(TimeUnit.SECONDS) + " seconds for unsampled report: " + response);
                        // Throw so that Retryer will retry
                        throw new RuntimeException("Not completed yet. This will be retried. " + response);
                    case COMPLETED:
                        return response;
                    default:
                        throw new NonTransientException(status + " is not supported. " + response);
                }
            }
        });
    } catch (ExecutionException e) {
        throw new IOException(e);
    } catch (RetryException e) {
        throw new RuntimeException(e);
    }
    LOG.info("Unsampled report creation has been completed. " + result);
    Preconditions.checkArgument(DOWNLOAD_TYPE_GOOGLE_DRIVE.equals(result.getDownloadType()), result.getDownloadType() + " DownloadType is not supported.");
    return result;
}
Also used : NonTransientException(org.apache.gobblin.exception.NonTransientException) Config(com.typesafe.config.Config) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RetryException(com.github.rholder.retry.RetryException) Callable(java.util.concurrent.Callable) 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) UnsampledReport(com.google.api.services.analytics.model.UnsampledReport) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

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