]>
git.ipfire.org Git - thirdparty/sarg.git/blob - totday.c
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
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.
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.
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.
27 #include "include/conf.h"
28 #include "include/defs.h"
30 //! The daily statistics for one user.
35 int daylist
[MAX_DATETIME_DAYS
];
36 long long int bytes
[MAX_DATETIME_DAYS
*24];
37 long long int elap
[MAX_DATETIME_DAYS
*24];
41 Prepare the object to store the daily statistics of one user.
43 \return The object to pass to other functions in this module.
44 The object must be freed with a call to day_cleanup().
46 DayObject
day_prepare(void)
50 ddata
=(DayObject
)malloc(sizeof(*ddata
));
53 debuga(_("Not enough memory to store the daily statistics\n"));
61 Free the memory allocated by day_prepare().
63 \param ddata The object returned by day_prepare().
65 void day_cleanup(DayObject ddata
)
67 if (ddata
) free(ddata
);
71 Prepare the object for a new user.
73 \param ddata The object created by day_prepare().
75 void day_newuser(DayObject ddata
)
81 memset(ddata
->bytes
,0,sizeof(ddata
->bytes
));
82 memset(ddata
->elap
,0,sizeof(ddata
->elap
));
87 Store one data point in the statistics.
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.
95 void day_addpoint(DayObject ddata
,const char *date
, const char *time
, long long int elap
, long long int bytes
)
104 if (sscanf(date
,"%d/%d/%d",&day
,&month
,&year
)!=3) {
105 debuga(_("Invalid date \"%s\" for the hourly statistics\n"),date
);
108 if (day
<1 || day
>31 || month
<1 || month
>12 || year
>9999) {
109 debuga(_("Invalid date component in \"%s\" for the hourly statistics\n"),date
);
113 if (hour
<0 || hour
>=24) {
114 debuga(_("Invalid hour %d for the hourly statistics\n"),hour
);
117 daynum
=(year
*10000)+(month
*100)+day
;
118 for (dayidx
=ddata
->ndaylist
-1 ; dayidx
>=0 && daynum
!=ddata
->daylist
[dayidx
] ; dayidx
--);
120 dayidx
=ddata
->ndaylist
++;
121 if (dayidx
>=sizeof(ddata
->daylist
)/sizeof(*ddata
->daylist
)) {
122 debuga(_("Too many different dates for the hourly statistics\n"));
125 ddata
->daylist
[dayidx
]=daynum
;
128 if (i
>=ddata
->maxindex
) ddata
->maxindex
=i
+1;
129 ddata
->bytes
[i
]+=bytes
;
130 ddata
->elap
[i
]+=elap
;
134 Store the dayly statistics in the file.
136 \param ddata The object containing the statistics.
137 \param tmp The temporary directory to store the file into.
138 \param uinfo The user's data.
140 void day_totalize(DayObject ddata
,const char *tmp
, const struct userinfostruct
*uinfo
)
150 if (datetimeby
==0) return;
153 if (snprintf(arqout
,sizeof(arqout
),"%s/%s.day",tmp
,uinfo
->filename
)>=sizeof(arqout
)) {
154 debuga(_("File name too long: %s/%s%s\n"),tmp
,uinfo
->filename
,".day");
158 if((fp_ou
=fopen(arqout
,"w"))==NULL
) {
159 debuga(_("(totday) Cannot open log file %s: %s\n"),arqout
,strerror(errno
));
163 for (i
=0 ; i
<ddata
->maxindex
; i
++) {
164 if (ddata
->bytes
[i
]==0 && ddata
->elap
[i
]==0) continue;
166 if (dayidx
>=sizeof(ddata
->daylist
)/sizeof(*ddata
->daylist
)) {
167 debuga(_("Invalid day index found in the hourly statistics\n"));
171 daynum
=ddata
->daylist
[dayidx
];
173 month
=(daynum
/100)%100;
175 fprintf(fp_ou
,"%d/%d/%d\t%d",day
,month
,year
,hour
);
176 if ((datetimeby
& DATETIME_BYTE
)!=0) fprintf(fp_ou
,"\t%"PRIu64
"",(uint64_t)ddata
->bytes
[i
]);
177 if ((datetimeby
& DATETIME_ELAP
)!=0) fprintf(fp_ou
,"\t%"PRIu64
"",(uint64_t)ddata
->elap
[i
]);
181 if (fclose(fp_ou
)==EOF
) {
182 debuga(_("Write error in %s: %s\n"),arqout
,strerror(errno
));