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

[Ft]:[DEVX-8764]:Clickstream Health refactor #28

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 1 addition & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ android {
useSupportLibrary = true
}

buildConfigField("String", "ACCOUNT_ID", "\"${props.getProperty("accountId")}\"")
buildConfigField("String", "SECRET_KEY", "\"${props.getProperty("secretKey")}\"")
buildConfigField("String", "ENDPOINT", "\"${props.getProperty("endpoint")}\"")
buildConfigField("String", "STUB_BEARER", "\"${props.getProperty("bearer")}\"")
buildConfigField("String", "API_KEY", "\"${props.getProperty("apiKey")}\"")
}

sourceSets {
Expand Down
51 changes: 30 additions & 21 deletions app/src/main/java/com/clickstream/app/App.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
package com.clickstream.app

import android.app.Application
import android.provider.Settings
import android.util.Base64
import clickstream.ClickStream
import clickstream.config.CSConfiguration
import clickstream.connection.CSConnectionEvent
import clickstream.connection.CSSocketConnectionListener
import clickstream.eventvisualiser.CSEventVisualiserListener
import clickstream.eventvisualiser.ui.CSEventVisualiserUI
import clickstream.health.constant.CSTrackedVia
import clickstream.lifecycle.impl.DefaultCSAppLifeCycleObserver
import clickstream.logger.CSLogLevel
import clickstream.logger.CSLogger
import com.clickstream.app.config.AccountId
import com.clickstream.app.config.EndPoint
import com.clickstream.app.config.SecretKey
import com.clickstream.app.config.StubBearer
import com.clickstream.app.config.csConfig
import com.clickstream.app.config.csInfo
import com.clickstream.app.config.getHealthGateway
import com.clickstream.app.helper.printMessage
import dagger.hilt.android.HiltAndroidApp
import kotlinx.coroutines.ExperimentalCoroutinesApi
import java.util.*

@OptIn(ExperimentalCoroutinesApi::class)
@HiltAndroidApp
Expand All @@ -28,25 +25,25 @@ class App : Application() {
override fun onCreate() {
super.onCreate()

val csLogger = CSLogger(CSLogLevel.DEBUG)

ClickStream.initialize(
configuration = CSConfiguration.Builder(
context = this,
info = csInfo(),
config = csConfig(
AccountId(BuildConfig.ACCOUNT_ID),
SecretKey(BuildConfig.SECRET_KEY),
EndPoint(BuildConfig.ENDPOINT),
StubBearer(BuildConfig.STUB_BEARER),
CSTrackedVia.Both
url = BuildConfig.ENDPOINT,
deviceId = deviceId(),
apiKey = String(
Base64.encode(
BuildConfig.API_KEY.toByteArray(),
Base64.NO_WRAP
)
)
),
appLifeCycle = DefaultCSAppLifeCycleObserver(csLogger)
)
.applyLogLevel()
.applyEventListener()
.applySocketConnectionListener()
.build()
info = csInfo()
).applyLogLevel()
.applyEventListener()
.applyHealthFactory()
.applySocketConnectionListener()
.build()
)

CSEventVisualiserUI.initialise(this)
Expand Down Expand Up @@ -77,4 +74,16 @@ class App : Application() {
})
return this
}

private fun CSConfiguration.Builder.applyHealthFactory(): CSConfiguration.Builder {
setHealthGateway(getHealthGateway(this@App))
return this
}

