Browse Source

server: Speed up the statistics computation from the database

prometheus-integration
Alexis Hernandez 6 years ago
parent
commit
e1a96c2c61
  1. 13
      server/app/com/xsn/explorer/data/anorm/dao/StatisticsPostgresDAO.scala
  2. 9
      server/test/com/xsn/explorer/data/StatisticsPostgresDataHandlerSpec.scala

13
server/app/com/xsn/explorer/data/anorm/dao/StatisticsPostgresDAO.scala

@ -13,16 +13,15 @@ class StatisticsPostgresDAO {
def getStatistics(implicit conn: Connection): Statistics = { def getStatistics(implicit conn: Connection): Statistics = {
val result = SQL( val result = SQL(
s""" s"""
|SELECT |SELECT (
| ( | (SELECT value FROM aggregated_amounts WHERE name = 'available_coins') -
| SELECT SUM(received - spent) FROM balances | (SELECT COALESCE(SUM(received - spent), 0) FROM balances WHERE address = '$BurnAddress')
| WHERE address <> '$BurnAddress'
| ) AS total_supply, | ) AS total_supply,
| ( | (
| SELECT SUM(received - spent) FROM balances | (SELECT value FROM aggregated_amounts WHERE name = 'available_coins') -
| WHERE address NOT IN (SELECT address FROM hidden_addresses) | (SELECT COALESCE(SUM(received - spent), 0) FROM balances WHERE address IN (SELECT address FROM hidden_addresses))
| ) AS circulating_supply, | ) AS circulating_supply,
| (SELECT COUNT(*) FROM transactions) AS transactions, | (SELECT n_live_tup FROM pg_stat_all_tables WHERE relname = 'transactions') AS transactions,
| (SELECT COALESCE(MAX(height), 0) FROM blocks) AS blocks | (SELECT COALESCE(MAX(height), 0) FROM blocks) AS blocks
""".stripMargin """.stripMargin
).as(StatisticsParsers.parseStatistics.single) ).as(StatisticsParsers.parseStatistics.single)

9
server/test/com/xsn/explorer/data/StatisticsPostgresDataHandlerSpec.scala

@ -6,6 +6,7 @@ import com.xsn.explorer.data.anorm.{BalancePostgresDataHandler, StatisticsPostgr
import com.xsn.explorer.data.common.PostgresDataHandlerSpec import com.xsn.explorer.data.common.PostgresDataHandlerSpec
import com.xsn.explorer.helpers.DataHelper import com.xsn.explorer.helpers.DataHelper
import com.xsn.explorer.models.{Address, Balance} import com.xsn.explorer.models.{Address, Balance}
import org.scalatest.OptionValues._
class StatisticsPostgresDataHandlerSpec extends PostgresDataHandlerSpec { class StatisticsPostgresDataHandlerSpec extends PostgresDataHandlerSpec {
@ -20,7 +21,7 @@ class StatisticsPostgresDataHandlerSpec extends PostgresDataHandlerSpec {
"exclude hidden_addresses from the circulating supply" in { "exclude hidden_addresses from the circulating supply" in {
val hiddenAddress = DataHelper.createAddress("XfAATXtkRgCdMTrj2fxHvLsKLLmqAjhEAt") val hiddenAddress = DataHelper.createAddress("XfAATXtkRgCdMTrj2fxHvLsKLLmqAjhEAt")
val circulatingSupply = dataHandler.getStatistics().get.circulatingSupply val circulatingSupply = dataHandler.getStatistics().get.circulatingSupply.getOrElse(0)
database.withConnection { implicit conn => database.withConnection { implicit conn =>
_root_.anorm.SQL( _root_.anorm.SQL(
@ -35,19 +36,19 @@ class StatisticsPostgresDataHandlerSpec extends PostgresDataHandlerSpec {
balanceDataHandler.upsert(balance).isGood mustEqual true balanceDataHandler.upsert(balance).isGood mustEqual true
val result = dataHandler.getStatistics().get val result = dataHandler.getStatistics().get
result.circulatingSupply mustEqual circulatingSupply result.circulatingSupply.value mustEqual circulatingSupply
} }
"exclude the burn address from the total supply" in { "exclude the burn address from the total supply" in {
val burnAddress = Address.from(StatisticsPostgresDAO.BurnAddress).get val burnAddress = Address.from(StatisticsPostgresDAO.BurnAddress).get
val totalSupply = dataHandler.getStatistics().get.totalSupply val totalSupply = dataHandler.getStatistics().get.totalSupply.getOrElse(0)
val balance = Balance(burnAddress, received = BigDecimal(1000), spent = BigDecimal(500)) val balance = Balance(burnAddress, received = BigDecimal(1000), spent = BigDecimal(500))
balanceDataHandler.upsert(balance).isGood mustEqual true balanceDataHandler.upsert(balance).isGood mustEqual true
val result = dataHandler.getStatistics().get val result = dataHandler.getStatistics().get
result.totalSupply mustEqual totalSupply result.totalSupply.value mustEqual totalSupply
} }
} }
} }

Loading…
Cancel
Save