diff --git a/server/app/com/xsn/explorer/services/StatisticsService.scala b/server/app/com/xsn/explorer/services/StatisticsService.scala new file mode 100644 index 0000000..9b26195 --- /dev/null +++ b/server/app/com/xsn/explorer/services/StatisticsService.scala @@ -0,0 +1,13 @@ +package com.xsn.explorer.services + +import javax.inject.Inject + +import com.alexitc.playsonify.core.FutureApplicationResult +import com.xsn.explorer.models.rpc.ServerStatistics + +class StatisticsService @Inject() (xsnService: XSNService) { + + def getServerStatistics(): FutureApplicationResult[ServerStatistics] = { + xsnService.getServerStatistics() + } +} diff --git a/server/app/controllers/StatisticsController.scala b/server/app/controllers/StatisticsController.scala new file mode 100644 index 0000000..6e132a1 --- /dev/null +++ b/server/app/controllers/StatisticsController.scala @@ -0,0 +1,16 @@ +package controllers + +import javax.inject.Inject + +import com.xsn.explorer.services.StatisticsService +import controllers.common.{MyJsonController, MyJsonControllerComponents} + +class StatisticsController @Inject() ( + statisticsService: StatisticsService, + cc: MyJsonControllerComponents) + extends MyJsonController(cc) { + + def getStatus() = publicNoInput { _ => + statisticsService.getServerStatistics() + } +} diff --git a/server/conf/routes b/server/conf/routes index 6a32944..7955816 100644 --- a/server/conf/routes +++ b/server/conf/routes @@ -9,3 +9,5 @@ GET /addresses/:address controllers.AddressesController.getDetails(address: GET /blocks controllers.BlocksController.getLatestBlocks() GET /blocks/:blockhash controllers.BlocksController.getDetails(blockhash: String) + +GET /stats controllers.StatisticsController.getStatus() diff --git a/server/test/controllers/StatisticsControllerSpec.scala b/server/test/controllers/StatisticsControllerSpec.scala new file mode 100644 index 0000000..86ad21b --- /dev/null +++ b/server/test/controllers/StatisticsControllerSpec.scala @@ -0,0 +1,41 @@ +package controllers + +import com.alexitc.playsonify.core.FutureApplicationResult +import com.xsn.explorer.helpers.DummyXSNService +import com.xsn.explorer.models.Height +import com.xsn.explorer.models.rpc.ServerStatistics +import com.xsn.explorer.services.XSNService +import controllers.common.MyAPISpec +import org.scalactic.Good +import play.api.inject.bind +import play.api.test.Helpers._ + +import scala.concurrent.Future + +class StatisticsControllerSpec extends MyAPISpec { + + val stats = ServerStatistics(Height(45454), 93548, BigDecimal("77645419.93177629")) + + val customXSNService = new DummyXSNService { + override def getServerStatistics(): FutureApplicationResult[ServerStatistics] = { + val result = Good(stats) + Future.successful(result) + } + } + + override val application = guiceApplicationBuilder + .overrides(bind[XSNService].to(customXSNService)) + .build() + + "GET /stats" should { + "return the server statistics" in { + val response = GET("/stats") + + status(response) mustEqual OK + val json = contentAsJson(response) + (json \ "height").as[Int] mustEqual stats.height.int + (json \ "transactions").as[Int] mustEqual stats.transactions + (json \ "totalSupply").as[BigDecimal] mustEqual stats.totalSupply + } + } +}