`
cleaneyes
  • 浏览: 335577 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

简单封装JRobin1.5.9

 
阅读更多

JRobin 1.5修正了中文字符定位的问题,对API做了不小的调整,一些方法被废弃掉了。解决一个生成图片的严重Bug, 1.4在针对某些RRD文件生成图片时,会出现以下异常

java.lang.ArrayIndexOutOfBoundsException: 414
	at org.jrobin.graph.ValueExtractor.extract(ValueExtractor.java:137)
	at org.jrobin.graph.RrdExporter.calculateSeries(RrdExporter.java:421)
	at org.jrobin.graph.Grapher.calculateSeries(Grapher.java:340)
	at org.jrobin.graph.Grapher.render(Grapher.java:315)
	at org.jrobin.graph.Grapher.createImage(Grapher.java:223)
	at org.jrobin.graph.RrdGraph.getBufferedImage(RrdGraph.java:468)
	at org.jrobin.graph.RrdGraph.saveAsPNG(RrdGraph.java:154)
	at org.jrobin.graph.RrdGraph.saveAsPNG(RrdGraph.java:137)
 
// datasource类型
	private static final String DS_TYPE = "GAUGE";

	// 默认从多个数据中取综合值的方式
	private static final ConsolFun DEFAULT_CONSOL_FUN = ConsolFun.MAX;

	// 默认曲线形状
	private static final ChartSeries DEFAULT_CHART_SERIES = ChartSeries.LINE;

	// 默认图片格式
	private static final GraphFormat DEFAULT_GRAPH_FORMAT = GraphFormat.PNG;

	// 默认顺序颜色
	private static final Color[] colors = new Color[] { Color.GREEN,
			Color.BLUE, Color.CYAN, Color.ORANGE, Color.PINK, Color.MAGENTA,
			Color.GRAY, Color.DARK_GRAY };
	

	/**
	 * 向RRD文件,插入数据
	 * 
	 * @param rrdPath
	 *            RRD文件路径
	 * @param dsNames dsName数组,类似于数据表的栏位概念
	 * @param values
	 * @param collectTimeUnitsSecond
	 *            数据所在时间,以秒为单位
	 * @throws RrdException
	 * @throws IOException
	 */
	public static void insertRrdData(String rrdPath, String[] dsNames,
			double[] values, long collectTimeUnitsSecond) throws RrdException,
			IOException {
		if (dsNames != null && dsNames.length > 0) {
			// 取得RRD数据库连接池
			RrdDbPool rrdPool = RrdDbPool.getInstance();

			// 取得rrd数据库文件
			RrdDb rrdDb = rrdPool.requestRrdDb(rrdPath);

			// 创建rrd记录
			Sample sample = rrdDb.createSample(collectTimeUnitsSecond);
			for (int i = 0; i < values.length; i++) {
				sample.setValue(dsNames[i], values[i]);
			}
			// update database
			sample.update();
			// release RRD database reference
			rrdPool.release(rrdDb);
		}
	}

	/**
	 * 重新创建RRD文件
	 * @param rrdPath RRD文件路径
	 * @param dsNames dsName数组,类似于数据表的栏位概念
	 * @param archiveModels 
	 * @param startTime 插入的数据不能早于这个时间,为0时,将默认使用当前时间
	 * @param rrdStep RRD文件接收数据的频率,即多少秒接收一次数据
	 * @throws RrdException
	 * @throws IOException
	 */
	public static void createRrdFile(String rrdPath, String[] dsNames,
			Collection<JRobinArchiveModel> archiveModels, long startTime,
			long rrdStep) throws RrdException, IOException {		
		RrdDef rrdDef = null;

		long step = rrdStep;

		// create RRD file since it does not exist
		if (step > 0) {
			if (startTime > 0) {
				rrdDef = new RrdDef(rrdPath, startTime - 1, step);
			} else {
				rrdDef = new RrdDef(rrdPath, startTime - 1);
			}
		} else {
			rrdDef = new RrdDef(rrdPath);
		}

		// (String dsName, String dsType, long heartbeat,double minValue,
		// double maxValue)
		for (int i = 0; i < dsNames.length; i++) {
			rrdDef.addDatasource(dsNames[i], DS_TYPE, 600, Double.NaN,
					Double.NaN);
		}

		for (Iterator<JRobinArchiveModel> iterator = archiveModels.iterator(); iterator
				.hasNext();) {
			JRobinArchiveModel robinArchiveModel = iterator.next();
			ConsolFun consolFun = robinArchiveModel.getConsolFun();
			if (consolFun == null) {
				consolFun = DEFAULT_CONSOL_FUN;
			}
			rrdDef.addArchive(consolFun.toString(), 0.5, robinArchiveModel
					.getSteps(), robinArchiveModel.getRows());
		}

		// create RRD file in the pool
		RrdDbPool rrdPool = RrdDbPool.getInstance();
		RrdDb rrdDb = rrdPool.requestRrdDb(rrdDef);
		
		rrdPool.release(rrdDb);
	}
	

	/**
	 * 返回生成图片的字节码,而不写入文件
	 * @param chartModels 一组曲线定义对象
	 * @param graphingParam 绘图的基本参数
	 * @return
	 * @throws RrdException
	 * @throws IOException
	 */
	public static byte[] graphBytes(Collection<JRobinChartModel> chartModels,
			JRobinGraphingParam graphingParam) throws RrdException, IOException {
		byte[] bytes = null;

		//文件名为"-",表示仅保存到内存,不写文件
		graphingParam.setGrapfilePath("-");
		RrdGraph rrdGraph = constructionRrdGraph(chartModels, graphingParam);
		
		// 取得图形的字节码
		//add by 1.5
		bytes = rrdGraph.getRrdGraphInfo().getBytes();		

		return bytes;
	}

	/**
	 * 返回生成图片输出到指定文件(文件位置由JRobinGraphingParam对象的grapfilePath指定)
	 * @param chartModels 一组曲线定义对象
	 * @param graphingParam 绘图的基本参数
	 * @throws RrdException
	 * @throws IOException
	 */
	public static void graphing(Collection<JRobinChartModel> chartModels,
			JRobinGraphingParam graphingParam)
			throws RrdException, IOException {

		constructionRrdGraph(chartModels, graphingParam);
	}

	/**
	 * 构建RrdGraph图形对象
	 * @param chartModels
	 * @param graphingParam
	 * @return
	 * @throws RrdException
	 * @throws IOException 
	 */
	private static RrdGraph constructionRrdGraph(
			Collection<JRobinChartModel> chartModels,
			JRobinGraphingParam graphingParam) throws RrdException, IOException {
		// 图形
		RrdGraph rrdGraph = null;
		String defaultRrdPath = graphingParam.getRrdPath();

		int legendColumns = graphingParam.getLegendColumns();
		if (legendColumns == 0) {
			legendColumns = 1;
		}

		// 图形定义
		RrdGraphDef graphDef = new RrdGraphDef();

		// 指定数据的时间跨度
		graphDef.setTimeSpan(graphingParam.getStartTime(), graphingParam
				.getEndTime());

		// 不显示JRobin的签名
		graphDef.setShowSignature(false);
		// 中文字体
		graphDef.setSmallFont(new Font("Monospaced", Font.PLAIN, 11));
		graphDef.setLargeFont(new Font("SansSerif", Font.BOLD, 14));		

		// 标题
		if (graphingParam.getTitle() != null) {
			graphDef.setTitle(graphingParam.getTitle());
		}

		// 指定Y轴取值范围
		if (graphingParam.isUseCustomGridYRangeFlag()) {
			graphDef.setMinValue(graphingParam.getGridYLower());
			graphDef.setMaxValue(graphingParam.getGridYUpper());
		}

		String comment = graphingParam.getComment();
		int i = 0;
		for (Iterator<JRobinChartModel> iterator = chartModels.iterator(); iterator
				.hasNext(); i++) {
			JRobinChartModel robinChartModel = iterator.next();

			// 合并方式
			ConsolFun consolFun = robinChartModel.getConsolFun();
			if (consolFun == null) {
				consolFun = DEFAULT_CONSOL_FUN;
			}

			// 曲线颜色
			Color color = robinChartModel.getColor();
			if (color == null) {
				color = colors[i % colors.length];
			}

			// 曲线说明
			String legend = robinChartModel.getLegend();
			// 曲线形状
			ChartSeries chartSeries = robinChartModel.getChartSeries();

			// 数据源名称
			String dsName = robinChartModel.getDsName();
			// 曲线标识
			String lineName = dsName + "_" + consolFun.toString();

			String rrdPath = robinChartModel.getRrdPath();
			//如果robinChartModel未指定rrdPath,则使用JRobinGraphingParam对象的rrdPath
			if (rrdPath == null || rrdPath.trim().length() == 0){
				rrdPath = defaultRrdPath;
			}
			// 加入该曲线的相关数据
			graphDef
					.datasource(lineName, rrdPath, dsName, consolFun.toString());

			// 根据每行显示几个legend,加入换行符
			if ((i + 1) % legendColumns == 0) {
				legend += "\\l";
			}
			// 绘制曲线
			graphingChar(graphDef, chartSeries, lineName, color, legend);
		}

		if (comment != null){
			//换行
			if (i%legendColumns >0){
				graphDef.comment("\\l");
			}
			// 注释
			graphDef.comment(comment);
		}		
			
		//指定图片格式
		GraphFormat graphFormat = graphingParam.getGraphFormat();
		if (graphFormat == null){
			graphFormat = GraphFormat.PNG;
		}
		graphDef.setImageFormat(graphFormat.toString());
		if (graphingParam.isUseCustomGraphSizeFlag()) {		
			graphDef.setWidth(graphingParam.getWidth());
			graphDef.setHeight(graphingParam.getHeight());
			graphDef.setImageQuality(graphingParam.getQuality());
		}
		//图片文件名
		graphDef.setFilename(graphingParam.getGrapfilePath());		
		
		// 图形
		//1.5版,构造的时候就创建图形
		rrdGraph = new RrdGraph(graphDef);		

		return rrdGraph;
	}

	/**
	 * 绘制一条曲线到graphDef
	 * 
	 * @param graphDef 定义图形的对象
	 * @param chartSeries 线型
	 * @param dsName 
	 * @param color
	 * @param legend
	 * @throws RrdException
	 */
	private static void graphingChar(RrdGraphDef graphDef,
			ChartSeries chartSeries, String dsName, Color color, String legend)
			throws RrdException {
		if (chartSeries == null) {
			chartSeries = DEFAULT_CHART_SERIES;
		}

		// 根据不同的图形形状类别,调用不同的方法
		if (chartSeries.equals(ChartSeries.LINE)) {
			graphDef.line(dsName, color, legend);
		} else if (chartSeries.equals(ChartSeries.AREA)) {
			graphDef.area(dsName, color, legend);
		} else if (chartSeries.equals(ChartSeries.STACK)) {
			graphDef.stack(dsName, color, legend);
		}
	}
 

 

 

 

 

分享到:
评论

相关推荐

    jrobin-1.5.9-API文档-中文版.zip

    赠送jar包:jrobin-1.5.9.jar; 赠送原API文档:jrobin-1.5.9-javadoc.jar; 赠送源代码:jrobin-1.5.9-sources.jar; 赠送Maven依赖信息文件:jrobin-1.5.9.pom; 包含翻译后的API文档:jrobin-1.5.9-javadoc-API...

    jrobin-1.5.9-API文档-中英对照版.zip

    赠送jar包:jrobin-1.5.9.jar; 赠送原API文档:jrobin-1.5.9-javadoc.jar; 赠送源代码:jrobin-1.5.9-sources.jar; 赠送Maven依赖信息文件:jrobin-1.5.9.pom; 包含翻译后的API文档:jrobin-1.5.9-javadoc-API...

    JavaMelody javamelody-core-1.52.0.jar jrobin-1.5.9.jar

    JavaMelody javamelody-core-1.52.0.jar jrobin-1.5.9.jar 集成所需的jar包

    jrobin-1.5.9.jar中文-英文对照文档.zip

    注:下文中的 *** 代表文件名中的组件名称。 # 包含: 中文-英文对照文档:【***-javadoc-API文档-中文(简体)-英语-对照版.zip】 jar包下载地址:【***.jar下载地址(官方地址+国内镜像地址).txt】 ...

    RRD与JRobin

    介绍RRD与RRDTool JRobin 分享给那些做流量监控的朋友们.

    jrobin-1.5.14.jar和源代码

    jrobin-1.5.14.jar和源代码

    jrobin学习例子程序

    学习用jrobin绘图的绝佳例子程序 学习用jrobin绘图的绝佳例子程序

    JRobin 流量报表

    JRboin 是java 一开源的流量图型开源框架

    jrobin流量监控代码

    这里是jrobin流量监控代码(我自己写的)! 可以创建一个rrd文件,然后对柔软的文件进行更新和画图;是一个很有用的画图代码!

    javaMelody+jrobin jar文件 .rar

    javaMelody jrobin java监控运行时系统; 提供一些图表让你知道项目的运行情况,可以按天、周、月、年、全部(从运行至今)来查看监控信息

    JRobin-开源

    JRobin是RRDTool的100%纯Java替代品,具有几乎完全相同的规格。 如果向RRDTool和JRobin提供相同的数据,则将获得完全相同的结果和图形。 支持所有标准RRDTool操作。

    javamelody.jar和 jrobin.jar

    监控器需要的jar,需在web.xml中配置 &lt;filter-name&gt;monitoring &lt;filter-class&gt;net.bull.javamelody.MonitoringFilter&lt;/filter-class&gt; &lt;filter-name&gt;monitoring &lt;url-pattern&gt;/* ...可以进入到监控页面

    snmp-tutorial:SNMP教程:Jrobin、SNMP4j

    snmp-tutorialSNMP tutorial :Jrobin、SNMP4jsnmp4j-1x-demoSNMP4j实现同步和异步的GET的示例SNMP4j实现同步和异步的Walk的示例SNMP4j实现Trap的示例SNMP4j实现SET的示例SNMP4j实现GETBLUK的示例robin-demoJRobin ...

    javamelody性能监控jar和war

    包含javamelody.jar、jrobin-1.5.9.1.jar和javamelody.war文件,测试可用

    监控JAVA应用的好工具javamelody

    javamelody.jar和jrobin-1.5.9.1.jar 博文链接:https://navylee.iteye.com/blog/746914

    jribin-1.5

    JRobin is just an API. With this API you are free to optimize your code so that RRD files are open or closed only when necessary. This could lead to huge performance benefits.

    javamelody 1.53整套源码及bin文件

    2014/03/07 14:17 238,016 jrobin-1.5.9.1.jar 2014/03/07 14:17 11,358 LICENSE 2014/10/01 23:31 18,519 pom.xml 2014/05/14 18:24 1,979 README.txt 2014/03/07 14:18 &lt;DIR&gt; src 2014/10/01 23:29 40 VERSION....

    jprofiler 监控容器 tomcat

    里面含有具体文件,web.xml 配置、 javamelody-1.32.1.jar、 jrobin-1.5.9.1.jar、javamelody-1.32.1.war, 把你的web.xml配置 然后把两个jar放到你的项目下面,然后把那个war包放到你的服务下,启动就可以监控你的...

    javamelogdy-1.51.0

    jrobin-1.5.9.1.jar 3、如果您的web项目有SpringSecurity,修改SpringSecurity资源过滤配置、不过滤/monitoring的访问 &lt;!-- -------------------------------------需监控工程修改结束-------------------------...

    RRDSharp-开源

    RRDSharp 是 rrdtool (www.rrdtool.org) 的 100% 托管实现,在 Mono 和 .NET 上运行,并基于 Java JRobin 项目。 该项目最初由 David Quintana 开发。

Global site tag (gtag.js) - Google Analytics