File: 1.00.4a/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: 
 56: // start image
 57: $im=@imagecreate($width,$height)
 58: 	or die("Cannot create image");
 59: 
 60: // setup colours
 61: $col=array();
 62: $col['white']=imagecolorallocate($im,255,255,255);
 63: $col['black']=imagecolorallocate($im,0,0,0);
 64: $col['red']=imagecolorallocate($im,250,50,50);
 65: $col['lightgrey']=imagecolorallocate($im,200,200,200);
 66: $col['blue']=imagecolorallocate($im,150,150,255);
 67: $col['green']=imagecolorallocate($im,0,200,0);
 68: $col['lightgreen']=imagecolorallocate($im,150,250,150);
 69: $col['grey']=imagecolorallocate($im,150,150,150);
 70: $col['orange']=imagecolorallocate($im,200,200,0);
 71: 
 72: if (isset($_REQUEST['bgcol'])) $c_bg=$col[$_REQUEST['bgcol']];
 73: else $c_bg=$col['white'];
 74: 
 75: if (isset($_REQUEST['txtcol'])) $c_txt=$col[$_REQUEST['txtcol']];
 76: else $c_txt=$col['black'];
 77: 
 78: if (isset($_REQUEST['axescol'])) $c_axes=$col[$_REQUEST['axescol']];
 79: else $c_axes=$col['lightgrey'];
 80: 
 81: if (isset($_REQUEST['trackcol'])) $c_track=$col[$_REQUEST['trackcol']];
 82: else $c_track=$col['blue'];
 83: 
 84: // fill background
 85: imagefill($im,1,1,$c_bg);
 86: 	
 87: function ierror($t)
 88: {
 89: global $im,$width,$height,$col;
 90: ob_clean();
 91: header("Content-type: image/png");
 92: imagestring($im,2,($width/2)-20,$height/2,"ERROR: ".$t,$col['red']);
 93: imagepng($im);
 94: imagedestroy($im);
 95: exit();
 96: }
 97: 
 98: //ierror("Test");
 99: 
