-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0923431
commit e065d72
Showing
10 changed files
with
342 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,5 @@ project/metals.sbt | |
|
||
config.env | ||
|
||
/.bsp/ | ||
/.bsp/ | ||
/.bsp/sbt.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...rkets-api/src/main/scala/org/ergoplatform/dex/markets/api/v1/models/charts/ChartGap.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package org.ergoplatform.dex.markets.api.v1.models.charts | ||
|
||
import cats.Show | ||
import doobie.util.Put | ||
import enumeratum.values.{StringEnum, StringEnumEntry} | ||
import io.circe.{Decoder, Encoder} | ||
import sttp.tapir.{Codec, Schema} | ||
import cats.syntax.either._ | ||
import org.ergoplatform.dex.markets.api.v1.services.{Utc, Zone} | ||
import sttp.tapir.Schema.SName | ||
import sttp.tapir.generic.Derived | ||
import tofu.logging.Loggable | ||
import sttp.tapir.generic.auto._ | ||
|
||
import java.time.{Instant, LocalDateTime, ZoneOffset} | ||
import java.time.temporal.ChronoUnit | ||
import scala.concurrent.duration.{DurationInt, FiniteDuration} | ||
|
||
sealed abstract class ChartGap( | ||
val value: String, | ||
val dateFormat: String, | ||
val timeWindow: FiniteDuration, | ||
val pgValue: Int, /** This value is used only for minutes aggregation. E.g. 5 means the gap is 5 minute, 15 - 15 means gap. */ | ||
val minimalGap: Long, | ||
val javaDateFormat: String | ||
) extends StringEnumEntry | ||
|
||
object ChartGap extends StringEnum[ChartGap] { | ||
case object Gap5min extends ChartGap("5min", "YYYY-mm-dd HH24:MI", 5.minutes, 5, 1.hour.toMillis, "yyyy-MM-dd HH:mm") | ||
|
||
case object Gap15min | ||
extends ChartGap("15min", "YYYY-mm-dd HH24:MI", 15.minutes, 15, 6.hours.toMillis, "yyyy-MM-dd HH:mm") | ||
|
||
case object Gap30min | ||
extends ChartGap("30min", "YYYY-mm-dd HH24:MI", 30.minutes, 30, 1.day.toMillis, "yyyy-MM-dd HH:mm") | ||
case object Gap1h extends ChartGap("1h", "YYYY-mm-dd HH24", 1.hour, 1, 1.day.toMillis, "yyyy-MM-dd HH") | ||
case object Gap1d extends ChartGap("1d", "YYYY-mm-dd", 1.day, 1, 7.days.toMillis, "yyyy-MM-dd") | ||
case object Gap1m extends ChartGap("1m", "YYYY-mm", 30.days, 1, 180.days.toMillis, "yyyy-MM") | ||
case object Gap1y extends ChartGap("1y", "YYYY", 365.days, 1, 1095.days.toMillis, "yyyy") | ||
|
||
val values = findValues | ||
|
||
implicit val put: Put[ChartGap] = implicitly[Put[String]].contramap(_.dateFormat) | ||
|
||
implicit val schema: Schema[ChartGap] = implicitly[Derived[Schema[ChartGap]]].value | ||
.modify(_.value)( | ||
_.description("The gap's value. min means minute, h means hour, d means day, m means month, y means year.") | ||
) | ||
.default(ChartGap.Gap1h) | ||
.name(SName("Chart gap")) | ||
|
||
implicit val decoder: Decoder[ChartGap] = | ||
Decoder.decodeString.emap(s => Either.catchNonFatal(ChartGap.withValue(s)).leftMap(_.getMessage)) | ||
|
||
implicit val encoder: Encoder[ChartGap] = Encoder.encodeString.contramap(_.value) | ||
|
||
implicit def plainCodec: Codec.PlainCodec[ChartGap] = Codec.stringCodec(s => ChartGap.withValue(s)) | ||
|
||
implicit val show: Show[ChartGap] = _.value | ||
|
||
implicit def loggable: Loggable[ChartGap] = Loggable.show | ||
|
||
def round(gap: ChartGap, from: Long): Long = | ||
gap match { | ||
case ChartGap.Gap5min | ChartGap.Gap15min | ChartGap.Gap30min | ChartGap.Gap1h => | ||
from / gap.timeWindow.toMillis * gap.timeWindow.toMillis | ||
case ChartGap.Gap1d => | ||
LocalDateTime | ||
.ofInstant(Instant.ofEpochMilli(from), Zone) | ||
.withHour(0) | ||
.withMinute(0) | ||
.withSecond(0) | ||
.truncatedTo(ChronoUnit.DAYS) | ||
.toEpochSecond(ZoneOffset.UTC) * 1000 | ||
case ChartGap.Gap1m => | ||
LocalDateTime | ||
.ofInstant(Instant.ofEpochMilli(from), Zone) | ||
.withDayOfMonth(1) | ||
.withHour(0) | ||
.withMinute(0) | ||
.withSecond(0) | ||
.toEpochSecond(ZoneOffset.UTC) * 1000 | ||
case ChartGap.Gap1y => | ||
LocalDateTime | ||
.ofInstant(Instant.ofEpochMilli(from), Zone) | ||
.withDayOfYear(1) | ||
.withDayOfMonth(1) | ||
.withHour(0) | ||
.withMinute(0) | ||
.withSecond(0) | ||
.toEpochSecond(ZoneOffset.UTC) * 1000 | ||
} | ||
|
||
def updateWithGap(gap: ChartGap, time: Long): Long = | ||
gap match { | ||
case ChartGap.Gap5min | ChartGap.Gap15min | ChartGap.Gap30min | ChartGap.Gap1h => | ||
time + gap.timeWindow.toMillis | ||
case ChartGap.Gap1d => | ||
LocalDateTime | ||
.ofInstant(Instant.ofEpochMilli(time), Zone) | ||
.plusDays(1) | ||
.toEpochSecond(Utc) * 1000 | ||
case ChartGap.Gap1m => | ||
LocalDateTime | ||
.ofInstant(Instant.ofEpochMilli(time), Zone) | ||
.plusMonths(1) | ||
.toEpochSecond(Utc) * 1000 | ||
case ChartGap.Gap1y => | ||
LocalDateTime | ||
.ofInstant(Instant.ofEpochMilli(time), Zone) | ||
.plusYears(1) | ||
.toEpochSecond(Utc) * 1000 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...les/markets-api/src/main/scala/org/ergoplatform/dex/markets/api/v1/services/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.ergoplatform.dex.markets.api.v1 | ||
|
||
import java.time.{ZoneId, ZoneOffset} | ||
import java.util.Locale | ||
|
||
package object services { | ||
val Zone: ZoneId = ZoneId.of("UTC") | ||
val Utc: ZoneOffset = ZoneOffset.UTC | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.