Skip to content

Commit

Permalink
Storage customization fix (#228)
Browse files Browse the repository at this point in the history
* add subject to file index key

* add unit test

---------

Co-authored-by: Wenxi Zeng <[email protected]>
  • Loading branch information
wenxi-zeng and Wenxi Zeng authored May 29, 2024
1 parent 1ccea75 commit c308c69
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
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

0 comments on commit c308c69

Please sign in to comment.