diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/StateManagerAdaptor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/StateManagerAdaptor.kt index 1795a65f0..2176dfd6d 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/StateManagerAdaptor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/StateManagerAdaptor.kt @@ -2323,7 +2323,7 @@ open class StateManagerAdaptor( ) } - internal fun tracking(eventName: String, params: IMap?) { + internal open fun tracking(eventName: String, params: IMap?) { val paramsAsString = jsonEncoder.encode(params) ioImplementations.threading?.async(ThreadingType.main) { ioImplementations.tracking?.log(eventName, paramsAsString) diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/V4StateManagerAdaptor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/V4StateManagerAdaptor.kt index 3d399dc89..80238b676 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/V4StateManagerAdaptor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/V4StateManagerAdaptor.kt @@ -1510,42 +1510,25 @@ class V4StateManagerAdaptor( fetchTransferStatus(hash, fromChainId, toChainId, isCctp, requestId) } - private fun validatorTrackingParams() = validatorUrl?.let { iMapOf("validatorUrl" to it) } ?: iMapOf() - private fun uiTrackingParams(interval: Double): IMap { - return ParsingHelper.merge( - validatorTrackingParams(), - iMapOf( - "clickToSubmitOrderDelayMs" to interval, - ), - )?.toIMap() ?: iMapOf() + return iMapOf( + "clickToSubmitOrderDelayMs" to interval, + ) } private fun errorTrackingParams(error: ParsingError): IMap { - return ParsingHelper.merge( - validatorTrackingParams(), - if (error.stringKey != null) { - iMapOf( - "errorType" to error.type.rawValue, - "errorMessage" to error.message, - "errorStringKey" to error.stringKey, - ) - } else { - iMapOf( - "errorType" to error.type.rawValue, - "errorMessage" to error.message, - ) - }, - )?.toIMap() ?: iMapOf() - } - - override fun trackingParams(interval: Double): IMap { - return ParsingHelper.merge( - validatorTrackingParams(), + return if (error.stringKey != null) { iMapOf( - "roundtripMs" to interval, - ), - )?.toIMap() ?: iMapOf() + "errorType" to error.type.rawValue, + "errorMessage" to error.message, + "errorStringKey" to error.stringKey, + ) + } else { + iMapOf( + "errorType" to error.type.rawValue, + "errorMessage" to error.message, + ) + } } private fun didSetApiState(apiState: ApiState?, oldValue: ApiState?) { @@ -1573,27 +1556,38 @@ class V4StateManagerAdaptor( trackApiStateIfNeeded(apiState, null) } + private fun apiStateParams(): IMap? { + val indexerTime = lastIndexerCallTime?.toEpochMilliseconds()?.toDouble() + val validatorTime = lastValidatorCallTime?.toEpochMilliseconds()?.toDouble() + val interval = if (indexerTime != null) { + ( + Clock.System.now().toEpochMilliseconds() + .toDouble() - indexerTime + ) + } else { + null + } + return iMapOf( + "lastSuccessfulIndexerRPC" to indexerTime, + "lastSuccessfulFullNodeRPC" to validatorTime, + "elapsedTime" to interval, + "blockHeight" to indexerState.blockAndTime?.block, + "nodeHeight" to validatorState.blockAndTime?.block, + "validatorUrl" to this.validatorUrl, + ) as IMap? + } + private fun trackApiStateIfNeeded(apiState: ApiState?, oldValue: ApiState?) { if (apiState?.abnormalState() == true || oldValue?.abnormalState() == true) { - val indexerTime = lastIndexerCallTime?.toEpochMilliseconds()?.toDouble() - val validatorTime = lastValidatorCallTime?.toEpochMilliseconds()?.toDouble() - val interval = if (indexerTime != null) { - ( - Clock.System.now().toEpochMilliseconds() - .toDouble() - indexerTime - ) - } else { - null - } - val params = mapOf( - "lastSuccessfulIndexerRPC" to indexerTime, - "lastSuccessfulFullNodeRPC" to validatorTime, - "elapsedTime" to interval, - "blockHeight" to indexerState.blockAndTime?.block, - "nodeHeight" to validatorState.blockAndTime?.block, - ).filterValues { it != null } as Map - - tracking(AnalyticsEvent.NetworkStatus.rawValue, params.toIMap()) + tracking(AnalyticsEvent.NetworkStatus.rawValue, null) + } + } + + override fun tracking(eventName: String, params: IMap?) { + val additionalParams = apiStateParams() + val paramsAsString = this.jsonEncoder.encode(params?.let { ParsingHelper.merge(it as IMap, additionalParams) } ?: additionalParams) + this.ioImplementations.threading?.async(ThreadingType.main) { + this.ioImplementations.tracking?.log(eventName, paramsAsString) } } diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/ConnectionStats.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/ConnectionStats.kt index 3e67452f5..9bed841e0 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/ConnectionStats.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/ConnectionStats.kt @@ -12,7 +12,8 @@ import exchange.dydx.abacus.state.manager.NetworkStatus import exchange.dydx.abacus.state.model.TradingStateMachine import exchange.dydx.abacus.state.model.updateHeight import exchange.dydx.abacus.utils.IMap -import kollections.toIMap +import exchange.dydx.abacus.utils.ParsingHelper +import exchange.dydx.abacus.utils.iMapOf import kotlinx.datetime.Clock import kotlinx.datetime.Instant import kotlin.math.max @@ -245,32 +246,36 @@ internal class ConnectionStats( updateApiState() } + private fun apiStateParams(): IMap? { + val indexerTime = lastIndexerCallTime?.toEpochMilliseconds()?.toDouble() + val validatorTime = lastValidatorCallTime?.toEpochMilliseconds()?.toDouble() + val interval = if (indexerTime != null) { + ( + Clock.System.now().toEpochMilliseconds() + .toDouble() - indexerTime + ) + } else { + null + } + return iMapOf( + "lastSuccessfulIndexerRPC" to indexerTime, + "lastSuccessfulFullNodeRPC" to validatorTime, + "elapsedTime" to interval, + "blockHeight" to indexerState.blockAndTime?.block, + "nodeHeight" to validatorState.blockAndTime?.block, + "validatorUrl" to helper.validatorUrl, + ) as IMap? + } + private fun trackApiStateIfNeeded(apiState: ApiState?, oldValue: ApiState?) { if (apiState?.abnormalState() == true || oldValue?.abnormalState() == true) { - val indexerTime = lastIndexerCallTime?.toEpochMilliseconds()?.toDouble() - val validatorTime = lastValidatorCallTime?.toEpochMilliseconds()?.toDouble() - val interval = if (indexerTime != null) { - ( - Clock.System.now().toEpochMilliseconds() - .toDouble() - indexerTime - ) - } else { - null - } - val params = mapOf( - "lastSuccessfulIndexerRPC" to indexerTime, - "lastSuccessfulFullNodeRPC" to validatorTime, - "elapsedTime" to interval, - "blockHeight" to indexerState.blockAndTime?.block, - "nodeHeight" to validatorState.blockAndTime?.block, - ).filterValues { it != null } as Map - - tracking(AnalyticsEvent.NetworkStatus.rawValue, params.toIMap()) + tracking(AnalyticsEvent.NetworkStatus.rawValue) } } - private fun tracking(eventName: String, params: IMap?) { - val paramsAsString = helper.jsonEncoder.encode(params) + private fun tracking(eventName: String, params: IMap? = null) { + val additionalParams = apiStateParams() + val paramsAsString = helper.jsonEncoder.encode(params?.let { ParsingHelper.merge(it, additionalParams) } ?: additionalParams) helper.ioImplementations.threading?.async(ThreadingType.main) { helper.ioImplementations.tracking?.log(eventName, paramsAsString) } diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/NetworkHelper.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/NetworkHelper.kt index 72a0b49a7..e6825f267 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/NetworkHelper.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/NetworkHelper.kt @@ -531,11 +531,4 @@ class NetworkHelper( } } } - - private fun tracking(eventName: String, params: IMap?) { - val paramsAsString = jsonEncoder.encode(params) - ioImplementations.threading?.async(ThreadingType.main) { - ioImplementations.tracking?.log(eventName, paramsAsString) - } - } } diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/NetworkSupervisor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/NetworkSupervisor.kt index 475a8a847..8c4b09311 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/NetworkSupervisor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/NetworkSupervisor.kt @@ -7,6 +7,8 @@ import exchange.dydx.abacus.state.changes.StateChanges import exchange.dydx.abacus.state.model.TradingStateMachine import exchange.dydx.abacus.utils.AnalyticsUtils import exchange.dydx.abacus.utils.IMap +import exchange.dydx.abacus.utils.ParsingHelper +import exchange.dydx.abacus.utils.iMapOf import kollections.iListOf internal open class NetworkSupervisor( @@ -81,8 +83,9 @@ internal open class NetworkSupervisor( } } - internal fun tracking(eventName: String, params: IMap?) { - val paramsAsString = helper.jsonEncoder.encode(params) + internal fun tracking(eventName: String, params: IMap?) { + val requiredParams = helper.validatorUrl?.let { iMapOf("validatorUrl" to it) } ?: iMapOf() + val paramsAsString = helper.jsonEncoder.encode(params?.let { ParsingHelper.merge(it, requiredParams) } ?: requiredParams) helper.ioImplementations.threading?.async(ThreadingType.main) { helper.ioImplementations.tracking?.log(eventName, paramsAsString) } diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountSupervisor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountSupervisor.kt index e57614823..794eaf888 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountSupervisor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountSupervisor.kt @@ -375,42 +375,29 @@ internal class SubaccountSupervisor( val transactionQueue = TransactionQueue(helper::transaction) - private fun validatorTrackingParams() = helper.validatorUrl?.let { iMapOf("validatorUrl" to it) } ?: iMapOf() - private fun uiTrackingParams(interval: Double): IMap { - return ParsingHelper.merge( - validatorTrackingParams(), - iMapOf( - "clickToSubmitOrderDelayMs" to interval, - ), - )?.toIMap() ?: iMapOf() + return iMapOf( + "clickToSubmitOrderDelayMs" to interval, + ) } private fun errorTrackingParams(error: ParsingError): IMap { - return ParsingHelper.merge( - validatorTrackingParams(), - if (error.stringKey != null) { - iMapOf( - "errorType" to error.type.rawValue, - "errorMessage" to error.message, - "errorStringKey" to error.stringKey, - ) - } else { - iMapOf( - "errorType" to error.type.rawValue, - "errorMessage" to error.message, - ) - }, - )?.toIMap() ?: iMapOf() + return if (error.stringKey != null) { + iMapOf( + "errorType" to error.type.rawValue, + "errorMessage" to error.message, + "errorStringKey" to error.stringKey, + ) + } else { + iMapOf( + "errorType" to error.type.rawValue, + "errorMessage" to error.message, + ) + } } - private fun trackingParams(interval: Double): IMap { - return ParsingHelper.merge( - validatorTrackingParams(), - iMapOf( - "roundtripMs" to interval, - ), - )?.toIMap() ?: iMapOf() + private fun trackingParams(interval: Double? = null): IMap { + return interval?.let { iMapOf("roundtripMs" to it) } ?: iMapOf() } fun closePosition( @@ -667,13 +654,17 @@ internal class SubaccountSupervisor( } private fun trackOrderClick( - analyticsPayload: IMap?, + analyticsPayload: IMap?, analyticsEvent: AnalyticsEvent, ): Double { val uiClickTimeMs = Clock.System.now().toEpochMilliseconds().toDouble() + val trackingPayload = analyticsPayload?.let { + ParsingHelper.merge(trackingParams(), it)?.toIMap() + } ?: trackingParams() + tracking( analyticsEvent.rawValue, - analyticsPayload, + trackingPayload, ) return uiClickTimeMs } @@ -707,7 +698,7 @@ internal class SubaccountSupervisor( } else { tracking( if (isCancel) AnalyticsEvent.TradeCancelOrderSubmissionConfirmed.rawValue else AnalyticsEvent.TradePlaceOrderSubmissionConfirmed.rawValue, - analyticsPayload, + ParsingHelper.merge(trackingParams(), analyticsPayload)?.toIMap(), ) } } diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/utils/AnalyticsUtils.kt b/src/commonMain/kotlin/exchange.dydx.abacus/utils/AnalyticsUtils.kt index 5ae5970ac..2c03ffc50 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/utils/AnalyticsUtils.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/utils/AnalyticsUtils.kt @@ -30,7 +30,7 @@ class AnalyticsUtils { */ fun triggerOrdersAnalyticsPayload( payload: HumanReadableTriggerOrdersPayload, - ): IMap? { + ): IMap? { val placeOrderPayloads = payload.placeOrderPayloads val cancelOrderPayloads = payload.cancelOrderPayloads @@ -84,7 +84,7 @@ class AnalyticsUtils { "takeProfitOrderAction" to takeProfitOrderAction?.rawValue, "takeProfitOrderCancelClientId" to takeProfitOrderCancelClientId, "takeProfitOrderPlaceClientId" to takeProfitOrderPlaceClientId, - ) + ) as IMap? } /**