From mboxrd@z Thu Jan  1 00:00:00 1970
Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org)
	by finch.gentoo.org with esmtp (Exim 4.60)
	(envelope-from <gentoo-commits+bounces-371015-garchives=archives.gentoo.org@lists.gentoo.org>)
	id 1Qrc47-0003XY-J2
	for garchives@archives.gentoo.org; Thu, 11 Aug 2011 20:41:25 +0000
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id 6B20621C120;
	Thu, 11 Aug 2011 20:41:09 +0000 (UTC)
Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183])
	by pigeon.gentoo.org (Postfix) with ESMTP id 24DBA21C120
	for <gentoo-commits@lists.gentoo.org>; Thu, 11 Aug 2011 20:41:08 +0000 (UTC)
Received: from pelican.gentoo.org (unknown [66.219.59.40])
	(using TLSv1 with cipher ADH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id 7E9391B4062
	for <gentoo-commits@lists.gentoo.org>; Thu, 11 Aug 2011 20:41:08 +0000 (UTC)
Received: from localhost.localdomain (localhost [127.0.0.1])
	by pelican.gentoo.org (Postfix) with ESMTP id 88CA180040
	for <gentoo-commits@lists.gentoo.org>; Thu, 11 Aug 2011 20:41:07 +0000 (UTC)
From: "Vikraman Choudhury" <vikraman.choudhury@gmail.com>
To: gentoo-commits@lists.gentoo.org
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "Vikraman Choudhury" <vikraman.choudhury@gmail.com>
Message-ID: <c6a27937f63a7629aeb235fb5ef8bef5c66aee78.vikraman@gentoo>
Subject: [gentoo-commits] proj/gentoostats:master commit in: server/, server/templates/
X-VCS-Repository: proj/gentoostats
X-VCS-Files: server/arch.py server/helpers.py server/kwd.py server/templates/arch.html server/templates/keyword.html
X-VCS-Directories: server/ server/templates/
X-VCS-Committer: vikraman
X-VCS-Committer-Name: Vikraman Choudhury
X-VCS-Revision: c6a27937f63a7629aeb235fb5ef8bef5c66aee78
Date: Thu, 11 Aug 2011 20:41:07 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: quoted-printable
X-Archives-Salt: 
X-Archives-Hash: c0b6d85079c7a0c3ca6830bdd389b266

commit:     c6a27937f63a7629aeb235fb5ef8bef5c66aee78
Author:     Vikraman Choudhury <vikraman.choudhury <AT> gmail <DOT> com>
AuthorDate: Thu Aug 11 20:40:36 2011 +0000
Commit:     Vikraman Choudhury <vikraman.choudhury <AT> gmail <DOT> com>
CommitDate: Thu Aug 11 20:40:36 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=3Dproj/gentoostats.g=
it;a=3Dcommit;h=3Dc6a27937

add barcharts for /arch and /keyword

---
 server/arch.py                |    7 ++++++-
 server/helpers.py             |   25 +++++++++++++++++++++++++
 server/kwd.py                 |    7 ++++++-
 server/templates/arch.html    |    4 +++-
 server/templates/keyword.html |    4 +++-
 5 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/server/arch.py b/server/arch.py
index 20dde69..814c7c4 100644
--- a/server/arch.py
+++ b/server/arch.py
@@ -11,4 +11,9 @@ class Arch(object):
         if helpers.is_json_request():
             return helpers.serialize(arch_data)
         else:
-            return render.arch(arch_data)
+            x_ticklabels =3D arch_data.keys()
+            y_values =3D [ arch_data[a]['HOSTS'] for a in x_ticklabels ]
+            arch_plot =3D helpers.barchart(title =3D 'Hosts per arch', x=
_label =3D 'Arch',
+                    y_label =3D 'Number of Hosts', x_ticklabels =3D x_ti=
cklabels,
+                    y_values =3D y_values)
+            return render.arch(arch_data, arch_plot)

diff --git a/server/helpers.py b/server/helpers.py
index a5977be..5b81cb3 100644
--- a/server/helpers.py
+++ b/server/helpers.py
@@ -3,8 +3,16 @@ import web
 import json
 import uuid
 import re
+import StringIO
+import base64
 from portage.versions import catpkgsplit
=20
+# matplotlib requires a writable home directory
+import os
+os.environ['HOME'] =3D '/tmp'
+from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCan=
vas
+from matplotlib.figure import Figure
+
 # check valid uuid
=20
 def is_uuid(uuid):
@@ -118,3 +126,20 @@ def serialize(object, human=3DTrue):
     else:
         indent =3D None
     return json.JSONEncoder(indent=3Dindent).encode(object)
+
+def barchart(title, x_label, x_ticklabels, y_label, y_values):
+    fig =3D Figure()
+    canvas =3D FigureCanvas(fig)
+    ind =3D range(len(y_values))
+
+    ax =3D fig.add_subplot(1, 1, 1)
+    ax.set_title(title)
+    ax.set_xlabel(x_label)
+    ax.set_ylabel(y_label)
+    ax.set_xticks(ind)
+    ax.set_xticklabels(x_ticklabels)
+    ax.bar(ind, y_values, align=3D'center')
+=20
+    ret =3D StringIO.StringIO()
+    canvas.print_figure(ret)
+    return base64.b64encode(ret.getvalue())

diff --git a/server/kwd.py b/server/kwd.py
index b8c50cd..779ac61 100644
--- a/server/kwd.py
+++ b/server/kwd.py
@@ -11,4 +11,9 @@ class Keyword(object):
         if helpers.is_json_request():
             return helpers.serialize(keyword_data)
         else:
-            return render.keyword(keyword_data)
+            x_ticklabels =3D keyword_data.keys()
+            y_values =3D [ keyword_data[k]['PACKAGES'] for k in x_tickla=
bels ]
+            keyword_plot =3D helpers.barchart(title =3D 'Installed packa=
ges per keyword',
+                    x_label =3D 'Keyword', y_label =3D 'Number of Packag=
es',
+                    x_ticklabels =3D x_ticklabels, y_values =3D y_values=
)
+            return render.keyword(keyword_data, keyword_plot)

diff --git a/server/templates/arch.html b/server/templates/arch.html
index 04ddcf6..8decac5 100644
--- a/server/templates/arch.html
+++ b/server/templates/arch.html
@@ -1,4 +1,4 @@
-$def with (arch_data)
+$def with (arch_data, arch_plot)
 $var title: Arch
=20
 <table border=3D"1">
@@ -9,3 +9,5 @@ $var title: Arch
     $for arch in arch_data.keys():
         <tr><td>$arch</td><td>$arch_data[arch]['HOSTS']</td></tr>
 </table>
+
+<img src=3D"data:image/png;base64,$arch_plot"/>

diff --git a/server/templates/keyword.html b/server/templates/keyword.htm=
l
index 2dee5ea..608693f 100644
--- a/server/templates/keyword.html
+++ b/server/templates/keyword.html
@@ -1,4 +1,4 @@
-$def with (keyword_data)
+$def with (keyword_data, keyword_plot)
 $var title: Keyword
=20
 <table border=3D"1">
@@ -10,3 +10,5 @@ $var title: Keyword
     $for keyword in keyword_data.keys():
         <tr><td>$keyword</td><td>$keyword_data[keyword]['HOSTS']</td><td=
>$keyword_data[keyword]['PACKAGES']</td></tr>
 </table>
+
+<img src=3D"data:image/png;base64,$keyword_plot" />