]> git.ipfire.org Git - thirdparty/sarg.git/blob - totday.c
Update the Russian translation.
[thirdparty/sarg.git] / totday.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2013
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 //! The daily statistics for one user.
31 struct DayStruct
32 {
33 int ndaylist;
34 int maxindex;
35 int daylist[MAX_DATETIME_DAYS];
36 long long int bytes[MAX_DATETIME_DAYS*24];
37 long long int elap[MAX_DATETIME_DAYS*24];
38 };
39
40 /*!
41 Prepare the object to store the daily statistics of one user.
42
43 \return The object to pass to other functions in this module.
44 The object must be freed with a call to day_cleanup().
45 */
46 DayObject day_prepare(void)
47 {
48 DayObject ddata;
49
50 ddata=(DayObject)malloc(sizeof(*ddata));
51 if (!ddata)
52 {
53 debuga(_("Not enough memory to store the daily statistics\n"));
54 exit(EXIT_FAILURE);
55 }
56
57 return(ddata);
58 }
59
60 /*!
61 Free the memory allocated by day_prepare().
62
63 \param ddata The object returned by day_prepare().
64 */
65 void day_cleanup(DayObject ddata)
66 {
67 if (ddata) free(ddata);
68 }
69
70 /*!
71 Prepare the object for a new user.
72
73 \param ddata The object created by day_prepare().
74 */
75 void day_newuser(DayObject ddata)
76 {
77 if (ddata)
78 {
79 ddata->ndaylist=0;
80 ddata->maxindex=0;
81 memset(ddata->bytes,0,sizeof(ddata->bytes));
82 memset(ddata->elap,0,sizeof(ddata->elap));
83 }
84 }
85
86 /*!
87 Store one data point in the statistics.
88
89 \param ddata The object to store the statistics.
90 \param date The date of the data point formated as day/month/year.
91 \param time The time of the data point.
92 \param elap The time spent processing the user's request on the proxy.
93 \param bytes The number of bytes transfered by the user.
94 */
95 void day_addpoint(DayObject ddata,const char *date, const char *time, long long int elap, long long int bytes)
96 {
97 int hour;
98 int day,month,year;
99 int daynum;
100 int dayidx;
101 int i;
102
103 if (sscanf(date,"%d/%d/%d",&day,&month,&year)!=3) {
104 debuga(_("Invalid date \"%s\" for the hourly statistics\n"),date);
105 exit(EXIT_FAILURE);
106 }
107 if (day<1 || day>31 || month<1 || month>12 || year>9999) {
108 debuga(_("Invalid date component in \"%s\" for the hourly statistics\n"),date);
109 exit(EXIT_FAILURE);
110 }
111 hour=atoi(time);
112 if (hour<0 || hour>=24) {
113 debuga(_("Invalid hour %d for the hourly statistics\n"),hour);
114 exit(EXIT_FAILURE);
115 }
116 daynum=(year*10000)+(month*100)+day;
117 for (dayidx=ddata->ndaylist-1 ; dayidx>=0 && daynum!=ddata->daylist[dayidx] ; dayidx--);
118 if (dayidx<0) {
119 dayidx=ddata->ndaylist++;
120 if (dayidx>=sizeof(ddata->daylist)/sizeof(*ddata->daylist)) {
121 debuga(_("Too many different dates for the hourly statistics\n"));
122 exit(EXIT_FAILURE);
123 }
124 ddata->daylist[dayidx]=daynum;
125 }
126 i=dayidx*24+hour;
127 if (i>=ddata->maxindex) ddata->maxindex=i+1;
128 ddata->bytes[i]+=bytes;
129 ddata->elap[i]+=elap;
130 }
131
132 /*!
133 Store the dayly statistics in the file.
134
135 \param ddata The object containing the statistics.
136 \param tmp The temporary directory to store the file into.
137 \param uinfo The user's data.
138 */
139 void day_totalize(DayObject ddata,const char *tmp, const struct userinfostruct *uinfo)
140 {
141 FILE *fp_ou;
142 int hour;
143 int day,month,year;
144 int i;
145 int daynum;
146 int dayidx;
147 char arqout[2048];
148 #ifdef ENABLE_DOUBLE_CHECK_DATA
149 long long int tt=0;
150 #endif
151
152 if (datetimeby==0) return;
153 if (!ddata) return;
154
155 if (snprintf(arqout,sizeof(arqout),"%s/%s.day",tmp,uinfo->filename)>=sizeof(arqout)) {
156 debuga(_("Path too long: "));
157 debuga_more("%s/%s%s\n",tmp,uinfo->filename,".day");
158 exit(EXIT_FAILURE);
159 }
160
161 if((fp_ou=fopen(arqout,"w"))==NULL) {
162 debugapos("totday",_("Cannot open file \"%s\": %s\n"),arqout,strerror(errno));
163 exit(EXIT_FAILURE);
164 }
165
166 for (i=0 ; i<ddata->maxindex ; i++) {
167 if (ddata->bytes[i]==0 && ddata->elap[i]==0) continue;
168 dayidx=i/24;
169 if (dayidx>=sizeof(ddata->daylist)/sizeof(*ddata->daylist)) {
170 debuga(_("Invalid day index found in the hourly statistics\n"));
171 exit(EXIT_FAILURE);
172 }
173 hour=i%24;
174 daynum=ddata->daylist[dayidx];
175 day=daynum%100;
176 month=(daynum/100)%100;
177 year=daynum/10000;
178 fprintf(fp_ou,"%d/%d/%d\t%d",day,month,year,hour);
179 if ((datetimeby & DATETIME_BYTE)!=0) fprintf(fp_ou,"\t%"PRIu64"",(uint64_t)ddata->bytes[i]);
180 if ((datetimeby & DATETIME_ELAP)!=0) fprintf(fp_ou,"\t%"PRIu64"",(uint64_t)ddata->elap[i]);
181 fputs("\n",fp_ou);
182 #ifdef ENABLE_DOUBLE_CHECK_DATA
183 tt+=ddata->bytes[i];
184 #endif
185 }
186
187 if (fclose(fp_ou)==EOF) {
188 debuga(_("Failed to close file \"%s\": %s\n"),arqout,strerror(errno));
189 exit(EXIT_FAILURE);
190 }
191 #ifdef ENABLE_DOUBLE_CHECK_DATA
192 if (tt!=uinfo->nbytes) {
193 debuga(_("Total downloaded bytes is %"PRIi64" instead of %"PRIi64" in the hourly report of user %s\n"),
194 (int64_t)tt,(int64_t)uinfo->nbytes,uinfo->label);
195 exit(EXIT_FAILURE);
196 }
197 #endif
198 return;
199 }