File: 0.02.59a/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: 
 37: // width height suid startx finishx
 38: 
 39: if (isset($_REQUEST['width'])) $width=$_REQUEST['width'];
 40: else $width=700;
 41: if (isset($_REQUEST['height'])) $width=$_REQUEST['height'];
 42: else $height=150;
 43: 
 44: // other incoming stuff
 45: if (isset($_REQUEST['draw_spike'])) $draw_spike=$_REQUEST['draw_spike'];
 46: else $draw_spike=1;
 47: if (isset($_REQUEST['draw_track'])) $draw_spike=$_REQUEST['draw_track'];
 48: else $draw_track=1;
 49: if (isset($_REQUEST['draw_under'])) $draw_spike=$_REQUEST['draw_under'];
 50: else $draw_under=1;
 51: 
 52: 
 53: // start image
 54: $im=@imagecreate($width,$height)
 55: 	or die("Cannot create image");
 56: 
 57: // setup colours
 58: $c_white=imagecolorallocate($im,255,255,255);
 59: $c_black=imagecolorallocate($im,0,0,0);
 60: $c_red=imagecolorallocate($im,250,50,50);
 61: $c_axes=imagecolorallocate($im,200,200,200);
 62: $c_blue=imagecolorallocate($im,150,150,255);
 63: $c_green=imagecolorallocate($im,0,200,0);
 64: $c_lightgreen=imagecolorallocate($im,150,250,150);
 65: $c_grey=imagecolorallocate($im,150,150,150);
 66: $c_orange=imagecolorallocate($im,200,200,0);
 67: 
 68: $c_bg=$c_white;
 69: $c_txt=$c_black;
 70: 
 71: // fill background
 72: imagefill($im,1,1,$c_bg);
 73: 	
 74: function ierror($t)
 75: {
 76: global $im,$width,$height,$c_red;
 77: header("Content-type: image/png");
 78: imagestring($im,2,($width/2)-20,$height/2,"ERROR: ".$t,$c_red);
 79: imagepng($im);
 80: imagedestroy($im);
 81: exit();
 82: }
 83: 
 84: if (!$NATS_Session->Check($NATS->DB))
 85: 	{
 86: 	ierror("Authorisation Failure");
 87: 	exit();
 88: 	}
 89: if ($NATS_Session->userlevel<1) ierror("User Disabled");
 90: 
 91: 
 92: if (!isset($_REQUEST['nodeid'])) ierror("No node ID");
 93: else $nodeid=$_REQUEST['nodeid'];
 94: 
 95: if (!isset($_REQUEST['testid'])) ierror("No test ID");
 96: 
 97: $day=date("d");
 98: $month=date("m");
 99: $year=date("Y");
