use of org.rrd4j.core.FetchData in project scheduling by ow2-proactive.
the class RMRest method getStatHistory.
/**
* Return the statistic history contained in the RM's RRD database,
* without redundancy, in a friendly JSON format.
*
* The following sources will be queried from the RRD DB :<pre>
* { "BusyNodesCount",
* "FreeNodesCount",
* "DownNodesCount",
* "AvailableNodesCount",
* "AverageActivity" }</pre>
*
* @param sessionId a valid session
* @param range a String of 5 chars, one for each stat history source, indicating the time range to fetch
* for each source. Each char can be:<ul>
* <li>'a' 1 minute
* <li>'m' 10 minutes
* <li>'h' 1 hour
* <li>'H' 8 hours
* <li>'d' 1 day
* <li>'w' 1 week
* <li>'M' 1 month
* <li>'y' 1 year</ul>
* @return a JSON object containing a key for each source
* @throws InstanceNotFoundException
* @throws IntrospectionException
* @throws ReflectionException
* @throws IOException
* @throws MalformedObjectNameException
* @throws NullPointerException
* @throws InterruptedException
* @throws NotConnectedException
*/
@Override
@GET
@GZIP
@Path("stathistory")
@Produces("application/json")
public String getStatHistory(@HeaderParam("sessionid") String sessionId, @QueryParam("range") String range) throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException, MalformedObjectNameException, NullPointerException, InterruptedException, NotConnectedException {
RMProxyUserInterface rm = checkAccess(sessionId);
// to make it recognizable by StatHistoryCaching
if (range.length() > dataSources.length) {
range = range.substring(0, dataSources.length);
}
// complete range if too short
while (range.length() < dataSources.length) {
range += 'a';
}
StatHistoryCacheEntry cache = StatHistoryCaching.getInstance().getEntry(range);
// found unexpired cache entry matching the parameters: return it immediately
if (cache != null) {
return cache.getValue();
}
long l1 = System.currentTimeMillis();
ObjectName on = new ObjectName(RMJMXBeans.RUNTIMEDATA_MBEAN_NAME);
AttributeList attrs = rm.getMBeanAttributes(on, new String[] { "StatisticHistory" });
Attribute attr = (Attribute) attrs.get(0);
// content of the RRD4J database backing file
byte[] rrd4j = (byte[]) attr.getValue();
File rrd4jDb = File.createTempFile("database", "rr4dj");
rrd4jDb.deleteOnExit();
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);
// construct the JSON response directly in a String
StringBuilder result = new StringBuilder();
result.append("{");
for (int i = 0; i < dataSources.length; i++) {
String dataSource = dataSources[i];
char zone = range.charAt(i);
long timeStart;
switch(zone) {
default:
case // 1 minute
'a':
timeStart = timeEnd - 60;
break;
case // 10 minute
'm':
timeStart = timeEnd - 60 * 10;
break;
case // 1 hours
'h':
timeStart = timeEnd - 60 * 60;
break;
case // 8 hours
'H':
timeStart = timeEnd - 60 * 60 * 8;
break;
case // 1 day
'd':
timeStart = timeEnd - 60 * 60 * 24;
break;
case // 1 week
'w':
timeStart = timeEnd - 60 * 60 * 24 * 7;
break;
case // 1 month
'M':
timeStart = timeEnd - 60 * 60 * 24 * 28;
break;
case // 1 year
'y':
timeStart = timeEnd - 60 * 60 * 24 * 365;
break;
}
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; j++) {
if (Double.compare(Double.NaN, values[j]) == 0) {
result.append("null");
} else {
result.append(formatter.format(values[j]));
}
if (j < values.length - 1)
result.append(',');
}
result.append(']');
if (i < dataSources.length - 1)
result.append(',');
}
result.append("}");
db.close();
rrd4jDb.delete();
String ret = result.toString();
StatHistoryCaching.getInstance().addEntry(range, l1, ret);
return ret;
}
use of org.rrd4j.core.FetchData in project scheduling by ow2-proactive.
the class SigarProcesses method getAttributesHistory.
@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;
switch(zone) {
default:
case // 1 minute
'a':
timeStart = timeEnd - 60;
break;
case // 10 minute
'm':
timeStart = timeEnd - 60 * 10;
break;
case // 1 hours
'h':
timeStart = timeEnd - 60 * 60;
break;
case // 8 hours
'H':
timeStart = timeEnd - 60 * 60 * 8;
break;
case // 1 day
'd':
timeStart = timeEnd - 60 * 60 * 24;
break;
case // 1 week
'w':
timeStart = timeEnd - 60 * 60 * 24 * 7;
break;
case // 1 month
'M':
timeStart = timeEnd - 60 * 60 * 24 * 28;
break;
case // 1 year
'y':
timeStart = timeEnd - 60 * 60 * 24 * 365;
break;
}
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();
}
Aggregations