Skip to content

Commit

Permalink
another approach
Browse files Browse the repository at this point in the history
  • Loading branch information
aforaleka committed May 9, 2024
1 parent e6f1313 commit 99339db
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2323,7 +2323,7 @@ open class StateManagerAdaptor(
)
}

internal fun tracking(eventName: String, params: IMap<String, Any?>?) {
internal open fun tracking(eventName: String, params: IMap<String, Any?>?) {
val paramsAsString = jsonEncoder.encode(params)
ioImplementations.threading?.async(ThreadingType.main) {
ioImplementations.tracking?.log(eventName, paramsAsString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Any> {
return ParsingHelper.merge(
validatorTrackingParams(),
iMapOf(
"clickToSubmitOrderDelayMs" to interval,
),
)?.toIMap() ?: iMapOf()
return iMapOf(
"clickToSubmitOrderDelayMs" to interval,
)
}

private fun errorTrackingParams(error: ParsingError): IMap<String, Any> {
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<String, Any> {
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?) {
Expand Down Expand Up @@ -1573,27 +1556,38 @@ class V4StateManagerAdaptor(
trackApiStateIfNeeded(apiState, null)
}

private fun apiStateParams(): IMap<String, Any>? {
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<String, Any>?
}

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<String, Any>

tracking(AnalyticsEvent.NetworkStatus.rawValue, params.toIMap())
tracking(AnalyticsEvent.NetworkStatus.rawValue, null)
}
}

override fun tracking(eventName: String, params: IMap<String, Any?>?) {
val additionalParams = apiStateParams()
val paramsAsString = this.jsonEncoder.encode(params?.let { ParsingHelper.merge(it as IMap<String, Any>, additionalParams) } ?: additionalParams)
this.ioImplementations.threading?.async(ThreadingType.main) {
this.ioImplementations.tracking?.log(eventName, paramsAsString)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -245,32 +246,36 @@ internal class ConnectionStats(
updateApiState()
}

private fun apiStateParams(): IMap<String, Any>? {
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<String, Any>?
}

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<String, Any>

tracking(AnalyticsEvent.NetworkStatus.rawValue, params.toIMap())
tracking(AnalyticsEvent.NetworkStatus.rawValue)
}
}

private fun tracking(eventName: String, params: IMap<String, Any>?) {
val paramsAsString = helper.jsonEncoder.encode(params)
private fun tracking(eventName: String, params: IMap<String, Any>? = 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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,11 +531,4 @@ class NetworkHelper(
}
}
}

private fun tracking(eventName: String, params: IMap<String, Any>?) {
val paramsAsString = jsonEncoder.encode(params)
ioImplementations.threading?.async(ThreadingType.main) {
ioImplementations.tracking?.log(eventName, paramsAsString)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -81,8 +83,9 @@ internal open class NetworkSupervisor(
}
}

internal fun tracking(eventName: String, params: IMap<String, Any?>?) {
val paramsAsString = helper.jsonEncoder.encode(params)
internal fun tracking(eventName: String, params: IMap<String, Any>?) {
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Any> {
return ParsingHelper.merge(
validatorTrackingParams(),
iMapOf(
"clickToSubmitOrderDelayMs" to interval,
),
)?.toIMap() ?: iMapOf()
return iMapOf(
"clickToSubmitOrderDelayMs" to interval,
)
}

private fun errorTrackingParams(error: ParsingError): IMap<String, Any> {
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<String, Any> {
return ParsingHelper.merge(
validatorTrackingParams(),
iMapOf(
"roundtripMs" to interval,
),
)?.toIMap() ?: iMapOf()
private fun trackingParams(interval: Double? = null): IMap<String, Any> {
return interval?.let { iMapOf("roundtripMs" to it) } ?: iMapOf()
}

fun closePosition(
Expand Down Expand Up @@ -667,13 +654,17 @@ internal class SubaccountSupervisor(
}

private fun trackOrderClick(
analyticsPayload: IMap<String, Any?>?,
analyticsPayload: IMap<String, Any>?,
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
}
Expand Down Expand Up @@ -707,7 +698,7 @@ internal class SubaccountSupervisor(
} else {
tracking(
if (isCancel) AnalyticsEvent.TradeCancelOrderSubmissionConfirmed.rawValue else AnalyticsEvent.TradePlaceOrderSubmissionConfirmed.rawValue,
analyticsPayload,
ParsingHelper.merge(trackingParams(), analyticsPayload)?.toIMap(),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AnalyticsUtils {
*/
fun triggerOrdersAnalyticsPayload(
payload: HumanReadableTriggerOrdersPayload,
): IMap<String, Any?>? {
): IMap<String, Any>? {
val placeOrderPayloads = payload.placeOrderPayloads
val cancelOrderPayloads = payload.cancelOrderPayloads

Expand Down Expand Up @@ -84,7 +84,7 @@ class AnalyticsUtils {
"takeProfitOrderAction" to takeProfitOrderAction?.rawValue,
"takeProfitOrderCancelClientId" to takeProfitOrderCancelClientId,
"takeProfitOrderPlaceClientId" to takeProfitOrderPlaceClientId,
)
) as IMap<String, Any>?
}

/**
Expand Down

0 comments on commit 99339db

Please sign in to comment.