org.rrd4j.core.FetchData Java Examples

The following examples show how to use org.rrd4j.core.FetchData. 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: RRDStorage.java    From component-runtime with Apache License 2.0 6 votes vote down vote up
public CompletionStage<FetchData> fetch(final long start, final long end) {
    return this.db.thenApply(db -> {
        try {
            final long correctedStart = start <= 0 ? db.getArchive(0).getStartTime() : start;
            final long correctedEnd = end <= 0 ? db.getArchive(0).getEndTime() : end;
            if (correctedStart >= correctedEnd) {
                throw new IllegalStateException("end can't be before start");
            }
            return db
                    .createFetchRequest(TOTAL, correctedStart, correctedEnd,
                            getFetchStep(correctedStart, correctedEnd))
                    .fetchData();
        } catch (final IOException e) {
            throw new IllegalStateException(e);
        }
    });
}
 
Example #2
Source File: RrdAccessor.java    From rheem with Apache License 2.0 6 votes vote down vote up
public double query(String dsName, long startTimestamp, long endTimestamp, ConsolFun consolidationFunction) {
    long tStart = convertToUnixTimestamp(startTimestamp);
    long tEnd = convertToUnixTimestamp(endTimestamp);
    if (tStart == tEnd) {
        this.logger.warn("Shifting end time by 1 second because it is identical with the start time.");
        tEnd++;
    }
    try {
        final FetchRequest request = this.rrdDb.createFetchRequest(consolidationFunction, tStart, tEnd);
        final FetchData fetchData = request.fetchData();
        return fetchData.getAggregate(dsName, consolidationFunction);
    } catch (IOException e) {
        e.printStackTrace();
        return Double.NaN;
    }
}
 
Example #3
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 #4
Source File: SolrRrdBackendFactoryTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private String dumpData(RrdDb db, FetchData fd) throws Exception {
  Map<String, Object> map = new LinkedHashMap<>();
  map.put("dbLastUpdateTime", db.getLastUpdateTime());
  map.put("firstTimestamp", fd.getFirstTimestamp());
  map.put("lastTimestamp", fd.getLastTimestamp());
  map.put("timestamps", fd.getTimestamps());
  map.put("data", fd.dump());
  return Utils.toJSONString(map);
}
 
Example #5
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 #6
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 #7
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();
}