-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from RADAR-base/release-0.2.4
Release 0.2.4
- Loading branch information
Showing
9 changed files
with
155 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...java/org/radarbase/connect/rest/fitbit/converter/FitbitIntradayCaloriesAvroConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package org.radarbase.connect.rest.fitbit.converter; | ||
|
||
import static org.radarbase.connect.rest.util.ThrowingFunction.tryOrNull; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import io.confluent.connect.avro.AvroData; | ||
import java.time.Instant; | ||
import java.time.LocalTime; | ||
import java.time.ZonedDateTime; | ||
import java.util.stream.Stream; | ||
import org.radarbase.connect.rest.RestSourceConnectorConfig; | ||
import org.radarbase.connect.rest.fitbit.FitbitRestSourceConnectorConfig; | ||
import org.radarbase.connect.rest.fitbit.request.FitbitRestRequest; | ||
import org.radarcns.connector.fitbit.FitbitIntradayCalories; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class FitbitIntradayCaloriesAvroConverter extends FitbitAvroConverter { | ||
|
||
private static final Logger logger = | ||
LoggerFactory.getLogger(FitbitIntradayCaloriesAvroConverter.class); | ||
|
||
private String caloriesTopic; | ||
|
||
public FitbitIntradayCaloriesAvroConverter(AvroData avroData) { | ||
super(avroData); | ||
} | ||
|
||
@Override | ||
protected Stream<TopicData> processRecords( | ||
FitbitRestRequest request, JsonNode root, double timeReceived) { | ||
JsonNode intraday = root.get("activities-calories-intraday"); | ||
if (intraday == null) { | ||
return Stream.empty(); | ||
} | ||
|
||
JsonNode dataset = intraday.get("dataset"); | ||
if (dataset == null) { | ||
return Stream.empty(); | ||
} | ||
|
||
int interval = getRecordInterval(intraday, 60); | ||
|
||
// Used as the date to convert the local times in the dataset to absolute times. | ||
ZonedDateTime startDate = request.getDateRange().end(); | ||
|
||
return iterableToStream(dataset) | ||
.map( | ||
tryOrNull( | ||
activity -> { | ||
Instant time = | ||
startDate.with(LocalTime.parse(activity.get("time").asText())).toInstant(); | ||
|
||
FitbitIntradayCalories calories = | ||
new FitbitIntradayCalories( | ||
time.toEpochMilli() / 1000d, | ||
timeReceived, | ||
interval, | ||
activity.get("value").asDouble(), | ||
activity.get("level").asInt(), | ||
activity.get("mets").asDouble()); | ||
|
||
return new TopicData(time, caloriesTopic, calories); | ||
}, | ||
(a, ex) -> | ||
logger.warn( | ||
"Failed to convert calories from request {} of user {}, {}", | ||
request.getRequest().url(), | ||
request.getUser(), | ||
a, | ||
ex))); | ||
} | ||
|
||
@Override | ||
public void initialize(RestSourceConnectorConfig config) { | ||
caloriesTopic = ((FitbitRestSourceConnectorConfig) config).getFitbitIntradayCaloriesTopic(); | ||
logger.info("Using calories topic {}", caloriesTopic); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...ce/src/main/java/org/radarbase/connect/rest/fitbit/route/FitbitIntradayCaloriesRoute.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.radarbase.connect.rest.fitbit.route; | ||
|
||
import static java.time.temporal.ChronoUnit.MINUTES; | ||
|
||
import io.confluent.connect.avro.AvroData; | ||
import java.util.stream.Stream; | ||
import org.radarbase.connect.rest.converter.PayloadToSourceRecordConverter; | ||
import org.radarbase.connect.rest.fitbit.converter.FitbitIntradayCaloriesAvroConverter; | ||
import org.radarbase.connect.rest.fitbit.request.FitbitRequestGenerator; | ||
import org.radarbase.connect.rest.fitbit.request.FitbitRestRequest; | ||
import org.radarbase.connect.rest.fitbit.user.User; | ||
import org.radarbase.connect.rest.fitbit.user.UserRepository; | ||
|
||
public class FitbitIntradayCaloriesRoute extends FitbitPollingRoute { | ||
|
||
private final FitbitIntradayCaloriesAvroConverter caloriesAvroConverter; | ||
|
||
public FitbitIntradayCaloriesRoute( | ||
FitbitRequestGenerator generator, UserRepository userRepository, AvroData avroData) { | ||
super(generator, userRepository, "intraday_calories"); | ||
caloriesAvroConverter = new FitbitIntradayCaloriesAvroConverter(avroData); | ||
} | ||
|
||
@Override | ||
protected Stream<FitbitRestRequest> createRequests(User user) { | ||
return startDateGenerator(this.getOffset(user).plus(ONE_MINUTE).truncatedTo(MINUTES)) | ||
.map( | ||
dateRange -> | ||
newRequest( | ||
user, | ||
dateRange, | ||
user.getExternalUserId(), | ||
DATE_FORMAT.format(dateRange.start()), | ||
TIME_FORMAT.format(dateRange.start()), | ||
TIME_FORMAT.format(dateRange.end()))); | ||
} | ||
|
||
@Override | ||
protected String getUrlFormat(String baseUrl) { | ||
return baseUrl + "/1/user/%s/activities/calories/date/%s/1d/1min/time/%s/%s.json?timezone=UTC"; | ||
} | ||
|
||
@Override | ||
public PayloadToSourceRecordConverter converter() { | ||
return caloriesAvroConverter; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters