]> git.ipfire.org Git - thirdparty/sarg.git/blob - repday.c
Merge commit '24eb624ea44bf4c82c602ce'
[thirdparty/sarg.git] / repday.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2012
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 FILE *fp_in, *fp_ou;
35
36 char buf[200];
37 char data[20];
38 long long int hour;
39 const char *label;
40 char arqout[MAXLEN];
41 char wdirname[2048];
42 char colsep;
43 int ihour=0;
44 int day,month,year;
45 int daylist[MAX_DATETIME_DAYS];
46 int daysort[MAX_DATETIME_DAYS];
47 int ndaylist;
48 int daynum;
49 int dayidx;
50 long long int bytes;
51 long long int elap;
52 long long int tbytes[MAX_DATETIME_DAYS*24];
53 long long int telap[MAX_DATETIME_DAYS*24];
54 long long int tt;
55 long long int tttime[24];
56 int i, j;
57 struct getwordstruct gwarea;
58 struct tm t;
59
60 snprintf(wdirname,sizeof(wdirname),"%s/%s.day",tmp,uinfo->filename);
61 if(access(wdirname, R_OK) != 0) return;
62
63 if (snprintf(arqout,sizeof(arqout),"%s/%s/d%s.html",outdirname,uinfo->filename,uinfo->filename)>=sizeof(arqout)) {
64 debuga(_("Output file name too long: %s/%s/d%s.html\n"),outdirname,uinfo->filename,uinfo->filename);
65 exit(EXIT_FAILURE);
66 }
67
68 if((fp_in=fopen(wdirname,"r"))==NULL) {
69 debuga(_("(repday) Cannot open log file %s\n"),wdirname);
70 exit(EXIT_FAILURE);
71 }
72
73 memset(tbytes,0,sizeof(tbytes));
74 memset(telap,0,sizeof(telap));
75 ndaylist=0;
76
77 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
78 fixendofline(buf);
79 getword_start(&gwarea,buf);
80 if (getword(data,sizeof(data),&gwarea,'\t')<0) {
81 debuga(_("Invalid date in file %s\n"),wdirname);
82 exit(EXIT_FAILURE);
83 }
84 if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue;
85 if (day<1 || day>31 || month<1 || month>12 || year>9999) continue;
86 daynum=(year*10000)+(month*100)+day;
87 for (dayidx=0 ; dayidx<ndaylist && daynum!=daylist[dayidx] ; dayidx++);
88 if (dayidx>=ndaylist) {
89 if (dayidx>=sizeof(daylist)/sizeof(*daylist)) {
90 debuga(_("Too many different dates in %s\n"),wdirname);
91 exit(EXIT_FAILURE);
92 }
93 daylist[ndaylist++]=daynum;
94 }
95
96 if (getword_atoll(&hour,&gwarea,'\t')<0) {
97 debuga(_("Invalid time in file %s\n"),wdirname);
98 exit(EXIT_FAILURE);
99 }
100 ihour=(int)hour;
101 i=dayidx*24+ihour;
102
103 if ((datetimeby & DATETIME_BYTE)!=0) {
104 colsep=((datetimeby & DATETIME_ELAP)!=0) ? '\t' : '\0';
105 if (getword_atoll(&bytes,&gwarea,colsep)<0) {
106 debuga(_("Invalid number of bytes in file %s\n"),wdirname);
107 exit(EXIT_FAILURE);
108 }
109 tbytes[i]+=bytes;
110 }
111 if ((datetimeby & DATETIME_ELAP)!=0) {
112 if (getword_atoll(&elap,&gwarea,'\0')<0) {
113 debuga(_("Invalid elapsed time in file %s\n"),wdirname);
114 exit(EXIT_FAILURE);
115 }
116 telap[i]+=elap;
117 }
118 }
119 fclose(fp_in);
120
121 if((fp_ou=fopen(arqout,"w"))==NULL) {
122 debuga(_("(repday) Cannot open log file %s\n"),arqout);
123 exit(EXIT_FAILURE);
124 }
125
126 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Day report"),HTML_JS_NONE);
127 fprintf(fp_ou,"<tr><td class=\"header_c\" colspan=\"2\">%s:&nbsp;%s</td></tr>\n",_("Period"),period.html);
128 fprintf(fp_ou,"<tr><th class=\"header_c\" colspan=\"2\">%s:&nbsp;%s</th></tr>\n",_("User"),uinfo->label);
129 close_html_header(fp_ou);
130
131 dayidx=0;
132 for (i=0 ; i<ndaylist ; i++) {
133 daynum=daylist[i];
134 for (j=dayidx ; j>0 && daynum<daylist[daysort[j-1]] ; j--) daysort[j]=daysort[j-1];
135 daysort[j]=i;
136 dayidx++;
137 }
138
139 if((datetimeby & DATETIME_BYTE)!=0) {
140 label=_("BYTES");
141 fputs("<table class=\"report\" cellpadding=\"0\" cellspacing=\"2\">\n", fp_ou);
142 fputs("<tr><th class=\"header_c\"></th>",fp_ou);
143 for( i = 0; i < hours.len; i++ )
144 fprintf(fp_ou, "<td class=\"header_c\">%02d%s<br>%s</td>\n", hours.list[ i ], _("H"), label );
145 fprintf(fp_ou, "<td class=\"header_c\">%s<br>%s</td></tr>\n", _("TOTAL"), label );
146
147 memset(tttime,0,sizeof(tttime));
148 for (dayidx=0 ; dayidx<ndaylist ; dayidx++) {
149 daynum=daysort[dayidx];
150 day=daylist[daynum]%100;
151 month=(daylist[daynum]/100)%100;
152 year=daylist[daynum]/10000;
153 computedate(year,month,day,&t);
154 strftime(data,sizeof(data),"%x",&t);
155 fprintf(fp_ou, "<tr><td class=\"data\">%s</td>\n", data );
156 tt=0;
157 for( i = 0; i < hours.len; i++ ) {
158 ihour=hours.list[i];
159 if (tbytes[daynum*24+ihour]>0) {
160 fprintf(fp_ou, "<td class=\"data\">%s</td>\n",fixnum(tbytes[daynum*24+ihour],1));
161 tt+=tbytes[daynum*24+ihour];
162 tttime[ihour]+=tbytes[daynum*24+ihour];
163 } else
164 fputs("<td class=\"data\"></td>\n",fp_ou);
165 }
166 fprintf(fp_ou, "<td class=\"data\">%s</td></tr>\n",fixnum(tt,1));
167 }
168
169 fprintf(fp_ou, "<tr><td class=\"header_l\">%s</td>\n", _("TOTAL") );
170 tt=0;
171 for( i = 0; i < hours.len; i++ ) {
172 if (tttime[i]>0) {
173 fprintf(fp_ou, "<td class=\"header_r\">%s</td>\n",fixnum(tttime[i],1));
174 tt+=tttime[i];
175 } else
176 fputs("<td class=\"header_r\"></td>\n",fp_ou);
177 }
178 fprintf(fp_ou, "<td class=\"header_r\">%s</td></tr>\n",fixnum(tt,1));
179 fputs("</table>\n",fp_ou);
180 #ifdef ENABLE_DOUBLE_CHECK_DATA
181 if (tt!=uinfo->nbytes) {
182 debuga(_("Total downloaded bytes is %"PRIi64" instead of %"PRIi64" in the hourly report of user %s\n"),
183 (int64_t)tt,(int64_t)uinfo->nbytes,uinfo->label);
184 exit(EXIT_FAILURE);
185 }
186 #endif
187 }
188
189 if((datetimeby & DATETIME_ELAP)!=0) {
190 label=_("H:M:S");
191 fputs("<table class=\"report\" cellpadding=\"0\" cellspacing=\"2\">\n", fp_ou);
192 fputs("<tr><th class=\"header_c\"></th>",fp_ou);
193 for( i = 0; i < hours.len; i++ )
194 fprintf(fp_ou, "<td class=\"header_c\">%02d%s<br>%s</td>\n", hours.list[ i ], _("H"), label );
195 fprintf(fp_ou, "<td class=\"header_c\">%s<br>%s</td></tr>\n", _("TOTAL"), label );
196
197 memset(tttime,0,sizeof(tttime));
198 for (dayidx=0 ; dayidx<ndaylist ; dayidx++) {
199 daynum=daysort[dayidx];
200 day=daylist[daynum]%100;
201 month=(daylist[daynum]/100)%100;
202 year=daylist[daynum]/10000;
203 computedate(year,month,day,&t);
204 strftime(data,sizeof(data),"%x",&t);
205 fprintf(fp_ou, "<tr><td class=\"data\">%s</td>\n", data );
206 tt=0;
207 for( i = 0; i < hours.len; i++ ) {
208 ihour=hours.list[i];
209 if (telap[daynum*24+ihour]>0) {
210 fprintf(fp_ou, "<td class=\"data\">%s</td>\n",fixtime(telap[daynum*24+ihour]));
211 tt+=telap[daynum*24+ihour];
212 tttime[ihour]+=telap[daynum*24+ihour];
213 } else
214 fputs("<td class=\"data\"></td>\n",fp_ou);
215 }
216 fprintf(fp_ou, "<td class=\"data\">%s</td></tr>\n",fixtime(tt));
217 }
218
219 fprintf(fp_ou, "<tr><td class=\"header_l\">%s</td>\n", _("TOTAL") );
220 tt=0;
221 for( i = 0; i < hours.len; i++ ) {
222 if (tttime[i]>0) {
223 fprintf(fp_ou, "<td class=\"header_r\">%s</td>\n",fixtime(tttime[i]));
224 tt+=tttime[i];
225 } else
226 fputs("<td class=\"header_r\"></td>\n",fp_ou);
227 }
228 fprintf(fp_ou, "<td class=\"header_r\">%s</td></tr>\n",fixtime(tt));
229 fputs("</table>\n",fp_ou);
230 #ifdef ENABLE_DOUBLE_CHECK_DATA
231 if (tt!=uinfo->elap) {
232 debuga(_("Total elapsed time is %"PRIi64" instead of %"PRIi64" in the hourly report of user %s\n"),
233 (int64_t)tt,(int64_t)uinfo->elap,uinfo->label);
234 exit(EXIT_FAILURE);
235 }
236 #endif
237 }
238
239 if (write_html_trailer(fp_ou)<0)
240 debuga(_("Write error in file %s\n"),arqout);
241 if (fclose(fp_ou)==EOF)
242 debuga(_("Failed to close file %s - %s\n"),arqout,strerror(errno));
243 return;
244 }