Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE ledger_entity
ADD COLUMN IF NOT EXISTS active BOOLEAN NOT NULL DEFAULT TRUE;
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.transactions.transaction

object LedgerEntityTable : IntIdTable(name = "ledger_entity") {
Expand All @@ -15,6 +17,7 @@ object LedgerEntityTable : IntIdTable(name = "ledger_entity") {
val dataSource = varchar("data_source", 32)
val marketId = integer("market_id").nullable()
val usdPricingExponent = integer("usd_pricing_exponent").nullable()
val active = bool("active").default(true)
}

class LedgerEntityRecord(id: EntityID<Int>) : IntEntity(id) {
Expand All @@ -25,15 +28,37 @@ class LedgerEntityRecord(id: EntityID<Int>) : IntEntity(id) {
LedgerEntityRecord.find { LedgerEntityTable.uuid eq uuid }.firstOrNull()
}

fun findActiveByUuid(uuid: String) = transaction {
LedgerEntityRecord.find {
(LedgerEntityTable.uuid eq uuid) and (LedgerEntityTable.active eq true)
}.firstOrNull()
}

fun findByType(type: EntityType) = transaction {
LedgerEntityRecord.find { LedgerEntityTable.type eq type }.toList()
}

fun findActiveByType(entityType: EntityType) = transaction {
LedgerEntityRecord.find {
(LedgerEntityTable.type eq entityType) and (LedgerEntityTable.active eq true)
}.toList()
}

fun getAllPaginated(offset: Int, limit: Int) = transaction {
LedgerEntityRecord.all()
.limit(limit, offset.toLong())
.toList()
}

fun getActivePaginated(offset: Int, limit: Int) = transaction {
LedgerEntityRecord.find { LedgerEntityTable.active eq true }
.limit(limit, offset.toLong())
.toList()
}

fun countActive() = transaction {
LedgerEntityRecord.find { LedgerEntityTable.active eq true }.count()
}
}

var uuid by LedgerEntityTable.uuid
Expand All @@ -42,6 +67,7 @@ class LedgerEntityRecord(id: EntityID<Int>) : IntEntity(id) {
var dataSource by LedgerEntityTable.dataSource
var marketId by LedgerEntityTable.marketId
var usdPricingExponent by LedgerEntityTable.usdPricingExponent
var active by LedgerEntityTable.active
}

object LedgerEntitySpecTable : IntIdTable(name = "ledger_entity_spec") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ class PulseMetricService(
atDateTime = atDateTime,
subtype = type.name,
) {
LedgerEntityRecord.findByType(type).sumOf {
LedgerEntityRecord.findActiveByType(type).sumOf {
totalBalanceByEntity(range, it, atDateTime).amount
}.let {
PulseMetric.build(
Expand All @@ -1471,7 +1471,7 @@ class PulseMetricService(
atDateTime = atDateTime,
subtype = type.name,
) {
LedgerEntityRecord.findByType(type).sumOf {
LedgerEntityRecord.findActiveByType(type).sumOf {
totalAssetsByEntity(range, it, atDateTime).amount
}.let {
PulseMetric.build(
Expand Down Expand Up @@ -2311,7 +2311,7 @@ class PulseMetricService(
* **********************/
fun ledgeredAssetsByEntity(
uuid: String,
): EntityLedgeredAsset = LedgerEntityRecord.findByUuid(uuid)?.toEntityLedgeredAsset()
): EntityLedgeredAsset = LedgerEntityRecord.findActiveByUuid(uuid)?.toEntityLedgeredAsset()
?: throw ResourceNotFoundException("Entity not found for id: $uuid")

fun ledgeredAssetsByEntity(
Expand All @@ -2320,10 +2320,10 @@ class PulseMetricService(
sort: List<SortOrder>,
sortColumn: List<String>,
): PagedResults<EntityLedgeredAsset> {
val entityLedgeredAssetList = LedgerEntityRecord.getAllPaginated(page.toOffset(count), count)
val entityLedgeredAssetList = LedgerEntityRecord.getActivePaginated(page.toOffset(count), count)
.map { it.toEntityLedgeredAsset() }

val totalEntities = transaction { LedgerEntityRecord.all().count() }
val totalEntities = LedgerEntityRecord.countActive()
return PagedResults(
pages = totalEntities.pageCountOfResults(count),
results = entityLedgeredAssetList,
Expand All @@ -2338,7 +2338,7 @@ class PulseMetricService(
sort: List<SortOrder>,
sortColumn: List<String>,
): PagedResults<EntityLedgeredAssetDetail>? {
val entity = LedgerEntityRecord.findByUuid(uuid)
val entity = LedgerEntityRecord.findActiveByUuid(uuid)
?: throw ResourceNotFoundException("Entity not found for id: $uuid")

val entityAssets: List<EntityLedgeredAssetDetail> = when (entity.type) {
Expand Down
Loading