]> git.ipfire.org Git - thirdparty/sarg.git/blob - repday.c
Fix a regression when processing an input log not in the squid format (the date was...
[thirdparty/sarg.git] / repday.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2010
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
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"
28 #include "include/defs.h"
29
30 extern numlist hours;
31
32 void report_day(const struct userinfostruct *uinfo)
33 {
34
35 FILE *fp_in, *fp_ou;
36
37 char buf[MAXLEN];
38 char data[20];
39 char odata[20];
40 char hour[20];
41 char html[8000];
42 char arqout[MAXLEN];
43 char wdirname[MAXLEN];
44 char c[ 24 ][20];
45 int count=0;
46 int ihour=0;
47 long long int elap;
48 long long int v[ 24 ] = { 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
49 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L };
50 long long int t[ 24 ] = { 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
51 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L };
52 long long int tt=0, ttt=0;
53 int i;
54 struct getwordstruct gwarea;
55
56 if (snprintf(arqout,sizeof(arqout),"%s/%s/d%s.html",outdirname,uinfo->filename,uinfo->filename)>=sizeof(arqout)) {
57 debuga(_("Output file name too long: %s/%s/d%s.html\n"),outdirname,uinfo->filename,uinfo->filename);
58 exit(EXIT_FAILURE);
59 }
60 snprintf(wdirname,sizeof(wdirname),"%s/%s.day",tmp,uinfo->filename);
61
62 if(access(wdirname, R_OK) != 0)
63 return;
64
65 if((fp_in=fopen(wdirname,"r"))==NULL) {
66 debuga(_("(repday) Cannot open log file %s\n"),wdirname);
67 exit(EXIT_FAILURE);
68 }
69
70 if((fp_ou=fopen(arqout,"w"))==NULL) {
71 debuga(_("(repday) Cannot open log file %s\n"),arqout);
72 exit(EXIT_FAILURE);
73 }
74
75 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Day report"));
76 fprintf(fp_ou,"<tr><td class=\"header_c\" colspan=\"2\">%s:&nbsp;%s</td></tr>\n",_("Period"),period.html);
77 fprintf(fp_ou,"<tr><th class=\"header_c\" colspan=\"2\">%s:&nbsp;%s</th></tr>\n",_("User"),uinfo->label);
78 close_html_header(fp_ou);
79
80 fputs("<table class=\"report\" cellpadding=\"0\" cellspacing=\"2\">\n", fp_ou);
81
82 if(datetimeby==DATETIME_BYTE)
83 strcpy( html, _("BYTES") );
84 else
85 strcpy( html, "H:M:S" );
86
87 for( i = 0; i < hours.len; i++ )
88 fprintf( fp_ou, "<td class=\"header_c\">%02dH<br>%s</td>\n", hours.list[ i ], html );
89 fprintf( fp_ou,
90 "<td class=\"header_c\">%s<br>%s</td></tr>\n", _("TOTAL"), html );
91
92 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
93 fixendofline(buf);
94 getword_start(&gwarea,buf);
95 if (getword(data,sizeof(data),&gwarea,'\t')<0) {
96 debuga(_("There is a broken date in file %s\n"),wdirname);
97 exit(EXIT_FAILURE);
98 }
99 if(!count) {
100 strcpy(odata,data);
101 count++;
102 }
103
104 if (getword(hour,sizeof(hour),&gwarea,'\t')<0) {
105 debuga(_("There is a broken time in file %s\n"),wdirname);
106 exit(EXIT_FAILURE);
107 }
108 if (getword_atoll(&elap,&gwarea,'\t')<0) {
109 debuga(_("There is a broken quantity in file %s\n"),wdirname);
110 exit(EXIT_FAILURE);
111 }
112
113 if(strcmp(data,odata) != 0) {
114 for( i = 0; i < hours.len; i++ )
115 sprintf(c[ hours.list[ i ] ],"%s",fixtime(v[ hours.list[ i ] ]));
116
117 for( i = 8; i <= 17; i++ )
118 if(strcmp(c[ hours.list[ i ] ],"00:00:00") == 0)
119 bzero(c[ hours.list[ i ] ],12);
120
121 fprintf(fp_ou, "<tr><td class=\"data\">%s</td>\n", odata );
122 for( i = 0; i < hours.len; i++ )
123 fprintf(fp_ou,"<td class=\"data\">%s</td>\n", c[ hours.list[ i ] ] );
124 fprintf( fp_ou, "<td class=\"data\">%s</td>\n", fixtime(tt) );
125
126 tt=0;
127 for( i = 0; i < hours.len; i++ ) v[ hours.list[ i ] ]=0;
128 strcpy(odata,data);
129 }
130
131 ihour=atoi(hour);
132
133 v[ ihour ]+=elap;
134 tt+=elap;
135 t[ ihour ]+=elap;
136 ttt+=elap;
137
138 }
139 fclose(fp_in);
140
141 for( i = 0; i < hours.len; i++ )
142 sprintf(c[ hours.list[ i ] ],"%s",fixtime(v[ hours.list[ i ] ]));
143
144 for( i = 0; i < hours.len; i++ )
145 if(strcmp(c[ hours.list[ i ] ],"00:00:00") == 0) bzero(c[ hours.list[ i ] ],12);
146
147 fprintf(fp_ou, "<tr><td class=\"data\">%s</td>\n", data );
148 for( i = 0; i < hours.len; i++ )
149 fprintf(fp_ou, "<td class=\"data\">%s</td>\n", c[ hours.list[ i ] ] );
150 fprintf(fp_ou, "<td class=\"data\">%s</td></tr>\n", fixtime(tt) );
151
152 for( i = 0; i < hours.len; i++ )
153 sprintf(c[ hours.list[ i ] ],"%s",fixtime(t[ hours.list[ i ] ]));
154
155 fprintf(fp_ou, "<tr><td class=\"header_l\">%s</td>\n", _("TOTAL") );
156 for( i = 0; i < hours.len; i++ )
157 fprintf(fp_ou, "<td class=\"header_r\">%s</td>\n", c[ hours.list[ i ] ] );
158 fprintf(fp_ou, "<td class=\"header_r\">%s</td></tr>\n", fixtime(ttt) );
159
160 fputs("</table>\n",fp_ou);
161 if (write_html_trailer(fp_ou)<0)
162 debuga(_("Write error in file %s\n"),arqout);
163 if (fclose(fp_ou)==EOF)
164 debuga(_("Failed to close file %s - %s\n"),arqout,strerror(errno));
165 return;
166 }