PageRenderTime 63ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/A2Billing_UI/graph_hourdetail.php

https://github.com/xrg/a2billing
PHP | 476 lines | 244 code | 144 blank | 88 comment | 69 complexity | 693a16ae07d80c85fd123994386e2233 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. include_once(dirname(__FILE__) . "/../lib/defines.php");
  3. include_once(dirname(__FILE__) . "/jpgraph_lib/jpgraph.php");
  4. include_once(dirname(__FILE__) . "/jpgraph_lib/jpgraph_line.php");
  5. include_once(dirname(__FILE__) . "/jpgraph_lib/jpgraph_bar.php");
  6. include_once(dirname(__FILE__) . "/../lib/module.access.php");
  7. if (! has_rights (ACX_CALL_REPORT)){
  8. Header ("HTTP/1.0 401 Unauthorized");
  9. Header ("Location: PP_error.php?c=accessdenied");
  10. die();
  11. }
  12. // this variable specifie the debug type (0 => nothing, 1 => sql result, 2 => boucle checking, 3 other value checking)
  13. $FG_DEBUG = 0;
  14. getpost_ifset(array('typegraph', 'min_call', 'fromstatsday_sday', 'days_compare', 'fromstatsmonth_sday', 'dsttype', 'srctype', 'clidtype', 'channel', 'resulttype', 'dst', 'src', 'clid', 'userfieldtype', 'userfield', 'accountcodetype', 'accountcode', 'hourinterval', 'customer', 'entercustomer', 'enterprovider', 'entertrunk'));
  15. //echo "posted=$posted 'accountcodetype=$accountcodetype', 'accountcode=$accountcode', 'fromstatsday_sday=$fromstatsday_sday', 'fromstatsmonth_sday=$fromstatsmonth_sday' hourinterval=$hourinterval ";
  16. //exit();
  17. //$hourinterval=12;
  18. // hourinterval -- 1 to 23
  19. if (!($hourinterval>=0) && ($hourinterval<=23)) exit();
  20. // http://localhost/Asterisk/asterisk-stat-v1_4/graph_stat.php?min_call=0&fromstatsday_sday=11&days_compare=2&fromstatsmonth_sday=2005-02&dsttype=1&srctype=1&clidtype=1&channel=&resulttype=&dst=1649&src=&clid=&userfieldtype=1&userfield=&accountcodetype=1&accountcode=
  21. // The variable FG_TABLE_NAME define the table name to use
  22. $FG_TABLE_NAME="cc_call t1 LEFT OUTER JOIN cc_trunk t3 ON t1.id_trunk = t3.id_trunk";
  23. //$link = DbConnect();
  24. $DBHandle = DbConnect();
  25. // The variable Var_col would define the col that we want show in your table
  26. // First Name of the column in the html page, second name of the field
  27. $FG_TABLE_COL = array();
  28. $FG_TABLE_DEFAULT_ORDER = "starttime";
  29. $FG_TABLE_DEFAULT_SENS = "DESC";
  30. // This Variable store the argument for the SQL query
  31. $FG_COL_QUERY_GRAPH = 'starttime, sessiontime';
  32. if ($FG_DEBUG == 3) echo "<br>Table : $FG_TABLE_NAME - Col_query : $FG_COL_QUERY_GRAPH";
  33. $instance_table_graph = new Table($FG_TABLE_NAME, $FG_COL_QUERY_GRAPH);
  34. if ( is_null ($order) || is_null($sens) ){
  35. $order = $FG_TABLE_DEFAULT_ORDER;
  36. $sens = $FG_TABLE_DEFAULT_SENS;
  37. }
  38. $SQLcmd = '';
  39. if ($_POST['before']) {
  40. if (strpos($SQLcmd, 'WHERE') > 0) { $SQLcmd = "$SQLcmd AND ";
  41. }else{ $SQLcmd = "$SQLcmd WHERE "; }
  42. $SQLcmd = "$SQLcmd starttime <'".$_POST['before']."'";
  43. }
  44. if ($_POST['after']) { if (strpos($SQLcmd, 'WHERE') > 0) { $SQLcmd = "$SQLcmd AND ";
  45. } else { $SQLcmd = "$SQLcmd WHERE "; }
  46. $SQLcmd = "$SQLcmd starttime >'".$_POST['after']."'";
  47. }
  48. $SQLcmd = do_field($SQLcmd, 'dst', 'calledstation');
  49. if (isset($customer) && ($customer>0)){
  50. if (strlen($SQLcmd)>0) $SQLcmd.=" AND ";
  51. else $SQLcmd.=" WHERE ";
  52. $SQLcmd.=" username='$customer' ";
  53. }else{
  54. if (isset($entercustomer) && ($entercustomer>0)){
  55. if (strlen($SQLcmd)>0) $SQLcmd.=" AND ";
  56. else $SQLcmd.=" WHERE ";
  57. $SQLcmd.=" username='$entercustomer' ";
  58. }
  59. }
  60. if ($_SESSION["is_admin"] == 1)
  61. {
  62. if (isset($enterprovider) && $enterprovider > 0) {
  63. if (strlen($SQLcmd) > 0) $SQLcmd .= " AND "; else $SQLcmd .= " WHERE ";
  64. $SQLcmd .= " t3.id_provider = '$enterprovider' ";
  65. }
  66. if (isset($entertrunk) && $entertrunk > 0) {
  67. if (strlen($SQLcmd) > 0) $SQLcmd .= " AND "; else $SQLcmd .= " WHERE ";
  68. $SQLcmd .= " t3.id_trunk = '$entertrunk' ";
  69. }
  70. }
  71. $date_clause='';
  72. $min_call= intval($min_call);
  73. if (($min_call!=0) && ($min_call!=1)) $min_call=0;
  74. if (!isset($fromstatsday_sday)){
  75. $fromstatsday_sday = date("d");
  76. $fromstatsmonth_sday = date("Y-m");
  77. }
  78. $hourintervalplus = $hourinterval+1;
  79. if (DB_TYPE == "postgres"){
  80. if (isset($fromstatsday_sday) && isset($fromstatsmonth_sday))
  81. $date_clause.=" AND t1.starttime < '$fromstatsmonth_sday-$fromstatsday_sday ".$hourintervalplus.":00:00' AND t1.starttime >= '$fromstatsmonth_sday-$fromstatsday_sday ".$hourinterval.":00:00' ";
  82. }else{
  83. if (isset($fromstatsday_sday) && isset($fromstatsmonth_sday))
  84. $date_clause.=" AND t1.starttime < '$fromstatsmonth_sday-$fromstatsday_sday ".$hourintervalplus.":00:00' AND t1.starttime >= '$fromstatsmonth_sday-$fromstatsday_sday ".$hourinterval.":00:00' ";
  85. }
  86. //-- $date_clause=" AND calldate < date'$fromstatsmonth_sday-$fromstatsday_sday'+ INTERVAL '1 DAY' AND calldate >= '$fromstatsmonth_sday-$fromstatsday_sday 12:00:00'";
  87. if (strpos($SQLcmd, 'WHERE') > 0) {
  88. $FG_TABLE_CLAUSE = substr($SQLcmd,6).$date_clause;
  89. }elseif (strpos($date_clause, 'AND') > 0){
  90. $FG_TABLE_CLAUSE = substr($date_clause,5);
  91. }
  92. if ($FG_DEBUG == 3) echo $FG_TABLE_CLAUSE;
  93. //$list = $instance_table -> Get_list ($DBHandle, $FG_TABLE_CLAUSE, $order, $sens, null, null, null, null);
  94. $list_total = $instance_table_graph -> Get_list ($DBHandle, $FG_TABLE_CLAUSE, 't1.starttime', 'ASC', null, null, null, null);
  95. //print_r($list_total);
  96. /**************************************/
  97. $nbcall = count($list_total);
  98. $mycall_min[0]=0;
  99. $mycall_dur[0]=0;
  100. /*
  101. WE WILL BUILD DIFFERENT TABLES,
  102. mycall_min FOR THE STARTDATE (MIN)
  103. mycall_dur FOR THE DURATION OF EACH CALLS (IN SECS)
  104. mycall_minsec_start FOR THE EXACT START OF THE CALL AND END
  105. mycall_minsec_start[i][0] - START DATE (MINSEC)1843 18em Minutes 43 sec
  106. mycall_minsec_start[i][1] - END DATE (MINSEC)2210 22em Minutes 10 sec
  107. */
  108. for ($i=1; $i <= $nbcall; $i++){
  109. $mycall_min[$i] = substr($list_total[$i-1][0],14,2);
  110. $mycall_minsec_start[$i][0] = substr($list_total[$i-1][0],14,2).substr($list_total[$i-1][0],17,2);
  111. $mycall_dur[$i] = $list_total[$i-1][1];
  112. $nx_sec_report = 0;
  113. $nx_sec = substr($list_total[$i-1][0],17,2) + ($mycall_dur[$i]%60);
  114. $nx_sec_report = intval($nx_sec/60);
  115. $nx_sec = $nx_sec%60;
  116. $nx_min = substr($list_total[$i-1][0],14,2) + intval($mycall_dur[$i]/60) + $nx_sec_report;
  117. if ($nx_min>59) { $nx_min=59; $nx_sec = 59; }
  118. $mycall_minsec_start[$i][1] = sprintf("%02d",$nx_min).sprintf("%02d",$nx_sec);
  119. //if ($i==10) break;
  120. }
  121. /*
  122. print_r ($list_total);
  123. print_r ($mycall_minsec_start);
  124. print_r ($mycall_dur);
  125. echo count($mycall_minsec_start)."<br>";
  126. */
  127. for ($k=0; $k<=count($mycall_minsec_start); $k++){
  128. if (is_numeric($fluctuation[$mycall_minsec_start[$k][0]])){
  129. $fluctuation[$mycall_minsec_start[$k][0]]++;
  130. }else{
  131. $fluctuation[$mycall_minsec_start[$k][0]]=1;
  132. }
  133. if (is_numeric($fluctuation[$mycall_minsec_start[$k][1]])){
  134. $fluctuation[$mycall_minsec_start[$k][1]]--;
  135. }else{
  136. $fluctuation[$mycall_minsec_start[$k][1]]=-1;
  137. }
  138. }
  139. ksort($fluctuation);
  140. //print_r($fluctuation);
  141. $maxload=1;
  142. $load=0;
  143. while (list ($key, $val) = each ($fluctuation)) {
  144. //echo "<br>$key => $val\n";
  145. $load = $load + $val;
  146. if (is_numeric($key)) $fluctuation_load[substr($key,0,2).':'.substr($key,2,2)] = $load;
  147. //echo "<br>:: ".$load;
  148. if ($load > $maxload){
  149. $maxload=$load;
  150. }
  151. }
  152. //echo $maxload;
  153. //print_r($fluctuation_load);
  154. //print_r(array_keys($fluctuation_load));
  155. function recursif_count_load ($ind, $table, $load)
  156. {
  157. $maxload=$load;
  158. $current_start = $table[$ind][0];
  159. $current_end = $table[$ind][1];
  160. for ($k=$ind+1; $k<=count($table); $k++){
  161. if ($table[$k][0]<= $current_end){
  162. $load = recursif_count_load ($k, $table, $load+1);
  163. if ($load > $maxload) $maxload=$load;
  164. }else{
  165. break;
  166. }
  167. }
  168. if ($k<count($table)) $load = recursif_count_load ($k, $table, $load);
  169. if ($load > $maxload) $maxload=$load;
  170. return $maxload;
  171. }
  172. // Some data
  173. for ($j=0; $j <= 59; $j++){
  174. if ($j==-1)
  175. $datax[] = '';
  176. else
  177. $datax[] = sprintf("%02d",$j);
  178. //$datax = array("","00","01","02","03","04","05","06","07","08","09","10","01","02","03","04","05","06","07","08","09","10","01","02","03","04","05","06","07","08","09","10","01","02","03","04","05","06","07","08","09","10","01","02","03","04","05","06","07","08","09","10");
  179. }
  180. /*
  181. $nbcall=20;
  182. $mycall_min[0]=0;
  183. $mycall_dur[0]=0;
  184. for ($i=1; $i <= $nbcall; $i++){
  185. $mycall_min[$i] = rand(0,59); // num min INPUT
  186. $mycall_dur[$i] = rand(40,1500); // num sec sessiontime INPUT
  187. }*/
  188. sort ($mycall_min);
  189. //print_r($mycall_min);
  190. $lineSetWeight = 500 / $nbcall;
  191. for ($k=1; $k <= $nbcall; $k++){
  192. $mycall_dur[$k] = intval($mycall_dur[$k] /60)+1;
  193. for ($j=0; $j <= 59; $j++){
  194. if ($j==-1){
  195. $datay[$k][]='';
  196. }else{
  197. if ($j==$mycall_min[$k]){
  198. $datay[$k][]=$k*1;
  199. }elseif ($j>$mycall_min[$k]){ // CHECK SESSIONTIME
  200. if ( ($mycall_min[$k]+$mycall_dur[$k]) >= $j ) $datay[$k][]=$k*1;
  201. else $datay[$k][]='';
  202. }else{ // FILL WITH BLANK
  203. $datay[$k][]='';
  204. }
  205. }
  206. }
  207. }
  208. //print_r($datay);
  209. //exit();
  210. /*
  211. $table_colors[]="yellow@0.3";
  212. $table_colors[]="purple@0.3";
  213. $table_colors[]="green@0.3";
  214. $table_colors[]="blue@0.3";
  215. $table_colors[]="red@0.3";
  216. */
  217. $myrgb = new RGB();
  218. foreach ($myrgb -> rgb_table as $minimecolor){
  219. $table_colors[]= $minimecolor;
  220. }
  221. /*****************************************************/
  222. /* 2 GRAPH - FLUCTUATION & WATCH TRAFFIC */
  223. /*****************************************************/
  224. if ($typegraph == 'fluctuation'){
  225. // Setup the graph
  226. $width_graph=750;
  227. if (count($fluctuation_load)>200){
  228. $multi_width = intval(count($fluctuation_load)/90);
  229. $width_graph =$width_graph * $multi_width;
  230. }
  231. $graph = new Graph($width_graph,450);
  232. $graph->SetMargin(40,40,45,90); //droit,gauche,haut,bas
  233. $graph->SetMarginColor('white');
  234. //$graph->SetScale("linlin");
  235. $graph->SetScale("textlin");
  236. $graph->yaxis->scale->SetGrace(3);
  237. // Hide the frame around the graph
  238. $graph->SetFrame(false);
  239. // Setup title
  240. $graph->title->Set("Graphic");
  241. //$graph->title->SetFont(FF_VERDANA,FS_BOLD,14);
  242. // Note: requires jpgraph 1.12p or higher
  243. $graph->SetBackgroundGradient('#FFFFFF','#CDDEFF:0.8',GRAD_HOR,BGRAD_PLOT);
  244. $graph->tabtitle->Set("$fromstatsmonth_sday-$fromstatsday_sday Hourly Graph - FROM $hourinterval to $hourintervalplus - NBCALLS $nbcall - "."MAX LOAD = $maxload");
  245. $graph->tabtitle->SetWidth(TABTITLE_WIDTHFULL);
  246. // Enable X and Y Grid
  247. $graph->xgrid->Show();
  248. $graph->xgrid->SetColor('gray@0.5');
  249. $graph->ygrid->SetColor('gray@0.5');
  250. $graph->yaxis->HideZeroLabel();
  251. $graph->xaxis->HideZeroLabel();
  252. $graph->ygrid->SetFill(true,'#EFEFEF@0.5','#CDDEFF@0.5');
  253. //$graph->xaxis->SetTickLabels($tableau_hours[0]);
  254. // initialisaton fixe de AXE X
  255. $graph->xaxis->SetTickLabels(array_keys($fluctuation_load));
  256. // Setup X-scale
  257. //$graph->xaxis->SetTickLabels($tableau_hours[0]);
  258. $graph->xaxis->SetLabelAngle(90);
  259. // Format the legend box
  260. $graph->legend->SetColor('firebrick1');
  261. $graph->legend->SetFillColor('gray@0.8');
  262. $graph->legend->SetLineWeight(2);
  263. $graph->legend->SetShadow('gray@0.4',3);
  264. $graph->legend->SetPos(0.1,0.15,'left','left');
  265. $graph->legend->SetMarkAbsSize(1);
  266. $graph->legend->SetFont(FF_FONT1,FS_BOLD);
  267. $indgraph=0;
  268. $bplot[$indgraph] = new BarPlot(array_values($fluctuation_load));
  269. //$bplot[$indgraph]->SetColor($table_colors[$indgraph]);
  270. $bplot[$indgraph]->SetWeight(1);
  271. $bplot[$indgraph]->SetFillColor('orange');
  272. $bplot[$indgraph]->SetShadow('black',1,1);
  273. $bplot[$indgraph]->value->Show();
  274. $bplot[$indgraph]->SetLegend("MAX LOAD = $maxload");
  275. $graph->Add($bplot[$indgraph]);
  276. $indgraph++;
  277. // Output the graph
  278. $graph->Stroke();
  279. }else{
  280. $graph = new Graph(750,800);
  281. $graph->SetMargin(60,40,45,90); //droit,gauche,haut,bas
  282. $graph->SetMarginColor('white');
  283. //$graph->SetScale("linlin");
  284. $graph->SetScale("textlin");
  285. $graph->yaxis->scale->SetGrace(1,1);
  286. // Hide the frame around the graph
  287. $graph->SetFrame(false);
  288. // Setup title
  289. $graph->title->Set("Graphic");
  290. //$graph->title->SetFont(FF_VERDANA,FS_BOLD,14);
  291. // Note: requires jpgraph 1.12p or higher
  292. $graph->SetBackgroundGradient('#FFFFFF','#CDDEFF:0.8',GRAD_HOR,BGRAD_PLOT);
  293. $graph->tabtitle->Set("$fromstatsmonth_sday-$fromstatsday_sday Hourly Graph - FROM $hourinterval to $hourintervalplus - NBCALLS $nbcall - "."MAX LOAD = $maxload");
  294. $graph->tabtitle->SetWidth(TABTITLE_WIDTHFULL);
  295. //$graph->yaxis->Hide();
  296. // Enable X and Y Grid
  297. //$graph->xgrid->Show();
  298. $graph->xgrid->SetColor('gray@0.5');
  299. $graph->ygrid->SetColor('gray@0.5');
  300. //$graph->yaxis->HideZeroLabel();
  301. $graph->xaxis->HideZeroLabel();
  302. $graph->ygrid->SetFill(true,'#EFEFEF@0.5','#CDDEFF@0.5');
  303. $graph->xaxis->SetTickLabels($datax);
  304. $graph->xaxis->SetLabelAngle(90);
  305. $graph->yaxis->HideFirstLastLabel();
  306. //$graph->yaxis->HideLine();
  307. $graph->yaxis->HideTicks();
  308. $graph->yaxis->SetLabelFormatString('%1d call');
  309. $graph->yaxis->SetTextLabelInterval(2);
  310. // Format the legend box
  311. $graph->legend->SetColor('firebrick1');
  312. $graph->legend->SetFillColor('gray@0.8');
  313. $graph->legend->SetLineWeight(2);
  314. $graph->legend->SetShadow('gray@0.4',3);
  315. $graph->legend->SetPos(0.2,0.2,'left','center');
  316. $graph->legend->SetMarkAbsSize(1);
  317. $graph->legend->SetFont(FF_FONT1,FS_BOLD);
  318. for ($i=1;$i<=count($datay);$i++){
  319. // Create the first line
  320. $p1[$i] = new LinePlot($datay[$i]);
  321. $p1[$i]->SetColor($table_colors[($i+20) % 436] );
  322. $p1[$i]->SetCenter();
  323. $p1[$i]->SetWeight($lineSetWeight);
  324. if ($i==1) $p1[$i]->SetLegend("MAX LOAD = $maxload");
  325. $graph->Add($p1[$i]);
  326. }
  327. // Output line
  328. $graph->Stroke();
  329. }//END IF (typegraph)
  330. ?>