Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Remove QuotaCalculator from hotfix 1.3.1 #1196

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import de.rki.coronawarnapp.CoronaWarnApplication
import de.rki.coronawarnapp.R
import de.rki.coronawarnapp.risk.RiskLevel
import de.rki.coronawarnapp.util.security.SecurityHelper.globalEncryptedSharedPreferencesInstance
import org.joda.time.Instant
import java.util.Date

/**
Expand All @@ -19,11 +18,6 @@ object LocalData {

private val TAG: String? = LocalData::class.simpleName

private const val PREFERENCE_NEXT_TIME_RATE_LIMITING_UNLOCKS =
"preference_next_time_rate_limiting_unlocks"
private const val PREFERENCE_GOOGLE_API_PROVIDE_DIAGNOSIS_KEYS_CALL_COUNT =
"preference_google_api_provide_diagnosis_keys_call_count"

/****************************************************
* ONBOARDING DATA
****************************************************/
Expand Down Expand Up @@ -396,40 +390,6 @@ object LocalData {
}
}

var nextTimeRateLimitingUnlocks: Instant
get() {
return Instant.ofEpochMilli(
getSharedPreferenceInstance().getLong(
PREFERENCE_NEXT_TIME_RATE_LIMITING_UNLOCKS,
0L
)
)
}
set(value) {
getSharedPreferenceInstance().edit(true) {
putLong(
PREFERENCE_NEXT_TIME_RATE_LIMITING_UNLOCKS,
value.millis
)
}
}

var googleAPIProvideDiagnosisKeysCallCount: Int
get() {
return getSharedPreferenceInstance().getInt(
PREFERENCE_GOOGLE_API_PROVIDE_DIAGNOSIS_KEYS_CALL_COUNT,
0
)
}
set(value) {
getSharedPreferenceInstance().edit(true) {
putInt(
PREFERENCE_GOOGLE_API_PROVIDE_DIAGNOSIS_KEYS_CALL_COUNT,
value
)
}
}

/**
* Gets the last time of successful risk level calculation as long
* from the EncryptedSharedPrefs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,16 @@ import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction.Retriev
import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction.RetrieveDiagnosisKeysTransactionState.CLOSE
import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction.RetrieveDiagnosisKeysTransactionState.FETCH_DATE_UPDATE
import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction.RetrieveDiagnosisKeysTransactionState.FILES_FROM_WEB_REQUESTS
import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction.RetrieveDiagnosisKeysTransactionState.QUOTA_CALCULATION
import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction.RetrieveDiagnosisKeysTransactionState.RETRIEVE_RISK_SCORE_PARAMS
import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction.RetrieveDiagnosisKeysTransactionState.SETUP
import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction.RetrieveDiagnosisKeysTransactionState.TOKEN
import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction.rollback
import de.rki.coronawarnapp.transaction.RetrieveDiagnosisKeysTransaction.start
import de.rki.coronawarnapp.util.CachedKeyFileHolder
import de.rki.coronawarnapp.util.GoogleQuotaCalculator
import de.rki.coronawarnapp.util.QuotaCalculator
import de.rki.coronawarnapp.worker.BackgroundWorkHelper
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import org.joda.time.Duration
import org.joda.time.Instant
import org.joda.time.chrono.GJChronology
import timber.log.Timber
import java.io.File
import java.util.Date
Expand Down Expand Up @@ -93,9 +88,6 @@ object RetrieveDiagnosisKeysTransaction : Transaction() {
/** Initial Setup of the Transaction and Transaction ID Generation and Date Lock */
SETUP,

/** calculates the Quota so that the rate limiting is caught gracefully*/
QUOTA_CALCULATION,

/** Initialisation of the identifying token used during the entire transaction */
TOKEN,

