@Override public String createCsvData(String rrdFilename, long startTime, long endTime) throws IOException, MetricsGraphException { LOGGER.trace("ENTERING: createCsvData"); MetricData metricData = getMetricData(rrdFilename, startTime, endTime); StringBuffer csv = new StringBuffer(""); csv.append("Timestamp,Value\n"); List<Long> timestamps = metricData.getTimestamps(); List<Double> values = metricData.getValues(); for (int i = 0; i < timestamps.size(); i++) { String timestamp = getCalendarTime(timestamps.get(i)); csv.append(timestamp + "," + new Double(values.get(i)).longValue() + "\n"); } LOGGER.debug("csv = " + csv.toString()); LOGGER.trace("EXITING: createCsvData"); return csv.toString(); }
@Override public String createJsonData(String metricName, String rrdFilename, long startTime, long endTime) throws IOException, MetricsGraphException { LOGGER.trace("ENTERING: createJsonData"); JSONObject obj = new JSONObject(); String displayableMetricName = convertCamelCase(metricName); MetricData metricData = getMetricData(rrdFilename, startTime, endTime); String title = displayableMetricName + " for " + getCalendarTime(startTime) + " to " + getCalendarTime(endTime); obj.put("title", title); List<Long> timestamps = metricData.getTimestamps(); List<Double> values = metricData.getValues(); JSONArray samples = new JSONArray(); for (int i = 0; i < timestamps.size(); i++) { String timestamp = getCalendarTime(timestamps.get(i)); JSONObject sample = new JSONObject(); sample.put("timestamp", timestamp); // convert value to a long to prevent fractional values sample.put("value", new Double(values.get(i)).longValue()); samples.add(sample); } obj.put("data", samples); if (metricData.hasTotalCount()) { obj.put("totalCount", metricData.getTotalCount()); } JsonWriter writer = new JsonWriter(); obj.writeJSONString(writer); String jsonText = writer.toString(); LOGGER.debug("jsonText = " + jsonText); LOGGER.trace("EXITING: createJsonData"); return jsonText; }
/** * Creates an Excel worksheet containing the metric's data (timestamps and values) for the * specified time range. This worksheet is titled with the trhe metric's name and added to the * specified Workbook. * * @param wb the workbook to add this worksheet to * @param metricName the name of the metric whose data is being rendered in this worksheet * @param rrdFilename the name of the RRD file to retrieve the metric's data from * @param startTime start time, in seconds since Unix epoch, to fetch metric's data * @param endTime end time, in seconds since Unix epoch, to fetch metric's data * @throws IOException * @throws MetricsGraphException */ private void createSheet( Workbook wb, String metricName, String rrdFilename, long startTime, long endTime) throws IOException, MetricsGraphException { LOGGER.trace("ENTERING: createSheet"); MetricData metricData = getMetricData(rrdFilename, startTime, endTime); String displayableMetricName = convertCamelCase(metricName); String title = displayableMetricName + " for " + getCalendarTime(startTime) + " to " + getCalendarTime(endTime); Sheet sheet = wb.createSheet(displayableMetricName); sheet.autoSizeColumn(0); sheet.autoSizeColumn(1); Font headerFont = wb.createFont(); headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD); CellStyle columnHeadingsStyle = wb.createCellStyle(); columnHeadingsStyle.setFont(headerFont); CellStyle bannerStyle = wb.createCellStyle(); bannerStyle.setFont(headerFont); bannerStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index); bannerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); int rowCount = 0; Row row = sheet.createRow((short) rowCount); Cell cell = row.createCell(0); cell.setCellValue(title); cell.setCellStyle(bannerStyle); rowCount++; // Blank row for spacing/readability row = sheet.createRow((short) rowCount); cell = row.createCell(0); cell.setCellValue(""); rowCount++; row = sheet.createRow((short) rowCount); cell = row.createCell(0); cell.setCellValue("Timestamp"); cell.setCellStyle(columnHeadingsStyle); cell = row.createCell(1); cell.setCellValue("Value"); cell.setCellStyle(columnHeadingsStyle); rowCount++; List<Long> timestamps = metricData.getTimestamps(); List<Double> values = metricData.getValues(); for (int i = 0; i < timestamps.size(); i++) { String timestamp = getCalendarTime(timestamps.get(i)); row = sheet.createRow((short) rowCount); row.createCell(0).setCellValue(timestamp); // convert value to a long to prevent fractional values row.createCell(1).setCellValue(new Double(values.get(i)).longValue()); rowCount++; } if (metricData.hasTotalCount()) { // Blank row for spacing/readability row = sheet.createRow((short) rowCount); cell = row.createCell(0); cell.setCellValue(""); rowCount++; row = sheet.createRow((short) rowCount); cell = row.createCell(0); cell.setCellValue("Total Count: "); cell.setCellStyle(columnHeadingsStyle); row.createCell(1).setCellValue(metricData.getTotalCount()); } LOGGER.trace("EXITING: createSheet"); }
@Override public String createXmlData(String metricName, String rrdFilename, long startTime, long endTime) throws IOException, MetricsGraphException { LOGGER.trace("ENTERING: createXmlData"); MetricData metricData = getMetricData(rrdFilename, startTime, endTime); StringWriter sw = new StringWriter(); String displayableMetricName = convertCamelCase(metricName); String title = displayableMetricName + " for " + getCalendarTime(startTime) + " to " + getCalendarTime(endTime); try { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // root elements Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement(metricName); doc.appendChild(rootElement); Element titleElement = doc.createElement("title"); titleElement.appendChild(doc.createTextNode(title)); rootElement.appendChild(titleElement); Element dataElement = doc.createElement("data"); rootElement.appendChild(dataElement); List<Long> timestamps = metricData.getTimestamps(); List<Double> values = metricData.getValues(); for (int i = 0; i < timestamps.size(); i++) { Element sampleElement = doc.createElement("sample"); dataElement.appendChild(sampleElement); String timestamp = getCalendarTime(timestamps.get(i)); Element timestampElement = doc.createElement("timestamp"); timestampElement.appendChild(doc.createTextNode(timestamp)); sampleElement.appendChild(timestampElement); Element valueElement = doc.createElement("value"); long value = new Double(values.get(i)).longValue(); valueElement.appendChild(doc.createTextNode(String.valueOf(value))); sampleElement.appendChild(valueElement); } if (metricData.hasTotalCount()) { Element totalCountElement = doc.createElement("totalCount"); totalCountElement.appendChild( doc.createTextNode(Long.toString(metricData.getTotalCount()))); dataElement.appendChild(totalCountElement); } // write the content into xml stringwriter TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(sw); transformer.transform(source, result); } catch (ParserConfigurationException pce) { pce.printStackTrace(); } catch (TransformerException tfe) { tfe.printStackTrace(); } LOGGER.debug("xml = " + sw.toString()); LOGGER.trace("EXITING: createXmlData"); return sw.toString(); }