Search in sources :

Example 11 with Log

use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.

the class DefaultChannel method deleteLogsOnSuspended.

private void deleteLogsOnSuspended(final GroupState groupState) {
    final List<Log> logs = new ArrayList<>();
    final int stateSnapshot = mCurrentState;
    mPersistence.getLogs(groupState.mName, CLEAR_BATCH_SIZE, logs, new AbstractDatabasePersistenceAsyncCallback() {

        @Override
        public void onSuccess(Object result) {
            deleteLogsOnSuspended(groupState, stateSnapshot, logs);
        }
    });
}
Also used : AbstractDatabasePersistenceAsyncCallback(com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync.AbstractDatabasePersistenceAsyncCallback) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) ArrayList(java.util.ArrayList)

Example 12 with Log

use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.

the class DefaultChannel method triggerIngestion.

/**
     * This will, if we're not using the limit for pending batches, trigger sending of a new request.
     * It will also reset the counters for sending out items for both the number of items enqueued and
     * the handlers. It will do this even if we don't have reached the limit
     * of pending batches or the time interval.
     *
     * @param groupName the group name
     */
private synchronized void triggerIngestion(@NonNull final String groupName) {
    if (!mEnabled) {
        return;
    }
    final GroupState groupState = mGroupStates.get(groupName);
    MobileCenterLog.debug(LOG_TAG, "triggerIngestion(" + groupName + ") pendingLogCount=" + groupState.mPendingLogCount);
    cancelTimer(groupState);
    /* Check if we have reached the maximum number of pending batches, log to LogCat and don't trigger another sending. */
    if (groupState.mSendingBatches.size() == groupState.mMaxParallelBatches) {
        MobileCenterLog.debug(LOG_TAG, "Already sending " + groupState.mMaxParallelBatches + " batches of analytics data to the server.");
        return;
    }
    /* Get a batch from Persistence. */
    final List<Log> batch = new ArrayList<>(groupState.mMaxLogsPerBatch);
    final int stateSnapshot = mCurrentState;
    mPersistence.getLogs(groupName, groupState.mMaxLogsPerBatch, batch, new AbstractDatabasePersistenceAsyncCallback() {

        @Override
        public void onSuccess(Object result) {
            triggerIngestion((String) result, groupState, stateSnapshot, batch);
        }
    });
}
Also used : AbstractDatabasePersistenceAsyncCallback(com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync.AbstractDatabasePersistenceAsyncCallback) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) ArrayList(java.util.ArrayList)

Example 13 with Log

use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.

the class DefaultChannel method handleSendingFailure.

/**
     * The actual implementation to react to not being able to send a batch to the server.
     * Will disable the sender in case of a recoverable error.
     * Will delete batch of data in case of a non-recoverable error.
     *
     * @param groupState   the group state
     * @param currentState the current state
     * @param batchId      the batch ID
     * @param e            the exception
     */
private synchronized void handleSendingFailure(@NonNull final GroupState groupState, int currentState, @NonNull final String batchId, @NonNull final Exception e) {
    if (checkStateDidNotChange(groupState, currentState)) {
        String groupName = groupState.mName;
        MobileCenterLog.error(LOG_TAG, "Sending logs groupName=" + groupName + " id=" + batchId + " failed", e);
        List<Log> removedLogsForBatchId = groupState.mSendingBatches.remove(batchId);
        boolean recoverableError = HttpUtils.isRecoverableError(e);
        if (recoverableError) {
            groupState.mPendingLogCount += removedLogsForBatchId.size();
        } else {
            GroupListener groupListener = groupState.mListener;
            if (groupListener != null) {
                for (Log log : removedLogsForBatchId) groupListener.onFailure(log, e);
            }
        }
        suspend(!recoverableError, e);
    }
}
Also used : MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log)

Example 14 with Log

use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.

the class DatabasePersistence method getLogs.

