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

  1: <?php // test.graph.php
  2: ob_start();
  3: require("include.php");
  4: $NATS->Start();
  5: if (!$NATS_Session->Check($NATS->DB))
  6: 	{
  7: 	header("Location: ./?login_msg=Invalid+Or+Expired+Session");
  8: 	exit();
  9: 	}
 10: if ($NATS_Session->userlevel<1) UL_Error("View Test History");
 11: 
 12: 
 13: function ty($y)
 14: {
 15: global $height;
 16: return $height-$y;
 17: }
 18: 
 19: 
 20: // width height suid startx finishx
 21: 
 22: if (isset($_REQUEST['width'])) $width=$_REQUEST['width'];
 23: else $width=600;
 24: if (isset($_REQUEST['height'])) $width=$_REQUEST['height'];
 25: else $height=200;
 26: // start image
 27: $im=@imagecreate($width,$height)
 28: 	or die("Cannot create image");
 29: 
 30: function ierror($t)
 31: {
 32: global $im,$width,$height,$c_red;
 33: header("Content-type: image/png");
 34: imagestring($im,2,($width/2)-20,$height/2,"ERROR: ".$t,$c_red);
 35: imagepng($im);
 36: imagedestroy($im);
 37: exit();
 38: }
 39: 
 40: 
 41: if (!isset($_REQUEST['nodeid'])) ierror("No node ID");
 42: else $nodeid=$_REQUEST['nodeid'];
 43: 
 44: $day=date("d");
 45: $month=date("m");
 46: $year=date("Y");
 47: 
 48: if (isset($_REQUEST['startx'])) $startx=$_REQUEST['startx'];
 49: else
 50: 	{ // 0:00 today    HMS mo da yr
 51: 	$startx=mktime(0,0,0,$month,$day,$year);
 52: 	//$startx=1203451396;
 53: 	}
 54: 	
 55: if (isset($_REQUEST['finishx'])) $finishx=$_REQUEST['finishx'];
 56: else $finishx=mktime(23,59,59,$month,$day,$year);
 57: //$finishx=1203454996;
 58: $periodx=$finishx-$startx;
 59: $startt=date("H:i:s d/m/y",$startx);
 60: $finisht=date("H:i:s d/m/y",$finishx);
 61: 
 62: 
 63: 	
 64: // setup colours
 65: $c_white=imagecolorallocate($im,255,255,255);
 66: $c_black=imagecolorallocate($im,0,0,0);
 67: $c_red=imagecolorallocate($im,250,50,50);
 68: $c_axes=imagecolorallocate($im,200,200,200);
 69: $c_blue=imagecolorallocate($im,50,50,255);
 70: $c_green=imagecolorallocate($im,0,200,0);
 71: $c_orange=imagecolorallocate($im,200,200,0);
 72: 
 73: $c_bg=$c_white;
 74: $c_txt=$c_black;
 75: 
 76: // fill background
 77: imagefill($im,1,1,$c_bg);
 78: 
 79: // titles and stuff
 80: imagestring($im,1,2,2,$startt,$c_txt);
 81: imagestring($im,1,$width-90,2,$finisht,$c_txt);
 82: imagestring($im,2,($width/2)-30,0,$nodeid,$c_txt);
 83: 
 84: // v-axes
 85: imageline($im,50,ty(1),50,ty($height-20),$c_axes);
 86: imageline($im,$width-5,ty(1),$width-5,ty($height-20),$c_axes);
 87: // y-axes
 88: imageline($im,50,ty(1),$width-5,ty(1),$c_axes);
 89: imageline($im,$width-5,ty($height-20),50,ty($height-20),$c_axes);
 90: $xoff=50+1;
 91: $xlen=$width-$xoff-5;
 92: 
 93: $yoff=1;
 94: $ylen=$height-20;
 95: 
 96: // range data
 97: $q="SELECT testvalue FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\" ORDER BY testvalue ASC LIMIT 0,1"; // lowest
 98: $r=$NATS->DB->Query($q);
 99: if (!$row=$NATS->DB->Fetch_Array($r)) ierror("No data for test");
100: $lowest=$row['testvalue'];
101: $dlow=$lowest;
102: if ($dlow>0) $dlow=0;
103: $NATS->DB->Free($r);
104: 
105: $q="SELECT testvalue FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\" ORDER BY testvalue DESC LIMIT 0,1"; //highest
106: $r=$NATS->DB->Query($q);
107: $row=$NATS->DB->Fetch_Array($r);
108: $highest=$row['testvalue'];
109: $dhigh=$highest;
110: $NATS->DB->Free($r);
111: 
112: $drange=$dhigh-$dlow;
113: 
114: // calculate scales
115: $xscale=$xlen/$periodx;
116: if ($drange>0) $yscale=$ylen/$drange;
117: else $yscale=1; // doesn't display but no change!
118: 
119: function posx($time) // timex
120: {
121: global $xscale,$startx,$xoff;
122: $drawx=$xscale*($time-$startx);
123: $screenx=$drawx+$xoff;
124: return $screenx;
125: }
126: 
127: function posy($value) // stock value
128: {
129: global $yscale,$dlow,$yoff;
130: $drawy=$yscale*($value-$dlow);
131: $screeny=$drawy+$yoff;
132: return $screeny;
133: }
134: 
135: 
136: 
137: // show axes scales
138: imagestring($im,1,2,ty(10),$dlow,$c_txt);
139: imagestring($im,1,2,ty($height-18),$dhigh,$c_txt);
140: 
141: 
142: 
143: // get data and draw
144: $q="SELECT testvalue,alertlevel,recordx FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\" ";
145: $q.="AND recordx>=".$startx." AND recordx<=".$finishx;
146: //$q.="LIMIT 0,100";
147: $r=$NATS->DB->Query($q);
148: $lastx=0;
149: $lasty=0;
150: 
151: $startval=0;
152: $finishval=0;
153: 
154: while ($row=mysql_fetch_array($r))
155: 	{
156: 	$x=posx($row['recordx']);
157: 	$y=posy($row['testvalue']);
158: 	
159: 	
160: 	if ($lastx!=0)
161: 		{
162: 		if ($row['alertlevel']==-1) $c=$c_black;
163: 		else if ($row['alertlevel']==0) $c=$c_green;
164: 		else if ($row['alertlevel']==1) $c=$c_orange;
165: 		else if ($row['alertlevel']==2) $c=$c_red;
166: 		else $c=$c_black;
167: 		imageline($im,$lastx,ty($lasty),$x,ty($y),$c);
168: 		}	
169: 	
170: 	//imageellipse($im,$x,ty($y),1,1,$c_red);
171: 	
172: 	$lastx=$x;
173: 	$lasty=$y;
174: 	
175: 	}
176: mysql_free_result($r);
177: 
178: 	
179: 
180: // output image
181: header("Content-type: image/png");
182: imagepng($im);
183: imagedestroy($im);
184: exit(); // just in case
185: 
186: ?>
187: