Exemplo n.º 1
0
 public static Double normalizeUnitsAndValues(
     MeasurementNumericValueAndUnits valueAndUnits, MeasurementUnits targetUnits) {
   if (valueAndUnits.getUnits() == MeasurementUnits.PERCENTAGE) {
     return valueAndUnits.getValue() * 100;
   }
   Double multiplier = MeasurementUnits.calculateOffset(valueAndUnits.getUnits(), targetUnits);
   return valueAndUnits.getValue() * multiplier;
 }
Exemplo n.º 2
0
  @Override
  /**
   * Format the json for the front JSNI(javascript) UI to consume.
   *
   * @todo: future: this should really use GSON or some Json marshaller
   */
  public String getJsonMetrics() {
    StringBuilder sb = new StringBuilder();
    if (null != metricData) {
      sb = new StringBuilder("[");
      long firstBarTime = metricData.get(0).getTimestamp();
      long secondBarTime = metricData.get(1).getTimestamp();
      long barDuration = secondBarTime - firstBarTime;
      String barDurationString =
          MeasurementConverterClient.format(
              (double) barDuration, MeasurementUnits.MILLISECONDS, true);

      calculateOOB();

      // find the lowest value and use it's UOM to translated everything else into
      MeasurementDataNumericHighLowComposite lowestValue = null;
      for (MeasurementDataNumericHighLowComposite measurement : metricData) {
        if (!Double.isNaN(measurement.getValue())) {
          if (null == lowestValue) {
            lowestValue = measurement;
          }
          if (measurement.getLowValue() < lowestValue.getLowValue()) {
            lowestValue = measurement;
          }
        }
      }
      MeasurementNumericValueAndUnits adjustedMeasurementUnitsAndValue =
          MeasurementConverterClient.fit(lowestValue.getLowValue(), definition.getUnits());
      adjustedMeasurementUnits = adjustedMeasurementUnitsAndValue.getUnits();

      for (MeasurementDataNumericHighLowComposite measurement : metricData) {
        sb.append("{ \"x\":" + measurement.getTimestamp() + ",");

        if (null != lastOOB) {
          sb.append(" \"baselineMin\":" + lastOOB.getBlMin() + ", ");
          sb.append(" \"baselineMax\":" + lastOOB.getBlMax() + ", ");
        }

        if (!Double.isNaN(measurement.getValue())) {

          Double newHigh =
              normalizeUnitsAndValues(
                  new MeasurementNumericValueAndUnits(
                      measurement.getHighValue(), definition.getUnits()),
                  adjustedMeasurementUnits);
          Double newAvg =
              normalizeUnitsAndValues(
                  new MeasurementNumericValueAndUnits(
                      measurement.getValue(), definition.getUnits()),
                  adjustedMeasurementUnits);
          Double newLow =
              normalizeUnitsAndValues(
                  new MeasurementNumericValueAndUnits(
                      measurement.getLowValue(), definition.getUnits()),
                  adjustedMeasurementUnits);

          sb.append(" \"barDuration\": \"" + barDurationString + "\", ");
          sb.append(" \"high\":" + newHigh + ",");
          sb.append(
              " \"highFormatted\":\""
                  + MeasurementConverterClient.format(
                      measurement.getHighValue(), definition.getUnits(), true, 0, 3)
                  + "\",");
          sb.append(" \"low\":" + cleanseLow(newLow, newAvg, newHigh) + ",");
          sb.append(
              " \"lowFormatted\":\""
                  + MeasurementConverterClient.format(
                      cleanseLow(
                          measurement.getLowValue(),
                          measurement.getValue(),
                          measurement.getHighValue()),
                      definition.getUnits(),
                      true,
                      0,
                      3)
                  + "\",");
          sb.append(" \"avg\":" + newAvg + ",");
          sb.append(
              " \"avgFormatted\":\""
                  + MeasurementConverterClient.format(
                      measurement.getValue(), definition.getUnits(), true, 0, 3)
                  + "\"},");
        } else {
          // give it some values so that we dont have NaN
          sb.append(" \"high\":0,");
          sb.append(" \"low\":0,");
          sb.append(" \"avg\":0,");
          sb.append(" \"nodata\":true },");
        }
        if (!sb.toString().endsWith("},")) {
          sb.append(" },");
        }
      }
      sb.setLength(sb.length() - 1); // delete the last ','
      sb.append("]");
    }
    Log.debug("Json data for: " + getChartTitle());
    Log.debug(sb.toString());
    return sb.toString();
  }