100: 
101: if (isset($_REQUEST['startx'])) $startx=$_REQUEST['startx'];
102: else
103: 	{ // 0:00 today    HMS mo da yr
104: 	$startx=mktime(0,0,0,$month,$day,$year);
105: 	//$startx=1203451396;
106: 	}
107: 	
108: if (isset($_REQUEST['finishx'])) $finishx=$_REQUEST['finishx'];
109: else $finishx=mktime(23,59,59,$month,$day,$year);
110: //$finishx=1203454996;
111: $periodx=$finishx-$startx;
112: $startt=date("H:i:s d/m/y",$startx);
113: $finisht=date("H:i:s d/m/y",$finishx);
114: 
115: 
116: // titles and stuff
117: imagestring($im,2,2,2,$startt,$c_txt);
118: // -90 for size 1
119: imagestring($im,2,$width-108,2,$finisht,$c_txt);
120: imagestring($im,4,($width/2)-40,2,$nodeid,$c_txt);
121: 
122: // offsets and lengths
123: $xoff=50+1;
124: $xlen=$width-$xoff-5;
125: 
126: $yoff=1;
127: $ylen=$height-$yoff-20;
128: 
129: 
130: // v-axes
131: imageline($im,50,ty(1),50,ty($height-20),$c_axes);
132: imageline($im,$width-5,ty(1),$width-5,ty($height-20),$c_axes);
133: // y-axes
134: imageline($im,50,ty(1),$width-5,ty(1),$c_axes);
135: imageline($im,$width-5,ty($height-20),50,ty($height-20),$c_axes);
136: //ierror("hello");
137: 
138: // range data
139: $q="SELECT testvalue FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\"";
140: $q.=" AND recordx>=".ss($startx)." AND recordx<=".ss($finishx)." ";
141: $q.="ORDER BY testvalue ASC LIMIT 0,1"; // lowest
142: $r=$NATS->DB->Query($q);
143: //ierror($q);
144: if (!$row=$NATS->DB->Fetch_Array($r)) ierror("No data for test");
145: $lowest=$row['testvalue'];
146: $dlow=$lowest;
147: if ($dlow>0) $dlow=0;
148: $NATS->DB->Free($r);
149: 
150: $q="SELECT testvalue FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\"";
151: 
152: $q.=" AND recordx>=".ss($startx)." AND recordx<=".ss($_REQUEST['finishx'])." ";
153: 
154: $q.= "ORDER BY testvalue DESC LIMIT 0,1"; //highest
155: 
156: $r=$NATS->DB->Query($q);
157: $row=$NATS->DB->Fetch_Array($r);
158: $highest=$row['testvalue'];
159: $dhigh=$highest;
160: $NATS->DB->Free($r);
161: 
162: $drange=$dhigh-$dlow;
163: 
164: // calculate scales
165: $xscale=$xlen/$periodx;
166: if ($drange>0) $yscale=$ylen/$drange;
167: else $yscale=1; // doesn't display but no change!
168: 
169: function posx($time) // timex
170: {
171: global $xscale,$startx,$xoff;
172: $drawx=$xscale*($time-$startx);
173: $screenx=$drawx+$xoff;
174: return $screenx;
175: }
176: 
177: function posy($value) // stock value
178: {
179: global $yscale,$dlow,$yoff;
180: $drawy=$yscale*($value-$dlow);
181: $screeny=$drawy+$yoff;
182: return $screeny;
183: }
184: 
185: 
186: 
187: // show axes scales
188: imagestring($im,1,2,ty(10),$dlow,$c_txt);
189: imagestring($im,1,2,ty($height-18),$dhigh,$c_txt);
190: 
191: 
192: 
193: // get data and draw
194: $q="SELECT testvalue,alertlevel,recordx FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\" ";
195: $q.="AND recordx>=".ss($startx)." AND recordx<=".ss($finishx)." ORDER BY recordx ASC";
196: //$q.="LIMIT 0,100";
197: $r=$NATS->DB->Query($q);
198: $lastx=0;
199: $lasty=0;
200: 
201: $startval=0;
202: $finishval=0;
203: 
204: while ($row=mysql_fetch_array($r))
205: 	{
206: 	$x=posx($row['recordx']);
207: 	$y=posy($row['testvalue']);
208: 	
209: 	if ($row['alertlevel']==-1) $c=$c_grey;
210: 	else if ($row['alertlevel']==0) $c=$c_lightgreen;
211: 	else if ($row['alertlevel']==1) $c=$c_orange;
212: 	else if ($row['alertlevel']==2) $c=$c_red;
213: 	else $c=$c_black;
214: 	
215: 	// up lines
216: 	if ($draw_spike==1) imageline($im,$x,ty(0),$x,ty($y),$c);
217: 
218: 
219: 	if ($lastx!=0)
220: 		{	
221: 		// join-the-dots
222: 		if ($draw_track==1) imageline($im,$lastx,ty($lasty),$x,ty($y),$c_blue);
223: 		
224: 		// bottom line
225: 		if ($draw_under==1)
226: 			{
227: 			if ($row['alertlevel']>0)
228: 				{
229: 				imageline($im,$lastx,ty(1),$x,ty(1),$c);
230: 				imageline($im,$lastx,ty(2),$x,ty(2),$c);
231: 				}
232: 			}
233: 		
234: 		}	
235: 	
236: 	//imageellipse($im,$x,ty($y),1,1,$c_red);
237: 	
238: 	$lastx=$x;
239: 	$lasty=$y;
240: 	
241: 	}
242: mysql_free_result($r);
243: 
244: 	
245: 
246: // output image
247: header("Content-type: image/png");
248: imagepng($im);
249: imagedestroy($im);
250: exit(); // just in case
251: 
252: ?>
253: