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