Skip to content
This repository has been archived by the owner on Jan 29, 2019. It is now read-only.

Commit

Permalink
#178 Refactoring of search API. Not found items handling. Reactive se…
Browse files Browse the repository at this point in the history
…arch controller.
  • Loading branch information
arturalbov committed Jun 8, 2018
1 parent 4efad3c commit 1f0ccd5
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package fund.cyber.contract.common

import fund.cyber.search.configuration.CHAIN_FAMILY
import fund.cyber.search.configuration.CHAIN_NAME
import fund.cyber.search.model.chains.ChainFamily
import fund.cyber.search.model.chains.ChainInfo
import fund.cyber.search.model.chains.Chain
import io.micrometer.core.instrument.MeterRegistry
import org.springframework.beans.factory.annotation.Value
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
Expand All @@ -15,17 +12,11 @@ import org.springframework.scheduling.annotation.EnableScheduling
@Configuration
class CommonConfiguration {

@Value("\${$CHAIN_FAMILY:}")
private lateinit var chainFamily: String

@Value("\${$CHAIN_NAME:}")
private lateinit var chainName: String

@Bean
fun chainInfo() = ChainInfo(ChainFamily.valueOf(chainFamily), chainName)
@Autowired
private lateinit var chain: Chain

@Bean
fun metricsCommonTags(): MeterRegistryCustomizer<MeterRegistry> {
return MeterRegistryCustomizer { registry -> registry.config().commonTags("chain", chainInfo().fullName) }
return MeterRegistryCustomizer { registry -> registry.config().commonTags("chain", chain.name) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration


@SpringBootApplication(exclude = [CassandraDataAutoConfiguration::class, KafkaAutoConfiguration::class])
class ApplicationConfiguration {
class UpdateEthereumContractSummaryApplication {

companion object {
@JvmStatic
fun main(args: Array<String>) {
SpringApplication.run(ApplicationConfiguration::class.java, *args)
SpringApplication.run(UpdateEthereumContractSummaryApplication::class.java, *args)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import fund.cyber.contract.ethereum.delta.EthereumTxDeltaProcessor
import fund.cyber.contract.ethereum.delta.EthereumUncleDeltaProcessor
import fund.cyber.search.configuration.KAFKA_BROKERS
import fund.cyber.search.configuration.KAFKA_BROKERS_DEFAULT
import fund.cyber.search.model.chains.ChainInfo
import fund.cyber.search.model.chains.Chain
import fund.cyber.search.model.ethereum.EthereumBlock
import fund.cyber.search.model.ethereum.EthereumTx
import fund.cyber.search.model.ethereum.EthereumUncle
Expand Down Expand Up @@ -62,7 +62,7 @@ class EthereumTxConsumerConfiguration {
private lateinit var monitoring: MeterRegistry

@Autowired
private lateinit var chainInfo: ChainInfo
private lateinit var chain: Chain


//todo generate containers on the fly from DeltaProcessor information
Expand All @@ -73,7 +73,7 @@ class EthereumTxConsumerConfiguration {
consumerConfigs(), JsonDeserializer(PumpEvent::class.java), JsonDeserializer(EthereumTx::class.java)
)

val containerProperties = ContainerProperties(chainInfo.txPumpTopic).apply {
val containerProperties = ContainerProperties(chain.txPumpTopic).apply {
setBatchErrorHandler(SeekToCurrentBatchErrorHandler())
messageListener = UpdateContractSummaryProcess(contractSummaryStorage, txDeltaProcessor, deltaMerger,
monitoring, kafkaBrokers)
Expand All @@ -93,7 +93,7 @@ class EthereumTxConsumerConfiguration {
consumerConfigs(), JsonDeserializer(PumpEvent::class.java), JsonDeserializer(EthereumBlock::class.java)
)

val containerProperties = ContainerProperties(chainInfo.blockPumpTopic).apply {
val containerProperties = ContainerProperties(chain.blockPumpTopic).apply {
setBatchErrorHandler(SeekToCurrentBatchErrorHandler())
messageListener = UpdateContractSummaryProcess(contractSummaryStorage, blockDeltaProcessor, deltaMerger,
monitoring, kafkaBrokers)
Expand All @@ -113,7 +113,7 @@ class EthereumTxConsumerConfiguration {
consumerConfigs(), JsonDeserializer(PumpEvent::class.java), JsonDeserializer(EthereumUncle::class.java)
)

val containerProperties = ContainerProperties(chainInfo.unclePumpTopic).apply {
val containerProperties = ContainerProperties(chain.unclePumpTopic).apply {
setBatchErrorHandler(SeekToCurrentBatchErrorHandler())
messageListener = UpdateContractSummaryProcess(contractSummaryStorage, uncleDeltaProcessor, deltaMerger,
monitoring, kafkaBrokers)
Expand Down
25 changes: 24 additions & 1 deletion docs/api/search.v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,28 @@ paths:
operationId: search-stats
responses:
'200':
description: 'Successful statistics'
description: 'Successful request'
content:
application/json:
schema:
$ref: '#/components/schemas/SearchStatsResponse'


/search/chains:
get:
tags:
- search
summary: Search API available chains and their entities
description: 'Endpoint to obtain search chains and their entities'
operationId: search-chains
responses:
'200':
description: 'Successful request'
content:
application/json:
schema:
$ref: '#/components/schemas/SearchChainsResponse'

components:
schemas:

Expand Down Expand Up @@ -143,3 +159,10 @@ components:
indexSizeBytes:
type: integer
format: int64

SearchChainsResponse:
type: object
additionalProperties:
type: array
items:
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ data class ContractTxSummaryDto(

data class ContractSummaryDto(
val hash: String,
val confirmedBalance: String,
val confirmedTotalReceived: String,
val confirmedBalance: BigDecimal,
val confirmedTotalReceived: BigDecimal,
val confirmedTxNumber: Int,
val firstActivityDate: Instant,
val lastActivityDate: Instant,
val unconfirmedTxValues: Map<String, ContractTxSummaryDto> = emptyMap()
) {

constructor(contract: CqlBitcoinContractSummary, txes: List<CqlBitcoinContractTxPreview>) : this(
hash = contract.hash, confirmedBalance = contract.confirmedBalance,
confirmedTotalReceived = contract.confirmedTotalReceived, confirmedTxNumber = contract.confirmedTxNumber,
hash = contract.hash, confirmedBalance = BigDecimal(contract.confirmedBalance),
confirmedTotalReceived = BigDecimal(contract.confirmedTotalReceived),
confirmedTxNumber = contract.confirmedTxNumber,
firstActivityDate = contract.firstActivityDate, lastActivityDate = contract.lastActivityDate,
unconfirmedTxValues = txes.map { contractTx -> contractTx.hash to ContractTxSummaryDto(contractTx) }.toMap()
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package fund.cyber.api.bitcoin.handlers

import fund.cyber.api.common.SingleRepositoryItemRequestHandler
import fund.cyber.api.common.asServerResponse
import fund.cyber.api.common.toPageableResponse
import fund.cyber.cassandra.bitcoin.model.CqlBitcoinBlock
import fund.cyber.cassandra.bitcoin.repository.BitcoinBlockRepository
import fund.cyber.cassandra.bitcoin.repository.PageableBitcoinBlockTxRepository
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.DependsOn
import org.springframework.web.reactive.function.server.ServerResponse

@Configuration
@DependsOn("bitcoin-search-repositories")
Expand All @@ -20,9 +19,9 @@ class BitcoinBlockHandlersConfiguration {
BitcoinBlockRepository::class.java
) { request, repository ->

val blockNumber = request.pathVariable("blockNumber").toLong()
val block = repository.findById(blockNumber)
ServerResponse.ok().body(block, CqlBitcoinBlock::class.java)
val blockNumber = request.pathVariable("blockNumber").toLong()
val block = repository.findById(blockNumber)
block.asServerResponse()
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fund.cyber.api.bitcoin.handlers
import fund.cyber.api.bitcoin.dto.ContractSummaryDto
import fund.cyber.api.common.BiRepositoryItemRequestHandler
import fund.cyber.api.common.SingleRepositoryItemRequestHandler
import fund.cyber.api.common.asServerResponse
import fund.cyber.api.common.toPageableResponse
import fund.cyber.cassandra.bitcoin.repository.BitcoinContractSummaryRepository
import fund.cyber.cassandra.bitcoin.repository.BitcoinContractTxRepository
Expand All @@ -11,7 +12,6 @@ import fund.cyber.cassandra.bitcoin.repository.PageableBitcoinContractTxReposito
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.DependsOn
import org.springframework.web.reactive.function.server.ServerResponse

@Configuration
@DependsOn("bitcoin-search-repositories")
Expand All @@ -32,7 +32,7 @@ class BitcoinContractHandlersConfiguration {
val result = contract.zipWith(contractUnconfirmedTxes.collectList()) { contr, txes ->
ContractSummaryDto(contr, txes)
}
ServerResponse.ok().body(result, ContractSummaryDto::class.java)
result.asServerResponse()
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package fund.cyber.api.bitcoin.handlers

import fund.cyber.api.common.SingleRepositoryItemRequestHandler
import fund.cyber.cassandra.bitcoin.model.CqlBitcoinTx
import fund.cyber.api.common.asServerResponse
import fund.cyber.cassandra.bitcoin.repository.BitcoinTxRepository
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.DependsOn
import org.springframework.web.reactive.function.server.ServerResponse

@Configuration
@DependsOn("bitcoin-search-repositories")
Expand All @@ -20,7 +19,7 @@ class BitcoinTxHandlersConfiguration {

val hash = request.pathVariable("hash")
val tx = repository.findById(hash)
ServerResponse.ok().body(tx, CqlBitcoinTx::class.java)
tx.asServerResponse()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,22 @@ import org.springframework.context.support.GenericApplicationContext
import org.springframework.web.cors.CorsConfiguration
import org.springframework.web.cors.reactive.CorsWebFilter
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource
import org.springframework.web.reactive.function.BodyInserters
import org.springframework.web.reactive.function.server.HandlerFunction
import org.springframework.web.reactive.function.server.RequestPredicates.path
import org.springframework.web.reactive.function.server.RouterFunction
import org.springframework.web.reactive.function.server.RouterFunctions
import org.springframework.web.reactive.function.server.ServerResponse
import org.springframework.web.reactive.function.server.ServerResponse.notFound
import org.springframework.web.reactive.function.server.ServerResponse.ok
import org.springframework.web.util.pattern.PathPatternParser
import reactor.core.publisher.Mono
import java.net.InetAddress


fun <T> Mono<T>.asServerResponse() = this.flatMap { obj -> ok().body(BodyInserters.fromObject(obj)) }
.switchIfEmpty(notFound().build())

fun <E : ServerResponse> List<RouterFunction<E>>.asSingleRouterFunction(): RouterFunction<E> {
return if (isEmpty()) {
RouterFunctions.route(path("/ping"), HandlerFunction<E> { _ -> ServerResponse.ok().build() as Mono<E> })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import java.time.Instant

data class ContractSummaryDto(
val hash: String,
val confirmedBalance: String,
val confirmedBalance: BigDecimal,
val smartContract: Boolean,
val confirmedTotalReceived: String,
val confirmedTotalReceived: BigDecimal,
val txNumber: Int,
val minedUncleNumber: Int,
val minedBlockNumber: Int,
Expand All @@ -19,10 +19,11 @@ data class ContractSummaryDto(
) {

constructor(contract: CqlEthereumContractSummary, txes: List<CqlEthereumContractTxPreview>) : this(
hash = contract.hash, confirmedBalance = contract.confirmedBalance, smartContract = contract.smartContract,
confirmedTotalReceived = contract.confirmedTotalReceived, txNumber = contract.txNumber,
minedUncleNumber = contract.minedUncleNumber, minedBlockNumber = contract.minedBlockNumber,
firstActivityDate = contract.firstActivityDate, lastActivityDate = contract.lastActivityDate,
hash = contract.hash, confirmedBalance = BigDecimal(contract.confirmedBalance),
smartContract = contract.smartContract, confirmedTotalReceived = BigDecimal(contract.confirmedTotalReceived),
txNumber = contract.txNumber, minedUncleNumber = contract.minedUncleNumber,
minedBlockNumber = contract.minedBlockNumber, firstActivityDate = contract.firstActivityDate,
lastActivityDate = contract.lastActivityDate,
unconfirmedTxValues = txes.map { contractTx -> contractTx.hash to BigDecimal(contractTx.value) }.toMap()
)
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package fund.cyber.api.ethereum.handlers

import fund.cyber.api.common.SingleRepositoryItemRequestHandler
import fund.cyber.api.common.asServerResponse
import fund.cyber.api.common.toPageableResponse
import fund.cyber.cassandra.ethereum.model.CqlEthereumBlock
import fund.cyber.cassandra.ethereum.repository.EthereumBlockRepository
import fund.cyber.cassandra.ethereum.repository.PageableEthereumBlockTxRepository
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.DependsOn
import org.springframework.web.reactive.function.server.ServerResponse

@Configuration
@DependsOn("ethereum-search-repositories")
Expand All @@ -22,7 +21,7 @@ class EthereumBlockHandlersConfiguration {

val blockNumber = request.pathVariable("blockNumber").toLong()
val block = repository.findById(blockNumber)
ServerResponse.ok().body(block, CqlEthereumBlock::class.java)
block.asServerResponse()
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fund.cyber.api.ethereum.handlers

import fund.cyber.api.common.BiRepositoryItemRequestHandler
import fund.cyber.api.common.SingleRepositoryItemRequestHandler
import fund.cyber.api.common.asServerResponse
import fund.cyber.api.common.toPageableResponse
import fund.cyber.api.ethereum.dto.ContractSummaryDto
import fund.cyber.cassandra.ethereum.repository.EthereumContractRepository
Expand All @@ -13,7 +14,6 @@ import fund.cyber.common.toSearchHashFormat
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.DependsOn
import org.springframework.web.reactive.function.server.ServerResponse

@Configuration
@DependsOn("ethereum-search-repositories")
Expand All @@ -34,7 +34,7 @@ class EthereumContractHandlersConfiguration {
val result = contract.zipWith(contractUnconfirmedTxes.collectList()) { contr, txes ->
ContractSummaryDto(contr, txes)
}
ServerResponse.ok().body(result, ContractSummaryDto::class.java)
result.asServerResponse()
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package fund.cyber.api.ethereum.handlers

import fund.cyber.api.common.SingleRepositoryItemRequestHandler
import fund.cyber.cassandra.ethereum.model.CqlEthereumTx
import fund.cyber.api.common.asServerResponse
import fund.cyber.cassandra.ethereum.repository.EthereumTxRepository
import fund.cyber.common.toSearchHashFormat
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.DependsOn
import org.springframework.web.reactive.function.server.ServerResponse

@Configuration
@DependsOn("ethereum-search-repositories")
Expand All @@ -21,7 +20,7 @@ class EthereumTxHandlersConfiguration {

val hash = request.pathVariable("hash")
val tx = repository.findById(hash.toSearchHashFormat())
ServerResponse.ok().body(tx, CqlEthereumTx::class.java).switchIfEmpty(ServerResponse.notFound().build())
tx.asServerResponse()
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package fund.cyber.api.ethereum.handlers

import fund.cyber.api.common.SingleRepositoryItemRequestHandler
import fund.cyber.cassandra.ethereum.model.CqlEthereumUncle
import fund.cyber.api.common.asServerResponse
import fund.cyber.cassandra.ethereum.repository.EthereumUncleRepository
import fund.cyber.common.toSearchHashFormat
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.DependsOn
import org.springframework.web.reactive.function.server.ServerResponse

@Configuration
@DependsOn("ethereum-search-repositories")
Expand All @@ -21,7 +20,7 @@ class EthereumUncleHandlersConfiguration {

val hash = request.pathVariable("hash")
val uncle = repository.findById(hash.toSearchHashFormat())
ServerResponse.ok().body(uncle, CqlEthereumUncle::class.java)
uncle.asServerResponse()
}

}
Loading

0 comments on commit 1f0ccd5

Please sign in to comment.