Skip to content

Commit

Permalink
Merge branch 'main' into mulan/ct-783-new-analytics-clickevent-for-sltp
Browse files Browse the repository at this point in the history
  • Loading branch information
moo-onthelawn committed Apr 30, 2024
2 parents d396798 + 0d50ae9 commit c843288
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 326 deletions.
289 changes: 0 additions & 289 deletions detekt-baseline.xml

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,20 @@ naming:
# some of our package folders are structured /exchange.dydx.abacus instead of:
# /exchange -> /dydx -> /abacus
# didn't seem worth the potential thrash in PRs to fix (feel free to fix if you feel differently)
active: false
active: false

complexity:
CognitiveComplexMethod:
active: false
ComplexCondition:
active: false
CyclomaticComplexMethod:
active: false
LongParameterList:
active: false
LargeClass:
threshold: 1000 # up from 600
LongMethod:
threshold: 100 # up from 60
TooManyFunctions:
active: false
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ import exchange.dydx.abacus.utils.IMap
import exchange.dydx.abacus.utils.IMutableList
import exchange.dydx.abacus.utils.IOImplementations
import exchange.dydx.abacus.utils.JsonEncoder
import exchange.dydx.abacus.utils.Logger
import exchange.dydx.abacus.utils.Parser
import exchange.dydx.abacus.utils.ParsingHelper
import exchange.dydx.abacus.utils.SHORT_TERM_ORDER_DURATION
Expand Down Expand Up @@ -2164,8 +2163,8 @@ open class StateManagerAdaptor(

return HumanReadableCancelOrderPayload(
subaccountNumber,
orderId,
type,
orderId,
clientId,
orderFlags,
clobPairId,
Expand Down Expand Up @@ -2307,7 +2306,6 @@ open class StateManagerAdaptor(
ioImplementations.threading?.async(ThreadingType.main) {
ioImplementations.tracking?.log(eventName, paramsAsString)
}
Logger.e { "xcxc $eventName $paramsAsString" }
}

private fun didSetPlaceOrderRecords() {
Expand All @@ -2333,7 +2331,11 @@ open class StateManagerAdaptor(
val interval = Clock.System.now().toEpochMilliseconds()
.toDouble() - placeOrderRecord.timestampInMilliseconds
tracking(
if (placeOrderRecord.isTriggerOrder) AnalyticsEvent.TriggerPlaceOrderConfirmed.rawValue else AnalyticsEvent.TradePlaceOrderConfirmed.rawValue,
if (placeOrderRecord.isTriggerOrder) {
AnalyticsEvent.TriggerPlaceOrderConfirmed.rawValue
} else {
AnalyticsEvent.TradePlaceOrderConfirmed.rawValue
},
ParsingHelper.merge(
trackingParams(interval),
orderAnalyticsPayload,
Expand All @@ -2349,7 +2351,11 @@ open class StateManagerAdaptor(
val interval = Clock.System.now().toEpochMilliseconds()
.toDouble() - cancelOrderRecord.timestampInMilliseconds
tracking(
if (cancelOrderRecord.isTriggerOrder) AnalyticsEvent.TriggerCancelOrderConfirmed.rawValue else AnalyticsEvent.TradeCancelOrderConfirmed.rawValue,
if (cancelOrderRecord.isTriggerOrder) {
AnalyticsEvent.TriggerCancelOrderConfirmed.rawValue
} else {
AnalyticsEvent.TradeCancelOrderConfirmed.rawValue
},
ParsingHelper.merge(
trackingParams(interval),
orderAnalyticsPayload,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1149,9 +1149,17 @@ class V4StateManagerAdaptor(

tracking(
if (isCancel) {
if (isTriggerOrder) AnalyticsEvent.TriggerCancelOrder.rawValue else AnalyticsEvent.TradeCancelOrder.rawValue
if (isTriggerOrder) {
AnalyticsEvent.TriggerCancelOrder.rawValue
} else {
AnalyticsEvent.TradeCancelOrder.rawValue
}
} else {
if (isTriggerOrder) AnalyticsEvent.TriggerPlaceOrder.rawValue else AnalyticsEvent.TradePlaceOrder.rawValue
if (isTriggerOrder) {
AnalyticsEvent.TriggerPlaceOrder.rawValue
} else {
AnalyticsEvent.TradePlaceOrder.rawValue
}
},
ParsingHelper.merge(uiTrackingParams(uiDelayTimeMs), analyticsPayload)
?.toIMap(),
Expand All @@ -1169,18 +1177,34 @@ class V4StateManagerAdaptor(
if (error != null) {
tracking(
if (isCancel) {
if (isTriggerOrder) AnalyticsEvent.TriggerCancelOrderSubmissionFailed.rawValue else AnalyticsEvent.TradeCancelOrderSubmissionFailed.rawValue
if (isTriggerOrder) {
AnalyticsEvent.TriggerCancelOrderSubmissionFailed.rawValue
} else {
AnalyticsEvent.TradeCancelOrderSubmissionFailed.rawValue
}
} else {
if (isTriggerOrder) AnalyticsEvent.TriggerPlaceOrderSubmissionFailed.rawValue else AnalyticsEvent.TradePlaceOrderSubmissionFailed.rawValue
if (isTriggerOrder) {
AnalyticsEvent.TriggerPlaceOrderSubmissionFailed.rawValue
} else {
AnalyticsEvent.TradePlaceOrderSubmissionFailed.rawValue
}
},
ParsingHelper.merge(errorTrackingParams(error), analyticsPayload)?.toIMap(),
)
} else {
tracking(
if (isCancel) {
if (isTriggerOrder) AnalyticsEvent.TriggerCancelOrderSubmissionConfirmed.rawValue else AnalyticsEvent.TradeCancelOrderSubmissionConfirmed.rawValue
if (isTriggerOrder) {
AnalyticsEvent.TriggerCancelOrderSubmissionConfirmed.rawValue
} else {
AnalyticsEvent.TradeCancelOrderSubmissionConfirmed.rawValue
}
} else {
if (isTriggerOrder) AnalyticsEvent.TriggerPlaceOrderSubmissionConfirmed.rawValue else AnalyticsEvent.TradePlaceOrderSubmissionConfirmed.rawValue
if (isTriggerOrder) {
AnalyticsEvent.TriggerPlaceOrderSubmissionConfirmed.rawValue
} else {
AnalyticsEvent.TradePlaceOrderSubmissionConfirmed.rawValue
}
},
analyticsPayload,
)
Expand Down Expand Up @@ -1234,7 +1258,15 @@ class V4StateManagerAdaptor(
cancelPayload,
existingOrder,
)
submitCancelOrder(cancelPayload.orderId, marketId, callback, cancelPayload, cancelOrderAnalyticsPayload, uiClickTimeMs, true)
submitCancelOrder(
cancelPayload.orderId,
marketId,
callback,
cancelPayload,
cancelOrderAnalyticsPayload,
uiClickTimeMs,
true,
)
}

payload.placeOrderPayloads.forEach { placePayload ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ data class HumanReadablePlaceOrderPayload(
@Serializable
data class HumanReadableCancelOrderPayload(
val subaccountNumber: Int,
val orderId: String,
val type: String,
val orderId: String,
val clientId: Int,
val orderFlags: Int,
val clobPairId: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,11 @@ internal class SubaccountSupervisor(
val interval = Clock.System.now().toEpochMilliseconds()
.toDouble() - placeOrderRecord.timestampInMilliseconds
tracking(
if (placeOrderRecord.isTriggerOrder) AnalyticsEvent.TriggerPlaceOrderConfirmed.rawValue else AnalyticsEvent.TradePlaceOrderConfirmed.rawValue,
if (placeOrderRecord.isTriggerOrder) {
AnalyticsEvent.TriggerPlaceOrderConfirmed.rawValue
} else {
AnalyticsEvent.TradePlaceOrderConfirmed.rawValue
},
ParsingHelper.merge(
trackingParams(interval),
orderAnalyticsPayload,
Expand All @@ -322,7 +326,11 @@ internal class SubaccountSupervisor(
val interval = Clock.System.now().toEpochMilliseconds()
.toDouble() - cancelOrderRecord.timestampInMilliseconds
tracking(
if (cancelOrderRecord.isTriggerOrder) AnalyticsEvent.TriggerCancelOrderConfirmed.rawValue else AnalyticsEvent.TradeCancelOrderConfirmed.rawValue,
if (cancelOrderRecord.isTriggerOrder) {
AnalyticsEvent.TriggerCancelOrderConfirmed.rawValue
} else {
AnalyticsEvent.TradeCancelOrderConfirmed.rawValue
},
ParsingHelper.merge(
trackingParams(interval),
orderAnalyticsPayload,
Expand Down Expand Up @@ -659,9 +667,17 @@ internal class SubaccountSupervisor(

tracking(
if (isCancel) {
if (isTriggerOrder) AnalyticsEvent.TriggerCancelOrder.rawValue else AnalyticsEvent.TradeCancelOrder.rawValue
if (isTriggerOrder) {
AnalyticsEvent.TriggerCancelOrder.rawValue
} else {
AnalyticsEvent.TradeCancelOrder.rawValue
}
} else {
if (isTriggerOrder) AnalyticsEvent.TriggerPlaceOrder.rawValue else AnalyticsEvent.TradePlaceOrder.rawValue
if (isTriggerOrder) {
AnalyticsEvent.TriggerPlaceOrder.rawValue
} else {
AnalyticsEvent.TradePlaceOrder.rawValue
}
},
ParsingHelper.merge(uiTrackingParams(uiDelayTimeMs), analyticsPayload)
?.toIMap(),
Expand All @@ -679,18 +695,34 @@ internal class SubaccountSupervisor(
if (error != null) {
tracking(
if (isCancel) {
if (isTriggerOrder) AnalyticsEvent.TriggerCancelOrderSubmissionFailed.rawValue else AnalyticsEvent.TradeCancelOrderSubmissionFailed.rawValue
if (isTriggerOrder) {
AnalyticsEvent.TriggerCancelOrderSubmissionFailed.rawValue
} else {
AnalyticsEvent.TradeCancelOrderSubmissionFailed.rawValue
}
} else {
if (isTriggerOrder) AnalyticsEvent.TriggerPlaceOrderSubmissionFailed.rawValue else AnalyticsEvent.TradePlaceOrderSubmissionFailed.rawValue
if (isTriggerOrder) {
AnalyticsEvent.TriggerPlaceOrderSubmissionFailed.rawValue
} else {
AnalyticsEvent.TradePlaceOrderSubmissionFailed.rawValue
}
},
ParsingHelper.merge(errorTrackingParams(error), analyticsPayload)?.toIMap(),
)
} else {
tracking(
if (isCancel) {
if (isTriggerOrder) AnalyticsEvent.TriggerCancelOrderSubmissionConfirmed.rawValue else AnalyticsEvent.TradeCancelOrderSubmissionConfirmed.rawValue
if (isTriggerOrder) {
AnalyticsEvent.TriggerCancelOrderSubmissionConfirmed.rawValue
} else {
AnalyticsEvent.TradeCancelOrderSubmissionConfirmed.rawValue
}
} else {
if (isTriggerOrder) AnalyticsEvent.TriggerPlaceOrderSubmissionConfirmed.rawValue else AnalyticsEvent.TradePlaceOrderSubmissionConfirmed.rawValue
if (isTriggerOrder) {
AnalyticsEvent.TriggerPlaceOrderSubmissionConfirmed.rawValue
} else {
AnalyticsEvent.TradePlaceOrderSubmissionConfirmed.rawValue
}
},
analyticsPayload,
)
Expand Down Expand Up @@ -821,7 +853,15 @@ internal class SubaccountSupervisor(
cancelPayload,
existingOrder,
)
submitCancelOrder(cancelPayload.orderId, marketId, callback, cancelPayload, cancelOrderAnalyticsPayload, uiClickTimeMs, true)
submitCancelOrder(
cancelPayload.orderId,
marketId,
callback,
cancelPayload,
cancelOrderAnalyticsPayload,
uiClickTimeMs,
true,
)
}

payload.placeOrderPayloads.forEach { placePayload ->
Expand Down Expand Up @@ -1007,8 +1047,8 @@ internal class SubaccountSupervisor(
val marketId = triggerOrders?.marketId ?: throw Exception("marketId is null")

val subaccount = stateMachine.state?.subaccount(subaccountNumber) ?: throw Exception("subaccount is null")
val position = helper.parser.asNativeMap(helper.parser.value(subaccount, "openPositions.$marketId"))
val positionSize = helper.parser.asDouble(helper.parser.value(position, "size.current"))
val position = stateMachine.state?.subaccount(subaccountNumber)?.openPositions?.find { it.id == marketId }
val positionSize = position?.size?.current

fun updateTriggerOrder(triggerOrder: TriggerOrder) {
// Cases
Expand Down Expand Up @@ -1115,8 +1155,8 @@ internal class SubaccountSupervisor(

return HumanReadableCancelOrderPayload(
orderSubaccountNumber,
orderId,
type,
orderId,
clientId,
orderFlags,
clobPairId,
Expand Down
40 changes: 29 additions & 11 deletions src/commonMain/kotlin/exchange.dydx.abacus/utils/AnalyticsUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ import exchange.dydx.abacus.state.manager.HumanReadableCancelOrderPayload
import exchange.dydx.abacus.state.manager.HumanReadablePlaceOrderPayload
import exchange.dydx.abacus.state.manager.HumanReadableTriggerOrdersPayload
import kollections.toIMap

enum class OrderAction(val rawValue: String) {
replace("REPLACE"),
cancel("CANCEL"),
create("CREATE"),
import kotlinx.serialization.Serializable
import kollections.JsExport

@JsExport
@Serializable
enum class TriggerOrderAction(val rawValue: String) {
REPLACE("REPLACE"),
CANCEL("CANCEL"),
CREATE("CREATE"),
;

companion object {
operator fun invoke(rawValue: String?) =
TriggerOrderAction.values().firstOrNull { it.rawValue == rawValue }
}
}

class AnalyticsUtils {
Expand All @@ -32,24 +42,32 @@ class AnalyticsUtils {
val stopLossOrderTypes = listOf(OrderType.stopMarket, OrderType.stopLimit)
val takeProfitOrderTypes = listOf(OrderType.takeProfitMarket, OrderType.takeProfitLimit)

var stopLossOrderAction: OrderAction? = null
var takeProfitOrderAction: OrderAction? = null
var stopLossOrderAction: TriggerOrderAction? = null
var takeProfitOrderAction: TriggerOrderAction? = null

placeOrderPayloads.forEach { placePayload ->
val orderType = OrderType.invoke(placePayload.type)
if (stopLossOrderTypes.contains(orderType)) {
stopLossOrderAction = OrderAction.create
stopLossOrderAction = TriggerOrderAction.CREATE
} else if (takeProfitOrderTypes.contains(orderType)) {
takeProfitOrderAction = OrderAction.create
takeProfitOrderAction = TriggerOrderAction.CREATE
}
}

cancelOrderPayloads.forEach { cancelPayload ->
val orderType = OrderType.invoke(cancelPayload.type)
if (stopLossOrderTypes.contains(orderType)) {
stopLossOrderAction = if (stopLossOrderAction == null) OrderAction.cancel else OrderAction.replace
stopLossOrderAction = if (stopLossOrderAction == null) {
TriggerOrderAction.CANCEL
} else {
TriggerOrderAction.REPLACE
}
} else if (takeProfitOrderTypes.contains(orderType)) {
takeProfitOrderAction = if (takeProfitOrderAction == null) OrderAction.cancel else OrderAction.replace
takeProfitOrderAction = if (takeProfitOrderAction == null) {
TriggerOrderAction.CANCEL
} else {
TriggerOrderAction.REPLACE
}
}
}

Expand Down

0 comments on commit c843288

Please sign in to comment.