Java Code Examples for org.rrd4j.core.FetchData#getValues()

The following examples show how to use org.rrd4j.core.FetchData#getValues() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RRDEndpoint.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
private Collection<Point> mapPoints(final boolean skipNan, final FetchData data, final String ds) {
    final double[] values = data.getValues(ds);
    final long[] timestamps = data.getTimestamps();
    return IntStream
            .range(0, timestamps.length)
            .filter(idx -> !skipNan || !Double.isNaN(values[idx]))
            .mapToObj(idx -> new Point(timestamps[idx], Double.isNaN(values[idx]) ? 0 : values[idx]))
            .collect(toList());
}
 
Example 2
Source File: MetricsHistoryHandler.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private NamedList<Object> getDbData(RrdDb db, String[] dsNames, Format format, SolrParams params) throws IOException {
  NamedList<Object> res = new SimpleOrderedMap<>();
  if (dsNames == null || dsNames.length == 0) {
    dsNames = db.getDsNames();
  }
  StringBuilder str = new StringBuilder();
  RrdDef def = db.getRrdDef();
  ArcDef[] arcDefs = def.getArcDefs();
  for (ArcDef arcDef : arcDefs) {
    SimpleOrderedMap<Object> map = new SimpleOrderedMap<>();
    res.add(arcDef.dump(), map);
    Archive a = db.getArchive(arcDef.getConsolFun(), arcDef.getSteps());
    // startTime / endTime, arcStep are in seconds
    FetchRequest fr = db.createFetchRequest(arcDef.getConsolFun(),
        a.getStartTime() - a.getArcStep(),
        a.getEndTime() + a.getArcStep());
    FetchData fd = fr.fetchData();
    if (format != Format.GRAPH) {
      // add timestamps separately from values
      long[] timestamps = fd.getTimestamps();
      if (format == Format.LIST) {
        // Arrays.asList works only on arrays of Objects
        map.add("timestamps", Arrays.stream(timestamps).boxed().collect(Collectors.toList()));
      } else {
        str.setLength(0);
        for (int i = 0; i < timestamps.length; i++) {
          if (i > 0) {
            str.append('\n');
          }
          str.append(String.valueOf(timestamps[i]));
        }
        map.add("timestamps", str.toString());
      }
    }
    SimpleOrderedMap<Object> values = new SimpleOrderedMap<>();
    map.add("values", values);
    for (String name : dsNames) {
      double[] vals = fd.getValues(name);
      switch (format) {
        case GRAPH:
          RrdGraphDef graphDef = new RrdGraphDef();
          graphDef.setTitle(name);
          graphDef.datasource(name, fd);
          graphDef.setStartTime(a.getStartTime() - a.getArcStep());
          graphDef.setEndTime(a.getEndTime() + a.getArcStep());
          graphDef.setPoolUsed(false);
          graphDef.setAltAutoscale(true);
          graphDef.setAltYGrid(true);
          graphDef.setAltYMrtg(true);
          graphDef.setSignature("Apache Solr " + versionString);
          graphDef.setNoLegend(true);
          graphDef.setAntiAliasing(true);
          graphDef.setTextAntiAliasing(true);
          graphDef.setWidth(500);
          graphDef.setHeight(175);
          graphDef.setTimeZone(TimeZone.getDefault());
          graphDef.setLocale(Locale.ROOT);
          // redraw immediately
          graphDef.setLazy(false);
          // area with a border
          graphDef.area(name, new Color(0xffb860), null);
          graphDef.line(name, Color.RED, null, 1.0f);
          RrdGraph graph = new RrdGraph(graphDef);
          BufferedImage bi = new BufferedImage(
              graph.getRrdGraphInfo().getWidth(),
              graph.getRrdGraphInfo().getHeight(),
              BufferedImage.TYPE_INT_RGB);
          graph.render(bi.getGraphics());
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          ImageIO.write(bi, "png", baos);
          values.add(name, Base64.byteArrayToBase64(baos.toByteArray()));
          break;
        case STRING:
          str.setLength(0);
          for (int i = 0; i < vals.length; i++) {
            if (i > 0) {
              str.append('\n');
            }
            str.append(String.valueOf(vals[i]));
          }
          values.add(name, str.toString());
          break;
        case LIST:
          values.add(name, Arrays.stream(vals).boxed().collect(Collectors.toList()));
          break;
      }
    }
  }
  return res;
}
 