private fun deviceId(): String {
return Settings.Secure.getString(
contentResolver,
Settings.Secure.ANDROID_ID
) ?: UUID.randomUUID().toString()
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/clickstream/app/config/CSInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import clickstream.api.CSSessionInfo
import clickstream.api.CSUserInfo

fun csInfo() = CSInfo(
appInfo = CSAppInfo(appVersion = "1.1.0"),
appInfo = CSAppInfo(appVersion = "2.1.0"),
locationInfo = CSLocationInfo(
latitude = -6.1753871,
longitude = 106.8249641,
Expand Down
19 changes: 9 additions & 10 deletions app/src/main/java/com/clickstream/app/config/DefaultCSConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ import clickstream.config.CSConfig
import clickstream.config.CSEventProcessorConfig
import clickstream.config.CSEventSchedulerConfig
import clickstream.config.CSNetworkConfig
import clickstream.health.constant.CSTrackedVia
import clickstream.health.model.CSHealthEventConfig
import com.clickstream.app.helper.load

fun csConfig(
accountId: AccountId,
secretKey: SecretKey,
endpoint: EndPoint,
stubBearer: StubBearer,
trackedVia: CSTrackedVia
url: String,
deviceId: String,
apiKey: String,
): CSConfig {
val eventClassification =
CSEventClassificationParser::class.java.load("clickstream_classifier.json")!!
Expand All @@ -23,10 +20,12 @@ fun csConfig(
realtimeEvents = eventClassification.realTimeEvents(),
instantEvent = eventClassification.instantEvents()
),
eventSchedulerConfig = CSEventSchedulerConfig.default(),
eventSchedulerConfig = CSEventSchedulerConfig.default().copy(backgroundTaskEnabled = true),
networkConfig = CSNetworkConfig.default(
CSNetworkModule.create(accountId, secretKey, stubBearer)
).copy(endPoint = endpoint.value),
healthEventConfig = CSHealthEventConfig.default(trackedVia)
url = url, mapOf(
"Authorization" to "Basic $apiKey",
"X-UniqueId" to deviceId
)
),
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.clickstream.app.config

import DefaultCSHealthGateway
import android.content.Context
import android.util.Log
import clickstream.health.intermediate.CSHealthEventLoggerListener
import clickstream.health.intermediate.CSMemoryStatusProvider
import clickstream.health.model.CSHealthEventConfig
import clickstream.health.time.CSHealthTimeStampGenerator
import clickstream.logger.CSLogLevel
import clickstream.logger.CSLogger


fun getHealthGateway(context: Context) = DefaultCSHealthGateway(
context = context,
csInfo = csInfo(),
logger = CSLogger(CSLogLevel.DEBUG),
timeStampGenerator = timeStampGenerator(),
csMemoryStatusProvider = memoryStatusProvider(),
csHealthEventConfig = healthConfig(),
csHealthEventLoggerListener = healthEventLogger()
)


fun healthEventLogger() = object : CSHealthEventLoggerListener {
override fun logEvent(eventName: String, healthData: HashMap<String, Any>) {
Log.d("CS External Logger", "$eventName: $healthData")
}

}

fun memoryStatusProvider() = object : CSMemoryStatusProvider {
override fun isLowMemory(): Boolean {
return false
}
}

fun timeStampGenerator() = object : CSHealthTimeStampGenerator {
override fun getTimeStamp(): Long {
return System.currentTimeMillis()
}
}

fun healthConfig() =
CSHealthEventConfig(
minTrackedVersion = "1.1.0",
randomUserIdRemainder = (0..9).toList(),
destination = listOf("CS", "CT"),
verbosityLevel = "minimum"
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.clickstream.app.main

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import clickstream.CSEvent
import clickstream.ClickStream
import clickstream.model.CSEvent
import com.clickstream.app.helper.Dispatcher
import com.clickstream.app.helper.printMessage
import com.clickstream.app.main.MainIntent.ConnectIntent
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import plugin.DetektConfigurationPlugin

plugins {
id("org.jetbrains.dokka") version "1.4.32"
id("org.jetbrains.dokka") version "1.6.0"
}

apply(plugin = "binary-compatibility-validator")
Expand All @@ -17,7 +17,7 @@ buildscript {

dependencies {
classpath("com.android.tools.build:gradle:7.0.4")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.32")
classpath("com.google.protobuf:protobuf-gradle-plugin:0.8.14")
classpath("org.jetbrains.kotlinx:binary-compatibility-validator:0.8.0")
classpath("com.google.dagger:hilt-android-gradle-plugin:2.41")
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ plugins {

dependencies {
implementation("com.android.tools.build:gradle:7.0.4")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0")
implementation("com.github.node-gradle:gradle-node-plugin:2.2.0")
implementation("org.codehaus.groovy:groovy:3.0.9")
implementation("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.15.0")
Expand Down
10 changes: 6 additions & 4 deletions buildSrc/src/main/kotlin/deps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

object versions {
internal const val jacoco = "0.8.4"
internal const val detekt = "1.1.1"
internal const val detekt = "1.16.0"

internal const val kotlin = "1.4.32"
internal const val kotlin = "1.5.32"
internal const val coroutines = "1.5.2"

internal const val scarlet = "0.1.10"
internal const val okHttp = "3.12.1"

internal const val room = "2.2.3"
internal const val lifecycle = "2.2.0"
internal const val room = "2.4.2"
internal const val lifecycle = "2.4.1"

internal const val workManagerVersion = "2.7.1"

internal const val csProtoVersion = "1.18.2"

internal const val csEventListenerVersion = "2.0.0-alpha-1"
}

object deps {
Expand Down
2 changes: 2 additions & 0 deletions clickstream-health-metrics-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ android {
dependencies {
// Clickstream
compileOnly(files("$rootDir/libs/proto-sdk-1.18.6.jar"))
implementation(deps.kotlin.coroutines.core)


implementation(deps.android.core.annotation)
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
package clickstream.health

import androidx.annotation.RestrictTo
import clickstream.health.intermediate.CSEventHealthListener
import clickstream.health.intermediate.CSHealthEventFactory
import clickstream.health.intermediate.CSHealthEventProcessor
import clickstream.health.intermediate.CSHealthEventRepository

/**
* Wrapper class that creates [CSHealthEventProcessor].
*
* */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public interface CSHealthGateway {
public val eventHealthListener: CSEventHealthListener

public val healthEventRepository: CSHealthEventRepository
/**
* Class to process health events.
*
* */
public val healthEventProcessor: CSHealthEventProcessor?

public val healthEventProcessor: CSHealthEventProcessor
/**
* Clears health events on app version upgrade.
*
* */
public suspend fun clearHealthEventsForVersionChange()

public val healthEventFactory: CSHealthEventFactory
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package clickstream.health.constant

public object CSErrorConstant {
public const val PARSING_EXCEPTION: String = "parsing_exception"
public const val LOW_BATTERY: String = "low_battery"
public const val NETWORK_UNAVAILABLE: String = "network_unavailable"
public const val SOCKET_NOT_OPEN: String = "socket_not_open"
public const val UNKNOWN: String = "unknown"
public const val USER_UNAUTHORIZED: String = "401 Unauthorized"
public const val SOCKET_TIMEOUT: String = "socket_timeout"
public const val MAX_USER_LIMIT_REACHED: String = "max_user_limit_reached"
public const val MAX_CONNECTION_LIMIT_REACHED: String = "max_connection_limit_reached"
}
Loading
Loading