@Override
@Nullable
public String getLogs(@NonNull String group, @IntRange(from = 0) int limit, @NonNull List<Log> outLogs) {
    /* Log. */
    MobileCenterLog.debug(LOG_TAG, "Trying to get " + limit + " logs from the Persistence database for " + group);
    /* Query database and get scanner. */
    DatabaseStorage.DatabaseScanner scanner = mDatabaseStorage.getScanner(COLUMN_GROUP, group);
    /* Add logs to output parameter after deserialization if logs are not already sent. */
    int count = 0;
    Map<Long, Log> candidates = new TreeMap<>();
    List<Long> failedDbIdentifiers = new ArrayList<>();
    for (Iterator<ContentValues> iterator = scanner.iterator(); iterator.hasNext() && count < limit; ) {
        ContentValues values = iterator.next();
        Long dbIdentifier = values.getAsLong(DatabaseManager.PRIMARY_KEY);
        /* If the log is already in pending state, then skip. Otherwise put the log to candidate container. */
        if (!mPendingDbIdentifiers.contains(dbIdentifier)) {
            try {
                /* Deserialize JSON to Log. */
                candidates.put(dbIdentifier, getLogSerializer().deserializeLog(values.getAsString(COLUMN_LOG)));
                count++;
            } catch (JSONException e) {
                /* If it is not able to deserialize, delete and get another log. */
                MobileCenterLog.error(LOG_TAG, "Cannot deserialize a log in the database", e);
                /* Put the failed identifier to delete. */
                failedDbIdentifiers.add(dbIdentifier);
            }
        }
    }
    scanner.close();
    /* Delete any logs that cannot be deserialized. */
    if (failedDbIdentifiers.size() > 0) {
        mDatabaseStorage.delete(failedDbIdentifiers);
        MobileCenterLog.warn(LOG_TAG, "Deleted logs that cannot be deserialized");
    }
    /* No logs found. */
    if (candidates.size() <= 0) {
        MobileCenterLog.debug(LOG_TAG, "No logs found in the Persistence database at the moment");
        return null;
    }
    /* Generate an ID. */
    String id = UUIDUtils.randomUUID().toString();
    /* Log. */
    MobileCenterLog.debug(LOG_TAG, "Returning " + candidates.size() + " log(s) with an ID, " + id);
    MobileCenterLog.debug(LOG_TAG, "The SID/ID pairs for returning log(s) is/are:");
    List<Long> pendingDbIdentifiersGroup = new ArrayList<>();
    for (Map.Entry<Long, Log> entry : candidates.entrySet()) {
        Long dbIdentifier = entry.getKey();
        /* Change a database identifier to pending state. */
        mPendingDbIdentifiers.add(dbIdentifier);
        /* Store a database identifier to a group of the ID. */
        pendingDbIdentifiersGroup.add(dbIdentifier);
        /* Add to output parameter. */
        outLogs.add(entry.getValue());
        /* Log. */
        MobileCenterLog.debug(LOG_TAG, "\t" + entry.getValue().getSid() + " / " + dbIdentifier);
    }
    /* Update pending IDs. */
    mPendingDbIdentifiersGroups.put(group + id, pendingDbIdentifiersGroup);
    return id;
}
Also used : ContentValues(android.content.ContentValues) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) TreeMap(java.util.TreeMap) DatabaseStorage(com.microsoft.azure.mobile.utils.storage.StorageHelper.DatabaseStorage) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Nullable(android.support.annotation.Nullable)

Example 15 with Log

use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.

the class DefaultLogSerializer method readLog.

@NonNull
private Log readLog(JSONObject object) throws JSONException {
    String type = object.getString(TYPE);
    LogFactory logFactory = mLogFactories.get(type);
    if (logFactory == null)
        throw new JSONException("Unknown log type: " + type);
    Log log = logFactory.create();
    log.read(object);
    return log;
}
Also used : MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) JSONException(org.json.JSONException) NonNull(android.support.annotation.NonNull)

Aggregations

Log (com.microsoft.azure.mobile.ingestion.models.Log)59 Test (org.junit.Test)44 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)37 ArrayList (java.util.ArrayList)24 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 Context (android.content.Context)18 UUID (java.util.UUID)18 EventLog (com.microsoft.azure.mobile.analytics.ingestion.models.EventLog)13 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)13 StartSessionLog (com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog)12 LogContainer (com.microsoft.azure.mobile.ingestion.models.LogContainer)12 DefaultLogSerializer (com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer)11 Persistence (com.microsoft.azure.mobile.persistence.Persistence)11 Matchers.anyString (org.mockito.Matchers.anyString)10 IOException (java.io.IOException)9 Channel (com.microsoft.azure.mobile.channel.Channel)8 IngestionHttp (com.microsoft.azure.mobile.ingestion.IngestionHttp)8 StartServiceLog (com.microsoft.azure.mobile.ingestion.models.StartServiceLog)8 JSONException (org.json.JSONException)8 MediumTest (android.support.test.filters.MediumTest)7