]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - repday.c
Tag the released 2.3 version.
[thirdparty/sarg.git] / repday.c
index 0a0f2504a6116d3dc4643b3405568549f03efaa0..4a610f1943fd9b8582142a565d43ac6db8e65d98 100644 (file)
--- a/repday.c
+++ b/repday.c
@@ -1,10 +1,11 @@
 /*
- * AUTHOR: Pedro Lineu Orso                         pedro.orso@gmail.com
- *                                                            1998, 2010
  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
+ *                                                            1998, 2010
  *
  * SARG donations:
  *      please look at http://sarg.sourceforge.net/donations.php
+ * Support:
+ *     http://sourceforge.net/projects/sarg/forums/forum/363374
  * ---------------------------------------------------------------------
  *
  *  This program is free software; you can redistribute it and/or modify
 
 extern numlist hours;
 
-void report_day(const char *user)
+void report_day(const struct userinfostruct *uinfo)
 {
 
    FILE *fp_in, *fp_ou;
 
+   char buf[200];
    char data[20];
-   char odata[20];
-   char hour[20];
-   char html[8000];
+   long long int hour;
+   const char *label;
    char arqout[MAXLEN];
-   char wdirname[MAXLEN];
-   char wuser[255];
-   char c[ 24 ][20];
-   int  count=0;
+   char wdirname[2048];
+   char colsep;
    int  ihour=0;
+   int day,month,year;
+   int daylist[MAX_DATETIME_DAYS];
+   int daysort[MAX_DATETIME_DAYS];
+   int ndaylist;
+   int daynum;
+   int dayidx;
+   long long int bytes;
    long long int elap;
-   long long int v[ 24 ] = { 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
-                            0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L };
-   long long int t[ 24 ] = { 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
-                            0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L };
-   long long int tt=0, ttt=0;
-   int i;
+   long long int tbytes[MAX_DATETIME_DAYS*24];
+   long long int telap[MAX_DATETIME_DAYS*24];
+   long long int tt;
+   long long int tttime[24];
+   int i, j;
    struct getwordstruct gwarea;
+   struct tm t;
 
-   sprintf(arqout,"%s/%s/d%s.html",dirname,user,user);
-   sprintf(wdirname,"%s/%s.day",tmp,user);
-
-   if(access(wdirname, R_OK) != 0)
-      return;
-
-   if((fp_in=fopen(wdirname,"r"))==NULL) {
-     fprintf(stderr, "SARG: (repday) %s: %s\n",text[8],wdirname);
-     exit(1);
-   }
-
-   if((fp_ou=fopen(arqout,"w"))==NULL) {
-     fprintf(stderr, "SARG: (repday) %s: %s\n",text[8],arqout);
-     exit(1);
-   }
-
-   fprintf(fp_ou, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
-   css(fp_ou);
-   fputs("</head>\n",fp_ou);
-   //if(strlen(FontFace) > 0) fprintf(fp_ou,"<font face=%s>\n",FontFace);
-   fprintf(fp_ou,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage);
-   write_logo_image(fp_ou);
-
-   if(strcmp(IndexTree,"date") == 0)
-      show_sarg(fp_ou, "../../../..");
-   else
-      show_sarg(fp_ou, "../..");
+   snprintf(wdirname,sizeof(wdirname),"%s/%s.day",tmp,uinfo->filename);
+   if(access(wdirname, R_OK) != 0) return;
 
-   fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou);
-   fprintf(fp_ou,"<tr><th class=\"title\" colspan=\"2\">%s</th></tr>\n",Title);
-
-   fputs("</table></div>\n",fp_ou);
-   fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou);
-
-   fprintf(fp_ou,"<tr><td class=\"header3\" colspan=\"2\">%s:&nbsp;%s</td></tr>\n",text[89],period);
-
-   strcpy(wuser,user);
-   if(strstr(wuser,"_") != 0)
-      fixip(wuser);
-
-   if(Ip2Name)
-      if((str=(char *) strstr(name, ".")) != (char *) NULL) {
-         if((str=(char *) strstr(str+1, ".")) != (char *) NULL)
-            ip2name(wuser,sizeof(wuser));
+   if (snprintf(arqout,sizeof(arqout),"%s/%s/d%s.html",outdirname,uinfo->filename,uinfo->filename)>=sizeof(arqout)) {
+      debuga(_("Output file name too long: %s/%s/d%s.html\n"),outdirname,uinfo->filename,uinfo->filename);
+      exit(EXIT_FAILURE);
    }
 
-   user_find(name, sizeof(name), wuser);
-
-   if(dotinuser && strchr(name,'_')) {
-      subs(name,sizeof(name),"_",".");
+   if((fp_in=fopen(wdirname,"r"))==NULL) {
+     debuga(_("(repday) Cannot open log file %s\n"),wdirname);
+     exit(EXIT_FAILURE);
    }
 
-   fprintf(fp_ou,"<tr><th class=\"header3\" colspan=\"2\">%s:&nbsp;%s</th></tr>\n",text[90],name);
-
-   fputs("<tr><td></td></tr><tr><td></td></tr>\n",fp_ou);
-   fputs("<tr><td></td></tr><tr><td></td></tr></table>\n",fp_ou);
-
-   fputs("<table cellpadding=\"0\" cellspacing=\"2\">\n", fp_ou);
-
-   fputs( "<tr><td></td>\n", fp_ou );
-
-   if(strcmp(datetimeby,"bytes") == 0)
-     strcpy( html, text[93] );
-   else
-     strcpy( html, "H:M:S" );
-
-   for( i = 0; i < hours.len; i++ )
-     fprintf( fp_ou, "<td class=\"header3\">%02dH<br>%s</td>\n", hours.list[ i ], html );
-   fprintf( fp_ou,
-     "<td class=\"header3\">%s<br>%s</td></tr>\n", text[107], html );
+   memset(tbytes,0,sizeof(tbytes));
+   memset(telap,0,sizeof(telap));
+   ndaylist=0;
 
    while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
       fixendofline(buf);
       getword_start(&gwarea,buf);
       if (getword(data,sizeof(data),&gwarea,'\t')<0) {
-         printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdirname);
-         exit(1);
+         debuga(_("Invalid date in file %s\n"),wdirname);
+         exit(EXIT_FAILURE);
       }
-      if(!count) {
-         strcpy(odata,data);
-         count++;
+      if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue;
+      if (day<1 || day>31 || month<1 || month>12 || year>9999) continue;
+      daynum=(year*10000)+(month*100)+day;
+      for (dayidx=0 ; dayidx<ndaylist && daynum!=daylist[dayidx] ; dayidx++);
+      if (dayidx>=ndaylist) {
+         if (dayidx>=sizeof(daylist)/sizeof(*daylist)) {
+            debuga(_("Too many different dates in %s\n"),wdirname);
+            exit(EXIT_FAILURE);
+         }
+         daylist[ndaylist++]=daynum;
       }
 
-      if (getword(hour,sizeof(hour),&gwarea,'\t')<0 || getword_atoll(&elap,&gwarea,'\t')<0) {
-         printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdirname);
-         exit(1);
+      if (getword_atoll(&hour,&gwarea,'\t')<0) {
+         debuga(_("Invalid time in file %s\n"),wdirname);
+         exit(EXIT_FAILURE);
       }
+      ihour=(int)hour;
+      i=dayidx*24+ihour;
+
+      if ((datetimeby & DATETIME_BYTE)!=0) {
+         colsep=((datetimeby & DATETIME_ELAP)!=0) ? '\t' : '\0';
+         if (getword_atoll(&bytes,&gwarea,colsep)<0) {
+            debuga(_("Invalid number of bytes in file %s\n"),wdirname);
+            exit(EXIT_FAILURE);
+         }
+         tbytes[i]+=bytes;
+      }
+      if ((datetimeby & DATETIME_ELAP)!=0) {
+         if (getword_atoll(&elap,&gwarea,'\0')<0) {
+            debuga(_("Invalid elapsed time in file %s\n"),wdirname);
+            exit(EXIT_FAILURE);
+         }
+         telap[i]+=elap;
+      }
+   }
+   fclose(fp_in);
 
-      if(strcmp(data,odata) != 0) {
-         for( i = 0; i < hours.len; i++ )
-            sprintf(c[ hours.list[ i ] ],"%s",fixtime(v[ hours.list[ i ] ]));
-
-         for( i = 8; i <= 17; i++ )
-            if(strcmp(c[ hours.list[ i ] ],"00:00:00") == 0)
-               bzero(c[ hours.list[ i ] ],12);
+   if((fp_ou=fopen(arqout,"w"))==NULL) {
+     debuga(_("(repday) Cannot open log file %s\n"),arqout);
+     exit(EXIT_FAILURE);
+   }
 
-         fprintf(fp_ou, "<tr><td class=\"data\">%s</td>\n", odata );
-         for( i = 0; i < hours.len; i++ )
-            fprintf(fp_ou,"<td class=\"data\">%s</td>\n", c[ hours.list[ i ] ] );
-         fprintf( fp_ou, "<td class=\"data\">%s</td>\n", fixtime(tt) );
+   write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Day report"));
+   fprintf(fp_ou,"<tr><td class=\"header_c\" colspan=\"2\">%s:&nbsp;%s</td></tr>\n",_("Period"),period.html);
+   fprintf(fp_ou,"<tr><th class=\"header_c\" colspan=\"2\">%s:&nbsp;%s</th></tr>\n",_("User"),uinfo->label);
+   close_html_header(fp_ou);
+
+   dayidx=0;
+   for (i=0 ; i<ndaylist ; i++) {
+      daynum=daylist[i];
+      for (j=dayidx ; j>0 && daynum<daylist[daysort[j-1]] ; j--) daysort[j]=daysort[j-1];
+      daysort[j]=i;
+      dayidx++;
+   }
 
+   if((datetimeby & DATETIME_BYTE)!=0) {
+      label=_("BYTES");
+      fputs("<table class=\"report\" cellpadding=\"0\" cellspacing=\"2\">\n", fp_ou);
+      fputs("<tr><th class=\"header_c\"></th>",fp_ou);
+      for( i = 0; i < hours.len; i++ )
+         fprintf(fp_ou, "<td class=\"header_c\">%02d%s<br>%s</td>\n", hours.list[ i ], _("H"), label );
+      fprintf(fp_ou, "<td class=\"header_c\">%s<br>%s</td></tr>\n", _("TOTAL"), label );
+
+      memset(tttime,0,sizeof(tttime));
+      for (dayidx=0 ; dayidx<ndaylist ; dayidx++) {
+         daynum=daysort[dayidx];
+         day=daylist[daynum]%100;
+         month=(daylist[daynum]/100)%100;
+         year=daylist[daynum]/10000;
+         computedate(year,month,day,&t);
+         strftime(data,sizeof(data),"%x",&t);
+         fprintf(fp_ou, "<tr><td class=\"data\">%s</td>\n", data );
          tt=0;
-         for( i = 0; i < hours.len; i++ ) v[ hours.list[ i ] ]=0;
-         strcpy(odata,data);
+         for( i = 0; i < hours.len; i++ ) {
+            ihour=hours.list[i];
+            if (tbytes[daynum*24+ihour]>0) {
+               fprintf(fp_ou, "<td class=\"data\">%s</td>\n",fixnum(tbytes[daynum*24+ihour],1));
+               tt+=tbytes[daynum*24+ihour];
+               tttime[ihour]+=tbytes[daynum*24+ihour];
+            } else
+               fputs("<td class=\"data\"></td>\n",fp_ou);
+         }
+         fprintf(fp_ou, "<td class=\"data\">%s</td></tr>\n",fixnum(tt,1));
       }
 
-      ihour=atoi(hour);
-
-      v[ ihour ]+=elap;
-      tt+=elap;
-      t[ ihour ]+=elap;
-      ttt+=elap;
-
+      fprintf(fp_ou, "<tr><td class=\"header_l\">%s</td>\n", _("TOTAL") );
+      tt=0;
+      for( i = 0; i < hours.len; i++ ) {
+         if (tttime[i]>0) {
+            fprintf(fp_ou, "<td class=\"header_r\">%s</td>\n",fixnum(tttime[i],1));
+            tt+=tttime[i];
+         } else
+            fputs("<td class=\"header_r\"></td>\n",fp_ou);
+      }
+      fprintf(fp_ou, "<td class=\"header_r\">%s</td></tr>\n",fixnum(tt,1));
+      fputs("</table>\n",fp_ou);
    }
 
-   for( i = 0; i < hours.len; i++ )
-     sprintf(c[ hours.list[ i ] ],"%s",fixtime(v[ hours.list[ i ] ]));
-
-   for( i = 0; i < hours.len; i++ )
-     if(strcmp(c[ hours.list[ i ] ],"00:00:00") == 0) bzero(c[ hours.list[ i ] ],12);
-
-   fprintf(fp_ou, "<tr><td class=\"data\">%s</td>\n", data );
-   for( i = 0; i < hours.len; i++ )
-     fprintf(fp_ou, "<td class=\"data\">%s</td>\n", c[ hours.list[ i ] ] );
-   fprintf(fp_ou, "<td class=\"data\">%s</td></tr>\n", fixtime(tt) );
-
-   for( i = 0; i < hours.len; i++ )
-     sprintf(c[ hours.list[ i ] ],"%s",fixtime(t[ hours.list[ i ] ]));
-
-   fprintf(fp_ou, "<tr><td class=\"header\">%s</td>\n", text[107] );
-   for( i = 0; i < hours.len; i++ )
-     fprintf(fp_ou, "<td class=\"header2\">%s</td>\n", c[ hours.list[ i ] ] );
-   fprintf(fp_ou, "<td class=\"header2\">%s</td></tr>\n", fixtime(ttt) );
-
-   fputs("</table></div>\n",fp_ou);
+   if((datetimeby & DATETIME_ELAP)!=0) {
+      label=_("H:M:S");
+      fputs("<table class=\"report\" cellpadding=\"0\" cellspacing=\"2\">\n", fp_ou);
+      fputs("<tr><th class=\"header_c\"></th>",fp_ou);
+      for( i = 0; i < hours.len; i++ )
+         fprintf(fp_ou, "<td class=\"header_c\">%02d%s<br>%s</td>\n", hours.list[ i ], _("H"), label );
+      fprintf(fp_ou, "<td class=\"header_c\">%s<br>%s</td></tr>\n", _("TOTAL"), label );
+
+      memset(tttime,0,sizeof(tttime));
+      for (dayidx=0 ; dayidx<ndaylist ; dayidx++) {
+         daynum=daysort[dayidx];
+         day=daylist[daynum]%100;
+         month=(daylist[daynum]/100)%100;
+         year=daylist[daynum]/10000;
+         computedate(year,month,day,&t);
+         strftime(data,sizeof(data),"%x",&t);
+         fprintf(fp_ou, "<tr><td class=\"data\">%s</td>\n", data );
+         tt=0;
+         for( i = 0; i < hours.len; i++ ) {
+            ihour=hours.list[i];
+            if (telap[daynum*24+ihour]>0) {
+               fprintf(fp_ou, "<td class=\"data\">%s</td>\n",fixtime(telap[daynum*24+ihour]));
+               tt+=telap[daynum*24+ihour];
+               tttime[ihour]+=telap[daynum*24+ihour];
+            } else
+               fputs("<td class=\"data\"></td>\n",fp_ou);
+         }
+         fprintf(fp_ou, "<td class=\"data\">%s</td></tr>\n",fixtime(tt));
+      }
 
-   show_info(fp_ou);
+      fprintf(fp_ou, "<tr><td class=\"header_l\">%s</td>\n", _("TOTAL") );
+      tt=0;
+      for( i = 0; i < hours.len; i++ ) {
+         if (tttime[i]>0) {
+            fprintf(fp_ou, "<td class=\"header_r\">%s</td>\n",fixtime(tttime[i]));
+            tt+=tttime[i];
+         } else
+            fputs("<td class=\"header_r\"></td>\n",fp_ou);
+      }
+      fprintf(fp_ou, "<td class=\"header_r\">%s</td></tr>\n",fixtime(tt));
+      fputs("</table>\n",fp_ou);
+   }
 
-   fputs("</body>\n</html>\n",fp_ou);
-   fclose(fp_in);
-   fclose(fp_ou);
+   if (write_html_trailer(fp_ou)<0)
+      debuga(_("Write error in file %s\n"),arqout);
+   if (fclose(fp_ou)==EOF)
+      debuga(_("Failed to close file %s - %s\n"),arqout,strerror(errno));
    return;
 }