Ejemplo n.º 1
0
  @Test
  public void testTimestampQueryingAndOrder() throws IOException {
    ServiceLocator locator = container;
    SearchByTimestampAndOrderByTimestampRepository analysisGridRepository =
        locator.resolve(SearchByTimestampAndOrderByTimestampRepository.class);

    String marker = UUID.randomUUID().toString();
    OffsetDateTime dt1 = OffsetDateTime.now();
    OffsetDateTime dt2 = dt1.plusDays(1);
    OffsetDateTime dt3 = dt1.plusDays(2);
    OffsetDateTime dt4 = dt1.plusDays(3);

    SearchByTimestampAndOrderByTimestamp[] arr =
        new SearchByTimestampAndOrderByTimestamp[] {
          new SearchByTimestampAndOrderByTimestamp().setOndate(dt1).setMarker(marker),
          new SearchByTimestampAndOrderByTimestamp().setOndate(dt2).setMarker(marker),
          new SearchByTimestampAndOrderByTimestamp().setOndate(dt3).setMarker(marker),
          new SearchByTimestampAndOrderByTimestamp().setOndate(dt4).setMarker(marker)
        };
    analysisGridRepository.insert(arr);

    List<SearchByTimestampAndOrderByTimestamp> queryResults =
        analysisGridRepository
            .query(it -> it.getMarker().equals(marker) && it.getOndate().isAfter(dt2))
            .sortedDescendingBy(SearchByTimestampAndOrderByTimestamp::getOndate)
            .list();
    Assert.assertEquals(queryResults.size(), arr.length - 2);
    Assert.assertTrue(queryResults.get(0).getOndate().isEqual(dt4));
  }
Ejemplo n.º 2
0
 public void calculateNextScheduledRun(Long immediateFrequency) {
   if (lastScheduledRun == null) {
     lastScheduledRun = OffsetDateTime.now();
   }
   switch (getFrequency().getFrequency()) {
     case IMMEDIATE:
       setNextScheduledRun(lastScheduledRun.plusMinutes(immediateFrequency));
       break;
     case DAILY:
       setNextScheduledRun(lastScheduledRun.plusDays(1));
       break;
     case WEEKLY:
       setNextScheduledRun(lastScheduledRun.plusWeeks(1));
       break;
     case MONTHLY:
       setNextScheduledRun(lastScheduledRun.plusMonths(1));
       break;
     default:
       setNextScheduledRun(null);
   }
 }
Ejemplo n.º 3
0
  protected ResponseEntity<ShimDataResponse> getData(
      OAuth2RestOperations restTemplate, ShimDataRequest shimDataRequest) throws ShimException {

    final JawboneDataTypes jawboneDataType;
    try {
      jawboneDataType =
          JawboneDataTypes.valueOf(shimDataRequest.getDataTypeKey().trim().toUpperCase());
    } catch (NullPointerException | IllegalArgumentException e) {
      throw new ShimException(
          "Null or Invalid data type parameter: "
              + shimDataRequest.getDataTypeKey()
              + " in shimDataRequest, cannot retrieve data.");
    }

    // FIXME this needs to get changed or documented
    long numToReturn = 100;
    if (shimDataRequest.getNumToReturn() != null) {
      numToReturn = shimDataRequest.getNumToReturn();
    }

    OffsetDateTime today = OffsetDateTime.now();

    OffsetDateTime startDateTime =
        shimDataRequest.getStartDateTime() == null
            ? today.minusDays(1)
            : shimDataRequest.getStartDateTime();
    long startTimeInEpochSecond = startDateTime.toEpochSecond();

    OffsetDateTime endDateTime =
        shimDataRequest.getEndDateTime() == null
            ? today.plusDays(1)
            : shimDataRequest.getEndDateTime();
    long endTimeInEpochSecond = endDateTime.toEpochSecond();

    UriComponentsBuilder uriComponentsBuilder =
        UriComponentsBuilder.fromUriString(DATA_URL)
            .path(jawboneDataType.getEndPoint())
            .queryParam("start_time", startTimeInEpochSecond)
            .queryParam("end_time", endTimeInEpochSecond)
            .queryParam("limit", numToReturn);

    ResponseEntity<JsonNode> responseEntity;
    try {
      responseEntity =
          restTemplate.getForEntity(uriComponentsBuilder.build().encode().toUri(), JsonNode.class);
    } catch (HttpClientErrorException | HttpServerErrorException e) {
      // FIXME figure out how to handle this
      logger.error("A request for Jawbone data failed.", e);
      throw e;
    }

    if (shimDataRequest.getNormalize()) {

      JawboneDataPointMapper mapper;
      switch (jawboneDataType) {
        case WEIGHT:
          mapper = new JawboneBodyWeightDataPointMapper();
          break;
        case STEPS:
          mapper = new JawboneStepCountDataPointMapper();
          break;
        case BODY_MASS_INDEX:
          mapper = new JawboneBodyMassIndexDataPointMapper();
          break;
        case ACTIVITY:
          mapper = new JawbonePhysicalActivityDataPointMapper();
          break;
        case SLEEP:
          mapper = new JawboneSleepDurationDataPointMapper();
          break;
        case HEART_RATE:
          mapper = new JawboneHeartRateDataPointMapper();
          break;
        default:
          throw new UnsupportedOperationException();
      }

      return ResponseEntity.ok()
          .body(
              ShimDataResponse.result(
                  JawboneShim.SHIM_KEY,
                  mapper.asDataPoints(singletonList(responseEntity.getBody()))));

    } else {

      return ResponseEntity.ok()
          .body(ShimDataResponse.result(JawboneShim.SHIM_KEY, responseEntity.getBody()));
    }
  }