Expand Down Expand Up @@ -124,25 +116,12 @@ object RetrieveDiagnosisKeysTransaction : Transaction() {
/** atomic reference for the rollback value for created files during the transaction */
private val exportFilesForRollback = AtomicReference<List<File>>()

private val progressTowardsQuotaForRollback = AtomicReference<Int>()

private const val QUOTA_RESET_PERIOD_IN_HOURS = 24

private val quotaCalculator: QuotaCalculator<Int> = GoogleQuotaCalculator(
incrementByAmount = 14,
quotaLimit = 20,
quotaResetPeriod = Duration.standardHours(QUOTA_RESET_PERIOD_IN_HOURS.toLong()),
quotaTimeZone = DateTimeZone.UTC,
quotaChronology = GJChronology.getInstanceUTC()
)

suspend fun startWithConstraints() {
val currentDate = DateTime(Instant.now(), DateTimeZone.UTC)
val lastFetch = DateTime(
LocalData.lastTimeDiagnosisKeysFromServerFetch(),
DateTimeZone.UTC
)

if (LocalData.lastTimeDiagnosisKeysFromServerFetch() == null ||
currentDate.withTimeAtStartOfDay() != lastFetch.withTimeAtStartOfDay()
) {
Expand Down Expand Up @@ -171,18 +150,6 @@ object RetrieveDiagnosisKeysTransaction : Transaction() {
****************************************************/
val currentDate = executeSetup()

/****************************************************
* CALCULATE QUOTA FOR PROVIDE DIAGNOSIS KEYS
****************************************************/
val hasExceededQuota = executeQuotaCalculation()

// When we are above the Quote, cancel the execution entirely
if (hasExceededQuota) {
Timber.tag(TAG).w("above quota, skipping RetrieveDiagnosisKeys")
executeClose()
return@lockAndExecuteUnique
}

/****************************************************
* RETRIEVE TOKEN
****************************************************/
Expand Down Expand Up @@ -225,10 +192,6 @@ object RetrieveDiagnosisKeysTransaction : Transaction() {
if (TOKEN.isInStateStack()) {
rollbackToken()
}
// we reset the quota only if the submission has not happened yet
if (QUOTA_CALCULATION.isInStateStack() && !API_SUBMISSION.isInStateStack()) {
rollbackProgressTowardsQuota()
}
} catch (e: Exception) {
// We handle every exception through a RollbackException to make sure that a single EntryPoint
// is available for the caller.
Expand All @@ -246,11 +209,6 @@ object RetrieveDiagnosisKeysTransaction : Transaction() {
LocalData.googleApiToken(googleAPITokenForRollback.get())
}

private fun rollbackProgressTowardsQuota() {
Timber.v("rollback $QUOTA_CALCULATION")
quotaCalculator.resetProgressTowardsQuota(progressTowardsQuotaForRollback.get())
}

/**
* Executes the INIT Transaction State
*/
Expand All @@ -261,16 +219,6 @@ object RetrieveDiagnosisKeysTransaction : Transaction() {
currentDate
}

/**
* Executes the QUOTA_CALCULATION Transaction State
*/
private suspend fun executeQuotaCalculation() = executeState(
QUOTA_CALCULATION
) {
progressTowardsQuotaForRollback.set(quotaCalculator.getProgressTowardsQuota())
quotaCalculator.calculateQuota()
}

/**
* Executes the TOKEN Transaction State
*/
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.unmockkAll
import kotlinx.coroutines.runBlocking
import org.joda.time.Instant
import org.junit.After
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -47,10 +46,6 @@ class RetrieveDiagnosisKeysTransactionTest {
every { LocalData.lastTimeDiagnosisKeysFromServerFetch() } returns Date()
every { LocalData.lastTimeDiagnosisKeysFromServerFetch(any()) } just Runs
every { LocalData.googleApiToken() } returns UUID.randomUUID().toString()
every { LocalData.googleAPIProvideDiagnosisKeysCallCount = any() } just Runs
every { LocalData.googleAPIProvideDiagnosisKeysCallCount } returns 0
every { LocalData.nextTimeRateLimitingUnlocks = any() } just Runs
every { LocalData.nextTimeRateLimitingUnlocks } returns Instant.now()
}

@Test
Expand All @@ -62,7 +57,6 @@ class RetrieveDiagnosisKeysTransactionTest {

coVerifyOrder {
RetrieveDiagnosisKeysTransaction["executeSetup"]()
RetrieveDiagnosisKeysTransaction["executeQuotaCalculation"]()
RetrieveDiagnosisKeysTransaction["executeRetrieveRiskScoreParams"]()
RetrieveDiagnosisKeysTransaction["executeFetchKeyFilesFromServer"](any<Date>())
RetrieveDiagnosisKeysTransaction["executeFetchDateUpdate"](any<Date>())
Expand All @@ -83,7 +77,6 @@ class RetrieveDiagnosisKeysTransactionTest {

coVerifyOrder {
RetrieveDiagnosisKeysTransaction["executeSetup"]()
RetrieveDiagnosisKeysTransaction["executeQuotaCalculation"]()
RetrieveDiagnosisKeysTransaction["executeRetrieveRiskScoreParams"]()
RetrieveDiagnosisKeysTransaction["executeFetchKeyFilesFromServer"](any<Date>())
RetrieveDiagnosisKeysTransaction["executeAPISubmission"](
Expand Down
Loading