]> git.ipfire.org Git - thirdparty/sarg.git/blame - repday.c
Add support to decompress xz files
[thirdparty/sarg.git] / repday.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
110ce984 3 * 1998, 2015
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
30extern numlist hours;
31
f2ec8c75 32void 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)) {
af961877 65 debuga(__FILE__,__LINE__,_("Path too long: "));
041018b6 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) {
af961877 71 debuga(__FILE__,__LINE__,_("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) {
af961877 83 debuga(__FILE__,__LINE__,_("Invalid date in file \"%s\"\n"),wdirname);
9bd92830
FM
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)) {
af961877 92 debuga(__FILE__,__LINE__,_("Too many different dates in file \"%s\"\n"),wdirname);
9bd92830
FM
93 exit(EXIT_FAILURE);
94 }
95 daylist[ndaylist++]=daynum;
96 }
97
98 if (getword_atoll(&hour,&gwarea,'\t')<0) {
af961877 99 debuga(__FILE__,__LINE__,_("Invalid time in file \"%s\"\n"),wdirname);
9bd92830
FM
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) {
af961877 108 debuga(__FILE__,__LINE__,_("Invalid number of bytes in file \"%s\"\n"),wdirname);
9bd92830
FM
109 exit(EXIT_FAILURE);
110 }
111 tbytes[i]+=bytes;
112 }
113 if ((datetimeby & DATETIME_ELAP)!=0) {
114 if (getword_atoll(&elap,&gwarea,'\0')<0) {
af961877 115 debuga(__FILE__,__LINE__,_("Invalid elapsed time in file \"%s\"\n"),wdirname);
9bd92830
FM
116 exit(EXIT_FAILURE);
117 }
118 telap[i]+=elap;
119 }
120 }
204781f4 121 if (fclose(fp_in)==EOF) {
af961877 122 debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wdirname,strerror(errno));
204781f4
FM
123 exit(EXIT_FAILURE);
124 }
9bd92830
FM
125
126 if((fp_ou=fopen(arqout,"w"))==NULL) {
af961877 127 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),arqout,strerror(errno));
007905af 128 exit(EXIT_FAILURE);
9bd92830
FM
129 }
130
131 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Day report"),HTML_JS_NONE);
132 fprintf(fp_ou,"<tr><td class=\"header_c\" colspan=\"2\">%s:&nbsp;%s</td></tr>\n",_("Period"),period.html);
133 fprintf(fp_ou,"<tr><th class=\"header_c\" colspan=\"2\">%s:&nbsp;%s</th></tr>\n",_("User"),uinfo->label);
134 close_html_header(fp_ou);
135
136 dayidx=0;
137 for (i=0 ; i<ndaylist ; i++) {
138 daynum=daylist[i];
139 for (j=dayidx ; j>0 && daynum<daylist[daysort[j-1]] ; j--) daysort[j]=daysort[j-1];
140 daysort[j]=i;
141 dayidx++;
142 }
143
144 if((datetimeby & DATETIME_BYTE)!=0) {
145 label=_("BYTES");
146 fputs("<table class=\"report\" cellpadding=\"0\" cellspacing=\"2\">\n", fp_ou);
147 fputs("<tr><th class=\"header_c\"></th>",fp_ou);
63aa600d
FM
148 for( i = 0; i < hours.len; i++ ) {
149 /* TRANSLATORS: It is an hour in the hourly report */
150 sprintf(data,_("%02dH"),hours.list[ i ]);
151 fprintf(fp_ou, "<td class=\"header_c\">%s<br>%s</td>\n", data, label );
152 }
9bd92830
FM
153 fprintf(fp_ou, "<td class=\"header_c\">%s<br>%s</td></tr>\n", _("TOTAL"), label );
154
155 memset(tttime,0,sizeof(tttime));
156 for (dayidx=0 ; dayidx<ndaylist ; dayidx++) {
157 daynum=daysort[dayidx];
158 day=daylist[daynum]%100;
159 month=(daylist[daynum]/100)%100;
160 year=daylist[daynum]/10000;
161 computedate(year,month,day,&t);
162 strftime(data,sizeof(data),"%x",&t);
163 fprintf(fp_ou, "<tr><td class=\"data\">%s</td>\n", data );
164 tt=0;
165 for( i = 0; i < hours.len; i++ ) {
166 ihour=hours.list[i];
167 if (tbytes[daynum*24+ihour]>0) {
168 fprintf(fp_ou, "<td class=\"data\">%s</td>\n",fixnum(tbytes[daynum*24+ihour],1));
169 tt+=tbytes[daynum*24+ihour];
170 tttime[ihour]+=tbytes[daynum*24+ihour];
171 } else
172 fputs("<td class=\"data\"></td>\n",fp_ou);
173 }
174 fprintf(fp_ou, "<td class=\"data\">%s</td></tr>\n",fixnum(tt,1));
175 }
176
177 fprintf(fp_ou, "<tr><td class=\"header_l\">%s</td>\n", _("TOTAL") );
178 tt=0;
179 for( i = 0; i < hours.len; i++ ) {
180 if (tttime[i]>0) {
181 fprintf(fp_ou, "<td class=\"header_r\">%s</td>\n",fixnum(tttime[i],1));
182 tt+=tttime[i];
183 } else
184 fputs("<td class=\"header_r\"></td>\n",fp_ou);
185 }
186 fprintf(fp_ou, "<td class=\"header_r\">%s</td></tr>\n",fixnum(tt,1));
187 fputs("</table>\n",fp_ou);
d91457d2
FM
188#ifdef ENABLE_DOUBLE_CHECK_DATA
189 if (tt!=uinfo->nbytes) {
af961877 190 debuga(__FILE__,__LINE__,_("Total downloaded bytes is %"PRIi64" instead of %"PRIi64" in the hourly report of user %s\n"),
d91457d2
FM
191 (int64_t)tt,(int64_t)uinfo->nbytes,uinfo->label);
192 exit(EXIT_FAILURE);
193 }
194#endif
9bd92830
FM
195 }
196
197 if((datetimeby & DATETIME_ELAP)!=0) {
198 label=_("H:M:S");
199 fputs("<table class=\"report\" cellpadding=\"0\" cellspacing=\"2\">\n", fp_ou);
200 fputs("<tr><th class=\"header_c\"></th>",fp_ou);
63aa600d
FM
201 for( i = 0; i < hours.len; i++ ) {
202 sprintf(data,_("%02dH"),hours.list[ i ]);
203 fprintf(fp_ou, "<td class=\"header_c\">%s<br>%s</td>\n", data, label );
204 }
9bd92830
FM
205 fprintf(fp_ou, "<td class=\"header_c\">%s<br>%s</td></tr>\n", _("TOTAL"), label );
206
207 memset(tttime,0,sizeof(tttime));
208 for (dayidx=0 ; dayidx<ndaylist ; dayidx++) {
209 daynum=daysort[dayidx];
210 day=daylist[daynum]%100;
211 month=(daylist[daynum]/100)%100;
212 year=daylist[daynum]/10000;
213 computedate(year,month,day,&t);
214 strftime(data,sizeof(data),"%x",&t);
215 fprintf(fp_ou, "<tr><td class=\"data\">%s</td>\n", data );
216 tt=0;
217 for( i = 0; i < hours.len; i++ ) {
218 ihour=hours.list[i];
219 if (telap[daynum*24+ihour]>0) {
220 fprintf(fp_ou, "<td class=\"data\">%s</td>\n",fixtime(telap[daynum*24+ihour]));
221 tt+=telap[daynum*24+ihour];
222 tttime[ihour]+=telap[daynum*24+ihour];
223 } else
224 fputs("<td class=\"data\"></td>\n",fp_ou);
225 }
226 fprintf(fp_ou, "<td class=\"data\">%s</td></tr>\n",fixtime(tt));
227 }
228
229 fprintf(fp_ou, "<tr><td class=\"header_l\">%s</td>\n", _("TOTAL") );
230 tt=0;
231 for( i = 0; i < hours.len; i++ ) {
232 if (tttime[i]>0) {
233 fprintf(fp_ou, "<td class=\"header_r\">%s</td>\n",fixtime(tttime[i]));
234 tt+=tttime[i];
235 } else
236 fputs("<td class=\"header_r\"></td>\n",fp_ou);
237 }
238 fprintf(fp_ou, "<td class=\"header_r\">%s</td></tr>\n",fixtime(tt));
239 fputs("</table>\n",fp_ou);
d91457d2
FM
240#ifdef ENABLE_DOUBLE_CHECK_DATA
241 if (tt!=uinfo->elap) {
af961877 242 debuga(__FILE__,__LINE__,_("Total elapsed time is %"PRIi64" instead of %"PRIi64" in the hourly report of user %s\n"),
d91457d2
FM
243 (int64_t)tt,(int64_t)uinfo->elap,uinfo->label);
244 exit(EXIT_FAILURE);
245 }
246#endif
9bd92830
FM
247 }
248
342bd723 249 write_html_trailer(fp_ou);
507460ae 250 if (fclose(fp_ou)==EOF) {
af961877 251 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),arqout,strerror(errno));
507460ae
FM
252 exit(EXIT_FAILURE);
253 }
9bd92830 254 return;
25697a35 255}