From: <ju...@us...> - 2006-03-25 21:31:50
|
Revision: 98 Author: jurjanw Date: 2006-03-25 13:31:23 -0800 (Sat, 25 Mar 2006) ViewCVS: http://svn.sourceforge.net/sensor/?rev=98&view=rev Log Message: ----------- Added Wrapper class for rendering a lsitg which Velocity can process. Modified Paths: -------------- trunk/sensor-console/src/java/net/sf/sensor/console/views/TimerStatistics.vm trunk/sensor-console/src/web/WEB-INF/sensor-servlet.xml Added Paths: ----------- trunk/sensor-console/src/java/net/sf/sensor/console/support/TimerStatisticsVelocitySupport.java trunk/sensor-console/test/java/net/sf/sensor/console/support/TimerStatisticsVelocitySupportTest.java Added: trunk/sensor-console/src/java/net/sf/sensor/console/support/TimerStatisticsVelocitySupport.java =================================================================== --- trunk/sensor-console/src/java/net/sf/sensor/console/support/TimerStatisticsVelocitySupport.java (rev 0) +++ trunk/sensor-console/src/java/net/sf/sensor/console/support/TimerStatisticsVelocitySupport.java 2006-03-25 21:31:23 UTC (rev 98) @@ -0,0 +1,130 @@ +/* + * Copyright 2005 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.sf.sensor.console.support; + +import java.util.ArrayList; +import java.util.List; + +import net.sf.sensor.timer.TimerStatistics; + +/** + * Support class to convert a nested statistics object to easy viewable list. + * + * @author Jurjan Woltman + */ +public class TimerStatisticsVelocitySupport { + + /** + * Recursive method to build a tree, which can be easily displayed. + * @param timerStatisticsList the list of statistics to build a tree of. + * @return a list of a tree which can be easily displayed. + */ + public List getTimerStatisticsDiplayTree(List timerStatisticsList) { + return buildTimerStatisticsDiplayTree(timerStatisticsList, null, 0); + } + + /** + * Recursive convenience method to build a tree, which can be easily displayed. + * @param timerStatisticsList the list of statistics to build a tree of. + * @param rootId the id of the parent node, where this tree belongs to. + * @param depth of the start of this tree. + * @return a tree with statistics. + */ + protected List buildTimerStatisticsDiplayTree(List timerStatisticsList, String rootId, int depth) { + List statisticsDisplayList = new ArrayList(); + + for (int i = 0; i < timerStatisticsList.size(); i++) { + TimerStatistics statistics = (TimerStatistics) timerStatisticsList.get(i); + + String nodeId = rootId; + + if (nodeId != null && nodeId.length() > 0) { + nodeId = nodeId + "-"; + } else { + nodeId = ""; + } + + nodeId = nodeId + i; + + statisticsDisplayList.add(new StatisticsTreeNode(nodeId, depth, statistics)); + + depth++; + statisticsDisplayList.addAll(buildTimerStatisticsDiplayTree(statistics.getChildren(), nodeId, depth)); + depth--; + } + + return statisticsDisplayList; + } + + /** + * Node in the displayTree, wrapper class to of the statistics. + * Besides the statistics it holds an unique id of the node in the tree + * and the node it's depth. + * + * @author Jurjan Woltman + */ + public class StatisticsTreeNode { + private String id; + private int depth; + private TimerStatistics statistics; + + /** + * Create a new node. + * + * @param id unique id of the node + * @param depth the depth of the node in the tree. + * @param statistics the statistics. + */ + public StatisticsTreeNode(String id, int depth, TimerStatistics statistics) { + this.id = id; + this.depth = depth; + this.statistics = statistics; + } + + /** + * Returns the depth. + * @return Returns the depth. + */ + public int getDepth() { + return depth; + } + + /** + * Returns the id. + * @return Returns the id. + */ + public String getId() { + return id; + } + + /** + * Returns the statistics. + * @return Returns the statistics. + */ + public TimerStatistics getStatistics() { + return statistics; + } + + /** + * @see java.lang.Object#toString() + */ + public String toString() { + return "StatisticsNode [id = '"+getId()+"' , depth = '"+getDepth()+"']"; + } + } + +} Modified: trunk/sensor-console/src/java/net/sf/sensor/console/views/TimerStatistics.vm =================================================================== --- trunk/sensor-console/src/java/net/sf/sensor/console/views/TimerStatistics.vm 2006-03-25 21:31:07 UTC (rev 97) +++ trunk/sensor-console/src/java/net/sf/sensor/console/views/TimerStatistics.vm 2006-03-25 21:31:23 UTC (rev 98) @@ -26,36 +26,50 @@ <script language="javascript"> - function toggleShowChildren(rowid) { + function showChildren(id) { trs = document.getElementsByTagName('tr'); + + for (i = 0; i < trs.length; i++) { + if(trs[i].id.indexOf('row-' + id + '-') >= 0 ) { + trs[i].style.display = ''; + } + } + //change event handler + document.getElementById('trigger-' + id).onclick = function() { + hideChildren(id); + }; + } + + function hideChildren(id) { + trs = document.getElementsByTagName('tr'); + for (i = 0; i < trs.length; i++) { - if (trs[i].id.indexOf(rowid + '-') >= 0) { - if (trs[i].style.display == 'none') { - trs[i].style.display = ''; - } else { + if(trs[i].id.indexOf('row-' + id + '-') >= 0 ) { trs[i].style.display = 'none'; - } } } + + //change event handler + document.getElementById('trigger-' + id).onclick = function() { + showChildren(id) + }; } - - - </script> </head> <body> - #macro ( displayTimerStatistics $parentStatistics $margin $rowid) - #set($storedRowId = $rowid) - #foreach($statistics in $parentStatistics) - #set($rowid = "${rowid}-${velocityCount}") - <tr id="${rowid}"> + #macro (displayTimerStatistics $timerStatistics) + #foreach($timerStatisticsNode in $statisticsVelocitySupport.getTimerStatisticsDiplayTree($timerStatistics)) + #set($margin = ($timerStatisticsNode.depth * 20)) + #set($statistics = $timerStatisticsNode.statistics) + + <tr id="row-${timerStatisticsNode.id}"> <td class="#if( $statistics.hasChildren()) sensor-node #else sensor-leaf #end" nowrap> <span style="margin:${margin}px;"> - <a href="#" onClick="toggleShowChildren('$rowid');">${statistics.id}</a> + <a href="#" onClick="hideChildren('${timerStatisticsNode.id}');" id="trigger-${timerStatisticsNode.id}">${statistics.id}</a> </span> </td> <td nowrap>${statistics.numberOfHits}</td> @@ -67,16 +81,12 @@ <td nowrap>$date.format('yyyy-MM-dd HH:mm:ss:S', $statistics.timeOfFirstUpdate)</td> <td nowrap>$date.format('yyyy-MM-dd HH:mm:ss:S', $statistics.timeOfLastUpdate)</td> </tr> - #set($margin = $margin + 20) - #displayTimerStatistics($statistics.children $margin $rowid) - #set($rowid = $storedRowId) - #set($margin = $margin - 20) #end #end <form action="statistics.htm" method="post"> <div id="sensor-container"> - <table cellspacing="0" cellpadding="0"> + <table id="statisticsData" cellspacing="0" cellpadding="0"> <thead> <th>Id</th> <th>Hits</th> @@ -89,9 +99,7 @@ <th>Last Access</th> </thead> <tbody id="statisticsTable"> - #set($margin = 0) - #set($rowid = "row") - #displayTimerStatistics($timerStatistics $margin $rowid) + #displayTimerStatistics($timerStatistics) </tbody> </table> </div> Modified: trunk/sensor-console/src/web/WEB-INF/sensor-servlet.xml =================================================================== --- trunk/sensor-console/src/web/WEB-INF/sensor-servlet.xml 2006-03-25 21:31:07 UTC (rev 97) +++ trunk/sensor-console/src/web/WEB-INF/sensor-servlet.xml 2006-03-25 21:31:23 UTC (rev 98) @@ -40,6 +40,13 @@ <property name="viewClass" value="org.springframework.web.servlet.view.velocity.VelocityView"/> <property name="exposeSpringMacroHelpers" value="true"/> <property name="dateToolAttribute" value="date"/> + <property name="attributesMap"> + <map> + <entry key="statisticsVelocitySupport"> + <bean class="net.sf.sensor.console.support.TimerStatisticsVelocitySupport"/> + </entry> + </map> + </property> <property name="cache" value="true"/> <property name="prefix" value=""/> <property name="suffix" value=".vm"/> Added: trunk/sensor-console/test/java/net/sf/sensor/console/support/TimerStatisticsVelocitySupportTest.java =================================================================== --- trunk/sensor-console/test/java/net/sf/sensor/console/support/TimerStatisticsVelocitySupportTest.java (rev 0) +++ trunk/sensor-console/test/java/net/sf/sensor/console/support/TimerStatisticsVelocitySupportTest.java 2006-03-25 21:31:23 UTC (rev 98) @@ -0,0 +1,73 @@ +/* + * Copyright 2005 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.sf.sensor.console.support; + +import java.util.List; + +import junit.framework.TestCase; + +import net.sf.sensor.timer.DefaultTimerRuntime; +import net.sf.sensor.timer.Timer; +import net.sf.sensor.timer.TimerRuntime; + +/** + * @author Jurjan Woltman + * + */ +public class TimerStatisticsVelocitySupportTest extends TestCase { + + /* + * Test method for 'net.sf.sensor.console.support.TimerStatisticsVelocitySupport.buildTimerStatisticsDiplayList(List, String, int)' + */ + public void testBuildTimerStatisticsDiplayList() { + + TimerRuntime timerRuntime = new DefaultTimerRuntime(); + + // timers purely for testing the display of nested timers + Timer timer1 = timerRuntime.getTimer("test timer 1"); + Timer timer2 = timerRuntime.getTimer("test timer 2"); + Timer timer3 = timerRuntime.getTimer("test timer 3"); + Timer timer4 = timerRuntime.getTimer("test timer 4"); + Timer timer5 = timerRuntime.getTimer("test timer 5"); + Timer timer6 = timerRuntime.getTimer("test timer 6"); + + timer1.start(); + timer2.start(); + timer3.start(); + timer3.stop(); + timer2.stop(); + timer4.start(); + timer5.start(); + timer6.start(); + timer6.stop(); + timer5.stop(); + timer4.stop(); + timer1.stop(); + timer2.start(); + timer2.stop(); + + TimerStatisticsVelocitySupport statisticsViewSupport = new TimerStatisticsVelocitySupport(); + List displayList = statisticsViewSupport.buildTimerStatisticsDiplayTree(timerRuntime.getTimerStatistics(), null, 0); + + assertNotNull(displayList); + assertEquals(7, displayList.size()); + TimerStatisticsVelocitySupport.StatisticsTreeNode node = (TimerStatisticsVelocitySupport.StatisticsTreeNode)displayList.get(6); + + assertEquals(0, node.getDepth()); + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |