Skip to content

Storage customization fix #228

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

Merged
merged 2 commits into from
May 29, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ class AndroidStorage(
private val store: Store,
writeKey: String,
private val ioDispatcher: CoroutineDispatcher,
directory: String? = null
directory: String? = null,
subject: String? = null
) : Subscriber, Storage {

private val sharedPreferences: SharedPreferences =
context.getSharedPreferences("analytics-android-$writeKey", Context.MODE_PRIVATE)
override val storageDirectory: File = context.getDir(directory ?: "segment-disk-queue", Context.MODE_PRIVATE)
internal val eventsFile =
EventsFileManager(storageDirectory, writeKey, AndroidKVS(sharedPreferences))
EventsFileManager(storageDirectory, writeKey, AndroidKVS(sharedPreferences), subject)

override suspend fun subscribeToStore() {
store.subscribe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ import java.io.FileOutputStream
class EventsFileManager(
val directory: File,
private val writeKey: String,
private val kvs: KVS
private val kvs: KVS,
subject: String? = null
) {

init {
createDirectory(directory)
registerShutdownHook()
}

private val fileIndexKey = "segment.events.file.index.$writeKey"
private val fileIndexKey = if(subject == null) "segment.events.file.index.$writeKey" else "segment.events.file.index.$writeKey.$subject"

private var os: FileOutputStream? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ class StorageImpl(
private val store: Store,
writeKey: String,
private val ioDispatcher: CoroutineDispatcher,
directory: String? = null
directory: String? = null,
subject: String? = null
) : Subscriber, Storage {

override val storageDirectory = File(directory ?: "/tmp/analytics-kotlin/$writeKey")
private val storageDirectoryEvents = File(storageDirectory, "events")

internal val propertiesFile = PropertiesFile(storageDirectory, writeKey)
internal val eventsFile = EventsFileManager(storageDirectoryEvents, writeKey, propertiesFile)
internal val eventsFile = EventsFileManager(storageDirectoryEvents, writeKey, propertiesFile, subject)

init {
propertiesFile.load()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ internal class EventsFileManagerTest{
assertEquals(expectedContents, actualContents)
}

@Test
fun `check if filename includes subject`() = runTest {
val file = EventsFileManager(directory, "123", kvStore, "test")
val trackEvent = TrackEvent(
event = "clicked",
properties = buildJsonObject { put("behaviour", "good") })
.apply {
messageId = "qwerty-1234"
anonymousId = "anonId"
integrations = emptyJsonObject
context = emptyJsonObject
timestamp = epochTimestamp
}
val eventString = EncodeDefaultsJson.encodeToString(trackEvent)
file.storeEvent(eventString)
file.rollover()

assertEquals(1, kvStore.getInt("segment.events.file.index.123.test", -1))
}

@Test
fun `storeEvent stores in existing file if available`() = runTest {
val file = EventsFileManager(directory, "123", kvStore)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.segment.analytics.kotlin.core.platform.Plugin
import com.segment.analytics.kotlin.core.platform.policies.CountBasedFlushPolicy
import com.segment.analytics.kotlin.core.platform.policies.FrequencyFlushPolicy
import com.segment.analytics.kotlin.core.utilities.*
import java.net.HttpURLConnection

class MainApplication : Application() {
companion object {
Expand All @@ -22,7 +23,7 @@ class MainApplication : Application() {
override fun onCreate() {
super.onCreate()

analytics = Analytics(BuildConfig.SEGMENT_WRITE_KEY, applicationContext) {
analytics = Analytics("tteOFND0bb5ugJfALOJWpF0wu1tcxYgr", applicationContext) {
this.collectDeviceId = true
this.trackApplicationLifecycleEvents = true
this.trackDeepLinks = true
Expand All @@ -32,6 +33,15 @@ class MainApplication : Application() {
UnmeteredFlushPolicy(applicationContext) // Flush if network is not metered
)
this.flushPolicies = listOf(UnmeteredFlushPolicy(applicationContext))
this.requestFactory = object : RequestFactory() {
override fun upload(apiHost: String): HttpURLConnection {
val connection: HttpURLConnection = openConnection("https://$apiHost/b")
connection.setRequestProperty("Content-Type", "text/plain")
connection.doOutput = true
connection.setChunkedStreamingMode(0)
return connection
}
}
}
analytics.add(AndroidRecordScreenPlugin())
analytics.add(object : Plugin {
Expand Down