Rate Limiting - add migration script

This commit is contained in:
Marko Milić 2019-10-10 10:56:51 +02:00
parent 50b7d757e1
commit 5975b65cec
3 changed files with 81 additions and 2 deletions

View File

@ -177,7 +177,7 @@ The two user models are now called AuthUser and ResourceUser
* Answer Transaction Request Challenge (updated)
* Get Transaction Requests (updated)
* Get Roles (new)
* Get Entitlements By Bank And User (new)
* Get Entitlements By Bank And User (naaew)
* Get Consumer (App) (new)
* Get Consumers (App) (new)
* Enable Disable Consumers (Apps) (new)

View File

@ -60,6 +60,7 @@ object Migration extends MdcLoggable {
populateTableAccountAccess()
generateAndPopulateMissingCustomerUUIDs()
generateAndPopulateMissingConsumersUUIDs()
populateTableRateLimiting()
}
private def dummyScript(): Boolean = {
@ -121,11 +122,18 @@ object Migration extends MdcLoggable {
isSuccessful
}
}
private def populateTableRateLimiting(): Boolean = {
val name = nameOf(populateTableRateLimiting)
runOnce(name) {
TableRateLmiting.populate(name)
}
}
}
/**
* In this object we put functions dedcated to common database tasks.
* In this object we put functions dedicated to common database tasks.
*/
object DbFunction {
/**

View File

@ -0,0 +1,71 @@
package code.api.util.migration
import java.time.format.DateTimeFormatter
import java.time.{ZoneId, ZonedDateTime}
import java.util.Date
import code.api.util.APIUtil
import code.api.util.migration.Migration.{DbFunction, saveLog}
import code.model.Consumer
import code.ratelimiting.RateLimiting
import net.liftweb.common.Full
import net.liftweb.mapper.{By, DB}
import net.liftweb.util.DefaultConnectionIdentifier
object TableRateLmiting {
val oneDayAgo = ZonedDateTime.now(ZoneId.of("UTC")).minusDays(1)
val oneYearInFuture = ZonedDateTime.now(ZoneId.of("UTC")).plusYears(1)
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm'Z'")
def populate(name: String): Boolean = {
DbFunction.tableExists(RateLimiting, (DB.use(DefaultConnectionIdentifier){ conn => conn})) match {
case true =>
val startDate = System.currentTimeMillis()
val commitId: String = APIUtil.gitCommit
val consumers = Consumer.findAll()
// Make back up
DbFunction.makeBackUpOfTable(RateLimiting)
// Insert rows into table "ratelimiting" based on data in the table consumer
val insertedRows: List[Boolean] =
for {
consumer <- consumers
} yield {
RateLimiting.find(By(RateLimiting.ConsumerId, consumer.consumerId.get)) match {
case Full(_) => // Already exist
true
case _ =>
RateLimiting.create
.ConsumerId(consumer.consumerId.get)
.PerSecondCallLimit(consumer.perSecondCallLimit.get)
.PerMinuteCallLimit(consumer.perMinuteCallLimit.get)
.PerHourCallLimit(consumer.perHourCallLimit.get)
.PerDayCallLimit(consumer.perDayCallLimit.get)
.PerWeekCallLimit(consumer.perWeekCallLimit.get)
.PerMonthCallLimit(consumer.perMonthCallLimit.get)
.FromDate(Date.from(oneDayAgo.toInstant()))
.ToDate(Date.from(oneYearInFuture.toInstant()))
.save()
}
}
val isSuccessful = insertedRows.forall(_ == true)
val endDate = System.currentTimeMillis()
val comment: String =
s"""Number of inserted rows: ${insertedRows.size}""".stripMargin
saveLog(name, commitId, isSuccessful, startDate, endDate, comment)
isSuccessful
case false =>
val startDate = System.currentTimeMillis()
val commitId: String = APIUtil.gitCommit
val isSuccessful = false
val endDate = System.currentTimeMillis()
val comment: String =
s"""Rate limiting table does not exist""".stripMargin
saveLog(name, commitId, isSuccessful, startDate, endDate, comment)
isSuccessful
}
}
}