]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
94ff9470 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
fbd133bb | 3 | * 1998, 2011 |
25697a35 GS |
4 | * |
5 | * SARG donations: | |
6 | * please look at http://sarg.sourceforge.net/donations.php | |
1164c474 FM |
7 | * Support: |
8 | * http://sourceforge.net/projects/sarg/forums/forum/363374 | |
25697a35 GS |
9 | * --------------------------------------------------------------------- |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
24 | * | |
25 | */ | |
26 | ||
27 | #include "include/conf.h" | |
5f3cfd1d | 28 | #include "include/defs.h" |
25697a35 | 29 | |
a1de61fe | 30 | #if defined(HAVE_GD) |
17c89803 | 31 | |
a1de61fe | 32 | #if defined(HAVE_ICONV_H) && defined(gdFTEX_Unicode) |
25697a35 | 33 | #include <iconv.h> |
e43854ef FM |
34 | #define USE_ICONV 1 |
35 | #endif | |
25697a35 | 36 | |
e43854ef | 37 | struct GraphDataStruct |
25697a35 | 38 | { |
9bd92830 FM |
39 | int lavender; |
40 | int darkblue; | |
41 | int dimgray; | |
42 | int goldenrod; | |
43 | int goldenrod2; | |
44 | int gray; | |
45 | int silver; | |
46 | int black; | |
47 | //! The color of the top side of a graph bar. | |
48 | int color1; | |
49 | //! The color of the right side of a graph bar. | |
50 | int color2; | |
51 | //! The color of the front side of a graph bar. | |
52 | int color3; | |
53 | //! The libgd image we are drawing on. | |
54 | gdImage *im; | |
55 | //! An allocated buffer to convert the string into UTF-8. | |
56 | char *string; | |
57 | //! The number of bytes allocated for the string buffer. | |
58 | size_t string_size; | |
59 | //! The bottom border of the graph. | |
60 | int BottomGraph; | |
61 | //! The top border of the graph. | |
62 | int TopGraph; | |
63 | //! The left border of the graph. | |
64 | int LeftGraph; | |
65 | //! The right border of the graph. | |
66 | int RightGraph; | |
67 | //! The height at which the bottom depth border of the graph extends. | |
68 | int BottomDepth; | |
69 | //! The distance between two ticks on the horizontal axis. | |
70 | double XScale; | |
71 | //! The distance between two ticks on the vertical axis. | |
72 | double YScale; | |
73 | //! The exterior length of a tick on the scales. | |
74 | int TickLength; | |
75 | //! The distance, in pixels, between two ticks along the Y axis. | |
76 | int YTickSpace; | |
286f212b FM |
77 | }; |
78 | ||
79 | enum PlotType | |
80 | { | |
9bd92830 FM |
81 | PTG_LinBin, |
82 | PTG_LogBin, | |
83 | PTG_Time, | |
286f212b FM |
84 | }; |
85 | ||
86 | struct PlotStruct | |
87 | { | |
9bd92830 FM |
88 | //! The data points to plot. |
89 | long long int *datapoints; | |
90 | //! The number of points to plot. | |
91 | int npoints; | |
92 | //! The minimum data to plot along the Y axis. | |
93 | long long int ymin; | |
94 | //! The maximum data to plot along the Y axis. | |
95 | long long int ymax; | |
96 | //! The type of Y axis to draw. | |
97 | enum PlotType ytype; | |
98 | //! The label to write on the X axis. | |
99 | const char *XLabel; | |
100 | //! The label to write on the Y axis. | |
101 | const char *YLabel; | |
102 | //! The name of the output PNG file. | |
103 | const char *pngfile; | |
e43854ef FM |
104 | }; |
105 | ||
106 | enum TextRefPos | |
107 | { | |
9bd92830 FM |
108 | TRP_TopLeft, |
109 | TRP_TopCenter, | |
110 | TRP_TopRight, | |
111 | TRP_BottomLeft, | |
112 | TRP_BottomCenter, | |
113 | TRP_BottomRight, | |
114 | TRP_CenterLeft, | |
115 | TRP_Center, | |
116 | TRP_CenterRight, | |
e43854ef FM |
117 | }; |
118 | ||
1f482a8d FM |
119 | #ifdef USE_ICONV |
120 | //! The iconv object to convert the text from the locale character set to UTF-8. | |
121 | iconv_t localtoutf=(iconv_t)-1; | |
122 | #endif | |
123 | ||
124 | static void Sarg_gdImageStringFT (struct GraphDataStruct *gdata, int fg, char *fontlist, | |
007905af | 125 | double ptsize, double angle, int x, int y, const char *string,enum TextRefPos RefPos) |
e43854ef | 126 | { |
9bd92830 FM |
127 | char *sstring; |
128 | char *retval; | |
129 | int brect[8]; | |
130 | int minx,miny,maxx,maxy; | |
131 | int i; | |
e43854ef FM |
132 | |
133 | #ifdef USE_ICONV | |
9bd92830 FM |
134 | if (localtoutf!=(iconv_t)-1) { |
135 | const char *str; | |
136 | char *sstr; | |
137 | size_t slen, sslen; | |
138 | ||
139 | slen = strlen(string) + 1; // We must include string termination character | |
140 | sslen = slen * 3; // We assume that the UTF8 string will not be bigger than 3 times the original size. | |
141 | if (sslen>gdata->string_size) { | |
142 | sstring = (char *)realloc(gdata->string,sslen); | |
143 | if (!sstring) { | |
144 | debuga(_("realloc error (%"PRIu64" bytes required)\n"),(uint64_t)sslen); | |
145 | exit(EXIT_FAILURE); | |
146 | } | |
147 | gdata->string=(char *)sstring; | |
148 | gdata->string_size=sslen; | |
149 | } else { | |
150 | sstring=gdata->string; | |
151 | sslen=gdata->string_size; | |
152 | } | |
153 | ||
154 | str = string; | |
155 | sstr = sstring; | |
156 | if (iconv (localtoutf, (ICONV_CONST char **)&str, &slen, &sstr, &sslen)==-1) { | |
157 | debuga(_("(grepday) iconv failed to convert string \"%s\" from %s to UTF-8 - %s\n"),string,CharSet,strerror(errno)); | |
158 | sstring=(char *)string; //show something sensible on the graph | |
159 | } | |
160 | } else { | |
161 | sstring=(char *)string; //show something sensible on the graph | |
162 | } | |
e43854ef | 163 | #else |
9bd92830 | 164 | sstring=(char *)string; |
e43854ef FM |
165 | #endif |
166 | ||
9bd92830 FM |
167 | if (RefPos!=TRP_BottomLeft) { |
168 | retval = gdImageStringFTEx (NULL, brect, fg, fontlist, ptsize, angle, 0, 0, sstring, gdFTEX_Unicode); | |
169 | if (retval) { | |
170 | debuga(_("libgd failed to calculate the bounding box of the text \"%s\": %s\n"),sstring,retval); | |
171 | exit(EXIT_FAILURE); | |
172 | } | |
173 | /* | |
174 | From libgd documentation, brect contains this without taking into account the angle: | |
175 | 0 lower left corner, X position | |
176 | 1 lower left corner, Y position | |
177 | 2 lower right corner, X position | |
178 | 3 lower right corner, Y position | |
179 | 4 upper right corner, X position | |
180 | 5 upper right corner, Y position | |
181 | 6 upper left corner, X position | |
182 | 7 upper left corner, Y position | |
183 | */ | |
184 | minx=maxx=brect[0]; | |
185 | miny=maxy=brect[1]; | |
186 | for (i=2 ; i<7 ; i+=2) { | |
187 | if (minx>brect[i]) minx=brect[i]; | |
188 | if (maxx<brect[i]) maxx=brect[i]; | |
189 | if (miny>brect[i+1]) miny=brect[i+1]; | |
190 | if (maxy<brect[i+1]) maxy=brect[i+1]; | |
191 | } | |
192 | } | |
193 | ||
194 | switch (RefPos) | |
195 | { | |
196 | case TRP_TopLeft: | |
197 | y-=miny; | |
198 | break; | |
199 | ||
200 | case TRP_TopCenter: | |
201 | x-=(maxx-minx)/2; | |
202 | y-=miny; | |
203 | break; | |
204 | ||
205 | case TRP_TopRight: | |
206 | x-=maxx; | |
207 | y-=miny; | |
208 | break; | |
209 | ||
210 | case TRP_BottomLeft: | |
211 | break; | |
212 | ||
213 | case TRP_BottomCenter: | |
214 | x-=(maxx-minx)/2; | |
215 | break; | |
216 | ||
217 | case TRP_BottomRight: | |
218 | x-=maxx; | |
219 | break; | |
220 | ||
221 | case TRP_Center: | |
222 | x-=(maxx-minx)/2; | |
223 | y+=(maxy-miny)/2; | |
224 | break; | |
225 | ||
226 | case TRP_CenterLeft: | |
227 | y+=(maxy-miny)/2; | |
228 | break; | |
229 | ||
230 | case TRP_CenterRight: | |
231 | x-=maxx; | |
232 | y+=(maxy-miny)/2; | |
233 | break; | |
234 | } | |
235 | retval = gdImageStringFTEx (gdata->im, brect, fg, fontlist, ptsize, angle, x, y, sstring, gdFTEX_Unicode); | |
236 | if (retval) { | |
237 | debuga(_("libgd failed to render the text \"%s\": %s\n"),sstring,retval); | |
238 | exit(EXIT_FAILURE); | |
239 | } | |
25697a35 | 240 | } |
25697a35 | 241 | |
286f212b | 242 | static void bar(struct GraphDataStruct *gdata,int x1,double y,const char *label) |
1dd9dcec | 243 | { |
9bd92830 FM |
244 | gdPoint points[4]; |
245 | int val=0; | |
246 | int width2; | |
247 | ||
248 | val=gdata->BottomGraph+5-(int)(y*gdata->YScale+0.5); | |
249 | width2=(int)(gdata->XScale/4.); | |
250 | ||
251 | // front side of the bar | |
252 | if (val<gdata->BottomDepth) | |
253 | gdImageFilledRectangle(gdata->im, x1-width2, val, x1+width2, gdata->BottomDepth, gdata->color3); | |
254 | ||
255 | // top side of the bar | |
256 | points[0].x = x1-width2+5; | |
257 | points[0].y = val-5; | |
258 | points[1].x = x1-width2; | |
259 | points[1].y = val; | |
260 | points[2].x = x1+width2; | |
261 | points[2].y = val; | |
262 | points[3].x = x1+width2+5; | |
263 | points[3].y = val-5; | |
264 | gdImageFilledPolygon(gdata->im, points, 4, gdata->color1); | |
265 | ||
266 | gdImageLine(gdata->im, x1+2, val-2, x1+2, val-10, gdata->dimgray); | |
267 | gdImageFilledRectangle(gdata->im, x1-8, val-20, x1+12, val-10, gdata->goldenrod); | |
268 | gdImageRectangle(gdata->im, x1-8, val-20, x1+12, val-10, gdata->goldenrod2); | |
269 | ||
270 | Sarg_gdImageStringFT(gdata,gdata->black,GraphFont,6,0.0,x1+2,val-12,label,TRP_BottomCenter); | |
271 | ||
272 | // lateral side of the bar | |
273 | if (val<gdata->BottomDepth) { | |
274 | points[0].x = x1+width2+5; | |
275 | points[0].y = val-5; | |
276 | points[1].x = x1+width2; | |
277 | points[1].y = val; | |
278 | points[2].x = x1+width2; | |
279 | points[2].y = gdata->BottomDepth; | |
280 | points[3].x = x1+width2+5; | |
281 | points[3].y = gdata->BottomGraph; | |
282 | gdImageFilledPolygon(gdata->im, points, 4, gdata->color2); | |
283 | } | |
284 | ||
285 | return; | |
25697a35 GS |
286 | } |
287 | ||
286f212b FM |
288 | static int greport_compute_yaxis(struct PlotStruct *pdata,struct GraphDataStruct *gdata) |
289 | { | |
9bd92830 FM |
290 | double symin,symax; |
291 | double range; | |
292 | double yscale; | |
293 | double ymag; | |
294 | ||
295 | if (pdata->ymin<0.) { | |
296 | debuga(_("Minimum for Y scale of the graph is out of range: %"PRId64"\n"),(int64_t)pdata->ymin); | |
297 | return(-1); | |
298 | } | |
299 | if (pdata->ymax<=0.) { | |
300 | debuga(_("Maximum for Y scale of the graph is out of range: %"PRId64"\n"),(int64_t)pdata->ymax); | |
301 | return(-1); | |
302 | } | |
303 | ||
304 | switch(pdata->ytype) | |
305 | { | |
306 | case PTG_LinBin: | |
307 | symin=(double)pdata->ymin; | |
308 | symax=(double)pdata->ymax; | |
309 | ymag=1000.; | |
310 | break; | |
311 | ||
312 | case PTG_LogBin: | |
313 | if (pdata->ymin>0.) | |
314 | symin=log(pdata->ymin); | |
315 | else | |
316 | symin=0.; | |
317 | symax=log(pdata->ymax); | |
318 | ymag=1000.; | |
319 | break; | |
320 | ||
321 | case PTG_Time: | |
322 | symin=(double)pdata->ymin; | |
323 | symax=(double)pdata->ymax; | |
324 | ymag=1.; | |
325 | break; | |
326 | ||
327 | default: | |
328 | debuga(_("Unknown type %d for Y axis scale\n"),pdata->ytype); | |
329 | return(-1); | |
330 | } | |
331 | gdata->YTickSpace=10; | |
332 | ||
333 | range=symax-symin; | |
334 | yscale=(double)(gdata->BottomGraph-gdata->TopGraph)/range; | |
335 | gdata->YScale=yscale; | |
336 | return(0); | |
286f212b FM |
337 | } |
338 | ||
1b81f396 | 339 | static void greport_formatbin(double yval,int maxdigits,char *string,int slen) |
286f212b | 340 | { |
9bd92830 FM |
341 | int len; |
342 | char schar[]={'\0','k','M','G','T','P'}; | |
343 | int scount; | |
344 | int i; | |
345 | int ndigits; | |
346 | ||
347 | for (scount=0 ; scount<sizeof(schar)/sizeof(*schar) && yval>=1000. ; scount++) | |
348 | yval/=1000.; | |
349 | if (yval<2.) | |
350 | ndigits=2; | |
351 | else if (yval<3.) | |
352 | ndigits=1; | |
353 | else | |
354 | ndigits=0; | |
355 | if (ndigits>maxdigits) ndigits=maxdigits; | |
356 | len=snprintf(string,slen,"%.*f",ndigits,(float)yval); | |
357 | if (UseComma) | |
358 | for (i=0 ; i<len ; i++) | |
359 | if (string[i]=='.') string[i]=','; | |
360 | if (schar[scount] && len<slen) { | |
361 | string[len++]=schar[scount]; | |
362 | string[len]='\0'; | |
363 | } | |
286f212b FM |
364 | } |
365 | ||
366 | static void greport_draw_yaxis(struct PlotStruct *pdata,struct GraphDataStruct *gdata) | |
367 | { | |
9bd92830 FM |
368 | double yval; |
369 | int y0; | |
370 | int y; | |
371 | int yt; | |
372 | char YLabel[50]; | |
373 | int xexterior; | |
374 | int xinterior; | |
375 | int xtick; | |
376 | ||
377 | y0=gdata->BottomGraph; | |
378 | yt=gdata->BottomDepth-gdata->BottomGraph; | |
379 | xexterior=gdata->LeftGraph-10; | |
380 | xinterior=gdata->LeftGraph; | |
381 | xtick=gdata->LeftGraph-10-gdata->TickLength; | |
382 | for(y=y0-gdata->YTickSpace ; y>=gdata->TopGraph ; y-=gdata->YTickSpace) { | |
383 | gdImageLine(gdata->im, xtick, y+yt, xexterior, y+yt, gdata->dimgray); | |
384 | gdImageLine(gdata->im, xexterior, y+yt, xinterior, y, gdata->dimgray); | |
385 | gdImageLine(gdata->im, xinterior, y, gdata->RightGraph, y, gdata->dimgray); | |
386 | switch (pdata->ytype) | |
387 | { | |
388 | case PTG_LinBin: | |
389 | yval=(double)(y0-y)/gdata->YScale+(double)pdata->ymin; | |
390 | greport_formatbin(yval,2,YLabel,sizeof(YLabel)); | |
391 | break; | |
392 | ||
393 | case PTG_LogBin: | |
394 | yval=exp((double)(y0-y)/gdata->YScale+log(pdata->ymin)); | |
395 | greport_formatbin(yval,2,YLabel,sizeof(YLabel)); | |
396 | break; | |
397 | ||
398 | case PTG_Time: | |
399 | { | |
400 | int t; | |
401 | ||
402 | yval=(double)(y0-y)/gdata->YScale+(double)pdata->ymin; | |
403 | t=(int)(yval/60000.+0.5); | |
404 | snprintf(YLabel,sizeof(YLabel),"%02d:%02d",t/60,t%60); | |
405 | break; | |
406 | } | |
407 | } | |
408 | Sarg_gdImageStringFT(gdata,gdata->dimgray,GraphFont,7,0.0,xtick,y+yt,YLabel,TRP_CenterRight); | |
409 | } | |
286f212b FM |
410 | } |
411 | ||
412 | static void greport_plot(const struct userinfostruct *uinfo,struct PlotStruct *pdata) | |
25697a35 | 413 | { |
9bd92830 FM |
414 | FILE *pngout; |
415 | int x, y; | |
416 | int day; | |
417 | int x1; | |
418 | char graph[MAXLEN]; | |
419 | char s[15]; | |
420 | char ftime[128]; | |
421 | time_t t; | |
422 | struct tm *local; | |
423 | gdPoint points[4]; | |
424 | struct GraphDataStruct gdata; | |
425 | const int ImgXSize=720; | |
426 | const int ImgYSize=480; | |
427 | const int LeftMargin=60; | |
428 | const int RightMargin=20; | |
429 | const int TopMargin=60; | |
430 | const int BottomMargin=60; | |
431 | const int TickLength=3; | |
432 | const int ZTickLength=5; | |
433 | double yval; | |
434 | char blabel[50]; | |
435 | double logpmin; | |
436 | ||
437 | memset(&gdata,0,sizeof(gdata)); | |
438 | ||
439 | gdata.im = gdImageCreate(ImgXSize, ImgYSize); | |
440 | gdata.BottomGraph=ImgYSize-BottomMargin; | |
441 | gdata.LeftGraph=LeftMargin; | |
442 | gdata.RightGraph=ImgXSize-RightMargin; | |
443 | gdata.TopGraph=TopMargin; | |
444 | gdata.BottomDepth=gdata.BottomGraph+5; | |
445 | gdata.XScale=(double)(gdata.RightGraph-gdata.LeftGraph)/(pdata->npoints+1); | |
446 | if (greport_compute_yaxis(pdata,&gdata)<0) return; | |
447 | gdata.TickLength=TickLength; | |
448 | ||
449 | // first allocated color is the background | |
450 | gdata.lavender = gdImageColorAllocate(gdata.im, 230, 230, 250); | |
451 | gdata.gray = gdImageColorAllocate(gdata.im, 192, 192, 192); | |
452 | gdata.silver = gdImageColorAllocate(gdata.im, 211, 211, 211); | |
453 | gdata.black = gdImageColorAllocate(gdata.im, 0, 0, 0); | |
454 | gdata.dimgray = gdImageColorAllocate(gdata.im, 105, 105, 105); | |
455 | gdata.darkblue = gdImageColorAllocate(gdata.im, 0, 0, 139); | |
456 | gdata.goldenrod = gdImageColorAllocate(gdata.im, 234, 234, 174); | |
457 | gdata.goldenrod2 = gdImageColorAllocate(gdata.im, 207, 181, 59); | |
458 | ||
459 | if(strcmp(GraphDaysBytesBarColor,"orange") == 0) { | |
460 | gdata.color1 = gdImageColorAllocate(gdata.im, 255, 233, 142); | |
461 | gdata.color2 = gdImageColorAllocate(gdata.im, 220, 163, 72); | |
462 | gdata.color3 = gdImageColorAllocate(gdata.im, 255, 198, 107); | |
463 | } | |
464 | else if(strcmp(GraphDaysBytesBarColor,"blue") == 0) { | |
465 | gdata.color1 = gdImageColorAllocate(gdata.im, 62, 80, 167); | |
466 | gdata.color2 = gdImageColorAllocate(gdata.im, 40, 51, 101); | |
467 | gdata.color3 = gdImageColorAllocate(gdata.im, 57, 73, 150); | |
468 | } | |
469 | else if(strcmp(GraphDaysBytesBarColor,"green") == 0) { | |
470 | gdata.color1 = gdImageColorAllocate(gdata.im,120,166,129); | |
471 | gdata.color2 = gdImageColorAllocate(gdata.im,84,113,82); | |
472 | gdata.color3 = gdImageColorAllocate(gdata.im,158,223,167); | |
473 | } | |
474 | else if(strcmp(GraphDaysBytesBarColor,"yellow") == 0) { | |
475 | gdata.color1 = gdImageColorAllocate(gdata.im,185,185,10); | |
476 | gdata.color2 = gdImageColorAllocate(gdata.im,111,111,10); | |
477 | gdata.color3 = gdImageColorAllocate(gdata.im,166,166,10); | |
478 | } | |
479 | else if(strcmp(GraphDaysBytesBarColor,"brown") == 0) { | |
480 | gdata.color1 = gdImageColorAllocate(gdata.im,97,45,27); | |
481 | gdata.color2 = gdImageColorAllocate(gdata.im,60,30,20); | |
482 | gdata.color3 = gdImageColorAllocate(gdata.im,88,41,26); | |
483 | } | |
484 | else if(strcmp(GraphDaysBytesBarColor,"red") == 0){ | |
485 | gdata.color1 = gdImageColorAllocate(gdata.im,185,10,10); | |
486 | gdata.color2 = gdImageColorAllocate(gdata.im,111,10,10); | |
487 | gdata.color3 = gdImageColorAllocate(gdata.im,166,10,10); | |
488 | } else { | |
489 | debuga(_("Unknown color \"%s\" requested for the graph. Using orange instead\n"),GraphDaysBytesBarColor); | |
490 | gdata.color1 = gdImageColorAllocate(gdata.im, 255, 233, 142); | |
491 | gdata.color2 = gdImageColorAllocate(gdata.im, 220, 163, 72); | |
492 | gdata.color3 = gdImageColorAllocate(gdata.im, 255, 198, 107); | |
493 | } | |
494 | ||
495 | // rectangle around the image | |
496 | gdImageRectangle(gdata.im, 0, 0, ImgXSize-1, ImgYSize-1, gdata.dimgray); | |
497 | // backtround of the graph | |
498 | gdImageFilledRectangle(gdata.im, LeftMargin, gdata.TopGraph, gdata.RightGraph, gdata.BottomGraph, gdata.silver); | |
499 | ||
500 | // depth of the left Y axis | |
501 | points[0].x = gdata.LeftGraph-10; | |
502 | points[0].y = gdata.TopGraph+5; | |
503 | points[1].x = gdata.LeftGraph-10; | |
504 | points[1].y = gdata.BottomDepth; | |
505 | points[2].x = gdata.LeftGraph; | |
506 | points[2].y = gdata.BottomGraph; | |
507 | points[3].x = gdata.LeftGraph; | |
508 | points[3].y = gdata.TopGraph; | |
509 | gdImageFilledPolygon(gdata.im, points, 4, gdata.gray); | |
510 | ||
511 | // depth of the bottom X axis | |
512 | points[0].x = gdata.LeftGraph; | |
513 | points[0].y = gdata.BottomGraph; | |
514 | points[1].x = gdata.LeftGraph-10; | |
515 | points[1].y = gdata.BottomDepth; | |
516 | points[2].x = gdata.RightGraph-10; | |
517 | points[2].y = gdata.BottomDepth; | |
518 | points[3].x = gdata.RightGraph; | |
519 | points[3].y = gdata.BottomGraph; | |
520 | gdImageFilledPolygon(gdata.im, points, 4, gdata.gray); | |
521 | ||
522 | // vertical exterior line of the depth | |
523 | gdImageLine(gdata.im, LeftMargin-10, TopMargin+5, LeftMargin-10, gdata.BottomDepth+ZTickLength, gdata.black); | |
524 | // horizontal exterior line of the depth | |
525 | gdImageLine(gdata.im, LeftMargin-10-ZTickLength, gdata.BottomDepth, gdata.RightGraph-10, gdata.BottomDepth, gdata.black); | |
526 | // diagonal line between the two depths | |
527 | gdImageLine(gdata.im, LeftMargin-10, gdata.BottomDepth, LeftMargin, gdata.BottomGraph, gdata.black); | |
528 | // vertical left line of the graph | |
529 | gdImageLine(gdata.im, LeftMargin, gdata.BottomGraph, LeftMargin, gdata.TopGraph, gdata.black); | |
530 | // horizontal bottom line of the graph | |
531 | gdImageLine(gdata.im, LeftMargin, gdata.BottomGraph, gdata.RightGraph, gdata.BottomGraph, gdata.black); | |
532 | // vertical right line of the graph | |
533 | gdImageLine(gdata.im, gdata.RightGraph, gdata.TopGraph, gdata.RightGraph, gdata.BottomGraph, gdata.black); | |
534 | // diagonal line to close the right of the bottom depth | |
535 | gdImageLine(gdata.im, gdata.RightGraph-10, gdata.BottomDepth, gdata.RightGraph, gdata.BottomGraph, gdata.black); | |
536 | ||
537 | // Y axis ticks | |
538 | greport_draw_yaxis(pdata,&gdata); | |
539 | ||
540 | // X axis ticks and labels | |
541 | for(y=1; y<=pdata->npoints; y++) { | |
542 | x=gdata.LeftGraph-10+(int)((double)y*gdata.XScale+0.5); | |
543 | gdImageLine(gdata.im, x, gdata.BottomDepth, x, gdata.BottomDepth+TickLength, gdata.dimgray); | |
544 | sprintf(s,"%02d",y); | |
545 | Sarg_gdImageStringFT(&gdata,gdata.dimgray,GraphFont,7,0.0,x,gdata.BottomDepth+TickLength+1,s,TRP_TopCenter); | |
546 | } | |
547 | ||
548 | t = time(NULL); | |
549 | local = localtime(&t); | |
550 | if(DateFormat[0]=='u') | |
551 | strftime(ftime, sizeof(ftime), "%b/%d/%Y %H:%M", local); | |
552 | if(DateFormat[0]=='e') | |
553 | strftime(ftime, sizeof(ftime), "%d/%b/%Y-%H:%M", local); | |
554 | ||
555 | x=ImgXSize*5/12; | |
556 | Sarg_gdImageStringFT(&gdata,gdata.darkblue,GraphFont,7,0.0,ImgXSize-10,ImgYSize-10,ftime,TRP_BottomRight); | |
557 | if(ShowSargInfo) Sarg_gdImageStringFT(&gdata,gdata.darkblue,GraphFont,10,0.0,x,15,_("SARG, "),TRP_BottomRight); | |
558 | Sarg_gdImageStringFT(&gdata,gdata.darkblue,GraphFont,10,0.0,x,15,Title,TRP_BottomLeft); | |
559 | sprintf(warea,_("Period: %s"),period.text); | |
560 | Sarg_gdImageStringFT(&gdata,gdata.darkblue,GraphFont,9,0.0,x,27,warea,TRP_BottomLeft); | |
561 | sprintf(warea,_("User: %s"),uinfo->label); | |
562 | Sarg_gdImageStringFT(&gdata,gdata.darkblue,GraphFont,9,0.0,x,38,warea,TRP_BottomLeft); | |
563 | ||
564 | Sarg_gdImageStringFT(&gdata,gdata.black,GraphFont,10,3.141592/2,15,ImgYSize/2,pdata->YLabel,TRP_CenterLeft); | |
565 | Sarg_gdImageStringFT(&gdata,gdata.black,GraphFont,10,0.0,ImgXSize/2,ImgYSize-20,pdata->XLabel,TRP_BottomCenter); | |
566 | ||
567 | logpmin=(pdata->ytype==PTG_LogBin && pdata->ymin>0.) ? log(pdata->ymin) : 0.; | |
568 | for (day=0 ; day<pdata->npoints ; day++) { | |
569 | if (pdata->datapoints[day]>0) { | |
570 | x1=gdata.LeftGraph-10+(int)((double)(day+1)*gdata.XScale+0.5); | |
571 | switch (pdata->ytype) | |
572 | { | |
573 | case PTG_LinBin: | |
574 | yval=(double)pdata->datapoints[day]; | |
575 | if (yval<pdata->ymin) | |
576 | yval=0.; | |
577 | else if (yval>pdata->ymax) | |
578 | yval=pdata->ymax; | |
579 | else | |
580 | yval-=pdata->ymin; | |
581 | greport_formatbin(pdata->datapoints[day],1,blabel,sizeof(blabel)); | |
582 | break; | |
583 | case PTG_LogBin: | |
584 | yval=(double)pdata->datapoints[day]; | |
585 | if (yval<=pdata->ymin) | |
586 | yval=0.; | |
587 | else if (yval>pdata->ymax) | |
588 | yval=log(pdata->ymax)-logpmin; | |
589 | else | |
590 | yval=log(yval)-logpmin; | |
591 | greport_formatbin(pdata->datapoints[day],1,blabel,sizeof(blabel)); | |
592 | break; | |
593 | case PTG_Time: | |
594 | { | |
595 | int t; | |
596 | ||
597 | yval=(double)pdata->datapoints[day]; | |
598 | if (yval<pdata->ymin) | |
599 | yval=0.; | |
600 | else if (yval>pdata->ymax) | |
601 | yval=pdata->ymax; | |
602 | else | |
603 | yval-=pdata->ymin; | |
604 | t=(int)(pdata->datapoints[day]/60000.); | |
605 | snprintf(blabel,sizeof(blabel),"%d:%02d",t/60,t%60); | |
606 | break; | |
607 | } | |
608 | default: | |
609 | yval=-1.; | |
610 | break; | |
611 | } | |
612 | if (yval>=0.) bar(&gdata,x1,yval,blabel); | |
613 | } | |
614 | } | |
615 | ||
616 | if (snprintf(graph,sizeof(graph),"%s/%s/%s",outdirname,uinfo->filename,pdata->pngfile)>=sizeof(graph)) { | |
617 | debuga(_("user name too long for %s/%s/%s\n"),outdirname,uinfo->filename,pdata->pngfile); | |
618 | exit(EXIT_FAILURE); | |
619 | } | |
620 | if((pngout=fopen(graph,"wb"))==NULL) { | |
621 | debuga(_("(grepday) Cannot open log file %s\n"),graph); | |
622 | exit(EXIT_FAILURE); | |
623 | } | |
624 | gdImagePng(gdata.im, pngout); | |
625 | fclose(pngout); | |
626 | gdImageDestroy(gdata.im); | |
627 | ||
628 | if (gdata.string) free(gdata.string); | |
1f482a8d FM |
629 | } |
630 | ||
631 | #endif //HAVE_GD | |
632 | ||
633 | void greport_prepare(void) | |
634 | { | |
635 | #ifdef HAVE_GD | |
9bd92830 FM |
636 | if(access(GraphFont, R_OK) != 0) { |
637 | debuga(_("(grepday) Fontname %s not found\n"),GraphFont); | |
638 | exit(EXIT_FAILURE); | |
639 | } | |
1f482a8d | 640 | |
e43854ef | 641 | #ifdef USE_ICONV |
9bd92830 FM |
642 | localtoutf = iconv_open ("UTF-8", CharSet); |
643 | if (localtoutf==(iconv_t)-1) { | |
644 | debuga(_("(grepday) iconv cannot convert from %s to UTF-8 - %s\n"),CharSet,strerror(errno)); | |
645 | } | |
e43854ef | 646 | #endif |
1dd9dcec | 647 | |
1f482a8d FM |
648 | #endif //HAVE_GD |
649 | } | |
650 | ||
651 | void greport_day(const struct userinfostruct *uinfo) | |
652 | { | |
653 | #ifdef HAVE_GD | |
9bd92830 FM |
654 | FILE *fp_in, *fp_ou; |
655 | char wdirname[MAXLEN]; | |
656 | char buf[MAXLEN]; | |
657 | char colsep; | |
658 | int day; | |
659 | long long int llday; | |
660 | long long int bytes; | |
661 | long long int elap; | |
662 | long long int bytespoints[31]; | |
663 | long long int elappoints[31]; | |
664 | struct getwordstruct gwarea; | |
665 | struct PlotStruct pdata; | |
666 | ||
667 | if (snprintf(wdirname,sizeof(wdirname),"%s/%s.day",tmp,uinfo->filename)>=sizeof(wdirname)) { | |
668 | debuga(_("user name too long for %s/%s.day\n"),tmp,uinfo->filename); | |
669 | exit(EXIT_FAILURE); | |
670 | } | |
671 | if(access(wdirname, R_OK) != 0) { | |
672 | return; | |
673 | } | |
674 | if(!Graphs || GraphFont[0]=='\0') { | |
675 | unlink(wdirname); | |
676 | return; | |
677 | } | |
678 | ||
679 | if((fp_in=fopen(wdirname,"r"))==NULL) { | |
680 | debuga(_("(grepday) Cannot open log file %s\n"),wdirname); | |
681 | exit(EXIT_FAILURE); | |
682 | } | |
683 | ||
684 | memset(bytespoints,0,sizeof(bytespoints)); | |
685 | memset(elappoints,0,sizeof(elappoints)); | |
686 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
687 | fixendofline(buf); | |
688 | getword_start(&gwarea,buf); | |
689 | if (getword_atoll(&llday,&gwarea,'/')<0) { | |
690 | debuga(_("Invalid date in file %s\n"),wdirname); | |
691 | exit(EXIT_FAILURE); | |
692 | } | |
693 | day=(int)llday; | |
694 | if (day<1 || day>31) continue; | |
695 | if (getword_skip(20,&gwarea,'\t')<0 || getword_skip(20,&gwarea,'\t')<0) { | |
696 | debuga(_("Invalid entry in file %s\n"),wdirname); | |
697 | exit(EXIT_FAILURE); | |
698 | } | |
699 | if ((datetimeby & DATETIME_BYTE)!=0) { | |
700 | colsep=((datetimeby & DATETIME_ELAP)!=0) ? '\t' : '\0'; | |
701 | if (getword_atoll(&bytes,&gwarea,'\t')<0) { | |
702 | debuga(_("Invalid number of bytes in file %s\n"),wdirname); | |
703 | exit(EXIT_FAILURE); | |
704 | } | |
705 | bytespoints[day-1]+=bytes; | |
706 | } | |
707 | if ((datetimeby & DATETIME_ELAP)!=0) { | |
708 | if (getword_atoll(&elap,&gwarea,'\0')<0) { | |
709 | debuga(_("Invalid elapsed time in file %s\n"),wdirname); | |
710 | exit(EXIT_FAILURE); | |
711 | } | |
712 | elappoints[day-1]+=elap; | |
713 | } | |
714 | } | |
715 | fclose(fp_in); | |
08f9b029 FM |
716 | if (unlink(wdirname)) { |
717 | debuga(_("Cannot delete %s - %s\n"),wdirname,strerror(errno)); | |
718 | exit(EXIT_FAILURE); | |
719 | } | |
9bd92830 FM |
720 | |
721 | if (snprintf(wdirname,sizeof(wdirname),"%s/%s/graph.html",outdirname,uinfo->filename)>=sizeof(wdirname)) { | |
722 | debuga(_("user name too long for %s/%s/%s\n"),outdirname,uinfo->filename,"graph.html"); | |
723 | exit(EXIT_FAILURE); | |
724 | } | |
725 | if ((fp_ou=fopen(wdirname,"wt"))==NULL) { | |
007905af FM |
726 | debuga(_("(grepday) Cannot open output file %s\n"),wdirname); |
727 | exit(EXIT_FAILURE); | |
9bd92830 FM |
728 | } |
729 | write_html_head(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Graph report"),HTML_JS_NONE); | |
730 | ||
731 | fputs("<table class=\"report\" cellpadding=\"0\" cellspacing=\"2\">\n", fp_ou); | |
732 | if((datetimeby & DATETIME_BYTE)!=0) { | |
733 | memset(&pdata,0,sizeof(pdata)); | |
734 | pdata.datapoints=bytespoints; | |
735 | pdata.npoints=31; | |
736 | pdata.XLabel=_("DAYS"); | |
737 | pdata.ymin=50LL*1000LL; | |
738 | pdata.ymax=5LL*1000LL*1000LL*1000LL; | |
739 | pdata.ytype=PTG_LogBin; | |
740 | pdata.YLabel=_("BYTES"); | |
741 | pdata.pngfile="graph_day_byte.png"; | |
742 | greport_plot(uinfo,&pdata); | |
743 | fprintf(fp_ou,"<tr><td><img src=\"%s\" alt=\"B\"></td></tr>\n",pdata.pngfile); | |
744 | } | |
745 | if((datetimeby & DATETIME_ELAP)!=0) { | |
746 | memset(&pdata,0,sizeof(pdata)); | |
747 | pdata.datapoints=elappoints; | |
748 | pdata.npoints=31; | |
749 | pdata.XLabel=_("DAYS"); | |
750 | pdata.ymin=0; | |
751 | pdata.ymax=86400000; | |
752 | pdata.ytype=PTG_Time; | |
753 | pdata.YLabel=_("ELAPSED TIME"); | |
754 | pdata.pngfile="graph_day_elap.png"; | |
755 | greport_plot(uinfo,&pdata); | |
756 | fprintf(fp_ou,"<tr><td><img src=\"%s\" alt=\"E\"></td></tr>\n",pdata.pngfile); | |
757 | } | |
758 | fputs("</table>\n",fp_ou); | |
759 | ||
760 | if (write_html_trailer(fp_ou)<0) | |
761 | debuga(_("Write error in file %s\n"),wdirname); | |
762 | if (fclose(fp_ou)==EOF) | |
763 | debuga(_("Failed to close file %s - %s\n"),wdirname,strerror(errno)); | |
a1de61fe | 764 | #endif //HAVE_GD |
d6e703cc | 765 | |
9bd92830 | 766 | return; |
25697a35 | 767 | } |
c274f011 FM |
768 | |
769 | void greport_cleanup(void) | |
770 | { | |
771 | #ifdef HAVE_GD | |
9bd92830 | 772 | gdFontCacheShutdown(); |
1f482a8d FM |
773 | |
774 | #ifdef USE_ICONV | |
9bd92830 FM |
775 | if (localtoutf!=(iconv_t)-1) |
776 | iconv_close (localtoutf); | |
c274f011 | 777 | #endif |
1f482a8d | 778 | #endif //HAVE_GD |
c274f011 | 779 | } |