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

Fetch Diagnosis Keys for current day (contributes to corona-warn-app/cwa-documentation#236) #453

Closed
Closed
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 @@ -28,6 +28,7 @@ import de.rki.coronawarnapp.storage.LocalData
import de.rki.coronawarnapp.storage.keycache.KeyCacheEntity
import de.rki.coronawarnapp.storage.keycache.KeyCacheRepository
import de.rki.coronawarnapp.storage.keycache.KeyCacheRepository.DateEntryType.DAY
import de.rki.coronawarnapp.storage.keycache.KeyCacheRepository.DateEntryType.HOUR
import de.rki.coronawarnapp.util.CachedKeyFileHolder.asyncFetchFiles
import de.rki.coronawarnapp.util.TimeAndDateExtensions.toServerFormat
import kotlinx.coroutines.Deferred
Expand Down Expand Up @@ -103,6 +104,16 @@ object CachedKeyFileHolder {
.map { url -> async { url.createDayEntryForUrl() } }
)
}
// there is no check if the date is available, since we want to get
// Diagnosis Keys for a day which is not yet complete
val missingHours = getMissingHoursFromDiff(currentDate)
if (missingHours.isNotEmpty()) {
deferredQueries.addAll(
missingHours
.map { getURLForHour(currentDate.toServerFormat(), it) }
.map { url -> async { url.createHourEntryForUrl() } }
)
}
// execute the query plan
try {
deferredQueries.awaitAll()
Expand Down Expand Up @@ -130,6 +141,20 @@ object CachedKeyFileHolder {
.also { Timber.d("${it.size} missing days") }
}

/**
* Calculates the missing hours based on current missing entries in the cache
*
* @param day current day
*/
private suspend fun getMissingHoursFromDiff(day: Date): List<String> {
val cacheEntries = keyCache.getHours()
return getHoursFromServer(day)
.also { Timber.v(TAG, "${it.size} hours from server") }
.filter { it.hourEntryCacheMiss(cacheEntries, day) }
.toList()
.also { Timber.d(TAG, "${it.size} missing hours") }
}

/**
* TODO remove before Release
*/
Expand All @@ -155,6 +180,17 @@ object CachedKeyFileHolder {
.map { date -> date.id }
.contains(getURLForDay(this).generateCacheKeyFromString())

/**
* Determines whether a given String has an existing hour cache entry under a unique name
* given from the URL that is based on this String
*
* @param cache the given cache entries
* @param day current day
*/
private fun String.hourEntryCacheMiss(cache: List<KeyCacheEntity>, day: Date) = !cache
.map { hour -> hour.id }
.contains(getURLForHour(day.toServerFormat(), this).generateCacheKeyFromString())

/**
* Creates a date entry in the Key Cache for a given String with a unique Key Name derived from the URL
* and the URI of the downloaded File for that given key
Expand All @@ -165,6 +201,16 @@ object CachedKeyFileHolder {
DAY
)

/**
* Creates an hour entry in the Key Cache for a given String with a unique Key Name derived from the URL
* and the URI of the downloaded File for that given key
*/
private suspend fun String.createHourEntryForUrl() = keyCache.createEntry(
this.generateCacheKeyFromString(),
WebRequestBuilder.getInstance().asyncGetKeyFilesFromServer(this).toURI(),
HOUR
)

/**
* Generates a unique key name (UUIDv3) for the cache entry based out of a string (e.g. an url)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ class CachedKeyFileHolderTest {
val date = Date()

coEvery { keyCacheRepository.getDates() } returns listOf()
coEvery { keyCacheRepository.getHours() } returns listOf()
coEvery { keyCacheRepository.getFilesFromEntries() } returns listOf()
every { CachedKeyFileHolder["isLast3HourFetchEnabled"]() } returns false
every { CachedKeyFileHolder["checkForFreeSpace"]() } returns Unit
every { CachedKeyFileHolder["getDatesFromServer"]() } returns arrayListOf<String>()
every { CachedKeyFileHolder["getHoursFromServer"](any<Date>()) } returns arrayListOf<String>()

runBlocking {

Expand All @@ -63,6 +65,8 @@ class CachedKeyFileHolderTest {
keyCacheRepository.deleteOutdatedEntries()
CachedKeyFileHolder["getMissingDaysFromDiff"](arrayListOf<String>())
keyCacheRepository.getDates()
CachedKeyFileHolder["getMissingHoursFromDiff"](any<Date>())
keyCacheRepository.getHours()
keyCacheRepository.getFilesFromEntries()
}
}
Expand Down