File: 1.14.1a/server/web/test.graph.php (View as HTML)

  1: <?php // test.graph.php
  2: ob_start();
  3: /* -------------------------------------------------------------
  4: This file is part of FreeNATS
  5: 
  6: FreeNATS is (C) Copyright 2008 PurplePixie Systems
  7: 
  8: FreeNATS is free software: you can redistribute it and/or modify
  9: it under the terms of the GNU General Public License as published by
 10: the Free Software Foundation, either version 3 of the License, or
 11: (at your option) any later version.
 12: 
 13: FreeNATS is distributed in the hope that it will be useful,
 14: but WITHOUT ANY WARRANTY; without even the implied warranty of
 15: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16: GNU General Public License for more details.
 17: 
 18: You should have received a copy of the GNU General Public License
 19: along with FreeNATS.  If not, see www.gnu.org/licenses
 20: 
 21: For more information see www.purplepixie.org/freenats
 22: -------------------------------------------------------------- */
 23: 
 24: require("include.php");
 25: $NATS->Start();
 26: 
 27: 
 28: 
 29: 
 30: function ty($y)
 31: {
 32: global $height;
 33: return $height-$y;
 34: }
 35: 
 36: //if (!isset($_REQUEST['testid'])) $_REQUEST['testid']="L26";
 37: 
 38: // width height suid startx finishx
 39: 
 40: if (isset($_REQUEST['width'])) $width=$_REQUEST['width'];
 41: else $width=700;
 42: if (isset($_REQUEST['height'])) $height=$_REQUEST['height'];
 43: else $height=150;
 44: 
 45: // other incoming stuff
 46: if (isset($_REQUEST['draw_spike'])) $draw_spike=$_REQUEST['draw_spike'];
 47: else $draw_spike=1;
 48: if (isset($_REQUEST['draw_track'])) $draw_track=$_REQUEST['draw_track'];
 49: else $draw_track=1;
 50: if (isset($_REQUEST['draw_under'])) $draw_under=$_REQUEST['draw_under'];
 51: else $draw_under=1;
 52: if (isset($_REQUEST['draw_under_pass'])) $draw_under=$_REQUEST['draw_under_pass'];
 53: else $draw_under_pass=0;
 54: if (isset($_REQUEST['transparent'])) $transparent=1;
 55: else $transparent=0;
 56: 
 57: $draw_x_grid=true;
 58: $draw_y_grid=true;
 59: 
 60: if (isset($_REQUEST['no_x_grid'])) $draw_x_grid=false;
 61: if (isset($_REQUEST['no_y_grid'])) $graw_y_grid=false;
 62: if (isset($_REQUEST['no_grid']))
 63: 	{
 64: 	$draw_x_grid=false;
 65: 	$draw_y_grid=false;
 66: 	}
 67: 
 68: // start image
 69: $im=@imagecreate($width,$height)
 70: 	or die("Cannot create image");
 71: 
 72: // setup colours
 73: $col=array();
 74: $col['white']=imagecolorallocate($im,255,255,255);
 75: $col['black']=imagecolorallocate($im,0,0,0);
 76: $col['red']=imagecolorallocate($im,250,50,50);
 77: $col['lightgrey']=imagecolorallocate($im,200,200,200);
 78: $col['verylightgrey']=imagecolorallocate($im,240,240,240);
 79: $col['blue']=imagecolorallocate($im,150,150,255);
 80: $col['green']=imagecolorallocate($im,0,200,0);
 81: $col['lightgreen']=imagecolorallocate($im,150,250,150);
 82: $col['grey']=imagecolorallocate($im,150,150,150);
 83: $col['orange']=imagecolorallocate($im,200,200,0);
 84: 
 85: if (isset($_REQUEST['bgcol'])) $c_bg=$col[$_REQUEST['bgcol']];
 86: else $c_bg=$col['white'];
 87: 
 88: if (isset($_REQUEST['txtcol'])) $c_txt=$col[$_REQUEST['txtcol']];
 89: else $c_txt=$col['black'];
 90: 
 91: if (isset($_REQUEST['axescol'])) $c_axes=$col[$_REQUEST['axescol']];
 92: else $c_axes=$col['lightgrey'];
 93: 
 94: if (isset($_REQUEST['trackcol'])) $c_track=$col[$_REQUEST['trackcol']];
 95: else $c_track=$col['blue'];
 96: 
 97: if (isset($_REQUEST['gridcol'])) $c_grid=$col[$_REQUEST['gridcol']];
 98: else $c_grid=$col['verylightgrey'];
 99: 
