From: Frédéric Marchal Date: Sat, 20 Nov 2010 22:07:40 +0000 (+0000) Subject: Add dynamic sorting of some tables X-Git-Tag: v2.3.2~131 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ea275279646d3f9a46a5fb7be0e8fa038dd42151;p=thirdparty%2Fsarg.git Add dynamic sorting of some tables Allow the use of a javascript script to dynamically sort some tables. The code is tailored for sorttable.js by Stuart Langridge found at http://www.kryogenix.org/code/browser/sorttable/. --- diff --git a/ChangeLog b/ChangeLog index e4ab1bd..c6159c3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ SARG ChangeLog +Nov-20-2010 Version 2.3.2-pre1 + - Add support for sorttable.js (http://www.kryogenix.org/code/browser/sorttable/) to dynamically sort some tables. + Sep-18-2010 Version 2.3.1 - Remove the distinct printf for the alpha architecture as it doesn't work anymore and is not necessary anyway. - Don't abort if "onload" or "script" is found in the user agent string. diff --git a/documentation/util.txt b/documentation/util.txt index 9758871..41772b2 100644 --- a/documentation/util.txt +++ b/documentation/util.txt @@ -416,15 +416,15 @@ the sarg-users file of the report data's directory. -/*! \fn void obttotal(const char *dirname, const char *name, char *tbytes, int nuser, char *media) +/*! \fn void obttotal(const char *dirname, const char *name, int nuser, long long int *tbytes, long long int *media) Count the total size transfered in a report directory and compute the average number of bytes per entry. \param dirname The directory containing the reports. \param name The name of the report directory whose sarg-general file must be read. -\param tbytes A buffer to store the total number of bytes from this report. \param nuser The number of entries in the report directory. -\param media A buffer to store the average number of bytes per entry. +\param tbytes A variable to store the total number of bytes from this report. +\param media A variable to store the average number of bytes per entry. */ diff --git a/getconf.c b/getconf.c index a502978..e85a3ca 100644 --- a/getconf.c +++ b/getconf.c @@ -726,6 +726,8 @@ static void parmtest(char *buf) if (getparam_string("graph_font",buf,GraphFont,sizeof(GraphFont))>0) return; + if (getparam_string("sorttable",buf,SortTableJs,sizeof(SortTableJs))>0) return; + if(strstr(buf,"squid24") != 0) { squid24=true; return; diff --git a/html.c b/html.c index 4611ebc..b84093a 100644 --- a/html.c +++ b/html.c @@ -234,13 +234,20 @@ void htmlrel(void) fprintf(fp_ou,"%s\n",_("User report")); close_html_header(fp_ou); - fputs("
\n",fp_ou); + fputs("
\n",fp_ou); + /*! \bug This line lays before the header of the table and is therefore invalid + HTML. It will also break the sorttable script. Beside, I suspect it serves no purpose. + */ if(denied_report[0]!='\0') { fprintf(fp_ou,"\n",denied_report,_("SmartFilter"),_("Report")); } - fprintf(fp_ou,"",_("ACCESSED SITE")); + fputs("",_("ACCESSED SITE")); if((UserReportFields & USERREPORTFIELDS_CONNECT) != 0) fprintf(fp_ou,"",_("CONNECT")); @@ -249,7 +256,7 @@ void htmlrel(void) if((UserReportFields & USERREPORTFIELDS_SETYB) != 0) fprintf(fp_ou,"",_("BYTES")); if((UserReportFields & USERREPORTFIELDS_IN_CACHE_OUT) != 0) - fprintf(fp_ou,"",_("IN-CACHE-OUT")); + fprintf(fp_ou,"",_("IN-CACHE-OUT")); if((UserReportFields & USERREPORTFIELDS_USED_TIME) != 0) fprintf(fp_ou,"",_("ELAPSED TIME")); if((UserReportFields & USERREPORTFIELDS_MILISEC) != 0) @@ -257,7 +264,7 @@ void htmlrel(void) if((UserReportFields & USERREPORTFIELDS_PTIME) != 0) fprintf(fp_ou,"",_("TIME")); - fputs("\n",fp_ou); + fputs("\n",fp_ou); if(debug) { debuga(_("Making report: %s\n"),uinfo->id); @@ -325,10 +332,16 @@ void htmlrel(void) fputs("",fp_ou); } - if((UserReportFields & USERREPORTFIELDS_CONNECT) != 0) - fprintf(fp_ou,"",fixnum(twork,1)); - if((UserReportFields & USERREPORTFIELDS_BYTES) != 0) - fprintf(fp_ou,"",fixnum(nnbytes,1)); + if((UserReportFields & USERREPORTFIELDS_CONNECT) != 0) { + fputs("",fixnum(twork,1)); + } + if((UserReportFields & USERREPORTFIELDS_BYTES) != 0) { + fputs("",fixnum(nnbytes,1)); + } if((UserReportFields & USERREPORTFIELDS_SETYB) != 0) { perc=(tnbytes) ? nnbytes * 100. / tnbytes : 0.; fprintf(fp_ou,"",perc); @@ -338,8 +351,11 @@ void htmlrel(void) ouperc=(nnbytes) ? oucache * 100. / nnbytes : 0.; fprintf(fp_ou,"",inperc,ouperc); } - if((UserReportFields & USERREPORTFIELDS_USED_TIME) != 0) - fprintf(fp_ou,"",buildtime(nnelap)); + if((UserReportFields & USERREPORTFIELDS_USED_TIME) != 0) { + fputs("",buildtime(nnelap)); + } if((UserReportFields & USERREPORTFIELDS_MILISEC) != 0) fprintf(fp_ou,"",fixnum2(nnelap,1)); if((UserReportFields & USERREPORTFIELDS_PTIME) != 0) { @@ -505,6 +521,9 @@ void htmlrel(void) unlink(arqip); unlink(arqin); + if ((UserReportFields & (USERREPORTFIELDS_TOTAL | USERREPORTFIELDS_AVERAGE)) != 0) + fputs("",fp_ou); + if((UserReportFields & USERREPORTFIELDS_TOTAL) != 0) { fprintf(fp_ou,"",_("TOTAL")); if((UserReportFields & USERREPORTFIELDS_CONNECT) != 0) @@ -589,6 +608,9 @@ void htmlrel(void) fputs("\n",fp_ou); } + if ((UserReportFields & (USERREPORTFIELDS_TOTAL | USERREPORTFIELDS_AVERAGE)) != 0) + fputs("",fp_ou); + fputs("
%s %s
%s
%s%s%%%s%s%s%s%%%s
%s%s%s%s%3.2lf%%%3.2lf%%%3.2lf%%%s%s%s
%s
\n",fp_ou); if (write_html_trailer(fp_ou)<0) debuga(_("Write error in file %s\n"),arqou); diff --git a/include/conf.h b/include/conf.h index 96b0c9d..0113364 100755 --- a/include/conf.h +++ b/include/conf.h @@ -390,9 +390,6 @@ char val8[MAXLEN]; char val9[MAXLEN]; char val10[MAXLEN]; char val11[MAXLEN]; -char wwork1[MAXLEN]; -char wwork2[MAXLEN]; -char wwork3[MAXLEN]; char mask[MAXLEN]; char site[MAXLEN]; char us[50]; @@ -415,6 +412,8 @@ char LDAPBaseSearch[255]; char LDAPFilterSearch[512]; char LDAPTargetAttr[64]; char GraphFont[MAXLEN]; +//! The full path to sorttable.js if the table in the reports must be dynamicaly sorted. +char SortTableJs[256]; int idate; int smartfilter; diff --git a/include/defs.h b/include/defs.h index 6bfe656..ee237f9 100755 --- a/include/defs.h +++ b/include/defs.h @@ -231,7 +231,7 @@ void obtdate(const char *dirname, const char *name, char *data); void formatdate(char *date,int date_size,int year,int month,int day,int hour,int minute,int second,int dst); void computedate(int year,int month,int day,struct tm *t); int obtuser(const char *dirname, const char *name); -void obttotal(const char *dirname, const char *name, char *tbytes, int nuser, char *media); +void obttotal(const char *dirname, const char *name, int nuser, long long int *tbytes, long long int *media); void version(void); int vercode(const char *code); void load_excludecodes(const char *ExcludeCodes); diff --git a/index.c b/index.c index 8529b09..25daa19 100644 --- a/index.c +++ b/index.c @@ -292,10 +292,10 @@ static void make_file_index(void) struct dirent *direntp; char wdir[MAXLEN]; char data[80]; - char tbytes[20]; - char media[20]; char ftime[128]; char day[6], mon[8], year[40], hour[10]; + long long int tbytes; + long long int media; int iyear, imonth, iday, ihour, iminute, isecond, idst; int nsort; int nallocated; @@ -412,16 +412,30 @@ static void make_file_index(void) } write_html_header(fp_ou,0,ngettext("SARG report","SARG reports",nsort)); close_html_header(fp_ou); - fputs("
\n\n",fp_ou); - fprintf(fp_ou,"\n",_("FILE/PERIOD"),_("CREATION DATE"),_("USERS"),_("BYTES"),_("AVERAGE")); + fputs("
%s%s%s%s%s
\n",fp_ou); + fprintf(fp_ou,"\n",_("FILE/PERIOD"),_("CREATION DATE"),_("USERS"),_("BYTES"),_("AVERAGE")); for (i=0 ; i0) item=sortlist[i]; else item=sortlist[nsort-i-1]; tuser=obtuser(outdir,item->dirname); - obttotal(outdir,item->dirname,tbytes,tuser,media); - fprintf(fp_ou,"\n",item->dirname,ReplaceIndex,item->dirname,item->date,tuser,tbytes,media); + obttotal(outdir,item->dirname,tuser,&tbytes,&media); + fputs("",item->dirname,ReplaceIndex,item->dirname); + fputs("",item->date); + fprintf(fp_ou,"",tuser); + fputs("",fixnum(tbytes,1)); + fputs("\n",fixnum(media,1)); } fputs("
%s%s%s%s%s
%s%s%d%s%s
sortnum); + fprintf(fp_ou,">%screationdate); + fprintf(fp_ou,">%s%d%s%s
\n",fp_ou); if (write_html_trailer(fp_ou)<0) diff --git a/log.c b/log.c index e5868ed..46485ed 100644 --- a/log.c +++ b/log.c @@ -290,6 +290,7 @@ int main(int argc,char *argv[]) LDAPBaseSearch[0]='\0'; strcpy(LDAPFilterSearch, "(uid=%s)"); strcpy(LDAPTargetAttr, "cn"); + SortTableJs[0]='\0'; dia[0]='\0'; mes[0]='\0'; diff --git a/sarg.conf b/sarg.conf index 3d72522..7bee126 100644 --- a/sarg.conf +++ b/sarg.conf @@ -694,3 +694,12 @@ # Compatilibity with squid version <= 2.4 when using emulate_http_log on # # squid24 off + +# TAG: sorttable path +# The full path to a javascript script to dynamically sort the tables. +# The path is the path a browser must follow to find the script. +# If this entry is set, each sortable table will have the "sortable" class set. +# You may have a look at http://www.kryogenix.org/code/browser/sorttable/ +# for such as implementation. +# +# sorttable /sorttable.js diff --git a/siteuser.c b/siteuser.c index 36e53f1..e1e6c28 100644 --- a/siteuser.c +++ b/siteuser.c @@ -86,11 +86,16 @@ void siteuser(void) fprintf(fp_ou,"%s\n",_("Sites & Users")); close_html_header(fp_ou); - fputs("
\n",fp_ou); + fputs("
\n",_("ACCESSED SITE")); if(BytesInSitesUsersReport) - fprintf(fp_ou,"\n",_("NUM"),_("ACCESSED SITE"),_("BYTES"),_("USERS")); - else - fprintf(fp_ou,"\n",_("NUM"),_("ACCESSED SITE"),_("USERS")); + fprintf(fp_ou,"",_("BYTES")); + fputs("\n",_("USERS")); ourl=NULL; ourl_size=0; @@ -161,8 +166,11 @@ void siteuser(void) output_html_string(fp_ou,ourl,100); fputs("",fp_ou); - if(BytesInSitesUsersReport) - fprintf(fp_ou,"",fixnum(obytes,1)); + if (BytesInSitesUsersReport) { + fputs("",fixnum(obytes,1)); + } fprintf(fp_ou,"\n",users); regs++; @@ -198,7 +206,9 @@ void siteuser(void) output_html_string(fp_ou,ourl,100); fputs("",fp_ou); if (BytesInSitesUsersReport) { - fprintf(fp_ou,"",fixnum(obytes,1)); + fputs("",fixnum(obytes,1)); } fprintf(fp_ou,"\n",users); } diff --git a/topsites.c b/topsites.c index 7207b4d..0cea0a5 100644 --- a/topsites.c +++ b/topsites.c @@ -194,8 +194,12 @@ void topsites(void) fputs("\n",fp_ou); close_html_header(fp_ou); - fputs("
%s%s
%s%s%s%s
%s%s%s
%s%s
%s%s%s
%s%s%s
\n",fp_ou); - fprintf(fp_ou,"\n",_("NUM"),_("ACCESSED SITE"),_("CONNECT"),_("BYTES"),_("TIME")); + fputs("
%s%s%s%s%s
\n",fp_ou); + fprintf(fp_ou,"\n",_("ACCESSED SITE"),_("CONNECT"),_("BYTES"),_("TIME")); regs=0; ntopsites = 0; @@ -225,10 +229,6 @@ void topsites(void) twork2=nbytes; twork3=ntime; - strcpy(wwork1,fixnum(twork1,1)); - strcpy(wwork2,fixnum(twork2,1)); - strcpy(wwork3,fixtime(twork3)); - fprintf(fp_ou,"\n",wwork1,wwork2,wwork3); + fputs("",fixnum(twork1,1)); + fputs("",fixnum(twork2,1)); + fputs("\n",fixtime(twork3)); } fclose(fp_in); longline_destroy(&line); diff --git a/topuser.c b/topuser.c index 37082b7..d4245f1 100644 --- a/topuser.c +++ b/topuser.c @@ -207,15 +207,23 @@ void topuser(void) return; } - fputs("
%s%s%s%s%s
%d",++regs); if(BlockIt[0] != '\0') { @@ -241,7 +241,15 @@ void topsites(void) output_html_url(fp_ou,url); fputs("\">",fp_ou); output_html_string(fp_ou,url,100); - fprintf(fp_ou,"%s%s%s
%s%s%s
\n",fp_top3); - fputs("",fp_top3); + fputs("
\n",fp_top3); if((TopUserFields & TOPUSERFIELDS_NUM) != 0) fprintf(fp_top3,"",_("NUM")); - if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0) - fputs("",fp_top3); - if((TopUserFields & TOPUSERFIELDS_USERID) != 0) - fprintf(fp_top3,"",_("USERID")); + if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0) { + fputs("",fp_top3); + } + if((TopUserFields & TOPUSERFIELDS_USERID) != 0) { + fputs("",_("USERID")); + } if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) fprintf(fp_top3,"",_("CONNECT")); if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) @@ -223,7 +231,7 @@ void topuser(void) if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) fprintf(fp_top3,"",_("BYTES")); if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) - fprintf(fp_top3,"",_("IN-CACHE-OUT")); + fprintf(fp_top3,"",_("IN-CACHE-OUT")); if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0) fprintf(fp_top3,"",_("ELAPSED TIME")); if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0) @@ -231,7 +239,7 @@ void topuser(void) if((TopUserFields & TOPUSERFIELDS_PTIME) != 0) fprintf(fp_top3,"",_("TIME")); - fputs("\n",fp_top3); + fputs("\n",fp_top3); ntopuser = 0; @@ -304,10 +312,16 @@ void topuser(void) else fprintf(fp_top3,"",uinfo->filename,uinfo->filename,uinfo->label); } - if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) - fprintf(fp_top3,"",fixnum(nacc,1)); - if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) - fprintf(fp_top3,"",fixnum(tnbytes,1)); + if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) { + fputs("",fixnum(nacc,1)); + } + if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) { + fputs("",fixnum(tnbytes,1)); + } if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) { perc=(ttnbytes) ? tnbytes * 100. / ttnbytes : 0.; fprintf(fp_top3,"",perc); @@ -322,8 +336,11 @@ void topuser(void) } #endif } - if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0) - fprintf(fp_top3,"",buildtime(tnelap)); + if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0) { + fputs("",buildtime(tnelap)); + } if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0) fprintf(fp_top3,"",fixnum2(tnelap,1)); if((TopUserFields & TOPUSERFIELDS_PTIME) != 0) { @@ -340,7 +357,7 @@ void topuser(void) longline_destroy(&line); if((TopUserFields & TOPUSERFIELDS_TOTAL) != 0) { - fputs("",fp_top3); + fputs("",fp_top3); if((TopUserFields & TOPUSERFIELDS_NUM) != 0) fputs("",fp_top3); if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0) @@ -394,7 +411,7 @@ void topuser(void) fprintf(fp_top3,"",buildtime(ttnelap/totuser)); if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0) fprintf(fp_top3,"",fixnum2(ttnelap/totuser,1)); - fputs("\n",fp_top3); + fputs("\n",fp_top3); } fputs("
%s%s%s%s%%%s%s%s%s%%%s
%s%s%s%s%s%3.2lf%%%s%s%s
%s%s
\n",fp_top3); diff --git a/util.c b/util.c index 9cb4f31..a0bb63c 100644 --- a/util.c +++ b/util.c @@ -681,21 +681,18 @@ int obtuser(const char *dirname, const char *name) } -void obttotal(const char *dirname, const char *name, char *tbytes, int nuser, char *media) +void obttotal(const char *dirname, const char *name, int nuser, long long int *tbytes, long long int *media) { FILE *fp_in; char *buf; char wdir[MAXLEN]; char user[MAX_USER_LEN]; char sep; - long long int med=0; - long long int twork=0; struct getwordstruct gwarea; longline line; - twork=0; - tbytes[0]='\0'; - media[0]='\0'; + *tbytes=0; + *media=0; sprintf(wdir,"%s%s/sarg-general",dirname,name); if ((fp_in = fopen(wdir, "r")) == 0) { @@ -728,24 +725,19 @@ void obttotal(const char *dirname, const char *name, char *tbytes, int nuser, ch debuga(_("There a broken total number of access in file %s\n"),wdir); exit(EXIT_FAILURE); } - if (getword_atoll(&twork,&gwarea,sep)<0) { + if (getword_atoll(tbytes,&gwarea,sep)<0) { debuga(_("There is a broken number of bytes in file %s\n"),wdir); exit(EXIT_FAILURE); } - strcpy(tbytes,fixnum(twork,1)); break; } fclose(fp_in); longline_destroy(&line); - if(nuser <= 0) { - strcpy(media,"0"); + if (nuser <= 0) return; - } - - med=twork / nuser; - strcpy(media,fixnum(med,1)); + *media=*tbytes / nuser; return; } @@ -1614,6 +1606,8 @@ void write_html_head(FILE *fp_ou, const char *page_title) fprintf(fp_ou, "\n \n",CharSet); if (page_title) fprintf(fp_ou,"%s\n",page_title); css(fp_ou); + if (SortTableJs[0]) + fprintf(fp_ou,"\n",SortTableJs); fputs("\n\n",fp_ou); }