From c6e4be01fe4adc9b09d424e30db6650c2043416d Mon Sep 17 00:00:00 2001 From: radityagumay Date: Wed, 1 Jun 2022 22:49:37 +0700 Subject: [PATCH] [Improvement] : Move CSHealthMetrics to Optional Setter --- CHANGELOG.md | 10 ++++++-- README.md | 2 +- buildSrc/src/main/kotlin/deps.kt | 1 - .../health/time/CSTimeStampGenerator.kt | 4 +--- .../health/DefaultCSHealthGateway.kt | 7 +++--- .../internal/DefaultCSHealthEventFactory.kt | 2 +- clickstream/build.gradle.kts | 4 ++++ .../clickstream/config/CSConfiguration.kt | 24 ++++++++++++++++--- .../di/impl/DefaultCServiceLocator.kt | 4 ++-- .../eventscheduler/CSBackgroundScheduler.kt | 2 +- .../eventscheduler/CSEventScheduler.kt | 8 +++---- .../networklayer/CSNetworkRepository.kt | 2 +- .../networklayer/CSRetryableCallback.kt | 2 +- .../clickstream/ClickStreamFunctionalTest.kt | 8 +++---- .../impl/DefaultCSHealthEventFactoryTest.kt | 2 +- .../CSBackgroundSchedulerTest.kt | 2 +- .../eventscheduler/CSEventSchedulerTest.kt | 2 +- .../networklayer/CSRetryableCallbackTest.kt | 2 +- scripts/versioning.gradle | 2 +- 19 files changed, 57 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8436fa68..4a5887f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,13 @@ Based on https://keepachangelog.com/en/1.0.0/ -## 0.2.36-snapshot +## 0.2.39 ### Update -- Support Pluggable HealthMetrics \ No newline at end of file +- [Improvement] : Move CSHealthMetrics to Optional Setter + + +## 0.2.36 + +### Update +- [[Feature] : Support Pluggable HealthMetrics](https://github.com/gojek/clickstream-android/discussions/6) \ No newline at end of file diff --git a/README.md b/README.md index 50ffe542..4d0af8ba 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,6 @@ dependencies { // Required implementation 'com.gojek.clickstream:clickstream-android:[latest_version]' implementation 'com.gojek.clickstream:clickstream-lifecycle:[latest_version]' - implementation 'com.gojek.clickstream:clickstream-health-metrics-noop:[latest_version]' // Optional implementation 'com.gojek.clickstream:clickstream-health-metrics:[latest_version]' @@ -105,6 +104,7 @@ class App : Application() { healthGateway = DefaultOpCSHealthGateway.factory(/*args*/) ).apply { setLogLevel(DEBUG) + /**/ setCSSocketConnectionListener(connectionListener()) }.build()) } diff --git a/buildSrc/src/main/kotlin/deps.kt b/buildSrc/src/main/kotlin/deps.kt index 126db54e..39270edd 100644 --- a/buildSrc/src/main/kotlin/deps.kt +++ b/buildSrc/src/main/kotlin/deps.kt @@ -126,7 +126,6 @@ object deps { object workManager { const val workRuntimeKtx = "androidx.work:work-runtime-ktx:${versions.workManagerVersion}" const val workTesting = "androidx.work:work-testing:${versions.workManagerVersion}" - } object utils { diff --git a/clickstream-health-metrics-api/src/main/kotlin/clickstream/health/time/CSTimeStampGenerator.kt b/clickstream-health-metrics-api/src/main/kotlin/clickstream/health/time/CSTimeStampGenerator.kt index a39904aa..958e1821 100644 --- a/clickstream-health-metrics-api/src/main/kotlin/clickstream/health/time/CSTimeStampGenerator.kt +++ b/clickstream-health-metrics-api/src/main/kotlin/clickstream/health/time/CSTimeStampGenerator.kt @@ -1,6 +1,4 @@ -package clickstream.health - -import clickstream.health.time.CSEventGeneratedTimestampListener +package clickstream.health.time public interface CSTimeStampGenerator { public fun getTimeStamp(): Long diff --git a/clickstream-health-metrics/src/main/kotlin/clickstream/health/DefaultCSHealthGateway.kt b/clickstream-health-metrics/src/main/kotlin/clickstream/health/DefaultCSHealthGateway.kt index 0d66af8b..01b2ef47 100644 --- a/clickstream-health-metrics/src/main/kotlin/clickstream/health/DefaultCSHealthGateway.kt +++ b/clickstream-health-metrics/src/main/kotlin/clickstream/health/DefaultCSHealthGateway.kt @@ -15,23 +15,24 @@ import clickstream.health.internal.DefaultCSHealthEventFactory import clickstream.health.internal.DefaultCSHealthEventProcessor import clickstream.health.internal.DefaultCSHealthEventRepository import clickstream.health.model.CSHealthEventConfig +import clickstream.health.time.CSTimeStampGenerator import clickstream.lifecycle.CSAppLifeCycle import clickstream.logger.CSLogger import clickstream.util.CSAppVersionSharedPref import clickstream.util.impl.DefaultCSAppVersionSharedPref -import java.util.UUID import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Dispatchers public object DefaultCSHealthGateway { public fun factory( + appVersion: String, + sessionId: String, context: Context, healthEventConfig: CSHealthEventConfig, csInfo: CSInfo, logger: CSLogger, healthEventLogger: CSHealthEventLoggerListener, - appVersion: String, timeStampGenerator: CSTimeStampGenerator, metaProvider: CSMetaProvider, eventHealthListener: CSEventHealthListener, @@ -45,7 +46,7 @@ public object DefaultCSHealthGateway { override val eventHealthListener: CSEventHealthListener = eventHealthListener override val healthEventRepository: CSHealthEventRepository by lazy { DefaultCSHealthEventRepository( - sessionId = UUID.randomUUID().toString(), + sessionId = sessionId, healthEventDao = CSHealthDatabase.getInstance(context).healthEventDao(), info = csInfo ) diff --git a/clickstream-health-metrics/src/main/kotlin/clickstream/health/internal/DefaultCSHealthEventFactory.kt b/clickstream-health-metrics/src/main/kotlin/clickstream/health/internal/DefaultCSHealthEventFactory.kt index c7b5c02d..27be101e 100644 --- a/clickstream-health-metrics/src/main/kotlin/clickstream/health/internal/DefaultCSHealthEventFactory.kt +++ b/clickstream-health-metrics/src/main/kotlin/clickstream/health/internal/DefaultCSHealthEventFactory.kt @@ -2,7 +2,7 @@ package clickstream.health.internal import androidx.annotation.RestrictTo import clickstream.api.CSMetaProvider -import clickstream.health.CSTimeStampGenerator +import clickstream.health.time.CSTimeStampGenerator import clickstream.health.identity.CSGuIdGenerator import clickstream.health.intermediate.CSHealthEventFactory import com.gojek.clickstream.internal.Health diff --git a/clickstream/build.gradle.kts b/clickstream/build.gradle.kts index 9acf3278..d4722095 100644 --- a/clickstream/build.gradle.kts +++ b/clickstream/build.gradle.kts @@ -34,10 +34,14 @@ dependencies { // Clickstream implementation(files("$rootDir/libs/proto-sdk-1.18.6.jar")) api(projects.clickstreamLogger) + api(projects.clickstreamHealthMetricsNoop) compileOnly(projects.clickstreamApi) compileOnly(projects.clickstreamHealthMetricsApi) compileOnly(projects.clickstreamLifecycle) + // Proto + api(deps.utils.protoLite) + // Common deps.common.list.forEach(::implementation) diff --git a/clickstream/src/main/kotlin/clickstream/config/CSConfiguration.kt b/clickstream/src/main/kotlin/clickstream/config/CSConfiguration.kt index b296a0ab..4eb8df0d 100644 --- a/clickstream/src/main/kotlin/clickstream/config/CSConfiguration.kt +++ b/clickstream/src/main/kotlin/clickstream/config/CSConfiguration.kt @@ -12,6 +12,7 @@ import clickstream.health.intermediate.CSHealthEventLoggerListener import clickstream.health.intermediate.CSHealthEventProcessor import clickstream.health.intermediate.CSHealthEventRepository import clickstream.health.CSHealthGateway +import clickstream.health.NoOpCSHealthGateway import clickstream.health.time.CSEventGeneratedTimestampListener import clickstream.internal.NoOpCSEventHealthListener import clickstream.internal.analytics.impl.NoOpCSHealthEventLogger @@ -88,14 +89,17 @@ public class CSConfiguration private constructor( */ private val config: CSConfig, - private val appLifeCycle: CSAppLifeCycle, - - private val healthGateway: CSHealthGateway + /** + * Specify Clicstream lifecycle, this is needed in order to send events + * to the backend. + */ + private val appLifeCycle: CSAppLifeCycle ) { private lateinit var dispatcher: CoroutineDispatcher private lateinit var eventGeneratedListener: CSEventGeneratedTimestampListener private lateinit var socketConnectionListener: CSSocketConnectionListener private lateinit var remoteConfig: CSRemoteConfig + private lateinit var healthGateway: CSHealthGateway private var logLevel: CSLogLevel = CSLogLevel.OFF /** @@ -155,6 +159,17 @@ public class CSConfiguration private constructor( this.remoteConfig = remoteConfig } + /** + * Specify implementation of [CSHealthGateway], by default it would use + * [NoOpCSHealthGateway] + * + * @return This [Builder] instance + */ + public fun setHealthGateway(healthGateway: CSHealthGateway): Builder = + apply { + this.healthGateway = healthGateway + } + /** * Builds a [CSConfiguration] object. * @@ -173,6 +188,9 @@ public class CSConfiguration private constructor( if (::remoteConfig.isInitialized.not()) { remoteConfig = NoOpCSRemoteConfig() } + if(::healthGateway.isInitialized.not()) { + healthGateway = NoOpCSHealthGateway.factory() + } return CSConfiguration( context, dispatcher, info, config, diff --git a/clickstream/src/main/kotlin/clickstream/internal/di/impl/DefaultCServiceLocator.kt b/clickstream/src/main/kotlin/clickstream/internal/di/impl/DefaultCServiceLocator.kt index f63a64ce..a3e5fbeb 100644 --- a/clickstream/src/main/kotlin/clickstream/internal/di/impl/DefaultCServiceLocator.kt +++ b/clickstream/src/main/kotlin/clickstream/internal/di/impl/DefaultCServiceLocator.kt @@ -7,8 +7,6 @@ import clickstream.config.CSConfig import clickstream.config.CSEventSchedulerConfig import clickstream.config.CSRemoteConfig import clickstream.connection.CSSocketConnectionListener -import clickstream.health.CSTimeStampGenerator -import clickstream.health.DefaultCSTimeStampGenerator import clickstream.health.identity.CSGuIdGenerator import clickstream.health.identity.DefaultCSGuIdGenerator import clickstream.health.intermediate.CSEventHealthListener @@ -16,6 +14,8 @@ import clickstream.health.intermediate.CSHealthEventFactory import clickstream.health.intermediate.CSHealthEventProcessor import clickstream.health.intermediate.CSHealthEventRepository import clickstream.health.time.CSEventGeneratedTimestampListener +import clickstream.health.time.CSTimeStampGenerator +import clickstream.health.time.DefaultCSTimeStampGenerator import clickstream.internal.db.CSDatabase import clickstream.internal.di.CSServiceLocator import clickstream.internal.eventprocessor.CSEventProcessor diff --git a/clickstream/src/main/kotlin/clickstream/internal/eventscheduler/CSBackgroundScheduler.kt b/clickstream/src/main/kotlin/clickstream/internal/eventscheduler/CSBackgroundScheduler.kt index 4a4f0eef..a957f10e 100644 --- a/clickstream/src/main/kotlin/clickstream/internal/eventscheduler/CSBackgroundScheduler.kt +++ b/clickstream/src/main/kotlin/clickstream/internal/eventscheduler/CSBackgroundScheduler.kt @@ -2,13 +2,13 @@ package clickstream.internal.eventscheduler import clickstream.api.CSInfo import clickstream.config.CSEventSchedulerConfig -import clickstream.health.CSTimeStampGenerator import clickstream.health.constant.CSEventNamesConstant.ClickStreamFlushOnBackground import clickstream.health.constant.CSEventTypesConstant import clickstream.health.identity.CSGuIdGenerator import clickstream.health.intermediate.CSEventHealthListener import clickstream.health.intermediate.CSHealthEventRepository import clickstream.health.model.CSHealthEventDTO +import clickstream.health.time.CSTimeStampGenerator import clickstream.internal.di.CSServiceLocator import clickstream.internal.networklayer.CSBackgroundNetworkManager import clickstream.internal.utils.CSBatteryStatusObserver diff --git a/clickstream/src/main/kotlin/clickstream/internal/eventscheduler/CSEventScheduler.kt b/clickstream/src/main/kotlin/clickstream/internal/eventscheduler/CSEventScheduler.kt index bfdcce14..d06ab40e 100644 --- a/clickstream/src/main/kotlin/clickstream/internal/eventscheduler/CSEventScheduler.kt +++ b/clickstream/src/main/kotlin/clickstream/internal/eventscheduler/CSEventScheduler.kt @@ -3,7 +3,6 @@ package clickstream.internal.eventscheduler import clickstream.api.CSInfo import clickstream.config.CSEventSchedulerConfig import clickstream.extension.isValidMessage -import clickstream.health.CSTimeStampGenerator import clickstream.health.constant.CSEventNamesConstant.ClickStreamEventBatchCreated import clickstream.health.constant.CSEventNamesConstant.ClickStreamEventBatchTriggerFailed import clickstream.health.constant.CSEventNamesConstant.ClickStreamEventCached @@ -13,6 +12,7 @@ import clickstream.health.identity.CSGuIdGenerator import clickstream.health.intermediate.CSEventHealthListener import clickstream.health.intermediate.CSHealthEventRepository import clickstream.health.model.CSHealthEventDTO +import clickstream.health.time.CSTimeStampGenerator import clickstream.internal.analytics.CSErrorReasons import clickstream.internal.networklayer.CSNetworkManager import clickstream.internal.utils.CSBatteryLevel @@ -236,12 +236,12 @@ internal open class CSEventScheduler( if (batch.isNotEmpty() && batch[0].messageName != Health::class.qualifiedName.orEmpty()) { logger.debug { "CSEventScheduler#forwardEvents#batch - " + - "eventBatchId : ${eventRequest.reqGuid}, " + - "eventId : ${batch.joinToString { it.eventGuid }}" + "eventBatchId : ${eventRequest.reqGuid}, " + + "eventId : ${batch.joinToString { it.eventGuid }}" } logger.debug { "CSEventScheduler#forwardEvents#batch - " + - "messageName : ${batch[0].messageName}" + "messageName : ${batch[0].messageName}" } logHealthEvent( CSHealthEventDTO( diff --git a/clickstream/src/main/kotlin/clickstream/internal/networklayer/CSNetworkRepository.kt b/clickstream/src/main/kotlin/clickstream/internal/networklayer/CSNetworkRepository.kt index aac43f5c..5e309317 100644 --- a/clickstream/src/main/kotlin/clickstream/internal/networklayer/CSNetworkRepository.kt +++ b/clickstream/src/main/kotlin/clickstream/internal/networklayer/CSNetworkRepository.kt @@ -3,7 +3,7 @@ package clickstream.internal.networklayer import clickstream.api.CSInfo import clickstream.config.CSNetworkConfig import clickstream.health.intermediate.CSHealthEventRepository -import clickstream.health.CSTimeStampGenerator +import clickstream.health.time.CSTimeStampGenerator import clickstream.internal.utils.CSCallback import clickstream.logger.CSLogger import com.gojek.clickstream.de.EventRequest diff --git a/clickstream/src/main/kotlin/clickstream/internal/networklayer/CSRetryableCallback.kt b/clickstream/src/main/kotlin/clickstream/internal/networklayer/CSRetryableCallback.kt index 2db56bed..179cdf30 100644 --- a/clickstream/src/main/kotlin/clickstream/internal/networklayer/CSRetryableCallback.kt +++ b/clickstream/src/main/kotlin/clickstream/internal/networklayer/CSRetryableCallback.kt @@ -3,11 +3,11 @@ package clickstream.internal.networklayer import clickstream.api.CSInfo import clickstream.config.CSNetworkConfig import clickstream.extension.isHealthEvent -import clickstream.health.CSTimeStampGenerator import clickstream.health.constant.CSEventNamesConstant import clickstream.health.constant.CSEventTypesConstant import clickstream.health.intermediate.CSHealthEventRepository import clickstream.health.model.CSHealthEventDTO +import clickstream.health.time.CSTimeStampGenerator import clickstream.internal.analytics.CSErrorReasons import clickstream.internal.utils.CSTimeStampMessageBuilder import clickstream.logger.CSLogger diff --git a/clickstream/src/test/kotlin/clickstream/ClickStreamFunctionalTest.kt b/clickstream/src/test/kotlin/clickstream/ClickStreamFunctionalTest.kt index 081c9dd7..2dcfa993 100644 --- a/clickstream/src/test/kotlin/clickstream/ClickStreamFunctionalTest.kt +++ b/clickstream/src/test/kotlin/clickstream/ClickStreamFunctionalTest.kt @@ -69,8 +69,7 @@ public class ClickStreamFunctionalTest { info = csInfo, config = createCSConfig(), appLifeCycle = appLifecycle, - healthGateway = fakeHealthGateway - ).setDispatcher(coroutineRule.testDispatcher).build() + ).setDispatcher(coroutineRule.testDispatcher).setHealthGateway(fakeHealthGateway).build() ) sut = ClickStream.getInstance() @@ -98,9 +97,8 @@ public class ClickStreamFunctionalTest { context = app, info = csInfo, config = createCSConfig(), - appLifeCycle = appLifecycle, - healthGateway = fakeHealthGateway - ).setDispatcher(coroutineRule.testDispatcher).build() + appLifeCycle = appLifecycle + ).setDispatcher(coroutineRule.testDispatcher).setHealthGateway(fakeHealthGateway).build() ) sut = ClickStream.getInstance() diff --git a/clickstream/src/test/kotlin/clickstream/internal/eventprocessor/impl/DefaultCSHealthEventFactoryTest.kt b/clickstream/src/test/kotlin/clickstream/internal/eventprocessor/impl/DefaultCSHealthEventFactoryTest.kt index 022a35ad..a4ec596a 100644 --- a/clickstream/src/test/kotlin/clickstream/internal/eventprocessor/impl/DefaultCSHealthEventFactoryTest.kt +++ b/clickstream/src/test/kotlin/clickstream/internal/eventprocessor/impl/DefaultCSHealthEventFactoryTest.kt @@ -2,7 +2,7 @@ package clickstream.internal.eventprocessor.impl import clickstream.api.CSMetaProvider import clickstream.extension.protoName -import clickstream.health.CSTimeStampGenerator +import clickstream.health.time.CSTimeStampGenerator import clickstream.health.identity.CSGuIdGenerator import clickstream.health.internal.DefaultCSHealthEventFactory import com.gojek.clickstream.internal.Health diff --git a/clickstream/src/test/kotlin/clickstream/internal/eventscheduler/CSBackgroundSchedulerTest.kt b/clickstream/src/test/kotlin/clickstream/internal/eventscheduler/CSBackgroundSchedulerTest.kt index 7ef16707..5487891d 100644 --- a/clickstream/src/test/kotlin/clickstream/internal/eventscheduler/CSBackgroundSchedulerTest.kt +++ b/clickstream/src/test/kotlin/clickstream/internal/eventscheduler/CSBackgroundSchedulerTest.kt @@ -4,7 +4,7 @@ import clickstream.config.CSEventSchedulerConfig import clickstream.fake.FakeEventBatchDao import clickstream.fake.defaultEventWrapperData import clickstream.fake.fakeInfo -import clickstream.health.CSTimeStampGenerator +import clickstream.health.time.CSTimeStampGenerator import clickstream.health.identity.CSGuIdGenerator import clickstream.health.intermediate.CSHealthEventProcessor import clickstream.health.intermediate.CSHealthEventRepository diff --git a/clickstream/src/test/kotlin/clickstream/internal/eventscheduler/CSEventSchedulerTest.kt b/clickstream/src/test/kotlin/clickstream/internal/eventscheduler/CSEventSchedulerTest.kt index c6bafd42..15390243 100644 --- a/clickstream/src/test/kotlin/clickstream/internal/eventscheduler/CSEventSchedulerTest.kt +++ b/clickstream/src/test/kotlin/clickstream/internal/eventscheduler/CSEventSchedulerTest.kt @@ -4,7 +4,7 @@ import clickstream.config.CSEventSchedulerConfig import clickstream.fake.FakeEventBatchDao import clickstream.fake.defaultEventWrapperData import clickstream.fake.fakeInfo -import clickstream.health.CSTimeStampGenerator +import clickstream.health.time.CSTimeStampGenerator import clickstream.health.identity.CSGuIdGenerator import clickstream.health.intermediate.CSHealthEventRepository import clickstream.internal.eventscheduler.impl.DefaultCSEventRepository diff --git a/clickstream/src/test/kotlin/clickstream/internal/networklayer/CSRetryableCallbackTest.kt b/clickstream/src/test/kotlin/clickstream/internal/networklayer/CSRetryableCallbackTest.kt index 20331df4..d4f8c70e 100644 --- a/clickstream/src/test/kotlin/clickstream/internal/networklayer/CSRetryableCallbackTest.kt +++ b/clickstream/src/test/kotlin/clickstream/internal/networklayer/CSRetryableCallbackTest.kt @@ -2,7 +2,7 @@ package clickstream.internal.networklayer import clickstream.config.CSNetworkConfig import clickstream.fake.fakeCSInfo -import clickstream.health.CSTimeStampGenerator +import clickstream.health.time.CSTimeStampGenerator import clickstream.health.intermediate.CSHealthEventRepository import clickstream.internal.utils.CSFlowStreamAdapterFactory import clickstream.logger.CSLogLevel.OFF diff --git a/scripts/versioning.gradle b/scripts/versioning.gradle index 2fa5d04d..993e7300 100644 --- a/scripts/versioning.gradle +++ b/scripts/versioning.gradle @@ -1,3 +1,3 @@ ext { - gitVersionName = "0.2.36" + gitVersionName = "0.2.39" } \ No newline at end of file