8 changed files with 105 additions and 13 deletions
@ -0,0 +1,13 @@ |
|||||
|
package com.xsn.explorer.data |
||||
|
|
||||
|
import com.alexitc.playsonify.core.ApplicationResult |
||||
|
import com.xsn.explorer.models.Statistics |
||||
|
|
||||
|
import scala.language.higherKinds |
||||
|
|
||||
|
trait StatisticsDataHandler[F[_]] { |
||||
|
|
||||
|
def getStatistics(): F[Statistics] |
||||
|
} |
||||
|
|
||||
|
trait StatisticsBlockingDataHandler extends StatisticsDataHandler[ApplicationResult] |
@ -0,0 +1,22 @@ |
|||||
|
package com.xsn.explorer.data.anorm |
||||
|
|
||||
|
import javax.inject.Inject |
||||
|
|
||||
|
import com.alexitc.playsonify.core.ApplicationResult |
||||
|
import com.xsn.explorer.data.StatisticsBlockingDataHandler |
||||
|
import com.xsn.explorer.data.anorm.dao.StatisticsPostgresDAO |
||||
|
import com.xsn.explorer.models.Statistics |
||||
|
import org.scalactic.Good |
||||
|
import play.api.db.Database |
||||
|
|
||||
|
class StatisticsPostgresDataHandler @Inject() ( |
||||
|
override val database: Database, |
||||
|
statisticsDAO: StatisticsPostgresDAO) |
||||
|
extends StatisticsBlockingDataHandler |
||||
|
with AnormPostgresDataHandler { |
||||
|
|
||||
|
override def getStatistics(): ApplicationResult[Statistics] = withConnection { implicit conn => |
||||
|
val result = statisticsDAO.getStatistics |
||||
|
Good(result) |
||||
|
} |
||||
|
} |
@ -0,0 +1,25 @@ |
|||||
|
package com.xsn.explorer.data.anorm.dao |
||||
|
|
||||
|
import java.sql.Connection |
||||
|
|
||||
|
import anorm._ |
||||
|
import com.xsn.explorer.data.anorm.parsers.StatisticsParsers |
||||
|
import com.xsn.explorer.models.Statistics |
||||
|
|
||||
|
class StatisticsPostgresDAO { |
||||
|
|
||||
|
def getStatistics(implicit conn: Connection): Statistics = { |
||||
|
SQL( |
||||
|
""" |
||||
|
|SELECT |
||||
|
| (SELECT SUM(available) FROM balances) AS total_supply, |
||||
|
| ( |
||||
|
| SELECT SUM(available) FROM balances |
||||
|
| WHERE address NOT IN (SELECT address FROM hidden_addresses) |
||||
|
| ) AS circulating_supply, |
||||
|
| (SELECT COUNT(*) FROM transactions) AS transactions, |
||||
|
| (SELECT MAX(height) FROM blocks) AS blocks |
||||
|
""".stripMargin |
||||
|
).as(StatisticsParsers.parseStatistics.single) |
||||
|
} |
||||
|
} |
@ -0,0 +1,18 @@ |
|||||
|
package com.xsn.explorer.data.anorm.parsers |
||||
|
|
||||
|
import anorm.SqlParser._ |
||||
|
import anorm._ |
||||
|
import com.xsn.explorer.models.Statistics |
||||
|
|
||||
|
object StatisticsParsers { |
||||
|
|
||||
|
val parseBlocks = int("blocks") |
||||
|
val parseTransactions = int("transactions") |
||||
|
val parseTotalSupply = get[BigDecimal]("total_supply") |
||||
|
val parseCirculatingSupply = get[BigDecimal]("circulating_supply") |
||||
|
|
||||
|
val parseStatistics = (parseBlocks ~ parseTransactions ~ parseTotalSupply ~ parseCirculatingSupply).map { |
||||
|
case blocks ~ transactions ~ totalSupply ~ circulatingSupply => |
||||
|
Statistics(blocks, transactions, totalSupply, circulatingSupply) |
||||
|
} |
||||
|
} |
@ -0,0 +1,20 @@ |
|||||
|
package com.xsn.explorer.data.async |
||||
|
|
||||
|
import javax.inject.Inject |
||||
|
|
||||
|
import com.alexitc.playsonify.core.FutureApplicationResult |
||||
|
import com.xsn.explorer.data.{StatisticsBlockingDataHandler, StatisticsDataHandler} |
||||
|
import com.xsn.explorer.executors.DatabaseExecutionContext |
||||
|
import com.xsn.explorer.models.Statistics |
||||
|
|
||||
|
import scala.concurrent.Future |
||||
|
|
||||
|
class StatisticsFutureDataHandler @Inject() ( |
||||
|
blockingDataHandler: StatisticsBlockingDataHandler)( |
||||
|
implicit ec: DatabaseExecutionContext) |
||||
|
extends StatisticsDataHandler[FutureApplicationResult] { |
||||
|
|
||||
|
override def getStatistics(): FutureApplicationResult[Statistics] = Future { |
||||
|
blockingDataHandler.getStatistics() |
||||
|
} |
||||
|
} |
@ -1,13 +1,14 @@ |
|||||
package com.xsn.explorer.modules |
package com.xsn.explorer.modules |
||||
|
|
||||
import com.google.inject.AbstractModule |
import com.google.inject.AbstractModule |
||||
import com.xsn.explorer.data.anorm.{BalancePostgresDataHandler, BlockPostgresDataHandler} |
import com.xsn.explorer.data.anorm.{BalancePostgresDataHandler, BlockPostgresDataHandler, StatisticsPostgresDataHandler} |
||||
import com.xsn.explorer.data.{BalanceBlockingDataHandler, BlockBlockingDataHandler} |
import com.xsn.explorer.data.{BalanceBlockingDataHandler, BlockBlockingDataHandler, StatisticsBlockingDataHandler} |
||||
|
|
||||
class DataHandlerModule extends AbstractModule { |
class DataHandlerModule extends AbstractModule { |
||||
|
|
||||
override def configure(): Unit = { |
override def configure(): Unit = { |
||||
bind(classOf[BlockBlockingDataHandler]).to(classOf[BlockPostgresDataHandler]) |
bind(classOf[BlockBlockingDataHandler]).to(classOf[BlockPostgresDataHandler]) |
||||
bind(classOf[BalanceBlockingDataHandler]).to(classOf[BalancePostgresDataHandler]) |
bind(classOf[BalanceBlockingDataHandler]).to(classOf[BalancePostgresDataHandler]) |
||||
|
bind(classOf[StatisticsBlockingDataHandler]).to(classOf[StatisticsPostgresDataHandler]) |
||||
} |
} |
||||
} |
} |
||||
|
Loading…
Reference in new issue