source: trunk/lib/first_page.cc @ 362

Last change on this file since 362 was 362, checked in by Peter Johansson, 14 years ago

fixes #205, summation row in authors table in main page

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.5 KB
Line 
1// $Id: first_page.cc 362 2007-06-10 22:26:41Z peter $
2
3/*
4  Copyright (C) 2006, 2007 Peter Johansson
5
6  This file is part of svndigest, http://lev.thep.lu.se/trac/svndigest
7
8  svndigest is free software; you can redistribute it and/or modify it
9  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  svndigest is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "first_page.h"
25
26#include "html_utility.h"
27
28#include "Commitment.h"
29#include "Configuration.h"
30#include "Date.h"
31#include "HtmlStream.h"
32#include "Stats.h"
33#include "SVNlog.h"
34#include "Trac.h"
35#include "utility.h"
36#include <config.h> // this header file is created by configure
37
38#include <algorithm>
39#include <cassert>
40#include <fstream>
41#include <iostream>
42#include <sstream>
43#include <stdexcept>
44#include <string>
45#include <sys/param.h>
46#include <unistd.h>
47#include <vector>
48
49namespace theplu{
50namespace svndigest{
51
52  void print_main_page(const std::string& dir, const SVNlog& log,
53                       const Stats& stats, std::string url)
54  {
55    std::string filename="index.html";
56    std::ofstream os(filename.c_str());
57    print_header(os, dir, 0, "all", "main", "index.html");
58
59    using namespace std;
60    set<string> authors;
61    authors.insert(log.author().begin(), log.author().end());
62    // erase invalid authors
63    authors.erase("");
64    authors.erase("no author");
65
66    vector<Commitment> latest_commit;
67    latest_commit.reserve(authors.size());
68    for (set<string>::const_iterator i(authors.begin()); i!=authors.end(); ++i)
69      latest_commit.push_back(log.latest_commit(*i));
70
71    print_summary_plot(os, stats);
72    print_general_information(os, log, authors.size(), url);
73    sort(latest_commit.begin(), latest_commit.end(), GreaterRevision());
74    print_authors(os, latest_commit, stats);
75    print_recent_logs(os, log);
76    os << "<hr width=100% />";
77    print_footer(os);
78    os.close();
79
80  }
81
82  void print_general_information(std::ostream& os, const SVNlog& log, 
83                                 size_t nof_authors, std::string url)
84  { 
85    Date begin(log.date()[0]);
86    if (log.date().size()>1)
87      begin = std::min(begin, Date(log.date()[1]));
88    Date end(log.date().back());
89    Date now;
90    std::string timefmt("%a, %e %b %Y");
91
92    os << "<div class=\"main\">" 
93       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
94       << "General Information"
95       << "</th></tr></thead>\n"
96       << "<tr><td>URL:</td><td>" 
97       << url << "</td></tr>\n"
98       << "<tr><td>First Revision:</td><td>" 
99       << begin(timefmt) << "</td></tr>\n"
100       << "<tr><td>Latest Revision:</td><td>" 
101       << end(timefmt) << "</td></tr>\n"
102       << "<tr><td>Report Generated:</td><td>" 
103       << now(timefmt) << "</td></tr>\n"
104       << "<tr><td>Repository Age:</td><td>"; 
105    os << now.difftime(begin);
106    os << "</td></tr>\n"
107       << "<tr><td>Number of Authors:</td><td>" << nof_authors
108       << "</td></tr>\n"
109       << "<tr><td>Revisions:</td><td>" << log.revision().size() 
110       << "</td></tr>\n"
111       << "</table></div>\n";
112  }
113
114
115  void print_authors(std::ostream& os, 
116                     const std::vector<Commitment>& lc,
117                     const Stats& stats)
118  {
119    os << "<div class=\"main\">"
120       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
121       << "Authors"
122       << "</th></tr></thead>\n";
123
124    os << "<tr><td>Author</td>" 
125       << "<td>Number of Lines</td>"
126       << "<td>Code Lines</td>"
127       << "<td>Comment Lines</td>"
128       << "<td>Latest Commitment</td>"
129       <<"</tr>";
130
131    std::string timefmt("%b %d %H:%M:%S %Y");
132    using namespace std;
133    for (vector<Commitment>::const_iterator i=lc.begin(); i!=lc.end(); ++i) {
134      os << "<tr><td>"; 
135      if (!stats.lines(i->author()))
136        os << i->author();
137      else
138        os << anchor(string(i->author()+"/total/index.html"),i->author());
139      os << "</td><td>" << stats.lines(i->author()) << " (" 
140         << 100*stats.lines(i->author())/stats.lines() << "%)</td>"
141         << "<td>" << stats.code(i->author()) << " (" 
142         << 100*stats.code(i->author())/stats.code() << "%)</td>"
143         << "<td>" << stats.comments(i->author()) << " (" 
144         << 100*stats.comments(i->author())/stats.comments() << "%)</td>"
145         << "<td>" << i->date()(timefmt) << "</td>"
146         <<"</tr>";
147    }
148    os << "<tr><td>Total</td>";
149    os << "<td>" << stats.lines() << "</td>"
150       << "<td>" << stats.code() << "</td>"
151       << "<td>" << stats.comments() << "</td>"
152       <<"</tr>";
153
154    os << "</table></div>\n";
155   
156  }
157
158
159  void print_recent_logs(std::ostream& os, const SVNlog& log)
160  {
161    os << "<div class=\"main\">\n"
162       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
163       << "Recent Log"
164       << "</th></tr></thead>\n";
165
166    std::vector<std::string>::const_reverse_iterator a=log.author().rbegin();
167    std::vector<std::string>::const_reverse_iterator d=log.date().rbegin();
168    std::vector<std::string>::const_reverse_iterator m=log.message().rbegin();
169    std::vector<size_t>::const_reverse_iterator r=log.revision().rbegin();
170    assert(log.author().size()==log.date().size());
171    assert(log.author().size()==log.message().size());
172    assert(log.author().size()==log.revision().size());
173    os << "<tr><td>Author</td><td>Date</td><td>Rev</td><td>Message</td></tr>\n";
174    HtmlStream hs(os);
175    std::string timefmt("%b %d %H:%M:%S %Y");
176    const size_t maxlength = 80;
177    const Configuration& conf = Configuration::instance();
178    for (size_t i=0; i<10 && a!=log.author().rend(); ++i) {
179      os << "<tr><td>" << anchor(*a+"/total/index.html",*a) << "</td>";
180      Date date(*d);
181      os << "<td>" << date(timefmt) << "</td>";
182      os << "<td>"; 
183      os << trac_revision(*r);
184      os << "</td>";
185      os << "<td>";
186      std::string mess = *m;
187      // replace newlines with space
188      std::replace(mess.begin(), mess.end(), '\n', ' ');
189      mess = htrim(mess);
190
191      if (conf.trac_root().empty()) {
192        // truncate message if too long
193        if (mess.size()>maxlength)
194          mess = mess.substr(0,maxlength-3) + "...";
195        hs << mess;
196      }
197      else {// make anchors to trac
198        Trac trac(hs);
199        trac.print(mess, maxlength);
200      }
201
202      os << "</td></tr>";
203      ++a;
204      ++d;
205      ++m;
206      ++r;
207    }
208    os << "</table></div>\n";
209  }
210
211
212  void print_summary_plot(std::ostream& os, const Stats& stats)
213  {
214    std::string name("summary_plot.png");
215    stats.plot_summary(name);
216    os << "<div class=\"main\">\n";
217    os << "<img src='" << name << "' alt='[plot]'>"; 
218    os << "</div>";
219  }
220
221}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.