NTP-60143 | Alfred events fixes (#335)

Co-authored-by: amansingh <aman.s@navi.com>
This commit is contained in:
Sayed Owais Ali
2025-05-27 16:26:32 +05:30
committed by GitHub
parent f7a84abae1
commit 86b540db29
9 changed files with 72 additions and 20 deletions

View File

@@ -192,13 +192,9 @@ object AlfredManager {
}
}
internal fun updateRecordingState(isBackground: Boolean) {
internal fun resetWhenAppGoesInBackground() {
sessionConfig.isCriticalUserJourneyActive.set(false)
isAppInForeground.set(isBackground.not())
if (isBackground) {
PixelCopyHandler.cleanup()
}
PixelCopyHandler.cleanup()
}
fun setUserId(userId: String?) {

View File

@@ -15,8 +15,8 @@ import com.navi.alfred.core.AlfredManager.initializeAlfredResources
import com.navi.alfred.core.AlfredManager.ioCoroutineScope
import com.navi.alfred.core.AlfredManager.isAlfredRecordingEnabled
import com.navi.alfred.core.AlfredManager.isAppInForeground
import com.navi.alfred.core.AlfredManager.resetWhenAppGoesInBackground
import com.navi.alfred.core.AlfredManager.screenRecordingScope
import com.navi.alfred.core.AlfredManager.updateRecordingState
import com.navi.alfred.core.handler.AlfredRecordingHandlerImpl.captureScreen
import com.navi.alfred.core.handler.interfaces.AlfredLifecycleHandler
import com.navi.alfred.model.EventType
@@ -100,13 +100,14 @@ object AlfredLifecycleHandlerImpl : AlfredLifecycleHandler {
}
override fun onApplicationInBackground() {
isAppInForeground.set(false)
if (!isAlfredRecordingEnabled()) {
return
}
ioCoroutineScope.launch {
prepareUploadWhenAppGoesInBackground(AlfredManager.sessionConfig.alfredZipName)
}
updateRecordingState(isBackground = true)
resetWhenAppGoesInBackground()
sendAnalyticsEvent(eventName = ALFRED_STOP_RECORDING)
dispatchEvent(
EventType.StopRecording,

View File

@@ -7,7 +7,6 @@
package com.navi.alfred.sevenzutils
import android.util.Log
import com.navi.alfred.sevenzutils.xz.LZMA2Options
import com.navi.alfred.sevenzutils.xz.XZOutputStream
import com.navi.alfred.utils.AnalyticsConstants.ALFRED_X_ZIP_COMPRESSED_FAILURE
@@ -22,20 +21,30 @@ internal fun compressTo7z(zipFileName: String): Boolean {
if (!File(inputPath).exists()) return false
try {
val lzma2Options = LZMA2Options()
lzma2Options.dictSize = 1 shl 20
val outputStream = XZOutputStream(FileOutputStream(zipFileName), lzma2Options)
val inputStream = FileInputStream(inputPath)
val buf = ByteArray(8192)
val buf = ByteArray(4096)
var size: Int
while ((inputStream.read(buf).also { size = it }) != -1) outputStream.write(buf, 0, size)
outputStream.finish()
sendAnalyticsEvent(ALFRED_X_ZIP_COMPRESSED_SUCCESS, mapOf("zipFileName" to zipFileName))
return true
} catch (e: OutOfMemoryError) {
sendAnalyticsEvent(
ALFRED_X_ZIP_COMPRESSED_FAILURE,
mapOf(
"exception" to "OutOfMemoryError",
"message" to (e.message ?: "Unknown OOM error"),
),
)
return false
} catch (e: Exception) {
sendAnalyticsEvent(
ALFRED_X_ZIP_COMPRESSED_FAILURE,
mapOf("exception" to e.message.toString()),
)
Log.e("GenerateZip", "Error compressing to 7z", e)
return false
}
}

View File

@@ -13,6 +13,7 @@ import com.navi.alfred.db.dao.ZipDetailsDao
import com.navi.alfred.db.model.ZipDetailsHelper
import com.navi.alfred.network.AlfredNetworkRepository
import com.navi.alfred.utils.AlfredConstants
import com.navi.alfred.utils.AlfredConstants.ALFRED_SESSION_ID
import com.navi.alfred.utils.AlfredConstants.FILE_TYPE_XZ
import com.navi.alfred.utils.AlfredConstants.FILE_TYPE_ZIP
import com.navi.alfred.utils.AnalyticsConstants.ALFRED_GET_PRE_SIGNED_URL_CALLED
@@ -45,6 +46,8 @@ class UploadZipUseCase(
mapOf(
"zipFileName" to zipFileDetail.zipFolderName,
"fileTypeExtension" to zipFileDetail.fileTypeExtension,
"source" to zipFileDetail.source,
ALFRED_SESSION_ID to zipFileDetail.alfredSessionId,
),
)
@@ -64,6 +67,8 @@ class UploadZipUseCase(
"fileTypeExtension" to zipFileDetail.fileTypeExtension,
"response_code" to preSignedUrl.code().toString(),
"response_message" to preSignedUrl.message(),
"source" to zipFileDetail.source,
ALFRED_SESSION_ID to zipFileDetail.alfredSessionId,
),
)
@@ -89,6 +94,8 @@ class UploadZipUseCase(
mapOf(
"zipFileName" to detail.zipFolderName,
"fileTypeExtension" to detail.fileTypeExtension,
"source" to detail.source,
ALFRED_SESSION_ID to detail.alfredSessionId,
),
)
@@ -101,6 +108,8 @@ class UploadZipUseCase(
"fileTypeExtension" to detail.fileTypeExtension,
"response_code" to resp.code().toString(),
"response_message" to resp.message(),
"source" to detail.source,
ALFRED_SESSION_ID to detail.alfredSessionId,
),
)
if (!resp.isSuccessful) return

View File

@@ -12,6 +12,8 @@ object AnalyticsConstants {
const val ALFRED_INIT = "ALFRED_INIT"
const val ALFRED_START_RECORDING = "START_RECORDING"
const val ALFRED_STOP_RECORDING = "STOP_RECORDING"
const val ALFRED_DELETE_FOLDER_CALLED = "ALFRED_DELETE_FOLDER_CALLED"
const val ALFRED_CLEAR_CORRUPT_FOLDER = "ALFRED_CLEAR_CORRUPT_FOLDER"
const val ALFRED_CRUISE_CALLED = "CRUISE_CALLED"
const val ALFRED_INIT_RECORDING_SETUP_COMPLETE = "INIT_RECORDING_SETUP_COMPLETE"
const val ALFRED_UPLOAD_FILE_WORKER_SCREENSHOT_SIZE = "UPLOAD_FILE_WORKER_SCREENSHOT_SIZE"

View File

@@ -34,7 +34,9 @@ fun recordException(e: Throwable) {
internal fun sendAnalyticsEvent(eventName: String, eventAttributes: Map<String, String>? = null) {
val attribute = eventAttributes?.toMutableMap() ?: mutableMapOf()
attribute[ALFRED_SESSION_ID] = AlfredManager.sessionConfig.alfredSessionId
if (attribute[ALFRED_SESSION_ID].isNullOrEmpty()) {
attribute[ALFRED_SESSION_ID] = AlfredManager.sessionConfig.alfredSessionId
}
AlfredAnalyticsManager.getAlfredAnalyticsProvider()
?.sendEvent(eventName = "ALFRED_$eventName", eventAttributes = attribute)
}

View File

@@ -19,6 +19,7 @@ import com.navi.alfred.model.EventAttribute
import com.navi.alfred.model.SessionEventAttribute
import com.navi.alfred.model.SessionRequest
import com.navi.alfred.model.UploadSource
import com.navi.alfred.utils.AlfredConstants.ALFRED_SESSION_ID
import com.navi.alfred.utils.AlfredConstants.DEFAULT_SEND_SESSION_POST_URL
import com.navi.alfred.utils.AlfredConstants.EMPTY
import com.navi.alfred.utils.AnalyticsConstants.ALFRED_INGEST_SESSION_CALLED
@@ -133,6 +134,8 @@ internal fun sendAlfredSessionEvent(zipFileDetails: ZipDetailsHelper) {
mapOf(
"zipFileName" to zipFileDetails.zipFolderName,
"fileTypeExtension" to zipFileDetails.fileTypeExtension,
"source" to zipFileDetails.source,
ALFRED_SESSION_ID to zipFileDetails.alfredSessionId,
),
)
@@ -151,6 +154,8 @@ internal fun sendAlfredSessionEvent(zipFileDetails: ZipDetailsHelper) {
"fileTypeExtension" to zipFileDetails.fileTypeExtension,
"response_code" to response.code().toString(),
"response_message" to response.message(),
"source" to zipFileDetails.source,
ALFRED_SESSION_ID to zipFileDetails.alfredSessionId,
),
)
if (response.isSuccessful && response.code() == AlfredConstants.CODE_API_SUCCESS) {

View File

@@ -12,6 +12,8 @@ import androidx.core.content.edit
import com.navi.alfred.core.AlfredManager
import com.navi.alfred.utils.AlfredConstants.PREFS_NAME
import com.navi.alfred.utils.AlfredConstants.PREF_KEY_CLEANUP_DONE
import com.navi.alfred.utils.AnalyticsConstants.ALFRED_CLEAR_CORRUPT_FOLDER
import com.navi.alfred.utils.AnalyticsConstants.ALFRED_DELETE_FOLDER_CALLED
import java.io.File
internal fun deleteFolderIfEmpty(folderName: String) {
@@ -19,6 +21,10 @@ internal fun deleteFolderIfEmpty(folderName: String) {
if (folder.exists() && folder.isDirectory && folder.list()?.isEmpty() == true) {
folder.delete()
}
sendAnalyticsEvent(
eventName = ALFRED_DELETE_FOLDER_CALLED,
eventAttributes = mapOf("folderName" to folderName),
)
}
internal fun cleanUpCorruptFilesIfNeeded() {
@@ -47,7 +53,10 @@ internal fun cleanUpCorruptAlfredSessions() {
file.name.endsWith(AlfredConstants.ALFRED_ZIP_IDENTIFIER) ||
file.name.endsWith(AlfredConstants.TEMP_ZIP_FILE_SUFFIX))
}
sendAnalyticsEvent(
ALFRED_CLEAR_CORRUPT_FOLDER,
mapOf("fileSize" to (files?.size ?: 0).toString()),
)
files?.forEach { file ->
if (file.exists()) {
file.delete()

View File

@@ -11,6 +11,7 @@ import com.navi.alfred.core.AlfredManager
import com.navi.alfred.db.model.ZipDetailsHelper
import com.navi.alfred.model.UploadSource
import com.navi.alfred.sevenzutils.compressTo7z
import com.navi.alfred.utils.AlfredConstants.ALFRED_SESSION_ID
import com.navi.alfred.utils.AnalyticsConstants.ALFRED_ZIP_CREATION_FAILED
import com.navi.alfred.utils.AnalyticsConstants.ALFRED_ZIP_CREATION_SUCCESS
import com.navi.alfred.utils.AnalyticsConstants.ALFRED_ZIP_INITIATED
@@ -48,7 +49,6 @@ internal fun toZip(
)
if (zip(fileList, zipFilePath, AlfredManager.config.zipFileTypeExtension) == true) {
sendAnalyticsEvent(
eventName = ALFRED_ZIP_CREATION_SUCCESS,
eventAttributes =
@@ -66,8 +66,8 @@ internal fun toZip(
zipFolderName = zipFolderName,
fileTypeExtension = AlfredManager.config.zipFileTypeExtension,
)
deleteScreenshotsInZipFolder(sessionFolderName, zipFolderName)
}
deleteScreenshotsInZipFolder(sessionFolderName, zipFolderName)
}
}
@@ -92,6 +92,7 @@ internal fun toZipForWorkManager(
"zipFilePath" to zipFilePath,
"fileTypeExtension" to zipFileTypeExtension,
"fileCount" to fileList.size.toString(),
ALFRED_SESSION_ID to sessionFolderName,
),
)
if (zip(fileList, zipFilePath, zipFileTypeExtension) == true) {
@@ -101,6 +102,7 @@ internal fun toZipForWorkManager(
mapOf(
"zipFilePath" to zipFilePath,
"fileTypeExtension" to AlfredManager.config.zipFileTypeExtension,
ALFRED_SESSION_ID to sessionFolderName,
),
)
insertZipDetailsToDbForUpload(
@@ -112,8 +114,8 @@ internal fun toZipForWorkManager(
fileTypeExtension = zipFileTypeExtension,
source = UploadSource.WORKER,
)
deleteScreenshotsInZipFolder(sessionFolderName, zipFolderName)
}
deleteScreenshotsInZipFolder(sessionFolderName, zipFolderName)
}
}
@@ -121,7 +123,7 @@ internal fun zip(files: List<String>, zipFilePath: String?, fileTypeExtension: S
if (zipFilePath == null) {
return false
}
val buffer = 8192
val buffer = 4096
val tempZipFilePath =
if (fileTypeExtension == AlfredConstants.XZ_FILE_EXTENSION) {
zipFilePath + "_zip"
@@ -169,9 +171,26 @@ internal fun zip(files: List<String>, zipFilePath: String?, fileTypeExtension: S
}
if (fileTypeExtension == AlfredConstants.XZ_FILE_EXTENSION) {
if (compressTo7z(zipFilePath)) {
File(tempZipFilePath).delete()
return true
try {
if (compressTo7z(zipFilePath)) {
File(tempZipFilePath).delete()
return true
} else {
File(tempZipFilePath).delete()
sendAnalyticsEvent(
ALFRED_ZIP_CREATION_FAILED,
mapOf(
"zipFilePath" to zipFilePath,
"fileTypeExtension" to fileTypeExtension,
),
)
return false
}
} catch (e: Exception) {
sendAnalyticsEvent(
"ALFRED_FALLBACK_TO_ZIP_EXCEPTION",
mapOf("error" to e.message.toString(), "zipFilePath" to zipFilePath),
)
}
} else {
return true