100: $session=$NATS_Session->Check($NATS->DB);
101: 
102: if (!$session)
103: 	{
104: 	if ($NATS->Cfg->Get("site.graph.public",0)!=1)
105: 		{
106: 		ierror("Authorisation Failure");
107: 		exit();
108: 		}
109: 	$key=$NATS->Cfg->Get("site.graph.key","");
110: 	if (isset($_REQUEST['graphkey'])) $userkey=$_REQUEST['graphkey'];
111: 	else $userkey="";
112: 	
113: 	if ( ($key!="") && ($key!=$userkey) )
114: 		{
115: 		ierror("Graph Key Failure");
116: 		exit();
117: 		}
118: 	}
119: 
120: 
121: if (!isset($_REQUEST['nodeid'])) $nodeid="";
122: else $nodeid=$_REQUEST['nodeid'];
123: 
124: if (!isset($_REQUEST['testid'])) ierror("No test ID");
125: 
126: $day=date("d");
127: $month=date("m");
128: $year=date("Y");
129: 
130: if (isset($_REQUEST['startx'])) $startx=$_REQUEST['startx'];
131: else
132: 	{ // 0:00 today    HMS mo da yr
133: 	$startx=mktime(0,0,0,$month,$day,$year);
134: 	//$startx=1203451396;
135: 	}
136: 	
137: if (isset($_REQUEST['finishx'])) $finishx=$_REQUEST['finishx'];
138: else $finishx=mktime(23,59,59,$month,$day,$year);
139: 
140: if ($startx<=0) $startx=time()+$startx;
141: if ($finishx<=0) $finishx=time()+$finishx;
142: 
143: //$finishx=1203454996;
144: $periodx=$finishx-$startx;
145: $startt=date("H:i:s d/m/y",$startx);
146: $finisht=date("H:i:s d/m/y",$finishx);
147: 
148: 
149: // titles and stuff
150: imagestring($im,2,2,2,$startt,$c_txt);
151: // -90 for size 1
152: imagestring($im,2,$width-108,2,$finisht,$c_txt);
153: 
154: if (isset($_REQUEST['title'])) $title=$_REQUEST['title'];
155: else $title=$nodeid;
156: $len=strlen($title)*4;
157: imagestring($im,4,($width/2)-$len,2,$title,$c_txt);
158: 
159: // offsets and lengths
160: $xoff=50+1;
161: $xlen=$width-$xoff-5;
162: 
163: $yoff=1;
164: $ylen=$height-$yoff-20;
165: 
166: 
167: // v-axes
168: imageline($im,50,ty(1),50,ty($height-20),$c_axes);
169: imageline($im,$width-5,ty(1),$width-5,ty($height-20),$c_axes);
170: // y-axes
171: imageline($im,50,ty(1),$width-5,ty(1),$c_axes);
172: imageline($im,$width-5,ty($height-20),50,ty($height-20),$c_axes);
173: //ierror("hello");
174: 
175: 
176: // range data
177: 
178: // Lowest
179: 
180: if (isset($_REQUEST['rangemin'])) $dlow=$_REQUEST['rangemin'];
181: else
182: 	{
183: 	$dlow=0;
184: 	/*
185: 	$q="SELECT testvalue FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\"";
186: 	$q.=" AND recordx>=".ss($startx)." AND recordx<=".ss($finishx)." ";
187: 	$q.="ORDER BY testvalue ASC LIMIT 0,1"; // lowest
188: 	$r=$NATS->DB->Query($q);
189: 	//ierror($q);
190: 	if (!$row=$NATS->DB->Fetch_Array($r)) ierror("No data for test");
191: 	$lowest=$row['testvalue'];
192: 	$dlow=$lowest;
193: 	if ($dlow>0) $dlow=0;
194: 	$NATS->DB->Free($r);
195: 	*/
196: 	}
197: 
198: 
199: // Highest
200: 
201: if (isset($_REQUEST['rangemax'])) $dhigh=$_REQUEST['rangemax'];
202: else
203: {
204: 	$q="SELECT testvalue FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\"";
205: 	$q.=" AND recordx>=".ss($startx)." AND recordx<=".ss($finishx)." ";
206: 	$q.= "ORDER BY testvalue DESC LIMIT 0,1"; //highest
207: 	
208: 	$r=$NATS->DB->Query($q);
209: 	$row=$NATS->DB->Fetch_Array($r);
210: 	$highest=$row['testvalue'];
211: 	$dhigh=$highest;
212: 	$NATS->DB->Free($r);
213: 	if (isset($_REQUEST['rangemaxmin']))
214: 		{
215: 		if ($dhigh<$_REQUEST['rangemaxmin']) $dhigh=$_REQUEST['rangemaxmin'];
216: 		}
217: }
218: 
219: $drange=$dhigh-$dlow;
220: 
221: // calculate scales
222: $xscale=$xlen/$periodx;
223: if ($drange>0) $yscale=$ylen/$drange;
224: else $yscale=1; // doesn't display but no change!
225: 
226: function posx($time) // timex
227: {
228: global $xscale,$startx,$xoff;
229: $drawx=$xscale*($time-$startx);
230: $screenx=$drawx+$xoff;
231: return $screenx;
232: }
233: 
234: function posy($value) 
235: {
236: global $yscale,$dlow,$yoff,$dhigh;
237: if ($value>$dhigh) $value=$dhigh;
238: $drawy=$yscale*($value-$dlow);
239: $screeny=$drawy+$yoff;
240: return $screeny;
241: }
242: 
243: 
244: 
245: // show axes scales
246: imagestring($im,1,2,ty(10),$dlow,$c_txt);
247: imagestring($im,1,2,ty($height-18),$dhigh,$c_txt);
248: 
249: 
250: if (isset($_REQUEST['units'])) 
251: 	{
252: 	if (strpos($_REQUEST['units'],"/")===false)
253: 		imagestring($im,1,2,ty($height-28),$_REQUEST['units'],$c_txt);
254: 	
255: 
256: 	else
257: 		{
258: 		$unit_array=explode("/",$_REQUEST['units']);
259: 		$a=0;
260: 		foreach($unit_array as $unit_string)
261: 			{
262: 			imagestring($im,1,2,ty($height-28-($a*8)),$unit_string,$c_txt);
263: 			$a++;
264: 			}
265: 		}
266: 	}	
267: 
268: 
269: // get data and draw
270: $q="SELECT testvalue,alertlevel,recordx FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\" ";
271: $q.="AND recordx>=".ss($startx)." AND recordx<=".ss($finishx)." ORDER BY recordx ASC";
272: //$q.="LIMIT 0,100";
273: $r=$NATS->DB->Query($q);
274: $lastx=0;
275: $lasty=0;
276: 
277: $startval=0;
278: $finishval=0;
279: 
280: while ($row=mysql_fetch_array($r))
281: 	{
282: 	$x=posx($row['recordx']);
283: 	//$y=posy($row['testvalue']); 
284: 	$val=$row['testvalue'];
285: 	if ($val<0) $y=posy(0);
286: 	else $y=posy($val);
287: 	
288: 	if ($row['alertlevel']==-1) $c=$col['grey'];
289: 	else if ($row['alertlevel']==0) $c=$col['lightgreen'];
290: 	else if ($row['alertlevel']==1) $c=$col['orange'];
291: 	else if ($row['alertlevel']==2) $c=$col['red'];
292: 	else $c=$col['black'];
293: 	
294: 	// up lines
295: 	if ($draw_spike==1) imageline($im,$x,ty(0),$x,ty($y),$c);
296: 
297: 	
298: 		
299: 	if ($lastx!=0)
300: 		{
301: 			
302: 		// join-the-dots
303: 		if ($draw_track==1) imageline($im,$lastx,ty($lasty),$x,ty($y),$c_track);
304: 		
305: 		// fill -- DOES NOT WORK
306: 		//if (($draw_fill==1)&&($draw_spike==1)&&($draw_track==1))
307: 			//imagefill($im,$x-1,ty(1),$c);
308: 		
309: 		// bottom line
310: 		if ($draw_under==1)
311: 			{
312: 			if (($row['alertlevel']>0)||
313: 				 (($row['alertlevel']==0)&&($draw_under_pass==1)) )
314: 				{
315: 				imageline($im,$lastx,ty(1),$x,ty(1),$c);
316: 				imageline($im,$lastx,ty(2),$x,ty(2),$c);
317: 				}
318: 			}
319: 		}	
320: 	
321: 	//imageellipse($im,$x,ty($y),1,1,$c_red);
322: 	
323: 	$lastx=$x;
324: 	$lasty=$y;
325: 	
326: 	}
327: mysql_free_result($r);
328: 
329: 	
330: 
331: // output image
332: ob_clean();
333: header("Content-type: image/png");
334: imagepng($im);
335: imagedestroy($im);
336: exit(); // just in case
337: 
338: ?>
339: