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