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

Commit

Permalink
#206 update tickers endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Stavetski committed Jun 15, 2018
1 parent 6f2d6bf commit 21c7839
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package fund.cyber.markets.cassandra.repository
import com.datastax.driver.core.ConsistencyLevel
import fund.cyber.markets.cassandra.model.CqlTokenTicker
import org.springframework.data.cassandra.core.mapping.MapId
import org.springframework.data.cassandra.repository.CassandraRepository
import org.springframework.data.cassandra.repository.Consistency
import org.springframework.data.cassandra.repository.Query
import org.springframework.data.cassandra.repository.ReactiveCassandraRepository
Expand All @@ -25,10 +24,6 @@ interface TickerRepository : ReactiveCassandraRepository<CqlTokenTicker, MapId>
@Param("timestampFrom") timestampFrom: Date,
@Param("timestampTo") timestampTo: Date,
@Param("interval") interval: Long): Flux<CqlTokenTicker>
}

@Repository
interface PageableTickerRepository : CassandraRepository<CqlTokenTicker, MapId> {

@Consistency(value = ConsistencyLevel.LOCAL_QUORUM)
@Query("""
Expand Down
2 changes: 2 additions & 0 deletions common/src/main/kotlin/fund/cyber/markets/common/Converter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const val MINUTES_TO_MILLIS: Double = 1000.0 * 60
const val MINUTES_TO_SECONDS: Double = 60.0
const val MINUTES_TO_HOURS: Double = 1.0 / 60

const val DAYS_TO_MILLIS: Double = 1000.0 * 60 * 60 *24

infix fun Long.convert(coefficient: Double): Long {
return (this * coefficient).toLong()
}
3 changes: 3 additions & 0 deletions dev-environment/elassandra-bootstrap.cql
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ CREATE TABLE IF NOT EXISTS markets.ticker (
price blob,

PRIMARY KEY ((tokenSymbol, epochDay, interval), timestampFrom)
)
WITH CLUSTERING ORDER BY (
timestampFrom DESC
);

CREATE TABLE IF NOT EXISTS markets.trade_last_timestamp (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package fund.cyber.markets.api.rest.handler

import fund.cyber.markets.cassandra.model.CqlTokenTicker
import fund.cyber.markets.cassandra.repository.PageableTickerRepository
import fund.cyber.markets.cassandra.repository.TickerRepository
import fund.cyber.markets.common.DAYS_TO_MILLIS
import fund.cyber.markets.common.Intervals
import fund.cyber.markets.common.MILLIS_TO_DAYS
import fund.cyber.markets.common.MINUTES_TO_MILLIS
import fund.cyber.markets.common.convert
Expand All @@ -18,15 +20,15 @@ const val LIMIT_DEFAULT = "100"

@Component
class TickerHandler(
private val tickerRepository: PageableTickerRepository
private val tickerRepository: TickerRepository
) {

fun getTickers(serverRequest: ServerRequest): Mono<ServerResponse> {
val symbol: String
val ts: Long
val interval: Long

val limit = serverRequest.queryParam("symbol").orElse(LIMIT_DEFAULT).toLong()
val limit = serverRequest.queryParam("limit").orElse(LIMIT_DEFAULT).toLong()
try {
symbol = serverRequest.queryParam("symbol").get().toUpperCase()
ts = serverRequest.queryParam("ts").get().toLong()
Expand All @@ -35,13 +37,43 @@ class TickerHandler(
return ServerResponse.status(HttpStatus.BAD_REQUEST).build()
}

val tickers = Flux
.empty<CqlTokenTicker>()
.concatWith(
tickerRepository.find(symbol, ts convert MILLIS_TO_DAYS, Date(ts), interval, limit)
)
val epochDay = ts convert MILLIS_TO_DAYS
val iterations = (ts - (epochDay convert DAYS_TO_MILLIS) + interval * limit) / Intervals.DAY

val dateTs = Date(ts)
var limitVar = limit

var tickers = Flux.empty<CqlTokenTicker>()
for (iteration in 0..iterations) {

val iterationLimit = getLimit(iteration, epochDay, ts, interval, limitVar)
limitVar -=iterationLimit

tickers = tickers
.concatWith(
tickerRepository.find(symbol, (epochDay + iteration), dateTs, interval, iterationLimit)
)
}

return tickers.asServerResponse()
}

private fun getLimit(iteration: Long, epochDay: Long, ts: Long, interval: Long, limit: Long): Long {
var limitResult: Long

if (iteration == 0L) {
limitResult = (((epochDay + 1) convert DAYS_TO_MILLIS) - ts) / interval
if (limitResult > limit) {
limitResult = limit
}
} else {
limitResult = Intervals.DAY / interval
if (limitResult > limit) {
limitResult = limit
}
}

return limitResult
}

}

0 comments on commit 21c7839

Please sign in to comment.