diff --git a/CHANGELOG.md b/CHANGELOG.md index ae33ef9..9e48929 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Piano Analytics SDK for Android ## v3.3.4-SNAPSHOT +* Added limit for event storage * Updated dependencies: - com.squareup.okhttp3:okhttp [4.11.0 -> 4.12.0] https://square.github.io/okhttp/ diff --git a/piano-analytics/src/main/java/io/piano/android/analytics/EventRepository.kt b/piano-analytics/src/main/java/io/piano/android/analytics/EventRepository.kt index d7aeda4..c12ed4e 100644 --- a/piano-analytics/src/main/java/io/piano/android/analytics/EventRepository.kt +++ b/piano-analytics/src/main/java/io/piano/android/analytics/EventRepository.kt @@ -22,6 +22,15 @@ internal class EventRepository( ) } + fun deleteOutOfLimitNotSentEvents(limit: Int) { + databaseHelper.delete( + "${EventRecord.IS_SENT} = 0 AND ${EventRecord.ID} <= (" + + "SELECT ${EventRecord.ID} FROM ${EventRecord.TABLE_NAME} WHERE ${EventRecord.IS_SENT} = 0 " + + "ORDER by ${EventRecord.ID} DESC LIMIT 1 OFFSET ?)", + limit.toString() + ) + } + fun getNotSentEvents(): List = databaseHelper.query( selection = "${EventRecord.IS_SENT} = 0", orderBy = "${EventRecord.TIME} ASC" diff --git a/piano-analytics/src/main/java/io/piano/android/analytics/SendTask.kt b/piano-analytics/src/main/java/io/piano/android/analytics/SendTask.kt index 8a1271e..94ece9b 100644 --- a/piano-analytics/src/main/java/io/piano/android/analytics/SendTask.kt +++ b/piano-analytics/src/main/java/io/piano/android/analytics/SendTask.kt @@ -24,6 +24,7 @@ internal class SendTask( ) : Runnable { override fun run() { eventRepository.deleteOldEvents(configuration.eventsOfflineStorageLifetime) + eventRepository.deleteOutOfLimitNotSentEvents(EVENTS_LIMIT) if (deviceInfoProvider.connectionType == ConnectionType.OFFLINE) { Timber.w("Can't send events - no connection") return @@ -61,5 +62,6 @@ internal class SendTask( companion object { internal val MEDIA_TYPE by lazy(LazyThreadSafetyMode.NONE) { "application/json; charset=UTF-8".toMediaType() } private const val CHUNK_SIZE = 50 + private const val EVENTS_LIMIT = 2000 } }