Add single-user Apache bandwidth report on front page
authorAdam Chlipala <adamc@hcoop.net>
Thu, 24 Apr 2008 19:28:07 +0000 (19:28 +0000)
committerAdam Chlipala <adamc@hcoop.net>
Thu, 24 Apr 2008 19:28:07 +0000 (19:28 +0000)
portal.mlt
stats.sig
stats.sml

index 8f0f553..7fb59ee 100644 (file)
@@ -46,4 +46,15 @@ end %>
 <% end
 end;
 
+val {total, vhosts} = Stats.getWebbwUser {user = Init.getUserName (), last = 0};
+switch vhosts of
+       _ :: _ =>
+       %><h3>Your web traffic this month</h3>
+       <table><tr><th>Site</th> <th>Data transferred (kB)</th></tr><%
+       foreach e in vhosts do %>
+                      <tr><td align="right"><a href="<% if #ssl (#host e) then %>https<% else %>http<% end %>://<% #hostname (#host e) %>/"><% #hostname (#host e) %><% if #ssl (#host e) then %> (SSL)<% end %></a> <a href="/webalizer/<% #id (#host e) %>/">[detail]</a></td><td><% #size e %></td></tr>
+       <% end %>
+       </table>
+<% end;
+
 @footer [] %>
index cbca44b..a8ca8d1 100644 (file)
--- a/stats.sig
+++ b/stats.sig
@@ -12,6 +12,9 @@ sig
      * vhosts to kilobytes, and a mapping from usernames to their vhosts and bandwidth
      * totals. *)
 
+    val getWebbwUser : {user : string, last : int} -> {total : int,
+                                                      vhosts : {host : host, size : int} list}
+
     type disk = {uname : string, (* UNIX username *)
                 kbs : int}      (* Number of kilobytes space used *)
 
index 72b5836..51f23f8 100644 (file)
--- a/stats.sml
+++ b/stats.sml
@@ -89,7 +89,18 @@ struct
           before TextIO.closeIn inf
        end
 
-    type disk = {uname : string,
+   fun getWebbwUser {user, last} =
+       let
+          val {vhosts, users, ...} = getWebbw last
+       in
+          case List.find (fn {user = u, ...} => u = user) users of
+              NONE => {total = 0, vhosts = []}
+            | SOME {hosts, size, ...} =>
+              {total = size, vhosts = List.filter (fn {host, ...} => List.exists (fn host' => host' = host) hosts) vhosts}
+       end
+          
+
+   type disk = {uname : string,
                 kbs : int}
 
     structure StringKey = struct