]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
94ff9470 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
67302a9e | 3 | * 1998, 2013 |
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 GS |
29 | |
30 | extern numlist hours; | |
31 | ||
f2ec8c75 | 32 | void report_day(const struct userinfostruct *uinfo) |
25697a35 | 33 | { |
9bd92830 FM |
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 | ||
e3e6aef4 | 60 | if (datetimeby==0) return; |
9bd92830 FM |
61 | snprintf(wdirname,sizeof(wdirname),"%s/%s.day",tmp,uinfo->filename); |
62 | if(access(wdirname, R_OK) != 0) return; | |
63 | ||
64 | if (snprintf(arqout,sizeof(arqout),"%s/%s/d%s.html",outdirname,uinfo->filename,uinfo->filename)>=sizeof(arqout)) { | |
041018b6 FM |
65 | debuga(_("Path too long: ")); |
66 | debuga_more("%s/%s/d%s.html\n",outdirname,uinfo->filename,uinfo->filename); | |
9bd92830 FM |
67 | exit(EXIT_FAILURE); |
68 | } | |
69 | ||
70 | if((fp_in=fopen(wdirname,"r"))==NULL) { | |
96dadc9f | 71 | debuga(_("Cannot open file \"%s\": %s\n"),wdirname,strerror(errno)); |
007905af | 72 | exit(EXIT_FAILURE); |
9bd92830 FM |
73 | } |
74 | ||
75 | memset(tbytes,0,sizeof(tbytes)); | |
76 | memset(telap,0,sizeof(telap)); | |
77 | ndaylist=0; | |
78 | ||
79 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
80 | fixendofline(buf); | |
81 | getword_start(&gwarea,buf); | |
82 | if (getword(data,sizeof(data),&gwarea,'\t')<0) { | |
83 | debuga(_("Invalid date in file %s\n"),wdirname); | |
84 | exit(EXIT_FAILURE); | |
85 | } | |
86 | if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue; | |
87 | if (day<1 || day>31 || month<1 || month>12 || year>9999) continue; | |
88 | daynum=(year*10000)+(month*100)+day; | |
89 | for (dayidx=0 ; dayidx<ndaylist && daynum!=daylist[dayidx] ; dayidx++); | |
90 | if (dayidx>=ndaylist) { | |
91 | if (dayidx>=sizeof(daylist)/sizeof(*daylist)) { | |
92 | debuga(_("Too many different dates in %s\n"),wdirname); | |
93 | exit(EXIT_FAILURE); | |
94 | } | |
95 | daylist[ndaylist++]=daynum; | |
96 | } | |
97 | ||
98 | if (getword_atoll(&hour,&gwarea,'\t')<0) { | |
99 | debuga(_("Invalid time in file %s\n"),wdirname); | |
100 | exit(EXIT_FAILURE); | |
101 | } | |
102 | ihour=(int)hour; | |
103 | i=dayidx*24+ihour; | |
104 | ||
105 | if ((datetimeby & DATETIME_BYTE)!=0) { | |
106 | colsep=((datetimeby & DATETIME_ELAP)!=0) ? '\t' : '\0'; | |
107 | if (getword_atoll(&bytes,&gwarea,colsep)<0) { | |
108 | debuga(_("Invalid number of bytes in file %s\n"),wdirname); | |
109 | exit(EXIT_FAILURE); | |
110 | } | |
111 | tbytes[i]+=bytes; | |
112 | } | |
113 | if ((datetimeby & DATETIME_ELAP)!=0) { | |
114 | if (getword_atoll(&elap,&gwarea,'\0')<0) { | |
115 | debuga(_("Invalid elapsed time in file %s\n"),wdirname); | |
116 | exit(EXIT_FAILURE); | |
117 | } | |
118 | telap[i]+=elap; | |
119 | } | |
120 | } | |
121 | fclose(fp_in); | |
122 | ||
123 | if((fp_ou=fopen(arqout,"w"))==NULL) { | |
96dadc9f | 124 | debuga(_("Cannot open file \"%s\": %s\n"),arqout,strerror(errno)); |
007905af | 125 | exit(EXIT_FAILURE); |
9bd92830 FM |
126 | } |
127 | ||
128 | write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Day report"),HTML_JS_NONE); | |
129 | fprintf(fp_ou,"<tr><td class=\"header_c\" colspan=\"2\">%s: %s</td></tr>\n",_("Period"),period.html); | |
130 | fprintf(fp_ou,"<tr><th class=\"header_c\" colspan=\"2\">%s: %s</th></tr>\n",_("User"),uinfo->label); | |
131 | close_html_header(fp_ou); | |
132 | ||
133 | dayidx=0; | |
134 | for (i=0 ; i<ndaylist ; i++) { | |
135 | daynum=daylist[i]; | |
136 | for (j=dayidx ; j>0 && daynum<daylist[daysort[j-1]] ; j--) daysort[j]=daysort[j-1]; | |
137 | daysort[j]=i; | |
138 | dayidx++; | |
139 | } | |
140 | ||
141 | if((datetimeby & DATETIME_BYTE)!=0) { | |
142 | label=_("BYTES"); | |
143 | fputs("<table class=\"report\" cellpadding=\"0\" cellspacing=\"2\">\n", fp_ou); | |
144 | fputs("<tr><th class=\"header_c\"></th>",fp_ou); | |
63aa600d FM |
145 | for( i = 0; i < hours.len; i++ ) { |
146 | /* TRANSLATORS: It is an hour in the hourly report */ | |
147 | sprintf(data,_("%02dH"),hours.list[ i ]); | |
148 | fprintf(fp_ou, "<td class=\"header_c\">%s<br>%s</td>\n", data, label ); | |
149 | } | |
9bd92830 FM |
150 | fprintf(fp_ou, "<td class=\"header_c\">%s<br>%s</td></tr>\n", _("TOTAL"), label ); |
151 | ||
152 | memset(tttime,0,sizeof(tttime)); | |
153 | for (dayidx=0 ; dayidx<ndaylist ; dayidx++) { | |
154 | daynum=daysort[dayidx]; | |
155 | day=daylist[daynum]%100; | |
156 | month=(daylist[daynum]/100)%100; | |
157 | year=daylist[daynum]/10000; | |
158 | computedate(year,month,day,&t); | |
159 | strftime(data,sizeof(data),"%x",&t); | |
160 | fprintf(fp_ou, "<tr><td class=\"data\">%s</td>\n", data ); | |
161 | tt=0; | |
162 | for( i = 0; i < hours.len; i++ ) { | |
163 | ihour=hours.list[i]; | |
164 | if (tbytes[daynum*24+ihour]>0) { | |
165 | fprintf(fp_ou, "<td class=\"data\">%s</td>\n",fixnum(tbytes[daynum*24+ihour],1)); | |
166 | tt+=tbytes[daynum*24+ihour]; | |
167 | tttime[ihour]+=tbytes[daynum*24+ihour]; | |
168 | } else | |
169 | fputs("<td class=\"data\"></td>\n",fp_ou); | |
170 | } | |
171 | fprintf(fp_ou, "<td class=\"data\">%s</td></tr>\n",fixnum(tt,1)); | |
172 | } | |
173 | ||
174 | fprintf(fp_ou, "<tr><td class=\"header_l\">%s</td>\n", _("TOTAL") ); | |
175 | tt=0; | |
176 | for( i = 0; i < hours.len; i++ ) { | |
177 | if (tttime[i]>0) { | |
178 | fprintf(fp_ou, "<td class=\"header_r\">%s</td>\n",fixnum(tttime[i],1)); | |
179 | tt+=tttime[i]; | |
180 | } else | |
181 | fputs("<td class=\"header_r\"></td>\n",fp_ou); | |
182 | } | |
183 | fprintf(fp_ou, "<td class=\"header_r\">%s</td></tr>\n",fixnum(tt,1)); | |
184 | fputs("</table>\n",fp_ou); | |
d91457d2 FM |
185 | #ifdef ENABLE_DOUBLE_CHECK_DATA |
186 | if (tt!=uinfo->nbytes) { | |
187 | debuga(_("Total downloaded bytes is %"PRIi64" instead of %"PRIi64" in the hourly report of user %s\n"), | |
188 | (int64_t)tt,(int64_t)uinfo->nbytes,uinfo->label); | |
189 | exit(EXIT_FAILURE); | |
190 | } | |
191 | #endif | |
9bd92830 FM |
192 | } |
193 | ||
194 | if((datetimeby & DATETIME_ELAP)!=0) { | |
195 | label=_("H:M:S"); | |
196 | fputs("<table class=\"report\" cellpadding=\"0\" cellspacing=\"2\">\n", fp_ou); | |
197 | fputs("<tr><th class=\"header_c\"></th>",fp_ou); | |
63aa600d FM |
198 | for( i = 0; i < hours.len; i++ ) { |
199 | sprintf(data,_("%02dH"),hours.list[ i ]); | |
200 | fprintf(fp_ou, "<td class=\"header_c\">%s<br>%s</td>\n", data, label ); | |
201 | } | |
9bd92830 FM |
202 | fprintf(fp_ou, "<td class=\"header_c\">%s<br>%s</td></tr>\n", _("TOTAL"), label ); |
203 | ||
204 | memset(tttime,0,sizeof(tttime)); | |
205 | for (dayidx=0 ; dayidx<ndaylist ; dayidx++) { | |
206 | daynum=daysort[dayidx]; | |
207 | day=daylist[daynum]%100; | |
208 | month=(daylist[daynum]/100)%100; | |
209 | year=daylist[daynum]/10000; | |
210 | computedate(year,month,day,&t); | |
211 | strftime(data,sizeof(data),"%x",&t); | |
212 | fprintf(fp_ou, "<tr><td class=\"data\">%s</td>\n", data ); | |
213 | tt=0; | |
214 | for( i = 0; i < hours.len; i++ ) { | |
215 | ihour=hours.list[i]; | |
216 | if (telap[daynum*24+ihour]>0) { | |
217 | fprintf(fp_ou, "<td class=\"data\">%s</td>\n",fixtime(telap[daynum*24+ihour])); | |
218 | tt+=telap[daynum*24+ihour]; | |
219 | tttime[ihour]+=telap[daynum*24+ihour]; | |
220 | } else | |
221 | fputs("<td class=\"data\"></td>\n",fp_ou); | |
222 | } | |
223 | fprintf(fp_ou, "<td class=\"data\">%s</td></tr>\n",fixtime(tt)); | |
224 | } | |
225 | ||
226 | fprintf(fp_ou, "<tr><td class=\"header_l\">%s</td>\n", _("TOTAL") ); | |
227 | tt=0; | |
228 | for( i = 0; i < hours.len; i++ ) { | |
229 | if (tttime[i]>0) { | |
230 | fprintf(fp_ou, "<td class=\"header_r\">%s</td>\n",fixtime(tttime[i])); | |
231 | tt+=tttime[i]; | |
232 | } else | |
233 | fputs("<td class=\"header_r\"></td>\n",fp_ou); | |
234 | } | |
235 | fprintf(fp_ou, "<td class=\"header_r\">%s</td></tr>\n",fixtime(tt)); | |
236 | fputs("</table>\n",fp_ou); | |
d91457d2 FM |
237 | #ifdef ENABLE_DOUBLE_CHECK_DATA |
238 | if (tt!=uinfo->elap) { | |
239 | debuga(_("Total elapsed time is %"PRIi64" instead of %"PRIi64" in the hourly report of user %s\n"), | |
240 | (int64_t)tt,(int64_t)uinfo->elap,uinfo->label); | |
241 | exit(EXIT_FAILURE); | |
242 | } | |
243 | #endif | |
9bd92830 FM |
244 | } |
245 | ||
342bd723 | 246 | write_html_trailer(fp_ou); |
507460ae | 247 | if (fclose(fp_ou)==EOF) { |
96dadc9f | 248 | debuga(_("Write error in \"%s\": %s\n"),arqout,strerror(errno)); |
507460ae FM |
249 | exit(EXIT_FAILURE); |
250 | } | |
9bd92830 | 251 | return; |
25697a35 | 252 | } |