source: trunk/lib/first_page.cc @ 1237

Last change on this file since 1237 was 1237, checked in by Peter Johansson, 12 years ago

remove unneeded include in Alias.h and missing includes (found after this removal).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.5 KB
Line 
1// $Id: first_page.cc 1237 2010-10-23 21:41:40Z peter $
2
3/*
4  Copyright (C) 2006 Peter Johansson
5  Copyright (C) 2007, 2008, 2009 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2010 Peter Johansson
7
8  This file is part of svndigest, http://dev.thep.lu.se/svndigest
9
10  svndigest is free software; you can redistribute it and/or modify it
11  under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 3 of the License, or
13  (at your option) any later version.
14
15  svndigest is distributed in the hope that it will be useful, but
16  WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
22*/
23
24#include "first_page.h"
25
26#include "Commitment.h"
27#include "Configuration.h"
28#include "Date.h"
29#include "HtmlStream.h"
30#include "html_utility.h"
31#include "Stats.h"
32#include "StatsCollection.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 StatsCollection& 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", "none");
58
59    using namespace std;
60    set<string> authors;
61    std::transform(log.commits().begin(), log.commits().end(),
62                   std::inserter(authors, authors.begin()), 
63                   std::mem_fun_ref(&Commitment::author));
64    // erase invalid authors
65    authors.erase("");
66    authors.erase("no author");
67
68    vector<Commitment> latest_commit;
69    latest_commit.reserve(authors.size());
70    for (set<string>::const_iterator i(authors.begin()); i!=authors.end(); ++i)
71      latest_commit.push_back(log.latest_commit(*i));
72
73    print_summary_plot(os, stats["classic"]);
74    print_general_information(os, log, authors.size(), url);
75    sort(latest_commit.begin(), latest_commit.end(), GreaterRevision());
76    print_authors(os, latest_commit, stats["classic"]);
77    print_recent_logs(os, log, stats);
78    os << "<hr width=100% />";
79    print_footer(os);
80    os.close();
81  }
82
83  void print_general_information(std::ostream& os, const SVNlog& log, 
84                                 size_t nof_authors, std::string url)
85  { 
86    assert(log.commits().size());
87    Date begin(log.commits().begin()->date());   
88    Date end(log.latest_commit().date());
89    std::string timefmt("%a %b %e %H:%M:%S %Y");
90
91    os << "<div class=\"main\">" 
92       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
93       << "General Information"
94       << "</th></tr></thead>\n"
95       << "<tbody>\n"
96       << "<tr><td>URL:</td><td>"; 
97    if (url.size()>=4 && url.substr(0,4)=="http")
98      os << anchor(url, url);
99    else
100      os << url;
101    os << "</td></tr>\n"
102       << "<tr><td>First Revision Date:</td><td>" 
103       << begin(timefmt) << "</td></tr>\n"
104       << "<tr><td>Latest Revision Date:</td><td>" 
105       << end(timefmt) << "</td></tr>\n"
106       << "<tr><td>Age:</td><td>"; 
107    os << end.difftime(begin);
108    os << "</td></tr>\n"
109       << "<tr><td>Smallest Revision:</td><td>" 
110       << log.commits().begin()->revision() 
111       << "</td></tr>\n"
112       << "<tr><td>Largest Revision:</td><td>" << log.latest_commit().revision() 
113       << "</td></tr>\n"
114       << "<tr><td>Revision Count:</td><td>" << log.commits().size() 
115       << "</td></tr>\n"
116       << "<tr><td>Number of Authors:</td><td>" << nof_authors
117       << "</td></tr>\n"
118       << "</tbody>\n"
119       << "</table></div>\n";
120  }
121
122
123  void print_authors(std::ostream& os, 
124                     const std::vector<Commitment>& lc,
125                     const Stats& stats)
126  {
127    HtmlStream hs(os);
128    os << "<div class=\"main\">"
129       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
130       << "Authors"
131       << "</th></tr></thead>\n";
132
133    os << "<tr><td>Author</td>" 
134       << "<td>Number of Lines</td>"
135       << "<td>Code Lines</td>"
136       << "<td>Comment Lines</td>"
137       << "<td>Latest Commitment</td>"
138       <<"</tr>";
139
140    std::string timefmt("%Y-%m-%d  %H:%M");
141    using namespace std;
142    for (vector<Commitment>::const_iterator i=lc.begin(); i!=lc.end(); ++i) {
143      os << "<tr><td>"; 
144      if (!stats.lines(i->author()))
145        os << i->author();
146      else
147        os << anchor(std::string("classic/"+i->author()+"/total/index.html"),
148                     i->author());
149      os << "</td><td>" << stats.lines(i->author()) << " (" 
150         << 100*stats.lines(i->author())/(stats.lines()?stats.lines():1)
151         << "%)</td><td>" << stats.code(i->author()) << " (" 
152         << 100*stats.code(i->author())/(stats.code()?stats.code():1)
153         << "%)</td><td>" << stats.comments(i->author()) << " (" 
154         << 100*stats.comments(i->author())/(stats.comments()?stats.comments():1)
155         << "%)</td><td>";
156      std::string date = Date(i->date())(timefmt);
157      const Configuration& conf = Configuration::instance();
158      if (conf.trac_root().empty())
159        hs << date;
160      else {
161        std::stringstream url;
162        url << conf.trac_root() << "changeset/" << i->revision();
163        os << anchor(url.str(), date);
164      }
165      os << "</td>" <<"</tr>";
166    }
167    os << "<tr><td>Total</td>";
168    os << "<td>" << stats.lines() << "</td>"
169       << "<td>" << stats.code() << "</td>"
170       << "<td>" << stats.comments() << "</td>"
171       <<"</tr>";
172
173    os << "</table></div>\n";
174  }
175
176
177  void print_recent_logs(std::ostream& os, const SVNlog& log,
178                         const StatsCollection& stats)
179  {
180    os << "<div class=\"main\">\n"
181       << "<table class=\"main\"><thead><tr><th colspan=\"2\">" 
182       << "Recent Log"
183       << "</th></tr></thead>\n";
184
185    os << "<tr><td>Author</td><td>Date</td><td>Rev</td><td>Added</td>"
186       << "<td>Removed</td><td>Message</td></tr>\n";
187    HtmlStream hs(os);
188    std::string timefmt("%Y-%m-%d  %H:%M");
189    const size_t maxlength = 80;
190    const Configuration& conf = Configuration::instance();
191    typedef SVNlog::container::const_reverse_iterator iter;
192    size_t count = 0;
193    for (iter i=log.commits().rbegin(); 
194         i != log.commits().rend() && count<10; ++i) {
195      os << "<tr><td>" 
196         << anchor(std::string("classic/")+i->author()+"/total/index.html",
197                   i->author()) 
198         << "</td>";
199      Date date(i->date());
200      os << "<td>";
201      hs << date(timefmt); 
202      os << "</td>";
203      os << "<td>"; 
204      os << trac_revision(i->revision());
205      os << "</td>";
206      os << "<td>";
207      int added = stats["add"](LineTypeParser::total, "all", i->revision()) -
208        stats["add"](LineTypeParser::total, "all", i->revision() - 1); 
209      os << added;
210      os << "</td>";
211      os << "<td>";
212      os << added-(stats["blame"](LineTypeParser::total, "all", i->revision()) -
213                   stats["blame"](LineTypeParser::total,"all",i->revision()-1)); 
214      os << "</td>";
215      os << "<td>";
216      std::string mess = i->message();
217      // replace newlines with space
218      std::replace(mess.begin(), mess.end(), '\n', ' ');
219      mess = htrim(mess);
220
221      if (conf.trac_root().empty()) {
222        // truncate message if too long
223        if (mess.size()>maxlength)
224          mess = mess.substr(0,maxlength-3) + "...";
225        hs << mess;
226      }
227      else {// make anchors to trac
228        Trac trac(hs);
229        trac.print(mess, maxlength);
230      }
231
232      os << "</td></tr>\n";
233      ++count;
234    }
235    os << "</table></div>\n";
236  }
237
238
239  void print_summary_plot(std::ostream& os, const Stats& stats)
240  {
241    std::string name("summary_plot");
242    stats.plot_summary(name);
243    os << "<div class=\"main\">\n";
244    os << image(name);
245    os << "</div>";
246
247
248  }
249
250}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.