Skip to content

Commit

Permalink
feat: replace instanceName field with the writeKey wherever applicable (
Browse files Browse the repository at this point in the history
#401)

* feat: remove occurrence of instanceName field from the SDK

* feat: remove occurrence of instanceName field from the metrics reporter
  • Loading branch information
1abhishekpandey authored Mar 15, 2024
1 parent d8f3e6a commit faa2dd5
Show file tree
Hide file tree
Showing 18 changed files with 38 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import com.rudderstack.android.storage.AndroidStorageImpl
import com.rudderstack.android.utilities.shutdownSessionManagement
import com.rudderstack.core.Analytics
import com.rudderstack.core.ConfigDownloadService
import com.rudderstack.core.DEFAULTS_ANALYTICS_INSTANCE_NAME
import com.rudderstack.core.DataUploadService
import com.rudderstack.core.holder.associateState
import com.rudderstack.core.holder.retrieveState
Expand All @@ -45,19 +44,17 @@ import com.rudderstack.models.MessageContext
fun RudderAnalytics(
writeKey: String,
configuration: ConfigurationAndroid,
instanceName: String = DEFAULTS_ANALYTICS_INSTANCE_NAME,
dataUploadService: DataUploadService? = null,
configDownloadService: ConfigDownloadService? = null,
storage: AndroidStorage = AndroidStorageImpl(
configuration.application,
instanceName = instanceName,
writeKey = writeKey,
useContentProvider = ConfigurationAndroid.Defaults.USE_CONTENT_PROVIDER
),
initializationListener: ((success: Boolean, message: String?) -> Unit)? = null
): Analytics {
return Analytics(writeKey,
configuration,
instanceName,
dataUploadService,
configDownloadService,
storage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package com.rudderstack.android.compat;

import static com.rudderstack.core.AnalyticsKt.DEFAULTS_ANALYTICS_INSTANCE_NAME;

import androidx.annotation.NonNull;

Expand All @@ -40,10 +39,9 @@ public final class RudderAnalyticsBuilderCompat {
private DataUploadService dataUploadService = null;
private ConfigDownloadService configDownloadService = null;
private InitializationListener initializationListener = null;
private String instanceName = DEFAULTS_ANALYTICS_INSTANCE_NAME;
private AndroidStorage storage = new AndroidStorageImpl(configuration.getApplication(),
ConfigurationAndroid.Defaults.USE_CONTENT_PROVIDER,
instanceName = "test_instance",
writeKey,
Executors.newSingleThreadExecutor());

public RudderAnalyticsBuilderCompat(@NonNull String writeKey, @NonNull ConfigurationAndroid configuration) {
Expand All @@ -67,7 +65,6 @@ public Analytics build() {
return RudderAnalytics.RudderAnalytics(
writeKey,
configuration,
instanceName,
dataUploadService,
configDownloadService,
storage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ private const val RUDDER_SESSION_ID_KEY = "rl_session_id_key"
private const val RUDDER_SESSION_LAST_ACTIVE_TIMESTAMP_KEY =
"rl_last_event_timestamp_key"
internal class RudderPreferenceManager(application: Application,
private val instanceName: String) {
private val writeKey: String) {

private val String.key: String
get() = "$this-$instanceName"
get() = "$this-$writeKey"

private lateinit var preferences: SharedPreferences
private lateinit var preferencesV1: SharedPreferences
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ import java.util.concurrent.atomic.AtomicLong
class AndroidStorageImpl(
private val application: Application,
private val useContentProvider: Boolean = false,
private val instanceName: String,
private val writeKey: String,
private val storageExecutor: ExecutorService = Executors.newSingleThreadExecutor()
) : AndroidStorage {

private var logger: Logger? = null
private val dbName get() = "rs_persistence_$instanceName"
private val dbName get() = "rs_persistence_$writeKey"
private var jsonAdapter: JsonAdapter? = null

private var preferenceManager: RudderPreferenceManager? = null
Expand All @@ -58,9 +58,9 @@ class AndroidStorageImpl(
}

private val serverConfigFileName
get() = "$SERVER_CONFIG_FILE_NAME-{instanceName}"
get() = "$SERVER_CONFIG_FILE_NAME-{$writeKey}"
private val contextFileName
get() = "$CONTEXT_FILE_NAME-{instanceName}"
get() = "$CONTEXT_FILE_NAME-{$writeKey}"

private var messageDao: Dao<MessageEntity>? = null

Expand Down Expand Up @@ -393,7 +393,7 @@ class AndroidStorageImpl(

override fun setup(analytics: Analytics) {
initDb(analytics)
preferenceManager = RudderPreferenceManager(application, analytics.instanceName)
preferenceManager = RudderPreferenceManager(application, writeKey)
_optOut = AtomicBoolean(preferenceManager?.optStatus ?: false)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package com.rudderstack.android.utilities

import android.content.Context
import com.rudderstack.android.internal.RudderPreferenceManager
import com.rudderstack.android.storage.fileExists
import java.io.ByteArrayOutputStream
import java.nio.charset.StandardCharsets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ abstract class AndroidStorageTest {
MockitoAnnotations.openMocks(this)
val storage = AndroidStorageImpl(
ApplicationProvider.getApplicationContext(),
false, instanceName = "test_instance",
false, writeKey = "test_writeKey",
storageExecutor = TestExecutor()
)
mockConfig = ConfigurationAndroid(ApplicationProvider.getApplicationContext(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import androidx.test.core.app.ApplicationProvider
import com.rudderstack.android.android.utils.TestExecutor
import com.rudderstack.android.contextState
import com.rudderstack.android.internal.states.ContextState
import com.rudderstack.android.processNewContext
import com.rudderstack.android.storage.AndroidStorageImpl
import com.rudderstack.core.Analytics
import com.rudderstack.core.Configuration
Expand All @@ -20,7 +19,6 @@ import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

Expand All @@ -34,7 +32,7 @@ class ResetImplementationPluginTest{
fun setup(){
analytics = generateTestAnalytics(Configuration(jsonAdapter = mock (),),
storage = AndroidStorageImpl(ApplicationProvider.getApplicationContext(),
instanceName = "test_instance",
writeKey = "test_writeKey",
storageExecutor = TestExecutor()))
analytics.associateState(ContextState())
}
Expand Down Expand Up @@ -63,4 +61,4 @@ class ResetImplementationPluginTest{
))
analytics.shutdown()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ReinstatePluginTest {
fun setUp() {
androidStorage = AndroidStorageImpl(
ApplicationProvider.getApplicationContext(),
instanceName = "testInstance",
writeKey = "testWriteKey",
storageExecutor = TestExecutor()
)
mockControlPlane = mock<ConfigDownloadService>()
Expand Down Expand Up @@ -198,4 +198,4 @@ class ReinstatePluginTest {
hasProperty("eventName", `is`("message3"))
)
}
}
}
6 changes: 2 additions & 4 deletions core/src/main/java/com/rudderstack/core/Analytics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import com.rudderstack.models.ScreenProperties
import com.rudderstack.models.TrackMessage
import com.rudderstack.models.TrackProperties

const val DEFAULTS_ANALYTICS_INSTANCE_NAME = "default"
class Analytics private constructor(
private val _delegate: AnalyticsDelegate,
) : Controller by _delegate {
Expand All @@ -50,7 +49,6 @@ class Analytics private constructor(
constructor(
writeKey: String,
configuration: Configuration,
instanceName: String = DEFAULTS_ANALYTICS_INSTANCE_NAME,
dataUploadService: DataUploadService? = null,
configDownloadService: ConfigDownloadService? = null,
storage: Storage? = null,
Expand All @@ -60,7 +58,7 @@ class Analytics private constructor(
shutdownHook: (Analytics.() -> Unit)? = null
) : this(
_delegate = AnalyticsDelegate(
configuration, storage?:BasicStorageImpl(), instanceName, dataUploadService ?:
configuration, storage?:BasicStorageImpl(), writeKey, dataUploadService ?:
DataUploadServiceImpl(
writeKey
), configDownloadService ?: ConfigDownloadServiceImpl(
Expand Down Expand Up @@ -310,4 +308,4 @@ class Analytics private constructor(
group(groupScope.message, groupScope.options)
}

}
}
9 changes: 5 additions & 4 deletions core/src/main/java/com/rudderstack/core/Controller.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ interface Controller {
val configDownloadService:ConfigDownloadService?

/**
* The name of the instance
* In case of multiple instances, this name is used to differentiate between them
* The write key
* In case of multiple instances, this key is used to differentiate between them
*/
val instanceName: String
val writeKey: String

fun addPlugin(vararg plugins: Plugin)
/**
* Custom plugins to be removed.
Expand Down Expand Up @@ -164,4 +165,4 @@ interface Controller {
*/
fun reset()

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

package com.rudderstack.core.compat;

import static com.rudderstack.core.AnalyticsKt.DEFAULTS_ANALYTICS_INSTANCE_NAME;

import com.rudderstack.core.Analytics;
import com.rudderstack.core.BasicStorageImpl;
import com.rudderstack.core.ConfigDownloadService;
Expand All @@ -34,8 +32,6 @@ public class AnalyticsBuilderCompat {

private final Configuration configuration;
private Storage storage = new BasicStorageImpl();

private String instanceName = DEFAULTS_ANALYTICS_INSTANCE_NAME;
private DataUploadService dataUploadService = null;
private ConfigDownloadService configDownloadService = null;

Expand All @@ -47,11 +43,6 @@ public AnalyticsBuilderCompat(String writeKey, Configuration configuration) {
this.configuration = configuration;
}

public AnalyticsBuilderCompat withInstanceName(String instanceName) {
this.instanceName = instanceName;
return this;
}

public AnalyticsBuilderCompat withDataUploadService(DataUploadService dataUploadService) {
this.dataUploadService = dataUploadService;
return this;
Expand Down Expand Up @@ -84,7 +75,7 @@ public AnalyticsBuilderCompat withStorage(Storage storage) {
}

public Analytics build() {
return new Analytics(writeKey, configuration, instanceName,
return new Analytics(writeKey, configuration,
dataUploadService == null ? new DataUploadServiceImpl(
writeKey) : dataUploadService, configDownloadService == null ?
new ConfigDownloadServiceImpl(writeKey) : configDownloadService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class IntervalBasedFlushPolicy : FlushPolicy {

override fun setup(analytics: Analytics) {
_isShutDown.set(false)
thresholdCountDownTimer = Timer(analytics.instanceName + "-IntervalBasedFlushPolicy")
thresholdCountDownTimer = Timer(analytics.writeKey + "-IntervalBasedFlushPolicy")
_analyticsRef.set(analytics)
}

Expand Down Expand Up @@ -103,4 +103,4 @@ class IntervalBasedFlushPolicy : FlushPolicy {
onRemoved()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ import java.util.concurrent.ConcurrentHashMap
*/
private val store = ConcurrentHashMap<String, ConcurrentHashMap<String, Any?>>()
fun Controller.store(identifier : String, value: Any){
val analyticsStorageMap = store[this.instanceName] ?: ConcurrentHashMap<String, Any?>().also {
store[instanceName] = it
val analyticsStorageMap = store[this.writeKey] ?: ConcurrentHashMap<String, Any?>().also {
store[writeKey] = it
}
analyticsStorageMap[identifier] = value
}
fun Controller.remove(identifier : String){
val analyticsStorageMap = store[this.instanceName] ?: return
val analyticsStorageMap = store[this.writeKey] ?: return
analyticsStorageMap.remove(identifier)
}
fun <T> Controller.retrieve(identifier: String) : T?{
val analyticsStorageMap = store[this.instanceName] ?: return null
val analyticsStorageMap = store[this.writeKey] ?: return null
return analyticsStorageMap[identifier] as? T
}

fun Controller.clearAll(){
store.remove(this.instanceName)
store.remove(this.writeKey)
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import com.rudderstack.core.Plugin
import com.rudderstack.core.RudderOptions
import com.rudderstack.core.RudderUtils.getUTF8Length
import com.rudderstack.core.RudderUtils.MAX_BATCH_SIZE
import com.rudderstack.core.State
import com.rudderstack.core.Storage
import com.rudderstack.core.flushpolicy.CountBasedFlushPolicy
import com.rudderstack.core.flushpolicy.IntervalBasedFlushPolicy
Expand Down Expand Up @@ -61,7 +60,7 @@ import java.util.concurrent.atomic.AtomicBoolean
internal class AnalyticsDelegate(
configuration: Configuration,
override val storage: Storage,
override val instanceName: String,
override val writeKey: String,
override val dataUploadService: DataUploadService,
override val configDownloadService: ConfigDownloadService?,
//optional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AnalyticsHolderTest {

@Before
fun setup() {
whenever(controller.instanceName).thenReturn("testInstance")
whenever(controller.writeKey).thenReturn("writeKey")
}
@After
fun tearDown() {
Expand Down Expand Up @@ -71,7 +71,7 @@ class AnalyticsHolderTest {
val valueForController1 = "testValue"
val valueForController2 = "testValue2"
val controller2 = mock<Controller>()
whenever(controller2.instanceName).thenReturn("testInstance2")
whenever(controller2.writeKey).thenReturn("writeKey2")

// Execute
controller.store(identifier, valueForController1)
Expand All @@ -81,4 +81,4 @@ class AnalyticsHolderTest {
assertEquals(valueForController1, controller.retrieve(identifier))
assertEquals(valueForController2, controller2.retrieve(identifier))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class StatesHolderTest {

@Before
fun setup() {
whenever(controller.instanceName).thenReturn("testInstance")
whenever(controller.writeKey).thenReturn("testInstance")
}
@After
fun tearDown() {
Expand Down Expand Up @@ -76,4 +76,4 @@ class StatesHolderTest {
// Verify
assertThat(controller.retrieveState<TestState>(), Matchers.nullValue())
}
}
}
Loading

0 comments on commit faa2dd5

Please sign in to comment.