private void onCounterData(MetricData metricData) {
   Observable<Metric<Long>> metrics =
       Observable.from(metricData.getData())
           .map(
               singleMetric -> {
                 MetricId<Long> id =
                     new MetricId<>(metricData.getTenantId(), COUNTER, singleMetric.getSource());
                 long value = (long) singleMetric.getValue();
                 DataPoint<Long> dataPoint = new DataPoint<>(singleMetric.getTimestamp(), value);
                 return new Metric<>(id, Collections.singletonList(dataPoint));
               });
   metricsService.addDataPoints(COUNTER, metrics).subscribe(new NewDataSubscriber());
 }
 private void onGaugeData(MetricData metricData) {
   Observable<Metric<Double>> metrics =
       Observable.from(metricData.getData())
           .map(
               singleMetric -> {
                 MetricId<Double> id =
                     new MetricId<>(metricData.getTenantId(), GAUGE, singleMetric.getSource());
                 DataPoint<Double> dataPoint =
                     new DataPoint<>(singleMetric.getTimestamp(), singleMetric.getValue());
                 return new Metric<>(id, Collections.singletonList(dataPoint));
               });
   metricsService.addDataPoints(GAUGE, metrics).subscribe(new NewDataSubscriber());
 }
  protected void findRateDataPointsForMetrics(
      AsyncResponse asyncResponse, QueryRequest query, MetricType<? extends Number> type) {
    TimeRange timeRange = new TimeRange(query.getStart(), query.getEnd());
    if (!timeRange.isValid()) {
      asyncResponse.resume(badRequest(new ApiError(timeRange.getProblem())));
      return;
    }

    int limit;
    if (query.getLimit() == null) {
      limit = 0;
    } else {
      limit = query.getLimit();
    }
    Order order;
    if (query.getOrder() == null) {
      order = Order.defaultValue(limit, timeRange.getStart(), timeRange.getEnd());
    } else {
      order = Order.fromText(query.getOrder());
    }

    if (query.getIds().isEmpty()) {
      asyncResponse.resume(badRequest(new ApiError("Metric ids must be specified")));
      return;
    }

    List<MetricId<? extends Number>> metricIds =
        query.getIds().stream().map(id -> new MetricId<>(getTenant(), type, id)).collect(toList());
    Observable<NamedDataPoint<Double>> dataPoints =
        metricsService
            .findRateData(metricIds, timeRange.getStart(), timeRange.getEnd(), limit, order)
            .observeOn(Schedulers.io());

    HttpServletRequest request = ResteasyProviderFactory.getContextData(HttpServletRequest.class);
    HttpServletResponse response =
        ResteasyProviderFactory.getContextData(HttpServletResponse.class);

    if (type == GAUGE) {
      dataPoints.subscribe(new NamedDataPointObserver<>(request, response, mapper, GAUGE));
    } else if (type == COUNTER) {
      dataPoints.subscribe(new NamedDataPointObserver<>(request, response, mapper, COUNTER_RATE));
    } else {
      throw new IllegalArgumentException(
          type + " is not a supported metric type for rate data points");
    }
  }
 private void onAvailData(AvailData availData) {
   Observable<Metric<AvailabilityType>> metrics =
       Observable.from(availData.getData())
           .map(
               singleMetric -> {
                 MetricId<AvailabilityType> id;
                 id =
                     new MetricId<>(
                         singleMetric.getTenantId(), AVAILABILITY, singleMetric.getId());
                 AvailabilityType availabilityType =
                     AvailabilityType.fromString(singleMetric.getAvail());
                 DataPoint<AvailabilityType> dataPoint =
                     new DataPoint<>(singleMetric.getTimestamp(), availabilityType);
                 return new Metric<>(id, Collections.singletonList(dataPoint));
               });
   metricsService.addDataPoints(AVAILABILITY, metrics).subscribe(new NewDataSubscriber());
 }
  protected <T> void findRawDataPointsForMetrics(
      AsyncResponse asyncResponse, QueryRequest query, MetricType<T> type) {
    TimeRange timeRange = new TimeRange(query.getStart(), query.getEnd());
    if (!timeRange.isValid()) {
      asyncResponse.resume(badRequest(new ApiError(timeRange.getProblem())));
      return;
    }

    int limit;
    if (query.getLimit() == null) {
      limit = 0;
    } else {
      limit = query.getLimit();
    }
    Order order;
    if (query.getOrder() == null) {
      order = Order.defaultValue(limit, timeRange.getStart(), timeRange.getEnd());
    } else {
      order = Order.fromText(query.getOrder());
    }

    if (query.getIds().isEmpty()) {
      asyncResponse.resume(badRequest(new ApiError("Metric ids must be specified")));
      return;
    }

    List<MetricId<T>> metricIds =
        query.getIds().stream().map(id -> new MetricId<>(getTenant(), type, id)).collect(toList());
    Observable<NamedDataPoint<T>> dataPoints =
        metricsService
            .findDataPoints(metricIds, timeRange.getStart(), timeRange.getEnd(), limit, order)
            .observeOn(Schedulers.io());

    HttpServletRequest request = ResteasyProviderFactory.getContextData(HttpServletRequest.class);
    HttpServletResponse response =
        ResteasyProviderFactory.getContextData(HttpServletResponse.class);

    dataPoints.subscribe(new NamedDataPointObserver<>(request, response, mapper, type));
  }