Advertisement
dveuve

Dynamic Timechart Bin Size

Apr 18th, 2011
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. <!-- The below segment is in your view. It includes the TimeRangePicker, a blanket search (the purpose of this search is to terminate quickly,
  2. and allow us to get absolute time when the actual search might include relative times), the CustomBehavior module, and finally an HTML module to
  3. brag about your success. -->
  4.  
  5. <module name="TimeRangePicker">
  6. <param name="default">Last 30 days</param>
  7. <param name="searchWhenChanged">True</param>
  8.  
  9. <module name="Search" autoRun="True">
  10. <param name="search">* | head 1</param>
  11.  
  12. <module name="CustomBehavior">
  13. <param name="customBehavior">GatherBins</param>
  14. <param name="requiresDispatch">True</param>
  15.  
  16.  
  17. <module name="HTML" layoutPanel="panel_row1_col1"><param name="html"><![CDATA[<br /><p>Looking at $Bins$ bins of $Binsize$ each.</p>]]></param></module>
  18.  
  19.  
  20.  
  21. // The below belongs in your application.js (commenting inline)
  22.  
  23. //Assign CustomBehavior triggers
  24. if(typeof(Sideview)!="undefined"){
  25. $(document).bind("allModulesInHierarchy",function(){
  26.  
  27. Sideview.utils.forEachModuleWithCustomBehavior("GatherBins",function(b,a){
  28.  
  29. //isReadyForContextPush -- don't push to the next modules, since the bins aren't assigned yet.
  30. a.isReadyForContextPush = function(){
  31. if(!this.RetrievedBinCount) return Splunk.Module.DEFER;
  32. if (this.getLoadState() < Splunk.util.moduleLoadStates.HAS_CONTEXT) return false;
  33. return true
  34. }
  35.  
  36.  
  37. //onJobProgress -- Actually figure out the number of bins.
  38. a.onJobProgress = function() {
  39. var c=this.getContext();
  40. //This will be the upstream * | head 1 search job, which will give us absolute values for the TimeRangePicker
  41. var d=c.get("search").job;
  42.  
  43.  
  44. var Bins = 0;
  45. var Binsize = "";
  46. var latest = new Date(d._latestTime);
  47. var earliest = new Date(d._earliestTime);
  48. //Handle latestTime = 0 (Not sure how often this should happen -- came up when I was testing)
  49. if(latest.valueOf() == 0){
  50. latest = new Date();
  51. }
  52.  
  53. //Calculate difference in seconds
  54. var Difference = (latest.valueOf() - earliest.valueOf()) / 1000;
  55.  
  56.  
  57. //Figure out how many bins to assign, based on the range. The below is for 10 minute data increments.
  58. //If you had only hourly data, and were searching over 10 years, you might need to add an additional layer of summary.
  59.  
  60. if(Difference > (730*24*60*60)){
  61. //alert("More than 730 days -- summarize four days");
  62. Bins = parseInt(Difference / (96*60*60))+2;
  63. Binsize = "Four Day";
  64. }else if(Difference > (450*24*60*60)){
  65. //alert("More than 450 days -- summarize two days");
  66. Bins = parseInt(Difference / (48*60*60))+2;
  67. Binsize = "Two Day";
  68. }else if(Difference > (150*24*60*60)){
  69. //alert("More than 150 days -- summarize daily");
  70. Bins = parseInt(Difference / (24*60*60))+2;
  71. Binsize = "One Day";
  72. }else if(Difference > (100*24*60*60)){
  73. //alert("More than 100 days -- summarize 12 hourly");
  74. Bins = parseInt(Difference / (12*60*60))+2;
  75. Binsize = "12 Hour";
  76. }else if(Difference > (50*24*60*60)){
  77. //alert("More than 50 days -- summarize 8 hourly");
  78. Bins = parseInt(Difference / (8*60*60))+2;
  79. Binsize = "8 Hour";
  80. }else if(Difference > (14*24*60*60)){
  81. //alert("More than 14 days -- summarize 4 hourly");
  82. Bins = parseInt(Difference / (4*60*60))+2;
  83. Binsize = "4 Hour";
  84. }else if(Difference > (6*24*60*60)){
  85. //alert("More than 6 days -- summarize hourly");
  86. Bins = parseInt(Difference / (60*60))+2;
  87. Binsize = "One Hour";
  88. }else if(Difference > (2*24*60*60)){
  89. //alert("More than 2 days -- summarize half-hourly");
  90. Bins = parseInt(Difference / (30*60))+2;
  91. Binsize = "30 Minute";
  92. }else{
  93. //alert("Less than 2 days -- summarize to 10 minutes");
  94. Bins = parseInt(Difference / (10*60))+2;
  95. Binsize = "10 Minute";
  96. }
  97.  
  98.  
  99. // Assign to context
  100. this.Bins = Bins;
  101. this.Binsize = Binsize;
  102. this.RetrievedBinCount = true;
  103.  
  104. //Now that we have everything we need, we're ready to roll on to the next modules.
  105. this.pushContextToChildren();
  106.  
  107. }
  108.  
  109. //getModifiedContent -- put the Bins into $Bins$
  110.  
  111. a.getModifiedContext=function(){
  112. var context=this.getContext();
  113. context.set("Bins", this.Bins);
  114. context.set("Binsize", this.Binsize);
  115. return context
  116. }
  117. })
  118. })
  119. }
  120.  
  121.  
  122.  
  123.  
  124. <!--- You can then do charts such as: -->
  125.  
  126.  
  127. <module name="Search" layoutPanel="panel_row1_col1">
  128. <param name="search"> * | timechart bins=$Bins$ count</param>
  129. <module name="HTML"><param name="html"><![CDATA[<h2>Count</h2>]]></param></module>
  130. <module name="ViewstateAdapter">
  131. <module name="HiddenFieldPicker">
  132. <param name="strictMode">True</param>
  133. <module name="JobProgressIndicator">
  134. <module name="EnablePreview">
  135. <param name="enable">True</param>
  136. <param name="display">False</param>
  137. <module name="HiddenChartFormatter">
  138. <param name="charting.chart">area</param>
  139. <param name="charting.secondaryAxisTitle.text">Count</param>
  140. <param name="charting.legend.placement">none</param>
  141. <param name="charting.primaryAxisTitle.text">Time (in $Binsize$ increments)</param>
  142. <module name="FlashChart">
  143. <param name="width">100%</param>
  144. <param name="height">275px</param>
  145. </module>
  146. <module name="ViewRedirectorLink">
  147. <param name="viewTarget">flashtimeline</param>
  148. </module>
  149. </module>
  150. </module>
  151. </module>
  152. </module>
  153. </module>
  154. </module>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement