diff --git a/navi-alfred/src/main/java/com/navi/alfred/AlfredManager.kt b/navi-alfred/src/main/java/com/navi/alfred/AlfredManager.kt index 423a7df..acf858b 100644 --- a/navi-alfred/src/main/java/com/navi/alfred/AlfredManager.kt +++ b/navi-alfred/src/main/java/com/navi/alfred/AlfredManager.kt @@ -657,48 +657,57 @@ object AlfredManager { fun saveApiLog(request: Request, response: Response, startTime: Long, endTime: Long) { coroutineDispatcher.executor.execute { - var bytesSent = 0L - var byteReceived = 0L - request.body?.let { requestBody -> - val buffer = Buffer() - requestBody.writeTo(buffer) - bytesSent = buffer.size - } - response.body?.let { responseBody -> - val source = responseBody.source() - source.request(Long.MAX_VALUE) - val buffer = source.buffer() - byteReceived = buffer.size - } - var errorMessage: String? = null - val duration: Long = endTime - startTime - val errorType: String? = null - if (response.code != CODE_API_SUCCESS) { - errorMessage = response.message - } - val attributes = hashMapOf().apply { - this[AlfredConstants.URL] = request.url.toString() - this[AlfredConstants.METHOD] = request.method - this[AlfredConstants.RESPONSE_CODE] = response.code - this[AlfredConstants.ERROR_MESSAGE] = errorMessage.toString() - this[AlfredConstants.ERROR_TYPE] = errorType.toString() - this[AlfredConstants.START_TIME] = startTime - this[AlfredConstants.END_TIME] = endTime - this[AlfredConstants.DURATION_IN_MS] = duration.toDouble() - this[AlfredConstants.BYTES_RECEIVED] = byteReceived - this[AlfredConstants.BYTES_SENT] = bytesSent - } - val appPerformanceEvent = buildAppPerformanceEvent( - AlfredConstants.API_METRIC_EVENT_NAME, API_METRICS, attributes - ) - AlfredDispatcher.addTaskToQueue( - AddMetricTask( - appPerformanceEvent, applicationContext + if (config.getAlfredSessionId().isNotEmpty()) { + var bytesSent = 0L + var byteReceived = 0L + if (request.body != null) { + val buffer = Buffer() + request.body?.writeTo(buffer) + bytesSent = buffer.size + } + try { + if (response.body != null) { + val responseBody = response.body + val source = responseBody?.source() + source?.request(Long.MAX_VALUE) + val buffer = source?.buffer + byteReceived = buffer?.size ?: 0 + } + } catch (e: Exception) { + byteReceived = -1 + e.log() + } + var errorMessage: String? = null + val duration: Long = endTime - startTime + val errorType: String? = null + if (response.code != CODE_API_SUCCESS) { + errorMessage = response.message + } + val attributes = hashMapOf().apply { + this[AlfredConstants.URL] = request.url.toString() + this[AlfredConstants.METHOD] = request.method + this[AlfredConstants.RESPONSE_CODE] = response.code + this[AlfredConstants.ERROR_MESSAGE] = errorMessage.toString() + this[AlfredConstants.ERROR_TYPE] = errorType.toString() + this[AlfredConstants.START_TIME] = startTime + this[AlfredConstants.END_TIME] = endTime + this[AlfredConstants.DURATION_IN_MS] = duration.toDouble() + this[AlfredConstants.BYTES_RECEIVED] = byteReceived + this[AlfredConstants.BYTES_SENT] = bytesSent + } + val appPerformanceEvent = buildAppPerformanceEvent( + AlfredConstants.API_METRIC_EVENT_NAME, API_METRICS, attributes ) - ) + AlfredDispatcher.addTaskToQueue( + AddMetricTask( + appPerformanceEvent, applicationContext + ) + ) + } } } + fun stopRecording(appBackgroundView: View) { isAppInBackground = true hasRecordingStarted = false