"Fossies" - the Fresh Open Source Software Archive

Member "srg-1.3.6/src/UserReport.cc" (5 Aug 2009, 10261 Bytes) of package /linux/privat/old/srg-1.3.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2     SRG - Squid Report Generator
    3     Report by user
    4     Copyright 2005 University of Waikato
    5 
    6     This file is part of SRG.
    7 
    8     SRG is free software; you can redistribute it and/or modify
    9     it under the terms of the GNU General Public License as published by
   10     the Free Software Foundation; either version 2 of the License, or
   11     (at your option) any later version.
   12 
   13     SRG is distributed in the hope that it will be useful,
   14     but WITHOUT ANY WARRANTY; without even the implied warranty of
   15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16     GNU General Public License for more details.
   17 
   18     You should have received a copy of the GNU General Public License
   19     along with SRG; if not, write to the Free Software
   20     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  
   21     USA
   22 
   23     $Id: UserReport.cc 218 2005-05-11 04:05:17Z matt $
   24     
   25 */
   26 
   27 #include "UserReport.h"
   28 #include "srg.h"
   29 #include "prototypes.h"
   30 
   31 UserReport::~UserReport() {
   32 
   33     list<SiteReport*>::const_iterator iter;
   34     for (iter=sites.begin(); iter != sites.end(); iter++) {
   35         delete (*iter);
   36     }
   37 
   38 }
   39 
   40 void UserReport::process_line(const log_line *line) {
   41 
   42     if (srg.debug)
   43         fprintf(stderr, "In UserReport::process_line for user '%s'\n",
   44                 mName);
   45     
   46     /* Work out what site this line is for */
   47     char *site = strdup(line->request->site);
   48 
   49     SiteReport *theSite = (SiteReport *)findSite(site);
   50     if (theSite==NULL) {
   51         if (srg.debug)
   52             fprintf(stderr, "Site not found in %s - creating %s\n",
   53                 mName, site);
   54         /* The site does not exist. Create it. */
   55         theSite = new SiteReport(site);
   56         sites.push_back(theSite);
   57     }
   58         
   59     /* Process the line */
   60     theSite->process_line(line);
   61 
   62     free(site);
   63 }
   64 
   65 void UserReport::updateStats() {
   66     
   67     /* Otherwise continue on and update our statistics */
   68     zeroStats();
   69 
   70     list<SiteReport*>::const_iterator iter;
   71 
   72     for (iter=sites.begin(); iter != sites.end(); iter++) {
   73         summary_info sitestats = (*iter)->getStats();
   74         if (srg.minimumConnects>0 && sitestats.connects<srg.minimumConnects)
   75             continue;
   76         stats.connects += sitestats.connects;
   77         stats.bytesTransferred += sitestats.bytesTransferred;
   78         stats.timeSpent += sitestats.timeSpent;
   79         stats.hits += sitestats.hits;
   80         stats.bytesHit += sitestats.bytesHit;
   81         stats.misses += sitestats.misses;
   82         stats.bytesMissed += sitestats.bytesMissed;
   83         stats.deniedHits += sitestats.deniedHits;
   84     }
   85 }
   86 
   87 void UserReport::generate_report(const char *basename) {
   88 
   89     char *t = NULL;
   90     char *basepath=NULL;
   91     char *filename=NULL;
   92     char *rootpath=NULL;
   93     FILE *outfile=NULL;
   94     float percentin = 0;
   95     float percentout = 0;
   96     
   97     /* Create the output filename */
   98     if (!singleUserMode) {
   99         t = md5this(mName);
  100         asprintf(&basepath, "%s/%s", basename, t);
  101         free(t);
  102         asprintf(&t, "%s/%s", srg.outputDir, basepath);
  103         if (mkdir(t, 0755) == -1 && errno != EEXIST) {
  104             fprintf(stderr, "Error (%s) creating directory in " 
  105                     "user report for %s: %s\n", strerror(errno), 
  106                     mName, t);
  107             exit(1);
  108         }
  109         free(t);
  110         rootpath = strdup("../../");
  111     } else {
  112         basepath = strdup(basename);
  113         rootpath = strdup("../");
  114     }
  115     
  116     if (srg.debug)
  117         fprintf(stderr, "Creating user report in %s\n", t);
  118         
  119     /* Create the index file */
  120     asprintf(&filename, "%s/%s/%s", srg.outputDir, basepath, 
  121             srg.indexfname);
  122     outfile = fopen(filename, "w");
  123     if(outfile==NULL) {
  124         fprintf(stderr,"SiteReport: Cannot open output file: %s\n",
  125                 filename);
  126         free(rootpath);
  127         exit(1);
  128     }
  129     free(filename);
  130 
  131     /* Header & Title */
  132     html_header(outfile, rootpath);
  133 
  134     fprintf(outfile, "<!-- SRG %s (%s) Generated UserReport -->\n", 
  135             version, HOME_URL);
  136 
  137     /* misc stats */
  138     fprintf(outfile, "<center><table cellpadding=2 cellspacing=2>\n");
  139     fprintf(outfile, "<tr><td class=\"bodyText\">Period:</td><td class="
  140             "\"bodyText\">&nbsp;%d %s %d",
  141             localtime(&srg.startTime)->tm_mday, 
  142             month_names[localtime(&srg.startTime)->tm_mon], 
  143             localtime(&srg.startTime)->tm_year+1900);
  144     fprintf(outfile, " - %d %s %d</td></tr>\n", 
  145             localtime(&srg.endTime)->tm_mday, 
  146             month_names[localtime(&srg.endTime)->tm_mon], 
  147             localtime(&srg.endTime)->tm_year+1900);
  148     if (srg.groupBy > 0) {
  149         fprintf(outfile, "<tr><td class=\"bodyText\">Group:</td><td "
  150                 "class=\"bodyText\">&nbsp;%s</td></tr>", mName);
  151     }
  152     fprintf(outfile, "</table></center>");
  153         
  154     /* Notices Row */
  155     fprintf(outfile, "<div align=\"center\" id=\"srg-message\">"
  156             "&nbsp;</div>\n");
  157 
  158     /* main table for location list */
  159     fprintf(outfile, "<center><table cellpadding=4 cellspacing=0 "
  160             "id=\"srgtable\">"
  161             "<thead><tr><th>LOCATION</th><th>REQUESTS</th><th>BYTES</th>"
  162             "<th>BYTES%%</th><th>HIT</th><th>MISS</th>");
  163     if (srg.showtimes) {
  164         fprintf(outfile, "<th>TIME%%</th><th>TIME(ms)</th>");
  165     }
  166     if (srg.showrates) {
  167         fprintf(outfile, "<th>RATE (kB/s)</th>");
  168     }
  169     fprintf(outfile, "</tr></thead>\n");
  170     
  171     /* Totals Row */
  172     summary_info thissitestats = getStats();
  173     fprintf(outfile, "<tfoot><tr><th class=\"cellText\">Totals:</th>");
  174     t = FormatOutput(thissitestats.connects);
  175     fprintf(outfile, "<th class=\"cellNum\">%s</th>", t);
  176     free(t);
  177     t = FormatOutput(thissitestats.bytesTransferred);
  178     fprintf(outfile, "<th class=\"cellNum\">%s</th>", t);
  179     free(t);
  180     fprintf(outfile, "<th class=\"cellNum\">100%%</th>");
  181     if (thissitestats.bytesTransferred > 0) {
  182         percentin = ((float)thissitestats.bytesHit/
  183                 (float)thissitestats.bytesTransferred)*100.0;
  184         percentout = ((float)thissitestats.bytesMissed/
  185                 (float)thissitestats.bytesTransferred)*100.0;    
  186         fprintf(outfile, "<th align=\"center\" class=\"cellNum\">"
  187                 "%2.2f%%</th><th align=\"center\" class=\""
  188                 "cellNum\">%2.2f%%</th>", percentin, 
  189                 percentout);
  190     } else {
  191         fprintf(outfile, "<th align=\"center\" class=\"cellNum\">"
  192                 "-</th><th align=\"center\" class=\""
  193                 "cellNum\">-</th>");
  194     }
  195     if (srg.showtimes) {
  196         t = FormatOutput(thissitestats.timeSpent);
  197         fprintf(outfile, "<th class=\"cellNum\">100%%</th><th class=\""
  198                 "cellNum\">%s</th>", t);
  199         free(t);
  200     }
  201     if (srg.showrates) {
  202         if (thissitestats.timeSpent == 0) {
  203             fprintf(outfile, "<th class=\"cellNum\">-</th>");
  204         } else {
  205             fprintf(outfile, "<th class=\"cellNum\">%2.0f</th>",
  206                 (float)thissitestats.bytesTransferred/
  207                 (float)thissitestats.timeSpent);
  208         }
  209     }
  210     fprintf(outfile, "</tr></tfoot>\n");
  211 
  212     summary_info sitestats;
  213     float timespent = 0;
  214     float bytespercent = 0;
  215     int rows = 0;
  216 
  217     /* Initialise the Summary Stats Structure */
  218     sitestats.connects = 0;
  219     sitestats.bytesTransferred = 0;
  220     sitestats.hits = 0;
  221     sitestats.misses = 0;
  222     sitestats.bytesHit = 0;
  223     sitestats.bytesMissed = 0;
  224     sitestats.timeSpent = 0;
  225     sitestats.deniedHits = 0;
  226 
  227     sites.sort(LessByBytesTransferred<SiteReport>());
  228 
  229     list<SiteReport*>::const_iterator iter;
  230     for (iter=sites.begin(); iter != sites.end(); iter++) {
  231 
  232         sitestats = (*iter)->getStats();
  233 
  234         if (srg.minimumConnects>0 && sitestats.connects<srg.minimumConnects)
  235             continue;
  236 
  237         if (srg.siteStats)
  238             (*iter)->generate_report(basepath, mName);
  239 
  240         if (sitestats.bytesTransferred>0 && sitestats.connects>0) {
  241             percentin = ((float)sitestats.bytesHit/
  242                     (float)sitestats.bytesTransferred)*100.0;
  243             percentout = ((float)sitestats.bytesMissed/
  244                     (float)sitestats.bytesTransferred)*100.0;
  245         } else {
  246             percentin = 0;
  247             percentout = 0;
  248         }
  249         if (thissitestats.timeSpent == 0) {
  250             timespent = 100;
  251         } else {
  252             timespent = (float)(100*sitestats.timeSpent) / 
  253                     thissitestats.timeSpent;
  254         }
  255         if (thissitestats.bytesTransferred == 0) {
  256             bytespercent = 100;
  257         } else {
  258             bytespercent = (float)(100*sitestats.bytesTransferred)
  259                     / thissitestats.bytesTransferred;
  260         }
  261         t = md5this((*iter)->getName());
  262         fprintf(outfile, "<tr%s><td class=\"bodyText\">", 
  263                 rows%2==0 ? " class=\"highlightRow\"" : "");
  264         if (srg.siteStats) {
  265             fprintf(outfile, "<a href=\"%s/%s\">", t, srg.indexfname);
  266         }
  267         fprintf(outfile, "%s", (*iter)->getName());
  268         if (srg.siteStats) {
  269             fprintf(outfile, "</a>");
  270         }
  271         fprintf(outfile, "</td>");
  272         free(t);
  273         t = FormatOutput(sitestats.connects);
  274         fprintf(outfile, "<td class=\"cellNum\">%s</td>", t);
  275         free(t);
  276         t = FormatOutput(sitestats.bytesTransferred);
  277         fprintf(outfile, "<td class=\"cellNum\">%s</td>", t);
  278         free(t);
  279         fprintf(outfile, "<td class=\"cellNum\">%2.2f%%</td>", 
  280                 bytespercent);
  281         if (percentin != -1) {
  282             fprintf(outfile, "<td class=\"cellNum\">%2.2f%%</td>"
  283                     "<td class=\"cellNum\">%2.2f%%</td>",
  284                     percentin, percentout);
  285         } else {
  286             fprintf(outfile, "<td class=\"cellNum\">-</td>"
  287                     "<td class=\"cellNum\">-</td>");
  288         }
  289         if (srg.showtimes) {
  290             t = FormatOutput(sitestats.timeSpent);
  291             fprintf(outfile, "<td class=\"cellNum\">%2.2f%%</td>"
  292                     "<td class=\"cellNum\">%s</td>",
  293                     timespent, t);
  294             free(t);
  295         }
  296         if (srg.showrates) {
  297             if (sitestats.timeSpent == 0) {
  298                 fprintf(outfile, "<td class=\"cellNum\">"
  299                         "-</td>");
  300             } else {
  301                 fprintf(outfile, "<td class=\"cellNum\">"
  302                         "%.2f</td>", 
  303                         (float)sitestats.bytesTransferred/
  304                         (float)sitestats.timeSpent);
  305             }
  306         }
  307         fprintf(outfile, "</tr>\n");
  308         rows++;
  309     }
  310 
  311     fprintf(outfile, "\t</table><br>");
  312       
  313     if (srg.authenticate) {
  314         fprintf(outfile, "\n\t<?php } else {\n report_error(\""
  315                 "Could not authenticate user\");\n}?>");
  316     }
  317 
  318 
  319     if (srg.groupBy > 0) {
  320         fprintf(outfile, "\n\t<p><a href=\"../%s\">Back</a> to groups "
  321                 "page</p></center>\n", srg.indexfname);
  322     } else {
  323         fprintf(outfile, "\n\t<p><a href=\"../%s\">Back</a> to dates "
  324                 "page</p></center>\n", srg.indexfname);
  325     }
  326     
  327     /* Finish off the HTML */
  328     html_footer(outfile, rootpath);
  329     free(rootpath);
  330     fclose(outfile);
  331 
  332     /* If no rows were ouput, delete it all */
  333     if (rows == 0) {
  334         recurse_unlink(basepath);
  335     }
  336     free(basepath);
  337 
  338 }
  339 
  340 SiteReport *UserReport::findSite(char *site) {
  341 
  342     list<SiteReport*>::const_iterator iter;
  343 
  344     // Iterate through list and compare each element.
  345     for (iter=sites.begin(); iter != sites.end(); iter++)
  346     {
  347         if(strcasecmp((*iter)->getName(), site)==0)
  348             return (SiteReport *)(*iter);
  349     }
  350 
  351     return NULL;
  352 
  353 }
  354 
  355 // Get statistics about this report
  356 summary_info UserReport::getStats() {
  357 
  358     summary_info returnInfo;
  359 
  360     updateStats();
  361 
  362     returnInfo = stats;
  363 
  364     return returnInfo;
  365 
  366 }