+csv dep for PG'OCaml.
[cocanwiki.git] / scripts / largest_pages.ml
1 (* COCANWIKI - a wiki written in Objective CAML.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: largest_pages.ml,v 1.7 2006/12/06 09:46:57 rich Exp $
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; see the file COPYING.  If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *)
21
22 open Apache
23 open Registry
24 open Cgi
25 open Printf
26
27 open Cocanwiki
28 open Cocanwiki_template
29
30 let limit = 50
31 let modem_speed = 56000 / 10            (* 56kbps modem. *)
32 let overhead = 2 (* Number of seconds to open connection + render page. *)
33
34 let run r (q : cgi) dbh hostid _ _ =
35   let template = get_template r dbh hostid "largest_pages.html" in
36
37   (* Grab the pages from the database, ordered by size.
38    * NB.  At the moment we count the size of the markup, which is only an
39    * approximation of the final size of the page.  Also, we are not taking
40    * into account images.
41    *)
42   let rows =
43     PGSQL(dbh)
44       "select p.id, p.url, p.title, sum (length (c.content))
45          from pages p, contents c
46         where p.hostid = $hostid and p.url is not null and p.redirect is null
47           and c.pageid = p.id group by 1, 2, 3 order by 4 desc" in
48
49   let table =
50     List.map
51       (fun (pageid, page, title, size) ->
52            let page = Option.get page in
53            let size = Int64.to_int (Option.get size) in
54            let download_time = overhead + size / modem_speed in (* seconds *)
55            let download_time =
56              if download_time <= 4 then "<= 4 s"
57              else if download_time < 60 then sprintf "%d s" download_time
58              else
59                sprintf "%d m %d s" (download_time / 60) (download_time mod 60)
60            in
61
62            let size =
63              if size < 4096 then sprintf "%d bytes" size
64              else sprintf "%d K" (size / 1024) in
65
66            [ "pageid", Template.VarString (Int32.to_string pageid);
67              "page", Template.VarString page;
68              "title", Template.VarString title;
69              "size", Template.VarString size;
70              "download_time", Template.VarString download_time ]
71       ) rows in
72   template#table "pages" table;
73
74   q#template template
75
76 let () =
77   register_script ~restrict:[CanView] run