Browse Source

server: Add getServerStatistics method to XSNService

scalafmt-draft
Alexis Hernandez 7 years ago
parent
commit
490724f39b
  1. 25
      server/app/com/xsn/explorer/models/rpc/ServerStatistics.scala
  2. 25
      server/app/com/xsn/explorer/services/XSNService.scala
  3. 3
      server/test/com/xsn/explorer/helpers/DummyXSNService.scala
  4. 35
      server/test/com/xsn/explorer/services/XSNServiceRPCImplSpec.scala

25
server/app/com/xsn/explorer/models/rpc/ServerStatistics.scala

@ -0,0 +1,25 @@
package com.xsn.explorer.models.rpc
import com.xsn.explorer.models.Height
import play.api.libs.functional.syntax._
import play.api.libs.json.{Json, Reads, Writes, __}
case class ServerStatistics(
height: Height,
transactions: Int,
totalSupply: BigDecimal)
object ServerStatistics {
implicit val reads: Reads[ServerStatistics] = {
val builder = (__ \ 'height).read[Height] and
(__ \ 'transactions).read[Int] and
(__ \ 'total_amount).read[BigDecimal]
builder.apply { (height, transactions, totalSupply) =>
ServerStatistics(height, transactions, totalSupply)
}
}
implicit val writes: Writes[ServerStatistics] = Json.writes[ServerStatistics]
}

25
server/app/com/xsn/explorer/services/XSNService.scala

@ -9,7 +9,7 @@ import com.xsn.explorer.config.RPCConfig
import com.xsn.explorer.errors._
import com.xsn.explorer.executors.ExternalServiceExecutionContext
import com.xsn.explorer.models._
import com.xsn.explorer.models.rpc.{AddressBalance, Block, Transaction}
import com.xsn.explorer.models.rpc.{AddressBalance, Block, ServerStatistics, Transaction}
import org.scalactic.{Bad, Good}
import org.slf4j.LoggerFactory
import play.api.libs.json.{JsNull, JsValue, Reads}
@ -28,6 +28,8 @@ trait XSNService {
def getBlock(blockhash: Blockhash): FutureApplicationResult[Block]
def getLatestBlock(): FutureApplicationResult[Block]
def getServerStatistics(): FutureApplicationResult[ServerStatistics]
}
class XSNServiceRPCImpl @Inject() (
@ -159,6 +161,27 @@ class XSNServiceRPCImpl @Inject() (
}
}
override def getServerStatistics(): FutureApplicationResult[ServerStatistics] = {
val body = s"""
|{
| "jsonrpc": "1.0",
| "method": "gettxoutsetinfo",
| "params": []
|}
|""".stripMargin
server
.post(body)
.map { response =>
val maybe = getResult[ServerStatistics](response)
maybe.getOrElse {
logger.warn(s"Unexpected response from XSN Server, status = ${response.status}, response = ${response.body}")
Bad(XSNUnexpectedResponseError).accumulating
}
}
}
private def mapError(json: JsValue, errorCodeMapper: Map[Int, ApplicationError]): Option[ApplicationError] = {
val jsonErrorMaybe = (json \ "error")
.asOpt[JsValue]

3
server/test/com/xsn/explorer/helpers/DummyXSNService.scala

@ -2,7 +2,7 @@ package com.xsn.explorer.helpers
import com.alexitc.playsonify.core.FutureApplicationResult
import com.xsn.explorer.models._
import com.xsn.explorer.models.rpc.{AddressBalance, Block, Transaction}
import com.xsn.explorer.models.rpc.{AddressBalance, Block, ServerStatistics, Transaction}
import com.xsn.explorer.services.XSNService
class DummyXSNService extends XSNService {
@ -12,4 +12,5 @@ class DummyXSNService extends XSNService {
override def getTransactions(address: Address): FutureApplicationResult[List[TransactionId]] = ???
override def getBlock(blockhash: Blockhash): FutureApplicationResult[Block] = ???
override def getLatestBlock(): FutureApplicationResult[Block] = ???
override def getServerStatistics(): FutureApplicationResult[ServerStatistics] = ???
}

35
server/test/com/xsn/explorer/services/XSNServiceRPCImplSpec.scala

@ -3,7 +3,7 @@ package com.xsn.explorer.services
import com.xsn.explorer.config.RPCConfig
import com.xsn.explorer.errors._
import com.xsn.explorer.helpers.{BlockLoader, DataHelper, Executors, TransactionLoader}
import com.xsn.explorer.models.{Address, Blockhash}
import com.xsn.explorer.models.{Address, Blockhash, Height}
import org.mockito.ArgumentMatchers._
import org.mockito.Mockito._
import org.scalactic.Bad
@ -312,4 +312,37 @@ class XSNServiceRPCImplSpec extends WordSpec with MustMatchers with ScalaFutures
}
}
}
"getServerStatistics" should {
"return the statistics" in {
val content =
"""
|{
| "height": 45204,
| "bestblock": "60da3eccf50f10254dcc35c9a25006e129bc5f0d101f83bad5ce008cc4b47c75",
| "transactions": 93047,
| "txouts": 142721,
| "hash_serialized_2": "1f439c1d43753b9935abeb8a8de9f9010b96f7533ccfdde4432de3648a6f20de",
| "disk_size": 7105097,
| "total_amount": 77634169.93285364
|}
""".stripMargin
val responseBody = createRPCSuccessfulResponse(Json.parse(content))
val json = Json.parse(responseBody)
when(response.status).thenReturn(200)
when(response.json).thenReturn(json)
when(request.post[String](anyString())(any())).thenReturn(Future.successful(response))
whenReady(service.getServerStatistics()) { result =>
result.isGood mustEqual true
val stats = result.get
stats.height mustEqual Height(45204)
stats.transactions mustEqual 93047
stats.totalSupply mustEqual BigDecimal("77634169.93285364")
}
}
}
}

Loading…
Cancel
Save