Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add remnant events migration plugin #40

Merged
merged 33 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
85a28b1
feat: add remnant events migration plugin
liuyang1520 Jun 24, 2022
2f998b3
delete events after migration
liuyang1520 Jun 25, 2022
68dfbd8
fix lint errors and add test file
liuyang1520 Jun 25, 2022
8cf06c2
slight refactor and add unit tests
liuyang1520 Jun 27, 2022
63debca
attempt to fix unit test failure
liuyang1520 Jun 27, 2022
2bfb684
attempt to fix unit test failure
liuyang1520 Jun 27, 2022
1bb7ab4
Merge branch 'main' of github.com:amplitude/Amplitude-Kotlin into add…
liuyang1520 Jun 27, 2022
576e2c6
use testImplementation instead of core, remove extension of Storage i…
liuyang1520 Jun 30, 2022
aec8802
Merge branch 'main' of github.com:amplitude/Amplitude-Kotlin into add…
liuyang1520 Jun 30, 2022
acc6857
Merge branch 'main' into add-remnant-events-migration-plugin
falconandy May 11, 2023
d760de4
added getDeviceId()/getUserId() methods, added instance name support
falconandy May 12, 2023
2843610
fix lint warnings
falconandy May 12, 2023
6c573cd
fix plugin logic (database version, library format)
falconandy May 12, 2023
3f3dee0
extend plugin logic (device/user id, identifies, intercepted identifies)
falconandy May 16, 2023
4a40c43
added support for db version 3
falconandy May 17, 2023
1706974
added test for db version 3
falconandy May 18, 2023
62f4337
move migration logic to 'migration' package
falconandy May 18, 2023
f7870ed
added test for missing legacy db
falconandy May 18, 2023
c0558c4
move MigrationPlugin to Initializer
falconandy May 18, 2023
9df4087
restore session data, do not clean device/user id, support first/next…
falconandy May 18, 2023
dfece83
Merge branch 'main' into add-remnant-events-migration-plugin
falconandy May 19, 2023
f97b7f7
fix tests
falconandy May 19, 2023
866cd6d
add short delay after each test to complete asynchronous handlers - t…
falconandy May 19, 2023
334b115
remove short delay after each test to complete asynchronous handlers …
falconandy May 22, 2023
3efc51e
add try/catch for each migrated event
falconandy May 22, 2023
d0cad5e
refactoring: added flag 'migrateLegacyData' instead of initializers
falconandy May 23, 2023
7ad4ad5
add test for migrateLegacyData=false
falconandy May 23, 2023
089c4fc
remove unnecessary permission READ_PHONE_STATE
falconandy May 24, 2023
d2386f7
move legacy 'uuid' field to 'insert_id'
falconandy May 24, 2023
2469017
make `build()` protected open for backward compatibility
falconandy May 24, 2023
848e1e9
write converted legacy event instead of json representation
falconandy May 25, 2023
d6c13d8
fix some "simple" build warnings
falconandy May 26, 2023
104e91d
make migrateLegacyData writable to set in Java code
falconandy May 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@ dependencies {

coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'

testImplementation 'io.mockk:mockk:1.10.6'
testImplementation 'io.mockk:mockk:1.12.4'
testImplementation project(':core')
testImplementation project(path: ':common-android')
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4'
testImplementation 'com.squareup.okhttp3:mockwebserver:4.10.0'

testImplementation 'org.junit.jupiter:junit-jupiter:5.7.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.7.2'
Expand Down
66 changes: 28 additions & 38 deletions android/src/main/java/com/amplitude/android/Amplitude.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.amplitude.android

import android.content.Context
import com.amplitude.android.migration.RemnantDataMigration
import com.amplitude.android.plugins.AnalyticsConnectorIdentityPlugin
import com.amplitude.android.plugins.AnalyticsConnectorPlugin
import com.amplitude.android.plugins.AndroidContextPlugin
Expand All @@ -9,15 +10,8 @@ import com.amplitude.core.Amplitude
import com.amplitude.core.events.BaseEvent
import com.amplitude.core.platform.plugins.AmplitudeDestination
import com.amplitude.core.platform.plugins.GetAmpliExtrasPlugin
import com.amplitude.core.utilities.AnalyticsIdentityListener
import com.amplitude.core.utilities.FileStorage
import com.amplitude.id.FileIdentityStorageProvider
import com.amplitude.id.IdentityConfiguration
import com.amplitude.id.IdentityContainer
import com.amplitude.id.IdentityUpdateType
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import kotlinx.coroutines.launch

open class Amplitude(
Expand All @@ -33,45 +27,41 @@ open class Amplitude(
}

init {
(timeline as Timeline).start()
registerShutdownHook()
}

override fun createTimeline(): Timeline {
return Timeline().also { it.amplitude = this }
}

override fun build(): Deferred<Boolean> {
val client = this

val built = amplitudeScope.async(amplitudeDispatcher, CoroutineStart.LAZY) {
storage = configuration.storageProvider.getStorage(client)

val storageDirectory = (configuration as Configuration).context.getDir("${FileStorage.STORAGE_PREFIX}-${configuration.instanceName}", Context.MODE_PRIVATE)
idContainer = IdentityContainer.getInstance(
IdentityConfiguration(
instanceName = configuration.instanceName,
apiKey = configuration.apiKey,
identityStorageProvider = FileIdentityStorageProvider(),
storageDirectory = storageDirectory,
logger = configuration.loggerProvider.getLogger(client)
)
)
val listener = AnalyticsIdentityListener(store)
idContainer.identityManager.addIdentityListener(listener)
if (idContainer.identityManager.isInitialized()) {
listener.onIdentityChanged(idContainer.identityManager.getIdentity(), IdentityUpdateType.Initialized)
}
androidContextPlugin = AndroidContextPlugin()
add(androidContextPlugin)
add(GetAmpliExtrasPlugin())
add(AndroidLifecyclePlugin())
add(AnalyticsConnectorIdentityPlugin())
add(AnalyticsConnectorPlugin())
add(AmplitudeDestination())
true
override fun createIdentityConfiguration(): IdentityConfiguration {
val configuration = configuration as Configuration
val storageDirectory = configuration.context.getDir("${FileStorage.STORAGE_PREFIX}-${configuration.instanceName}", Context.MODE_PRIVATE)

return IdentityConfiguration(
instanceName = configuration.instanceName,
apiKey = configuration.apiKey,
identityStorageProvider = configuration.identityStorageProvider,
storageDirectory = storageDirectory,
logger = configuration.loggerProvider.getLogger(this)
)
}

override suspend fun buildInternal(identityConfiguration: IdentityConfiguration) {
if ((this.configuration as Configuration).migrateLegacyData) {
RemnantDataMigration(this).execute()
}
return built
this.createIdentityContainer(identityConfiguration)

androidContextPlugin = AndroidContextPlugin()
add(androidContextPlugin)
add(GetAmpliExtrasPlugin())
add(AndroidLifecyclePlugin())
add(AnalyticsConnectorIdentityPlugin())
add(AnalyticsConnectorPlugin())
add(AmplitudeDestination())

(timeline as Timeline).start()
}

/**
Expand Down
8 changes: 6 additions & 2 deletions android/src/main/java/com/amplitude/android/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.amplitude.core.ServerZone
import com.amplitude.core.StorageProvider
import com.amplitude.core.events.IngestionMetadata
import com.amplitude.core.events.Plan
import com.amplitude.id.FileIdentityStorageProvider
import com.amplitude.id.IdentityStorageProvider

open class Configuration @JvmOverloads constructor(
apiKey: String,
Expand Down Expand Up @@ -39,8 +41,10 @@ open class Configuration @JvmOverloads constructor(
var minTimeBetweenSessionsMillis: Long = MIN_TIME_BETWEEN_SESSIONS_MILLIS,
var trackingSessionEvents: Boolean = true,
override var identifyBatchIntervalMillis: Long = IDENTIFY_BATCH_INTERVAL_MILLIS,
override var identifyInterceptStorageProvider: StorageProvider = AndroidStorageProvider()
) : Configuration(apiKey, flushQueueSize, flushIntervalMillis, instanceName, optOut, storageProvider, loggerProvider, minIdLength, partnerId, callback, flushMaxRetries, useBatch, serverZone, serverUrl, plan, ingestionMetadata, identifyBatchIntervalMillis, identifyInterceptStorageProvider) {
override var identifyInterceptStorageProvider: StorageProvider = AndroidStorageProvider(),
override var identityStorageProvider: IdentityStorageProvider = FileIdentityStorageProvider(),
val migrateLegacyData: Boolean = false,
) : Configuration(apiKey, flushQueueSize, flushIntervalMillis, instanceName, optOut, storageProvider, loggerProvider, minIdLength, partnerId, callback, flushMaxRetries, useBatch, serverZone, serverUrl, plan, ingestionMetadata, identifyBatchIntervalMillis, identifyInterceptStorageProvider, identityStorageProvider) {
yuhao900914 marked this conversation as resolved.
Show resolved Hide resolved
companion object {
const val MIN_TIME_BETWEEN_SESSIONS_MILLIS: Long = 300000
}
Expand Down
2 changes: 0 additions & 2 deletions android/src/main/java/com/amplitude/android/Timeline.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class Timeline : Timeline() {

internal fun start() {
amplitude.amplitudeScope.launch(amplitude.storageIODispatcher) {
amplitude.isBuilt.await()

_sessionId.set(amplitude.storage.read(Storage.Constants.PREVIOUS_SESSION_ID)?.toLongOrNull() ?: -1)
lastEventId = amplitude.storage.read(Storage.Constants.LAST_EVENT_ID)?.toLongOrNull() ?: 0
lastEventTime = amplitude.storage.read(Storage.Constants.LAST_EVENT_TIME)?.toLongOrNull() ?: -1
Expand Down
Loading