100: // transparent check 1.14.1
101: if ($transparent == 1)
102:    imagecolortransparent($im, $c_bg);
103: 
104: // fill background
105: imagefill($im,1,1,$c_bg);
106: 	
107: function ierror($t)
108: {
109: global $im,$width,$height,$col;
110: ob_clean();
111: header("Content-type: image/png");
112: imagestring($im,2,($width/2)-20,$height/2,"ERROR: ".$t,$col['red']);
113: imagepng($im);
114: imagedestroy($im);
115: exit();
116: }
117: 
118: //ierror("Test");
119: 
120: $session=$NATS_Session->Check($NATS->DB);
121: 
122: if (!$session)
123: 	{
124: 	if ($NATS->Cfg->Get("site.graph.public",0)!=1)
125: 		{
126: 		ierror("Authorisation Failure");
127: 		exit();
128: 		}
129: 	$key=$NATS->Cfg->Get("site.graph.key","");
130: 	if (isset($_REQUEST['graphkey'])) $userkey=$_REQUEST['graphkey'];
131: 	else $userkey="";
132: 	
133: 	if ( ($key!="") && ($key!=$userkey) )
134: 		{
135: 		ierror("Graph Key Failure");
136: 		exit();
137: 		}
138: 	}
139: 
140: 
141: if (!isset($_REQUEST['nodeid'])) $nodeid="";
142: else $nodeid=$_REQUEST['nodeid'];
143: 
144: if (!isset($_REQUEST['testid'])) ierror("No test ID");
145: 
146: $day=date("d");
147: $month=date("m");
148: $year=date("Y");
149: 
150: if (isset($_REQUEST['startx'])) $startx=$_REQUEST['startx'];
151: else
152: 	{ // 0:00 today    HMS mo da yr
153: 	$startx=mktime(0,0,0,$month,$day,$year);
154: 	//$startx=1203451396;
155: 	}
156: 	
157: if (isset($_REQUEST['finishx'])) $finishx=$_REQUEST['finishx'];
158: else $finishx=mktime(23,59,59,$month,$day,$year);
159: 
160: if ($startx<=0) $startx=time()+$startx;
161: if ($finishx<=0) $finishx=time()+$finishx;
162: 
163: //$finishx=1203454996;
164: $periodx=$finishx-$startx;
165: $startt=date("H:i:s d/m/y",$startx);
166: $finisht=date("H:i:s d/m/y",$finishx);
167: 
168: 
169: // titles and stuff
170: imagestring($im,2,2,2,$startt,$c_txt);
171: // -90 for size 1
172: imagestring($im,2,$width-108,2,$finisht,$c_txt);
173: 
174: if (isset($_REQUEST['title'])) $title=$_REQUEST['title'];
175: else $title=$nodeid;
176: $len=strlen($title)*4;
177: imagestring($im,4,($width/2)-$len,2,$title,$c_txt);
178: 
179: // offsets and lengths
180: $xoff=50+1;
181: $xlen=$width-$xoff-5;
182: 
183: $yoff=1;
184: $ylen=$height-$yoff-20;
185: 
186: 
187: // v-axes
188: imageline($im,50,ty(1),50,ty($height-20),$c_axes);
189: imageline($im,$width-5,ty(1),$width-5,ty($height-20),$c_axes);
190: // y-axes
191: imageline($im,50,ty(1),$width-5,ty(1),$c_axes);
192: imageline($im,$width-5,ty($height-20),50,ty($height-20),$c_axes);
193: //ierror("hello");
194: 
195: 
196: // range data
197: 
198: // Lowest
199: 
200: if (isset($_REQUEST['rangemin'])) $dlow=$_REQUEST['rangemin'];
201: else
202: 	{
203: 	$dlow=0;
204: 	/*
205: 	$q="SELECT testvalue FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\"";
206: 	$q.=" AND recordx>=".ss($startx)." AND recordx<=".ss($finishx)." ";
207: 	$q.="ORDER BY testvalue ASC LIMIT 0,1"; // lowest
208: 	$r=$NATS->DB->Query($q);
209: 	//ierror($q);
210: 	if (!$row=$NATS->DB->Fetch_Array($r)) ierror("No data for test");
211: 	$lowest=$row['testvalue'];
212: 	$dlow=$lowest;
213: 	if ($dlow>0) $dlow=0;
214: 	$NATS->DB->Free($r);
215: 	*/
216: 	}
217: 
218: 
219: // Highest
220: 
221: if (isset($_REQUEST['rangemax'])) $dhigh=$_REQUEST['rangemax'];
222: else
223: {
224: 	$q="SELECT testvalue FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\"";
225: 	$q.=" AND recordx>=".ss($startx)." AND recordx<=".ss($finishx)." ";
226: 	$q.= "ORDER BY testvalue DESC LIMIT 0,1"; //highest
227: 	
228: 	$r=$NATS->DB->Query($q);
229: 	$row=$NATS->DB->Fetch_Array($r);
230: 	$highest=$row['testvalue'];
231: 	$dhigh=$highest;
232: 	$NATS->DB->Free($r);
233: 	if (isset($_REQUEST['rangemaxmin']))
234: 		{
235: 		if ($dhigh<$_REQUEST['rangemaxmin']) $dhigh=$_REQUEST['rangemaxmin'];
236: 		}
237: }
238: 
239: $drange=$dhigh-$dlow;
240: 
241: // calculate scales
242: $xscale=$xlen/$periodx;
243: if ($drange>0) $yscale=$ylen/$drange;
244: else $yscale=1; // doesn't display but no change!
245: 
246: // Grid Lines
247: 
248: // Grid X - the X values
249: 
250: /* Oh if only I was actually any good at programming. There MUST be soooo many better ways to do these ranges
251: with all sorts of calculations to round to the nearest power of ten etc etc. Unfortunately I have the programming
252: skill of a walrus and typing code with tusks is difficult! */
253: 
254: /* TODO: Make this all like really elegant and stuff */
255: 
256: if ($draw_x_grid)
257: 	{
258: 	$xg_step=1;
259: 	$xg_scale=1;
260: 	if ($drange<0.001) $xg_scale=0.0001;
261: 	else if ($drange<0.01) $xg_scale=0.001;
262: 	else if ($drange<0.1) $xg_scale=0.01;
263: 	else if ($drange<1) $xg_scale=0.1;
264: 	else if ($drange<6) $xg_scale=1;
265: 	else if ($drange<11) $xg_scale=2;
266: 	else if ($drange<16) $xg_scale=4;
267: 	else if ($drange<21) $xg_scale=5;
268: 	else if ($drange<51) $xg_scale=10;
269: 	else if ($drange<101) $xg_scale=20;
270: 	else if ($drange<201) $xg_scale=50;
271: 	else if ($drange<501) $xg_scale=100;
272: 	else if ($drange<1001) $xg_scale=250;
273: 	else if ($drange<10001) $xg_scale=1000;
274: 	else if ($drange<100001) $xg_scale=10000;
275: 	else if ($drange<1000001) $xg_scale=100000;
276: 	else $draw_x_grid=false;
277: 	
278: 	if ($xg_scale<0) $xg_step=($xg_scale);
279: 	}
280: 	
281: if ($draw_x_grid)
282: 	{
283: 	//imagestring($im,1,2,ty(50),"Drawing X Grid ".$xg_scale,$c_txt);
284: 	for ($a=$dlow; $a<=$dhigh; $a+=$xg_scale)
285: 		{
286: 		if (($a!=0)&&( ($a % $xg_scale) == 0))
287: 			{
288: 			//imagestring($im,1,2,ty(50+($a*5)),"Drawing X Grid ".$a,$c_txt);
289: 			// draw a line
290: 			imageline($im,posx($startx),ty(posy($a)),posx($finishx),ty(posy($a)),$c_grid);
291: //			imageline($im,$lastx,ty($lasty),$x,ty($y),$c_track);
292: 			}
293: 		}
294: 	}
295: 
296: // Grid Y - the time values
297: 
298: if ($draw_y_grid)
299: 	{
300: 	$min=60;
301: 	$hour=$min*60;
302: 	$day=$hour*24;
303: 	
304: 	$syr=date("Y",$startx);
305: 	$smo=date("m",$startx);
306: 	$sda=date("d",$startx);
307: 	$shr=date("H",$startx);
308: 	// h m s mo da yr
309: 	$lhr=mktime($shr,0,0,$smo,$sda,$syr);
310: 	$lda=mktime(0,0,0,$smo,$sda,$syr);
311: 	
312: 	if ($periodx< (($hour*2)+1) )
313: 		{
314: 		$yg_scale=$min;
315: 		$yg_start=$lhr;
316: 		}
317: 	else if ($periodx< (($day*2)+1) )
318: 		{
319: 		$yg_scale=$hour;
320: 		$yg_start=$lhr;
321: 		}
322: 	else if ($periodx < ($day*32) )
323: 		{
324: 		$yg_scale=$day;
325: 		$yg_start=$lda;
326: 		}
327: 	else $draw_y_grid=false;
328: 	}
329: 	
330: if ($draw_y_grid)
331: 	{
332: 	//imagestring($im,1,2,ty(50),"Drawing Y Grid ",$c_txt);
333: 	for ($a=$yg_start; $a<$finishx; $a+=$yg_scale)
334: 		{
335: 		if ($a>$startx)
336: 			{
337: 			// draw line
338: 			imageline($im,posx($a),ty(posy($dlow)),posx($a),ty(posy($dhigh)),$c_grid);
339: 			}
340: 		}
341: 	}
342: function posx($time) // timex
343: {
344: global $xscale,$startx,$xoff;
345: $drawx=$xscale*($time-$startx);
346: $screenx=$drawx+$xoff;
347: $screenx=floor($screenx);
348: return $screenx;
349: }
350: 
351: function posy($value) 
352: {
353: global $yscale,$dlow,$yoff,$dhigh;
354: if ($value>$dhigh) $value=$dhigh;
355: $drawy=$yscale*($value-$dlow);
356: $screeny=$drawy+$yoff;
357: $screeny=floor($screeny);
358: return $screeny;
359: }
360: 
361: 
362: 
363: // show axes scales
364: imagestring($im,1,2,ty(10),$dlow,$c_txt);
365: imagestring($im,1,2,ty($height-18),$dhigh,$c_txt);
366: 
367: 
368: if (isset($_REQUEST['units'])) 
369: 	{
370: 	if (strpos($_REQUEST['units'],"/")===false)
371: 		imagestring($im,1,2,ty($height-28),$_REQUEST['units'],$c_txt);
372: 	
373: 
374: 	else
375: 		{
376: 		$unit_array=explode("/",$_REQUEST['units']);
377: 		$a=0;
378: 		foreach($unit_array as $unit_string)
379: 			{
380: 			imagestring($im,1,2,ty($height-28-($a*8)),$unit_string,$c_txt);
381: 			$a++;
382: 			}
383: 		}
384: 	}	
385: 
386: 
387: // get data and draw
388: $q="SELECT testvalue,alertlevel,recordx FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\" ";
389: $q.="AND recordx>=".ss($startx)." AND recordx<=".ss($finishx)." ORDER BY recordx ASC";
390: //$q.="LIMIT 0,100";
391: $r=$NATS->DB->Query($q);
392: $lastx=0;
393: $lasty=0;
394: 
395: $startval=0;
396: $finishval=0;
397: 
398: while ($row=mysql_fetch_array($r))
399: 	{
400: 	$x=posx($row['recordx']);
401: 	//$y=posy($row['testvalue']); 
402: 	$val=$row['testvalue'];
403: 	if ($val<0) $y=posy(0);
404: 	else $y=posy($val);
405: 	
406: 	if ($row['alertlevel']==-1) $c=$col['grey'];
407: 	else if ($row['alertlevel']==0) $c=$col['lightgreen'];
408: 	else if ($row['alertlevel']==1) $c=$col['orange'];
409: 	else if ($row['alertlevel']==2) $c=$col['red'];
410: 	else $c=$col['black'];
411: 	
412: 	// up lines
413: 	if ($draw_spike==1) imageline($im,$x,ty(0),$x,ty($y),$c);
414: 
415: 	
416: 		
417: 	if ($lastx!=0)
418: 		{
419: 			
420: 		// join-the-dots
421: 		if ($draw_track==1) imageline($im,$lastx,ty($lasty),$x,ty($y),$c_track);
422: 		
423: 		// fill -- DOES NOT WORK
424: 		//if (($draw_fill==1)&&($draw_spike==1)&&($draw_track==1))
425: 			//imagefill($im,$x-1,ty(1),$c);
426: 		
427: 		// bottom line
428: 		if ($draw_under==1)
429: 			{
430: 			if (($row['alertlevel']>0)||
431: 				 (($row['alertlevel']==0)&&($draw_under_pass==1)) )
432: 				{
433: 				imageline($im,$lastx,ty(1),$x,ty(1),$c);
434: 				imageline($im,$lastx,ty(2),$x,ty(2),$c);
435: 				}
436: 			}
437: 		}	
438: 	
439: 	//imageellipse($im,$x,ty($y),1,1,$c_red);
440: 	
441: 	$lastx=$x;
442: 	$lasty=$y;
443: 	
444: 	}
445: mysql_free_result($r);
446: 
447: 	
448: 
449: // output image
450: ob_clean();
451: header("Content-type: image/png");
452: imagepng($im);
453: imagedestroy($im);
454: exit(); // just in case
455: 
456: ?>
457: