/* SRG - Squid Report Generator Report by user Copyright 2003 University of Waikato This file is part of SRG. SRG is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. SRG is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with SRG; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA $Id: UserReport.cc,v 1.20 2004/09/25 03:41:48 mattgbrown Exp $ */ #include "UserReport.h" #include "srg.h" #include "prototypes.h" UserReport::~UserReport() { list::const_iterator iter; for (iter=sites.begin(); iter != sites.end(); iter++) { delete (*iter); } } void UserReport::process_line(const log_line *line) { if (srg.debug) fprintf(stderr, "In UserReport::process_line for user '%s'\n", mName); // Work out what site this line is for char *site = strdup(line->request->site); SiteReport *theSite = (SiteReport *)findSite(site); if (theSite==NULL) { if (srg.debug) fprintf(stderr, "Site not found in %s - creating %s\n", mName, site); // The site does not exist. Create it. theSite = new SiteReport(site); sites.push_back(theSite); } // Process the line theSite->process_line(line); free(site); } void UserReport::updateStats() { zeroStats(); list::const_iterator iter; for (iter=sites.begin(); iter != sites.end(); iter++) { summary_info sitestats = (*iter)->getStats(); stats.connects += sitestats.connects; stats.bytesTransferred += sitestats.bytesTransferred; stats.timeSpent += sitestats.timeSpent; stats.hits += sitestats.hits; stats.bytesHit += sitestats.bytesHit; stats.misses += sitestats.misses; stats.bytesMissed += sitestats.bytesMissed; stats.deniedHits += sitestats.deniedHits; } } void UserReport::generate_report(const char *basename) { char *t = NULL; char *basepath=NULL; char *filename=NULL; char *rootpath=NULL; FILE *outfile=NULL; /* Create the output filename */ if (!singleUserMode) { t = md5this(mName); asprintf(&basepath, "%s/%s", basename, t); free(t); asprintf(&t, "%s/%s", srg.outputDir, basepath); if (mkdir(t, 0755) == -1 && errno != EEXIST) { fprintf(stderr, "Error (%s) creating directory in " "user report for %s: %s\n", strerror(errno), mName, t); exit(1); } free(t); rootpath = strdup("../../"); } else { basepath = strdup(basename); rootpath = strdup("../"); } if (srg.debug) fprintf(stderr, "Creating user report in %s\n", t); /* Create the index file */ asprintf(&filename, "%s/%s/%s", srg.outputDir, basepath, srg.indexfname); outfile = fopen(filename, "w"); if(outfile==NULL) { fprintf(stderr,"SiteReport: Cannot open output file: %s\n", filename); free(rootpath); exit(1); } free(filename); /* Header & Title */ html_header(outfile, rootpath); fprintf(outfile, "\n", version, HOME_URL); //misc stats fprintf(outfile, "
\n"); fprintf(outfile, "\n", localtime(&srg.endTime)->tm_mday, month_names[localtime(&srg.endTime)->tm_mon], localtime(&srg.endTime)->tm_year+1900); if (srg.groupBy > 0) { fprintf(outfile, "", mName); } fprintf(outfile, "
Period: %d %s %d", localtime(&srg.startTime)->tm_mday, month_names[localtime(&srg.startTime)->tm_mon], localtime(&srg.startTime)->tm_year+1900); fprintf(outfile, " - %d %s %d
Group: %s
"); /* Notices Row */ fprintf(outfile, "
" " 
\n"); //main table for location list fprintf(outfile, "
" "" ""); if (srg.showtimes) { fprintf(outfile, ""); } if (srg.showrates) { fprintf(outfile, ""); } fprintf(outfile, "\n"); summary_info sitestats; float percentin = 0; float percentout = 0; float timespent = 0; float bytespercent = 0; summary_info thissitestats = getStats(); float thissitestatsfhits = 0; float thissitestatsfmisses = 0; int rows = 0; /* Initialise the Summary Stats Structure */ sitestats.connects = 0; sitestats.bytesTransferred = 0; sitestats.hits = 0; sitestats.misses = 0; sitestats.bytesHit = 0; sitestats.bytesMissed = 0; sitestats.timeSpent = 0; sitestats.deniedHits = 0; sites.sort(LessByBytesTransferred()); list::const_iterator iter; for (iter=sites.begin(); iter != sites.end(); iter++) { sitestats = (*iter)->getStats(); (*iter)->generate_report(basepath, mName); if (sitestats.hits+sitestats.misses == 0) { percentin = -1; percentout = -1; } else { thissitestatsfhits += percentin = (float)(100*sitestats.hits) / (sitestats.hits+sitestats.misses); thissitestatsfmisses += percentout = (float)(100*sitestats.misses) / (sitestats.hits+sitestats.misses); } if (thissitestats.timeSpent == 0) { timespent = 100; } else { timespent = (float)(100*sitestats.timeSpent) / thissitestats.timeSpent; } if (thissitestats.bytesTransferred == 0) { bytespercent = 100; } else { bytespercent = (float)(100*sitestats.bytesTransferred) / thissitestats.bytesTransferred; } t = md5this((*iter)->getName()); fprintf(outfile, "\n", rows%2==0 ? " class=\"highlightRow\"" : "", t, srg.indexfname, (*iter)->getName()); free(t); t = FormatOutput(sitestats.connects); fprintf(outfile, "", t); free(t); t = FormatOutput(sitestats.bytesTransferred); fprintf(outfile, "", t); free(t); fprintf(outfile, "", bytespercent); if (percentin != -1) { fprintf(outfile, "" "", percentin, percentout); } else { fprintf(outfile, "" ""); } if (srg.showtimes) { t = FormatOutput(sitestats.timeSpent); fprintf(outfile, "" "", timespent, t); free(t); } if (srg.showrates) { if (sitestats.timeSpent == 0) { fprintf(outfile, ""); } else { fprintf(outfile, "", (float)sitestats.bytesTransferred/ (float)sitestats.timeSpent); } } fprintf(outfile, "\n"); rows++; } if (thissitestats.hits+thissitestats.misses == 0) { percentin = -1; percentout = -1; } else { percentin = thissitestatsfhits / rows; percentout = thissitestatsfmisses / rows; } /* Totals Row */ fprintf(outfile, ""); t = FormatOutput(thissitestats.connects); fprintf(outfile, "", t); free(t); t = FormatOutput(thissitestats.bytesTransferred); fprintf(outfile, "", t); free(t); fprintf(outfile, ""); if (percentin != -1) { fprintf(outfile, "", percentin, percentout); } else { fprintf(outfile, ""); } if (srg.showtimes) { t = FormatOutput(thissitestats.timeSpent); fprintf(outfile, "", t); free(t); } if (srg.showrates) { if (thissitestats.timeSpent == 0) { fprintf(outfile, ""); } else { fprintf(outfile, "", (float)thissitestats.bytesTransferred/ (float)thissitestats.timeSpent); } } fprintf(outfile, "\n\t
LOCATIONHITSBYTESBYTES%%HITMISSTIME%%TIME(ms)RATE (kB/s)
%s%s%s%2.2f%%%2.2f%%%2.2f%%--%2.2f%%%s" "-" "%2.0f
Totals:%s%s100%%" "%2.2f%%%2.2f%%" "--100%%%s-%2.0f

"); if (srg.usephp && srg.authenticate) { fprintf(outfile, "\n\t"); } if (srg.groupBy > 0) { fprintf(outfile, "\n\t

Back to groups " "page

\n", srg.indexfname); } else { fprintf(outfile, "\n\t

Back to dates " "page

\n", srg.indexfname); } /* Finish off the HTML */ html_footer(outfile, rootpath); free(rootpath); fclose(outfile); /* If no rows were ouput, delete it all */ if (rows == 0) { recurse_unlink(basepath); } free(basepath); } SiteReport *UserReport::findSite(char *site) { list::const_iterator iter; // Iterate through list and compare each element. for (iter=sites.begin(); iter != sites.end(); iter++) { if(strcasecmp((*iter)->getName(), site)==0) return (SiteReport *)(*iter); } return NULL; } // Get statistics about this report summary_info UserReport::getStats() { summary_info returnInfo; updateStats(); returnInfo = stats; return returnInfo; }