Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First draft to fix a bug when patching timeline events with extreme timestamp #1285

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -8,6 +8,7 @@
import com.hello.suripu.core.db.TrackerMotionDAO;
import com.hello.suripu.core.logging.DataLogger;
import com.hello.suripu.core.models.timeline.v2.ScoreCondition;
import com.hello.suripu.core.models.timeline.v2.SleepMetrics;
import com.hello.suripu.core.models.timeline.v2.TimelineLog;
import com.hello.suripu.core.models.AggregateSleepStats;
import com.hello.suripu.core.models.Event;
Expand Down Expand Up @@ -258,6 +259,16 @@ private void checkValidTimelineOrThrow(final long accountId,final Timeline timel
LOGGER.info("rejected feedback from account_id {} for evening of {} because it resulted in an empty timeline",accountId,timeline.dateNight);
throw new WebApplicationException(Response.status(Response.Status.PRECONDITION_FAILED).entity(new JsonError(Response.Status.PRECONDITION_FAILED.getStatusCode(), English.FEEDBACK_CAUSED_INVALID_SLEEP_SCORE)).build());
}

for (final SleepMetrics sleepMetrics : timeline.metrics) {
if (!sleepMetrics.name.equals(SleepMetrics.TOTAL_SLEEP_NAME)) {
continue;
}
LOGGER.info("rejected feedback from account_id {} for evening of {} because it resulted in timeline with invalid sleep duration ",accountId,timeline.dateNight);
if (!sleepMetrics.value.isPresent() || (sleepMetrics.value.isPresent() && sleepMetrics.value.get() <= SleepMetrics.MINIMUM_TOTAL_SLEEP_DURATION_MINUTES)) {
throw new WebApplicationException(Response.status(Response.Status.PRECONDITION_FAILED).entity(new JsonError(Response.Status.PRECONDITION_FAILED.getStatusCode(), English.FEEDBACK_CAUSED_INVALID_SLEEP_SCORE)).build());
}
}
}

private Timeline getTimelineForNightInternal(final long accountId, final String night, final Optional<TimelineFeedback> newFeedback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@

public class SleepMetrics {

public static final String TOTAL_SLEEP_NAME = "total_sleep";
public static final String SOUND_SLEEP_NAME = "sound_sleep";
public static final String TIME_TO_SLEEP_NAME = "time_to_sleep";
public static final String TIMES_AWAKE_NAME = "times_awake";
public static final String FELL_ASLEEP_NAME = "fell_asleep";
public static final String WOKE_UP_NAME = "woke_up";
public static final Integer MINIMUM_TOTAL_SLEEP_DURATION_MINUTES = 120;

@JsonProperty("name")
public final String name;

Expand Down Expand Up @@ -50,18 +58,18 @@ public static List<SleepMetrics> fromV1(final com.hello.suripu.core.models.Timel
if (maybeStatistics.isPresent() && !maybeStatistics.get().isFromNull()) {
SleepStats statistics = maybeStatistics.get();

metrics.add(create("total_sleep", Optional.of(statistics.sleepDurationInMinutes.longValue()),
metrics.add(create(TOTAL_SLEEP_NAME, Optional.of(statistics.sleepDurationInMinutes.longValue()),
Unit.MINUTES, CurrentRoomState.State.Condition.IDEAL));
metrics.add(create("sound_sleep", Optional.of(statistics.soundSleepDurationInMinutes.longValue()),
metrics.add(create(SOUND_SLEEP_NAME, Optional.of(statistics.soundSleepDurationInMinutes.longValue()),
Unit.MINUTES, CurrentRoomState.State.Condition.IDEAL));
metrics.add(create("time_to_sleep", Optional.of(statistics.sleepOnsetTimeMinutes.longValue()),
metrics.add(create(TIME_TO_SLEEP_NAME, Optional.of(statistics.sleepOnsetTimeMinutes.longValue()),
Unit.MINUTES, CurrentRoomState.State.Condition.IDEAL));
metrics.add(create("times_awake", Optional.of(statistics.numberOfMotionEvents.longValue()),
metrics.add(create(TIMES_AWAKE_NAME, Optional.of(statistics.numberOfMotionEvents.longValue()),
Unit.QUANTITY, CurrentRoomState.State.Condition.IDEAL));

metrics.add(create("fell_asleep", Optional.of(statistics.sleepTime),
metrics.add(create(FELL_ASLEEP_NAME, Optional.of(statistics.sleepTime),
Unit.TIMESTAMP, CurrentRoomState.State.Condition.IDEAL));
metrics.add(create("woke_up", Optional.of(statistics.wakeTime),
metrics.add(create(WOKE_UP_NAME, Optional.of(statistics.wakeTime),
Unit.TIMESTAMP, CurrentRoomState.State.Condition.IDEAL));
}

Expand Down