Skip to content

Commit

Permalink
Merge pull request #54 from psuzn/develop
Browse files Browse the repository at this point in the history
Develop -> Main
  • Loading branch information
psuzn authored May 26, 2024
2 parents 08b8b09 + 94dd013 commit 1d0511a
Show file tree
Hide file tree
Showing 7 changed files with 771 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.sujanpoudel.playdeals

import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
Expand Down Expand Up @@ -129,7 +130,8 @@ fun configureDI(
.useJobActivator(instance())
.useBackgroundJobServer(
BackgroundJobServerConfiguration.usingStandardBackgroundJobServerConfiguration()
.andDeleteSucceededJobsAfter(Duration.ofHours(6))
.andDeleteSucceededJobsAfter(Duration.ofMinutes(10))
.andPermanentlyDeleteDeletedJobsAfter(Duration.ofMinutes(10))
.andWorkerCount(1)
.andPollIntervalInSeconds(10)
)
Expand All @@ -154,7 +156,8 @@ fun configureDI(
bindSingleton {
AndroidAppExpiryCheckScheduler(
repository = instance(),
requestScheduler = instance()
requestScheduler = instance(),
storageProvider = instance()
)
}
bindSingleton {
Expand Down Expand Up @@ -193,5 +196,6 @@ private fun configureObjectMapper(): ObjectMapper {
registerKotlinModule()
registerModule(JavaTimeModule())
configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ data class ForexRate(
val rates: List<ConversionRate>
)

data class ConversionRate(val currency: String, val rate: Float)
data class ConversionRate(
val currency: String,
val symbol: String,
val name: String,
val rate: Float
)
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ private fun Float.formatAsPrice(): String {
return "$int.$formattedDecimal"
}

fun DealEntity.formattedCurrentPrice() = "${currency.asCurrencySymbol()}${normalPrice.formatAsPrice()}"
fun DealEntity.formattedCurrentPrice() = "${currency.asCurrencySymbol()}${currentPrice.formatAsPrice()}"

fun DealEntity.formattedNormalPrice() = "${currency.asCurrencySymbol()}${normalPrice.formatAsPrice()}"
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ package me.sujanpoudel.playdeals.jobs

import me.sujanpoudel.playdeals.common.SIMPLE_NAME
import me.sujanpoudel.playdeals.common.loggingExecutionTime
import me.sujanpoudel.playdeals.logger
import me.sujanpoudel.playdeals.repositories.DealRepository
import org.jobrunr.jobs.lambdas.JobRequest
import org.jobrunr.jobs.states.StateName
import org.jobrunr.scheduling.JobRequestScheduler
import org.jobrunr.scheduling.RecurringJobBuilder
import org.jobrunr.storage.StorageProvider
import java.time.Duration
import java.time.Instant
import java.time.temporal.ChronoUnit
import java.util.UUID

class AndroidAppExpiryCheckScheduler(
private val repository: DealRepository,
private val requestScheduler: JobRequestScheduler
private val requestScheduler: JobRequestScheduler,
private val storageProvider: StorageProvider
) : CoJobRequestHandler<AndroidAppExpiryCheckScheduler.Request>() {

override suspend fun handleRequest(jobRequest: Request): Unit = loggingExecutionTime(
Expand All @@ -21,6 +27,10 @@ class AndroidAppExpiryCheckScheduler(
.map { AppDetailScrapper.Request(it.id) }

requestScheduler.enqueue(apps)

val lastUpdatedTime = Instant.now().minus(1, ChronoUnit.HOURS)
val jobs = storageProvider.deleteJobsPermanently(StateName.FAILED, lastUpdatedTime)
logger.info("deleted FAILED `$jobs`")
}

class Request private constructor() : JobRequest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.sujanpoudel.playdeals.jobs

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import io.vertx.core.json.Json
import io.vertx.ext.web.client.WebClient
import io.vertx.ext.web.client.WebClientOptions
Expand All @@ -19,8 +21,21 @@ import org.kodein.di.instance
import java.time.Duration
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util.Locale
import java.util.UUID

data class Currency(
val name: String,
val symbol: String
)

fun loadCurrencies(): HashMap<String, me.sujanpoudel.playdeals.jobs.Currency> {
return Gson().fromJson(
Thread.currentThread().contextClassLoader.getResource("currencies.json")?.readText() ?: "{}",
object : TypeToken<HashMap<String, me.sujanpoudel.playdeals.jobs.Currency>>() {}
)
}

class ForexFetcher(
override val di: DI,
private val conf: Conf
Expand All @@ -46,6 +61,8 @@ class ForexFetcher(
}

private suspend fun getForexRates(): ForexRate {
val currencies = loadCurrencies()

val response = webClient.get("/v1/latest?access_key=${conf.forexApiKey}&format=1&base=EUR")
.send()
.await()
Expand All @@ -60,7 +77,14 @@ class ForexFetcher(
return ForexRate(
timestamp = OffsetDateTime.ofInstant(java.time.Instant.ofEpochSecond(epochSeconds), ZoneOffset.UTC),
rates = response.getJsonObject("rates").map {
ConversionRate(it.key, (it.value as Number).toFloat() / usdRate)
val currency = currencies[it.key]

ConversionRate(
currency = it.key,
symbol = currency?.symbol ?: "$",
name = currency?.name ?: it.key,
rate = (it.value as Number).toFloat() / usdRate
)
}
)
}
Expand Down Expand Up @@ -89,3 +113,10 @@ suspend fun KeyValuesRepository.getForexRate(): ForexRate? = get(KEY_FOREX_RATE)
}

suspend fun KeyValuesRepository.saveForexRate(forexRate: ForexRate) = set(KEY_FOREX_RATE, Json.encode(forexRate))

private val Locale.flagEmoji: String
get() {
val firstLetter = Character.codePointAt(country, 0) - 0x41 + 0x1F1E6
val secondLetter = Character.codePointAt(country, 1) - 0x41 + 0x1F1E6
return String(Character.toChars(firstLetter)) + String(Character.toChars(secondLetter))
}
Loading

0 comments on commit 1d0511a

Please sign in to comment.