Example 3
Source File: MBeanInfoViewer.java    From scheduling with GNU Affero General Public License v3.0 4 votes vote down vote up
public static String rrdContent(byte[] rrd4j, String newRange, String[] dataSources, String function)
        throws IOException {

    File rrd4jDb = File.createTempFile("database", "rr4dj");
    // construct the JSON response directly in a String
    StringBuilder result = new StringBuilder();
    try {
        try (OutputStream out = new FileOutputStream(rrd4jDb)) {
            out.write(rrd4j);
        }

        // create RRD4J DB, should be identical to the one held by the RM
        RrdDb db = new RrdDb(rrd4jDb.getAbsolutePath(), true);

        long timeEnd = db.getLastUpdateTime();
        // force float separator for JSON parsing
        DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.US);
        otherSymbols.setDecimalSeparator('.');
        // formatting will greatly reduce response size
        DecimalFormat formatter = new DecimalFormat("###.###", otherSymbols);

        result.append("{");

        for (int i = 0; i < dataSources.length; i++) {
            String dataSource = dataSources[i];
            char zone = newRange.charAt(i);
            long timeStart = timeEnd - secondsInZone(zone);

            FetchRequest req = db.createFetchRequest(function != null ? ConsolFun.valueOf(function)
                                                                      : ConsolFun.AVERAGE,
                                                     timeStart,
                                                     timeEnd);
            req.setFilter(dataSource);
            FetchData fetchData = req.fetchData();
            result.append("\"").append(dataSource).append("\":[");

            double[] values = fetchData.getValues(dataSource);

            int nValuesToTake = values.length;
            // if the last value is NaN then we decide to not send it to the client
            // why? Because when we retrieve from RDD file, somehow the latest is
            // always NaN, which is not what we want.
            if (Double.compare(values[values.length - 1], Double.NaN) == 0) {
                --nValuesToTake;
            }

            String collect = Arrays.stream(values)
                                   .boxed()
                                   .collect(Collectors.toList())
                                   .subList(0, nValuesToTake)
                                   .stream()
                                   .map(value -> {
                                       if (Double.compare(Double.NaN, value) == 0) {
                                           return "null";
                                       } else {
                                           return formatter.format(value);
                                       }
                                   })
                                   .collect(Collectors.joining(","));
            result.append(collect);
            result.append(']');
            if (i < dataSources.length - 1) {
                result.append(',');
            }
        }
        result.append("}");

        db.close();
    } finally {
        FileUtils.deleteQuietly(rrd4jDb);
    }

    return result.toString();
}
 
Example 4
Source File: SigarProcesses.java    From scheduling with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public String getAttributesHistory(String objectName, String[] attrs, String range) throws IOException {

    RrdDb db = new RrdDb(statBaseName, true);

    long timeEnd = db.getLastUpdateTime();
    // force float separator for JSON parsing
    DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.US);
    otherSymbols.setDecimalSeparator('.');
    // formatting will greatly reduce response size
    DecimalFormat formatter = new DecimalFormat("###.###", otherSymbols);

    // construct the JSON response directly in a String
    StringBuilder result = new StringBuilder();
    result.append("{");

    for (int i = 0; i < attrs.length; i++) {

        String dataSource = RRDSigarDataStore.toDataStoreName(attrs[i] + "-" + objectName);

        char zone = range.charAt(0);
        long timeStart = timeEnd - MBeanInfoViewer.secondsInZone(zone);

        FetchRequest req = db.createFetchRequest(ConsolFun.AVERAGE, timeStart, timeEnd);
        req.setFilter(dataSource);
        FetchData fetchData = req.fetchData();
        result.append("\"").append(dataSource).append("\":[");

        double[] values = fetchData.getValues(dataSource);
        for (int j = 0; j < values.length - 1; j++) {
            if (Double.compare(Double.NaN, values[j]) == 0) {
                result.append("null");
            } else {
                result.append(formatter.format(values[j]));
            }
            if (j < values.length - 2)
                result.append(',');
        }
        result.append(']');
        if (i < attrs.length - 1)
            result.append(',');
    }
    result.append("}");

    db.close();

    return result.toString();
}