Skip to content

Instantly share code, notes, and snippets.

@deeperunderstanding
Created December 15, 2019 16:33
Show Gist options
  • Save deeperunderstanding/57e7e69a90773ca8940abcd7512490e0 to your computer and use it in GitHub Desktop.
Save deeperunderstanding/57e7e69a90773ca8940abcd7512490e0 to your computer and use it in GitHub Desktop.
@Component
class TickRepository(
@Value("\${db.influx.url}") val dburl : String,
@Value("\${db.influx.user}") val user : String,
@Value("\${db.influx.pw}") val pw : String,
@Value("\${db.influx.dbname}") val dbname : String
) {
private final val logger = LoggerFactory.getLogger(javaClass)
val influxDB = lazy { InfluxDBFactory.connect(dburl, user, pw).setDatabase(dbname) }
fun store(ticks: List<Tick>) {
logger.info("Storing ${ticks.size} ticks.")
val batchPoints = BatchPoints
.database(dbname)
.build()
ticks.map(::convertToPoint).forEach { batchPoints.point(it) }
influxDB.value.write(batchPoints)
}
fun convertToPoint(tick: Tick) = Point.measurement("tick")
.time(Instant.parse(tick.time).toEpochMilli(), TimeUnit.MILLISECONDS)
.tag("name", tick.product_id)
.tag("side", tick.side)
.addField("price", tick.price)
.addField("bid", tick.best_bid)
.addField("ask", tick.best_ask)
.addField("size", tick.last_size)
.build()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment