Skip to content

Commit

Permalink
Merge pull request #50 from RADAR-base/release-0.2.3
Browse files Browse the repository at this point in the history
Release 0.2.3
  • Loading branch information
yatharthranjan authored Oct 28, 2022
2 parents 84ffea9 + 7518ef6 commit 640a882
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 20 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ description = "RADAR Push API Gateway to handle secured data flow to backend."

allprojects {
group = "org.radarbase"
version = "0.2.2"
version = "0.2.3"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,46 @@ class GarminRequestGenerator(
),
)

private val userNextRequest: MutableMap<String, Instant> = mutableMapOf()

private var nextRequestTime: Instant = Instant.MIN

private val shouldBackoff: Boolean
get() = Instant.now() < nextRequestTime

override fun requests(user: User, max: Int): Sequence<RestRequest> {
return routes.asSequence()
.flatMap { route ->
val offsets: Offsets? = offsetPersistenceFactory.read(user.versionedId)
val startDate = userRepository.getBackfillStartDate(user)
val startOffset: Instant = if (offsets == null) {
logger.debug("No offsets found for $user, using the start date.")
startDate
} else {
logger.debug("Offsets found in persistence.")
offsets.offsetsMap.getOrDefault(
UserRoute(user.versionedId, route.toString()), startDate
).coerceAtLeast(startDate)
return if (user.ready()) {
routes.asSequence()
.flatMap { route ->
val offsets: Offsets? = offsetPersistenceFactory.read(user.versionedId)
val backfillLimit = Instant.now().minus(route.maxBackfillPeriod())
val startDate = userRepository.getBackfillStartDate(user)
var startOffset: Instant = if (offsets == null) {
logger.debug("No offsets found for $user, using the start date.")
startDate
} else {
logger.debug("Offsets found in persistence.")
offsets.offsetsMap.getOrDefault(
UserRoute(user.versionedId, route.toString()), startDate
).coerceAtLeast(startDate)
}

if (startOffset <= backfillLimit) {
// the start date is before the backfill limits
logger.warn(
"Backfill limit exceeded for $user and $route. " +
"Resetting to earliest allowed start offset."
)
startOffset = backfillLimit.plus(Duration.ofDays(2))
}

val endDate = userRepository.getBackfillEndDate(user)
if (endDate <= startOffset) return@flatMap emptySequence()
val endTime = (startOffset + defaultQueryRange).coerceAtMost(endDate)
route.generateRequests(user, startOffset, endTime, max / routes.size)
}
val endDate = userRepository.getBackfillEndDate(user)
if (endDate <= startOffset) return@flatMap emptySequence()
val endTime = (startOffset + defaultQueryRange).coerceAtMost(endDate)
route.generateRequests(user, startOffset, endTime, max / routes.size)
}
.takeWhile { !shouldBackoff }
.takeWhile { !shouldBackoff }
} else emptySequence()
}

override fun requestSuccessful(request: RestRequest, response: Response) {
Expand All @@ -119,12 +134,28 @@ class GarminRequestGenerator(
logger.info("A duplicate request was made. Marking successful...")
requestSuccessful(request, response)
}
412 -> {
logger.warn(
"User ${request.user} does not have correct permissions/scopes enabled. " +
"Please enable in garmin connect. User backing off for $USER_BACK_OFF_TIME..."
)
userNextRequest[request.user.versionedId] = Instant.now().plus(USER_BACK_OFF_TIME)
}
else -> logger.warn("Request Failed: {}, {}", request, response)
}
}

private fun User.ready(): Boolean {
return if (versionedId in userNextRequest) {
Instant.now() > userNextRequest[versionedId]
} else {
true
}
}

companion object {
private val logger = LoggerFactory.getLogger(GarminRequestGenerator::class.java)
private val BACK_OFF_TIME = Duration.ofMinutes(1L)
private val USER_BACK_OFF_TIME = Duration.ofDays(1L)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ interface Route {
* This is how it would appear in the offsets
*/
override fun toString(): String

fun maxBackfillPeriod(): Duration
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.radarbase.push.integration.garmin.backfill.route

import org.radarbase.push.integration.garmin.user.GarminUserRepository
import java.time.Duration

class GarminActivitiesRoute(
consumerKey: String,
Expand All @@ -10,4 +11,9 @@ class GarminActivitiesRoute(
override fun subPath(): String = "activities"

override fun toString(): String = "garmin_activities"

override fun maxBackfillPeriod(): Duration {
// 2 years default. Activity API routes will override this with 5 years
return Duration.ofDays(365 * 5)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.radarbase.push.integration.garmin.backfill.route

import org.radarbase.push.integration.garmin.user.GarminUserRepository
import java.time.Duration

class GarminActivityDetailsRoute(
consumerKey: String,
Expand All @@ -10,4 +11,9 @@ class GarminActivityDetailsRoute(
override fun subPath(): String = "activityDetails"

override fun toString(): String = "garmin_activity_details"

override fun maxBackfillPeriod(): Duration {
// 2 years default. Activity API routes will override this with 5 years
return Duration.ofDays(365 * 5)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.radarbase.push.integration.garmin.backfill.route

import org.radarbase.push.integration.garmin.user.GarminUserRepository
import java.time.Duration

class GarminMoveIQRoute(
consumerKey: String,
Expand All @@ -10,4 +11,9 @@ class GarminMoveIQRoute(
override fun subPath(): String = "moveiq"

override fun toString(): String = "garmin_move_iq"

override fun maxBackfillPeriod(): Duration {
// 2 years default. Activity API routes will override this with 5 years
return Duration.ofDays(365 * 2)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ abstract class GarminRoute(
}
}

override fun maxBackfillPeriod(): Duration {
// 2 years default. Activity API routes will override this with 5 years
return Duration.ofDays(365 * 2)
}

abstract fun subPath(): String

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class GarminServiceUserRepository(
@Throws(IOException::class)
override fun applyPendingUpdates() {
logger.info("Requesting user information from webservice")
val request = requestFor("users?source-type=$GARMIN_SOURCE").build()
val request = requestFor("users?source-type=$GARMIN_SOURCE&authorized=true").build()
timedCachedUsers = makeRequest<Users>(request, userListReader).users

nextFetch = Instant.now().plus(FETCH_THRESHOLD)
Expand Down

0 comments on commit 640a882

Please sign in to comment.