Skip to content

Instantly share code, notes, and snippets.

@eutkin
Created October 20, 2021 18:43
Show Gist options
  • Save eutkin/9ebc9f03479da4fed943656eb8f76cd4 to your computer and use it in GitHub Desktop.
Save eutkin/9ebc9f03479da4fed943656eb8f76cd4 to your computer and use it in GitHub Desktop.
import java.time.Duration
import javax.cache.processor.EntryProcessor
EntryProcessor<String, List<Map<String, Any?>>, List<Any?>> { entry, args ->
data class ExtParameter(
val function: String,
val historyFilter: (Map<String, Any?>) -> Boolean,
val timeWindow: Long
)
val extParameters = listOf(
ExtParameter(
// "Сумма SERVICE платежей за 2 часа"
"SUM",
{ it["subtype"] == "PAYMENT~SERVICE" },
Duration.ofHours(2).seconds
),
ExtParameter(
// "Количество Internal платежей в мобильном канале"
"COUNT",
{ it["subtype"] == "PAYMENT~INTERNAL" && it["channel"] == "MOBILE" },
Long.MAX_VALUE
)
)
val currentTimestamp: Long = args[0] as Long
var sum0 = 0
var count1 = 0
entry.value.forEach { record ->
extParameters.forEach { (function, historyFilter, timeWindow) ->
if (currentTimestamp - (record["timestamp"] as Long) <= timeWindow && historyFilter(record)) {
when (function) {
"SUM" -> sum0 += record["amount"] as Int
"COUNT" -> count1++
else -> {
}
}
}
}
}
listOf(sum0, count1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment