Skip to content

Commit

Permalink
Fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkordic committed Oct 20, 2023
1 parent 71ccb61 commit 2ca72a8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,10 @@ internal class TranscoderContext private constructor(
logger.i(LOG_TAG, "Original file size: ${uploadInfo.inputFile.length()}")
logger.i(LOG_TAG, "Transcoded file size: ${uploadInfo.standardizedFile?.length()}")
maxStandardInputRes = (MAX_ALLOWED_WIDTH / MAX_ALLOWED_HEIGTH).toString()
if (fileTranscoded) {
if (fileTranscoded && uploadInfo.optOut) {
metrics.reportStandardizationSuccess(started, ended, inputFileDurationMs,
nonStandardInputReasons, maxStandardInputRes, sessionId, uploadInfo)
} else {
} else if(uploadInfo.optOut) {
metrics.reportStandardizationFailed(started, ended, inputFileDurationMs,
errorDescription, nonStandardInputReasons, maxStandardInputRes, sessionId, uploadInfo)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,35 +174,31 @@ internal class UploadJobFactory private constructor(

// We made it!
val finalProgress = createFinalState(fileSize, startTime)
// report this upload asynchronously (unless a debug build of the SDK)
@Suppress("KotlinConstantConditions")
if (BuildConfig.BUILD_TYPE != "debug" && !innerUploadInfo.optOut) {
launch {
metrics.reportUpload(
startTimeMillis = finalProgress.startTime,
endTimeMillis = finalProgress.updatedTime,
uploadInfo = innerUploadInfo,
)
}
}

// finish up
MainScope().launch { MuxUploadManager.jobFinished(innerUploadInfo) }
val success = UploadStatus.UploadSuccess(finalProgress)
metrics.reportUploadSucceeded(startTime, finalProgress.updatedTime, 0,
sessionId,
uploadInfo)
if (!innerUploadInfo.optOut) {
metrics.reportUploadSucceeded(
startTime, finalProgress.updatedTime, 0,
sessionId,
uploadInfo
)
}
statusFlow.value = success
Result.success(success)
} catch (e: Exception) {
MuxUploadSdk.logger.e("MuxUpload", "Upload of ${innerUploadInfo.inputFile} failed", e)
val finalState = createFinalState(fileSize, startTime)
val failStatus = UploadStatus.UploadFailed(e, finalState)
statusFlow.value = failStatus
metrics.reportUploadFailed(startTime, System.currentTimeMillis(), 0,
e.localizedMessage,
sessionId,
uploadInfo)
if (!innerUploadInfo.optOut) {
metrics.reportUploadFailed(
startTime, System.currentTimeMillis(), 0,
e.localizedMessage,
sessionId,
uploadInfo
)
}
MainScope().launch { MuxUploadManager.jobFinished(innerUploadInfo, false) }
Result.failure(e)
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ internal class UploadMetrics private constructor() {
}
return JSONObject().apply {
put("upload_start_time", iso8601Sdf.format(startTimeMillis)) // ISO8601
put("upload_start_time", iso8601Sdf.format(endTimeMillis)) // ISO8601
put("upload_end_time", iso8601Sdf.format(endTimeMillis)) // ISO8601
put("input_size", uploadInfo.inputFile.length())
put("input_duration", formatMilliseconds(videoDuration)) // HH:mm:ss
put("upload_url", uploadInfo.remoteUri.toString())
put("sdk_version", BuildConfig.LIB_VERSION)
put("platform_name", "Android")
put("platform_version", Build.VERSION.RELEASE)
put("device_model", Build.MODEL)
put("device_version", Build.VERSION.SDK_INT)
put("app_name", appName)
put("app_version", appVersion)
put("region_code", Locale.getDefault().country)
Expand Down Expand Up @@ -106,7 +107,7 @@ internal class UploadMetrics private constructor() {
})
.build()
val request = Request.Builder()
.url("https://mux-sdks-telemetry.vercel.app/api/upload-sdk-native")
.url("https://mobile.muxanalytics.com/api/upload-sdk-native")
.method("POST", body.toString().toRequestBody("application/json".toMediaType()))
.build()
// The HTTP Client will log if this fails or succeeds
Expand Down Expand Up @@ -175,8 +176,6 @@ internal class UploadMetrics private constructor() {
val data = getEventInfo(startTimeMillis, endTimeMillis, inputFileDurationMs, uploadInfo)
data.put("input_standardization_requested", uploadInfo.standardizationRequested
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
data.remove("upload_start_time")
data.remove("upload_start_time")
body.put("data", data)
sendPost(body)
}
Expand All @@ -198,47 +197,10 @@ internal class UploadMetrics private constructor() {
data.put("input_standardization_requested", uploadInfo.standardizationRequested
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
data.put("error_description", errorDescription)
data.remove("upload_start_time")
data.remove("upload_start_time")
body.put("data", data)
sendPost(body)
}

@JvmSynthetic
internal suspend fun reportUpload(
startTimeMillis: Long,
endTimeMillis: Long,
uploadInfo: UploadInfo
) {
val videoDuration = withContext(Dispatchers.IO) {
try {
MediaMetadataRetriever().use { retriever ->
retriever.setDataSource(uploadInfo.inputFile.absolutePath)
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)?.toInt()
}
} catch (e: Exception) {
MuxUploadSdk.logger.e("UploadMetrics", "Failed to get video duration", e)
null
}
}

val eventJson = UploadEvent(
startTimeMillis = startTimeMillis,
endTimeMillis = endTimeMillis,
fileSize = uploadInfo.inputFile.length(),
videoDuration = videoDuration ?: 0,
uploadURL = uploadInfo.remoteUri.toString(),
sdkVersion = BuildConfig.LIB_VERSION,
osName = "Android",
osVersion = Build.VERSION.RELEASE,
deviceModel = Build.MODEL,
appName = appName,
appVersion = appVersion,
regionCode = Locale.getDefault().country
).toJson()
sendPost(JSONObject(eventJson))
}

companion object {
// Set by initialize()
private lateinit var appName: String
Expand All @@ -263,38 +225,3 @@ internal class UploadMetrics private constructor() {
}
}
}

private data class UploadEvent(
// Video-Specific
val startTimeMillis: Long,
val endTimeMillis: Long,
val fileSize: Long,
val videoDuration: Int,
val uploadURL: String,
// Device-Derived
val sdkVersion: String,
val osName: String,
val osVersion: String,
val deviceModel: String,
val appName: String,
val appVersion: String,
val regionCode: String
) {
fun toJson(): String {
return JSONObject().apply {
put("type", "upload")
put("start_time", startTimeMillis / 1000.0)
put("end_time", endTimeMillis / 1000.0)
put("file_size", fileSize)
put("video_duration", videoDuration)
put("upload_url", uploadURL)
put("sdk_version", sdkVersion)
put("os_name", osName)
put("os_version", osVersion)
put("device_model", deviceModel)
put("app_name", appName)
put("app_version", appVersion)
put("region_code", regionCode)
}.toString()
}
}

0 comments on commit 2ca72a8

Please sign in to comment.