File:
0.04.26a/server/web/test.graph.php (
View as Code)
1: 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: //if (!isset($_REQUEST['testid'])) $_REQUEST['testid']="L26";
37:
38: // width height suid startx finishx
39:
40: if (isset($_REQUEST['width'])) $width=$_REQUEST['width'];
41: else $width=700;
42: if (isset($_REQUEST['height'])) $height=$_REQUEST['height'];
43: else $height=150;
44:
45: // other incoming stuff
46: if (isset($_REQUEST['draw_spike'])) $draw_spike=$_REQUEST['draw_spike'];
47: else $draw_spike=1;
48: if (isset($_REQUEST['draw_track'])) $draw_track=$_REQUEST['draw_track'];
49: else $draw_track=1;
50: if (isset($_REQUEST['draw_under'])) $draw_under=$_REQUEST['draw_under'];
51: else $draw_under=1;
52: if (isset($_REQUEST['draw_under_pass'])) $draw_under=$_REQUEST['draw_under_pass'];
53: else $draw_under_pass=0;
54:
55:
56: // start image
57: $im=@imagecreate($width,$height)
58: or die("Cannot create image");
59:
60: // setup colours
61: $col=array();
62: $col['white']=imagecolorallocate($im,255,255,255);
63: $col['black']=imagecolorallocate($im,0,0,0);
64: $col['red']=imagecolorallocate($im,250,50,50);
65: $col['lightgrey']=imagecolorallocate($im,200,200,200);
66: $col['blue']=imagecolorallocate($im,150,150,255);
67: $col['green']=imagecolorallocate($im,0,200,0);
68: $col['lightgreen']=imagecolorallocate($im,150,250,150);
69: $col['grey']=imagecolorallocate($im,150,150,150);
70: $col['orange']=imagecolorallocate($im,200,200,0);
71:
72: if (isset($_REQUEST['bgcol'])) $c_bg=$col[$_REQUEST['bgcol']];
73: else $c_bg=$col['white'];
74:
75: if (isset($_REQUEST['txtcol'])) $c_txt=$col[$_REQUEST['txtcol']];
76: else $c_txt=$col['black'];
77:
78: if (isset($_REQUEST['axescol'])) $c_axes=$col[$_REQUEST['axescol']];
79: else $c_axes=$col['lightgrey'];
80:
81: if (isset($_REQUEST['trackcol'])) $c_track=$col[$_REQUEST['trackcol']];
82: else $c_track=$col['blue'];
83:
84: // fill background
85: imagefill($im,1,1,$c_bg);
86:
87: function ierror($t)
88: {
89: global $im,$width,$height,$col;
90: header("Content-type: image/png");
91: imagestring($im,2,($width/2)-20,$height/2,"ERROR: ".$t,$col['red']);
92: imagepng($im);
93: imagedestroy($im);
94: exit();
95: }
96:
97: //ierror("Test");
98:
99: $session=$NATS_Session->Check($NATS->DB);
100:
101: if (!$session)
102: {
103: if ($NATS->Cfg->Get("site.graph.public",0)!=1)
104: {
105: ierror("Authorisation Failure");
106: exit();
107: }
108: $key=$NATS->Cfg->Get("site.graph.key","");
109: if (isset($_REQUEST['graphkey'])) $userkey=$_REQUEST['graphkey'];
110: else $userkey="";
111:
112: if ( ($key!="") && ($key!=$userkey) )
113: {
114: ierror("Graph Key Failure");
115: exit();
116: }
117: }
118:
119:
120: if (!isset($_REQUEST['nodeid'])) $nodeid="";
121: else $nodeid=$_REQUEST['nodeid'];
122:
123: if (!isset($_REQUEST['testid'])) ierror("No test ID");
124:
125: $day=date("d");
126: $month=date("m");
127: $year=date("Y");
128:
129: if (isset($_REQUEST['startx'])) $startx=$_REQUEST['startx'];
130: else
131: { // 0:00 today HMS mo da yr
132: $startx=mktime(0,0,0,$month,$day,$year);
133: //$startx=1203451396;
134: }
135:
136: if (isset($_REQUEST['finishx'])) $finishx=$_REQUEST['finishx'];
137: else $finishx=mktime(23,59,59,$month,$day,$year);
138:
139: if ($startx<=0) $startx=time()+$startx;
140: if ($finishx<=0) $finishx=time()+$finishx;
141:
142: //$finishx=1203454996;
143: $periodx=$finishx-$startx;
144: $startt=date("H:i:s d/m/y",$startx);
145: $finisht=date("H:i:s d/m/y",$finishx);
146:
147:
148: // titles and stuff
149: imagestring($im,2,2,2,$startt,$c_txt);
150: // -90 for size 1
151: imagestring($im,2,$width-108,2,$finisht,$c_txt);
152:
153: if (isset($_REQUEST['title'])) $title=$_REQUEST['title'];
154: else $title=$nodeid;
155: $len=strlen($title)*4;
156: imagestring($im,4,($width/2)-$len,2,$title,$c_txt);
157:
158: // offsets and lengths
159: $xoff=50+1;
160: $xlen=$width-$xoff-5;
161:
162: $yoff=1;
163: $ylen=$height-$yoff-20;
164:
165:
166: // v-axes
167: imageline($im,50,ty(1),50,ty($height-20),$c_axes);
168: imageline($im,$width-5,ty(1),$width-5,ty($height-20),$c_axes);
169: // y-axes
170: imageline($im,50,ty(1),$width-5,ty(1),$c_axes);
171: imageline($im,$width-5,ty($height-20),50,ty($height-20),$c_axes);
172: //ierror("hello");
173:
174:
175: // range data
176:
177: // Lowest
178:
179: if (isset($_REQUEST['rangemin'])) $dlow=$_REQUEST['rangemin'];
180: else
181: {
182: $dlow=0;
183: /*
184: $q="SELECT testvalue FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\"";
185: $q.=" AND recordx>=".ss($startx)." AND recordx<=".ss($finishx)." ";
186: $q.="ORDER BY testvalue ASC LIMIT 0,1"; // lowest
187: $r=$NATS->DB->Query($q);
188: //ierror($q);
189: if (!$row=$NATS->DB->Fetch_Array($r)) ierror("No data for test");
190: $lowest=$row['testvalue'];
191: $dlow=$lowest;
192: if ($dlow>0) $dlow=0;
193: $NATS->DB->Free($r);
194: */
195: }
196:
197:
198: // Highest
199:
200: if (isset($_REQUEST['rangemax'])) $dhigh=$_REQUEST['rangemax'];
201: else
202: {
203: $q="SELECT testvalue FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\"";
204: $q.=" AND recordx>=".ss($startx)." AND recordx<=".ss($finishx)." ";
205: $q.= "ORDER BY testvalue DESC LIMIT 0,1"; //highest
206:
207: $r=$NATS->DB->Query($q);
208: $row=$NATS->DB->Fetch_Array($r);
209: $highest=$row['testvalue'];
210: $dhigh=$highest;
211: $NATS->DB->Free($r);
212: if (isset($_REQUEST['rangemaxmin']))
213: {
214: if ($dhigh<$_REQUEST['rangemaxmin']) $dhigh=$_REQUEST['rangemaxmin'];
215: }
216: }
217:
218: $drange=$dhigh-$dlow;
219:
220: // calculate scales
221: $xscale=$xlen/$periodx;
222: if ($drange>0) $yscale=$ylen/$drange;
223: else $yscale=1; // doesn't display but no change!
224:
225: function posx($time) // timex
226: {
227: global $xscale,$startx,$xoff;
228: $drawx=$xscale*($time-$startx);
229: $screenx=$drawx+$xoff;
230: return $screenx;
231: }
232:
233: function posy($value)
234: {
235: global $yscale,$dlow,$yoff,$dhigh;
236: if ($value>$dhigh) $value=$dhigh;
237: $drawy=$yscale*($value-$dlow);
238: $screeny=$drawy+$yoff;
239: return $screeny;
240: }
241:
242:
243:
244: // show axes scales
245: imagestring($im,1,2,ty(10),$dlow,$c_txt);
246: imagestring($im,1,2,ty($height-18),$dhigh,$c_txt);
247:
248:
249: if (isset($_REQUEST['units']))
250: {
251: if (strpos($_REQUEST['units'],"/")===false)
252: imagestring($im,1,2,ty($height-28),$_REQUEST['units'],$c_txt);
253:
254:
255: else
256: {
257: $unit_array=explode("/",$_REQUEST['units']);
258: $a=0;
259: foreach($unit_array as $unit_string)
260: {
261: imagestring($im,1,2,ty($height-28-($a*8)),$unit_string,$c_txt);
262: $a++;
263: }
264: }
265: }
266:
267:
268: // get data and draw
269: $q="SELECT testvalue,alertlevel,recordx FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\" ";
270: $q.="AND recordx>=".ss($startx)." AND recordx<=".ss($finishx)." ORDER BY recordx ASC";
271: //$q.="LIMIT 0,100";
272: $r=$NATS->DB->Query($q);
273: $lastx=0;
274: $lasty=0;
275:
276: $startval=0;
277: $finishval=0;
278:
279: while ($row=mysql_fetch_array($r))
280: {
281: $x=posx($row['recordx']);
282: //$y=posy($row['testvalue']);
283: $val=$row['testvalue'];
284: if ($val<0) $y=posy(0);
285: else $y=posy($val);
286:
287: if ($row['alertlevel']==-1) $c=$col['grey'];
288: else if ($row['alertlevel']==0) $c=$col['lightgreen'];
289: else if ($row['alertlevel']==1) $c=$col['orange'];
290: else if ($row['alertlevel']==2) $c=$col['red'];
291: else $c=$col['black'];
292:
293: // up lines
294: if ($draw_spike==1) imageline($im,$x,ty(0),$x,ty($y),$c);
295:
296:
297:
298: if ($lastx!=0)
299: {
300:
301: // join-the-dots
302: if ($draw_track==1) imageline($im,$lastx,ty($lasty),$x,ty($y),$c_track);
303:
304: // fill -- DOES NOT WORK
305: //if (($draw_fill==1)&&($draw_spike==1)&&($draw_track==1))
306: //imagefill($im,$x-1,ty(1),$c);
307:
308: // bottom line
309: if ($draw_under==1)
310: {
311: if (($row['alertlevel']>0)||
312: (($row['alertlevel']==0)&&($draw_under_pass==1)) )
313: {
314: imageline($im,$lastx,ty(1),$x,ty(1),$c);
315: imageline($im,$lastx,ty(2),$x,ty(2),$c);
316: }
317: }
318: }
319:
320: //imageellipse($im,$x,ty($y),1,1,$c_red);
321:
322: $lastx=$x;
323: $lasty=$y;
324:
325: }
326: mysql_free_result($r);
327:
328:
329:
330: // output image
331: header("Content-type: image/png");
332: imagepng($im);
333: imagedestroy($im);
334: exit(); // just in case
335:
336: ?>
337: