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

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