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