Skip to content

Commit

Permalink
Merge pull request #38 from horizontalsystems/price-chart-refactoring
Browse files Browse the repository at this point in the history
Price chart refactoring
  • Loading branch information
abdrasulov authored Mar 23, 2023
2 parents aad3b73 + a246d4d commit 460dde0
Show file tree
Hide file tree
Showing 14 changed files with 20 additions and 277 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,12 @@ class MainViewModel(private val marketKit: MarketKit) : ViewModel() {
val time = Date().time / 1000 - TimeUnit.DAYS.toSeconds(7)

val interval = HsPeriodType.ByStartTime(time)
//get stored chart info
val storedChartInfo = marketKit.chartInfo(coinUid, currencyCode, interval)
Log.w("AAA", "storedChartInfo: ${storedChartInfo?.points}")

//fetch chartInfo from API
marketKit.chartInfoSingle(coinUid, currencyCode, interval)
.subscribeOn(Schedulers.io())
.subscribe({
Log.w("AAA", "fetchChartInfo: ${it.points}")
Log.w("AAA", "fetchChartInfo: ${it}")
}, {
Log.e("AAA", "fetchChartInfo Error", it)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,7 @@ class MarketKit(

// Chart Info

fun chartInfo(coinUid: String, currencyCode: String, periodType: HsPeriodType): ChartInfo? {
return chartManager.getChartInfo(coinUid, currencyCode, periodType)
}

fun chartInfoSingle(coinUid: String, currencyCode: String, periodType: HsPeriodType): Single<ChartInfo> {
fun chartInfoSingle(coinUid: String, currencyCode: String, periodType: HsPeriodType): Single<List<ChartPoint>> {
return chartManager.chartInfoSingle(coinUid, currencyCode, periodType)
}

Expand Down Expand Up @@ -339,10 +335,7 @@ class MarketKit(
coinPriceManager.listener = coinPriceSyncManager
val cryptoCompareProvider = CryptoCompareProvider(cryptoCompareApiKey)
val postManager = PostManager(cryptoCompareProvider)
val chartManager = ChartManager(
ChartPointStorage(marketDatabase),
hsProvider
)
val chartManager = ChartManager(hsProvider)
val globalMarketInfoStorage = GlobalMarketInfoStorage(marketDatabase)
val globalMarketInfoManager = GlobalMarketInfoManager(hsProvider, globalMarketInfoStorage)
val hsDataSyncer = HsDataSyncer(coinSyncer, hsProvider)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,89 +1,23 @@
package io.horizontalsystems.marketkit.chart

import io.horizontalsystems.marketkit.NoChartData
import io.horizontalsystems.marketkit.models.*
import io.horizontalsystems.marketkit.providers.HsProvider
import io.horizontalsystems.marketkit.storage.ChartPointStorage
import io.reactivex.Single
import java.util.*

class ChartManager(
private val storage: ChartPointStorage,
private val provider: HsProvider,
) {
private fun chartInfo(points: List<ChartPoint>, periodType: HsPeriodType): ChartInfo? {
val lastPoint = points.lastOrNull() ?: return null

val lastPointTimestamp = lastPoint.timestamp
val intervalRange = periodType.range
val startTimestamp = intervalRange?.let {
lastPointTimestamp - it
} ?: points.first().timestamp

val currentTimestamp = Date().time / 1000
val lastPointGap = currentTimestamp - lastPointTimestamp

// if points not in visible window (too early) just return null
if (intervalRange != null && lastPointGap > intervalRange) {
return null
}

return ChartInfo(
points,
startTimestamp,
currentTimestamp,
isExpired = lastPointGap > periodType.expiration
)
}

private fun storedChartPoints(key: ChartInfoKey): List<ChartPoint> {
return storage.getList(key.coinUid, key.currencyCode, key.periodType).map { point ->
ChartPoint(
point.value,
point.timestamp,
point.volume?.let { mapOf(ChartPointType.Volume to it) } ?: emptyMap()
)
}
}

fun update(points: List<ChartPoint>, key: ChartInfoKey) {
val records = points.map { point ->
ChartPointEntity(
key.coinUid,
key.currencyCode,
key.periodType,
point.value,
point.extra[ChartPointType.Volume],
point.timestamp,
)
}

storage.delete(key)
storage.save(records)
}

fun getChartInfo(coinUid: String, currencyCode: String, periodType: HsPeriodType): ChartInfo? {
val key = ChartInfoKey(coinUid, currencyCode, periodType)
return chartInfo(storedChartPoints(key), periodType)
}
class ChartManager(private val provider: HsProvider) {

fun chartInfoSingle(
coinUid: String,
currencyCode: String,
periodType: HsPeriodType
): Single<ChartInfo> {
): Single<List<ChartPoint>> {
return provider.coinPriceChartSingle(
coinUid,
currencyCode,
periodType
)
.flatMap { response ->
val points = response.map { it.chartPoint }

chartInfo(points, periodType)?.let {
Single.just(it)
} ?: Single.error(NoChartData())
}
).map { response ->
response.map { it.chartPoint }
}
}

fun chartStartTimeSingle(coinUid: String): Single<Long> {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,5 @@ import java.math.BigDecimal
data class ChartPoint(
val value: BigDecimal,
val timestamp: Long,
val extra: Map<ChartPointType, BigDecimal>
val volume: BigDecimal?
)

enum class ChartPointType(val value: String) {
Volume("volume")
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ data class ProChartPointDataRaw(
ChartPoint(
it,
raw.timestamp,
mapOf()
null
)
}
}
Expand All @@ -28,9 +28,7 @@ data class ProChartPointDataRaw(
ChartPoint(
it.toBigDecimal(),
raw.timestamp,
raw.volume?.let {
mapOf(ChartPointType.Volume to it)
} ?: mapOf()
raw.volume
)
}
}
Expand Down Expand Up @@ -110,7 +108,7 @@ data class Analytics(
ChartPoint(
it.volume,
it.timestamp,
mapOf()
null
)
}
}
Expand All @@ -125,7 +123,7 @@ data class Analytics(
ChartPoint(
it.volume,
it.timestamp,
mapOf()
null
)
}
}
Expand All @@ -143,7 +141,7 @@ data class Analytics(
ChartPoint(
it.count,
it.timestamp,
mapOf()
null
)
}
}
Expand All @@ -161,7 +159,7 @@ data class Analytics(
ChartPoint(
it.count,
it.timestamp,
mapOf()
null
)
}
}
Expand All @@ -177,7 +175,7 @@ data class Analytics(
ChartPoint(
it.tvl,
it.timestamp,
mapOf()
null
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ sealed class HsPeriodType {
data class ByPeriod(val timePeriod: HsTimePeriod) : HsPeriodType()
data class ByStartTime(val startTime: Long) : HsPeriodType()

val expiration: Long
get() = HsChartRequestHelper.pointInterval(this).interval

val range: Long?
get() = when (this) {
is ByPeriod -> timePeriod.range
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class HsProvider(baseUrl: String, apiKey: String) {
return service.getMarketInfoTvl(coinUid, currencyCode, timePeriod.value)
.map { responseList ->
responseList.mapNotNull {
it.tvl?.let { tvl -> ChartPoint(tvl, it.timestamp, emptyMap()) }
it.tvl?.let { tvl -> ChartPoint(tvl, it.timestamp, null) }
}
}
}
Expand All @@ -135,7 +135,7 @@ class HsProvider(baseUrl: String, apiKey: String) {
).map { responseList ->
responseList.mapNotNull {
it.tvl?.let { tvl ->
ChartPoint(tvl, it.timestamp, emptyMap())
ChartPoint(tvl, it.timestamp, null)
}
}
}
Expand Down Expand Up @@ -492,7 +492,7 @@ data class ChartCoinPriceResponse(
return ChartPoint(
price,
timestamp,
totalVolume?.let { mapOf(ChartPointType.Volume to it) } ?: emptyMap()
totalVolume
)
}
}

This file was deleted.

Loading

0 comments on commit 460dde0

Please sign in to comment.