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