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);
}
});
}
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);
}
});
}
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);
}
}
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;
}
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;
}
Aggregations