soc.2012.www-statscollector: 32c5118a: Switch to lxml and render OS/Architectur..

sanket at soc.pidgin.im sanket at soc.pidgin.im
Wed May 30 23:55:55 EDT 2012


----------------------------------------------------------------------
Revision: 32c5118af135840f5c99cc264cb9f90e4fe669ae
Parent:   1b3e285030d0d9c0a1fea2b90b0060c256102bb5
Author:   sanket at soc.pidgin.im
Date:     05/31/12 00:45:02
Branch:   im.pidgin.soc.2012.www-statscollector
URL: http://d.pidgin.im/viewmtn/revision/info/32c5118af135840f5c99cc264cb9f90e4fe669ae

Changelog: 

Switch to lxml and render OS/Architecture/Purple-Version information

While trying to deploy it on a free-server I realized that lxml is the
more popular choice (also available in PyPi database) for parsing the
XML dom. I have changed related code fom libxml2 -> lxml.

This patch also implements some of the statistics such as Operating System,
Architecture and Version of Purple. Though they are still in their naive
state it's a good example of the coming stats implementation.

Changes against parent 1b3e285030d0d9c0a1fea2b90b0060c256102bb5

  patched  pidgin_stats_collector/display/views.py

-------------- next part --------------
============================================================
--- pidgin_stats_collector/display/views.py	b68fac47fc125af1a156dd5b94aab893bd638fdc
+++ pidgin_stats_collector/display/views.py	66c69ae752f532fdc75a8b6fb5b8e4eb66f2ca4b
@@ -8,8 +8,10 @@ import random
 from django.core.files.temp import NamedTemporaryFile
 from django.core.files import File
 import random
-import libxml2
+import lxml
+from lxml import etree
 import simplejson
+import re
 
 # List of statistics that we are going to measure
 
@@ -28,7 +30,11 @@ ARCH_NAME = ('x86', 'x86_64', 'ppc', 'pp
 ARCH_NAME_STR = 'arch-name'
 ARCH_NAME = ('x86', 'x86_64', 'ppc', 'ppc64', 'ia64', 'unknown')
 
+# Purple version information
+PRPL_V_STR = 'purple-version'
+PRPL_V = ('unknown',)
 
+
 def init_stats_dict():
 
   """
@@ -57,6 +63,9 @@ def init_stats_dict():
   # Arch name
   stats_d[ARCH_NAME_STR] = dict(zip(ARCH_NAME, [0]*len(ARCH_NAME)))
 
+  # Purple Version
+  stats_d[PRPL_V_STR] = dict(zip(PRPL_V, [0]*len(PRPL_V)))
+
   return stats_d
 
 def process_stats():
@@ -76,52 +85,51 @@ def process_stats():
   stats_app_bit = stats_dict[APP_BIT_STR]
   stats_os_bit = stats_dict[OS_BIT_STR]
   stats_arch_name = stats_dict[ARCH_NAME_STR]
+  stats_prpl_v = stats_dict[PRPL_V_STR]
 
   for o in RawXML.objects.all():
 
     # Process each element to get stats
 
     stats_str = o.stats_xml.read()
-    stats_dom = libxml2.parseDoc(stats_str)
+    stats_dom = etree.fromstring(stats_str)
 
     # Start with cpuinfo
 
     # Type of the OS (basic)
 
-    e1 = stats_dom.xpathEval('/stats/cpuinfo/cpu/os-info')
-    id_prop = e1[0].hasProp('id')
+    e1 = stats_dom.xpath('/stats/cpuinfo/cpu/os-info')[0]
 
-    if(id_prop):
+    if('id' in e1.attrib):
 
-      os_name = id_prop.get_content().lower()
+      os_name = e1.attrib['id'].lower()
 
       if os_name in stats_os_name.keys():
         stats_os_name[os_name] += 1
       else: stats_os_name['unkwown'] += 1
 
     # The bitness of the application
-    e2 = stats_dom.xpathEval('/stats/cpuinfo/cpu/app-bit')
-    app_bit = str(e2[0].get_content())
+    e2 = stats_dom.xpath('/stats/cpuinfo/cpu/app-bit')
+    app_bit = e2[0].text
 
     if app_bit in stats_app_bit:
       stats_app_bit[app_bit] += 1
     else: stats_app_bit['unknwon'] += 1
 
     # The bitness of the kernel
-    e2 = stats_dom.xpathEval('/stats/cpuinfo/cpu/os-bit')
-    os_bit = str(e2[0].get_content())
+    e2 = stats_dom.xpath('/stats/cpuinfo/cpu/os-bit')
+    os_bit = e2[0].text
 
     if os_bit in stats_os_bit:
       stats_os_bit[os_bit] += 1
     else: stats_os_bit['unknwon'] += 1
 
     # Type of architecture we are dealing with
-    e3 = stats_dom.xpathEval('/stats/cpuinfo/cpu/arch')
-    arch_id = e3[0].hasProp('id')
+    e3 = stats_dom.xpath('/stats/cpuinfo/cpu/arch')[0]
 
-    if(arch_id):
+    if('id' in e3.attrib):
 
-      arch_name = arch_id.get_content().lower()
+      arch_name = e3.attrib['id'].lower()
 
       if arch_name == 'amd64': arch_name = 'x86_64'
       elif arch_name in ['i386', 'i486', 'i586', 'i686']:
@@ -131,6 +139,27 @@ def process_stats():
         stats_arch_name[arch_name] += 1
       else: stats_arch_name['unknown'] += 1
 
+    # Version of libpurple installation
+
+    e4 = stats_dom.xpath('/stats/cpuinfo/purple-version')[0]
+    purple_v_str = e4.text
+
+    print purple_v_str
+
+    rel_re = '^([0-9]+)\.([0-9]+)\.([0-9]+)$'
+    dev_re = '^([0-9]+)\.([0-9]+)\.([0-9]+)(devel)(.*)$'
+
+    rel_res = re.search(rel_re, purple_v_str)
+    dev_res = re.search(dev_re, purple_v_str)
+
+    if (rel_res and rel_res.group(0)==purple_v_str) or \
+        (dev_res and dev_res.group(0)==purple_v_str):
+          if not purple_v_str in stats_prpl_v:
+            stats_prpl_v[purple_v_str] = 0
+          stats_prpl_v[purple_v_str] += 1
+    else:
+      stats_prpl_v['unknown'] += 1
+
   print stats_dict
   return stats_dict
 
@@ -138,10 +167,15 @@ def index(request):
 
   stats_dict = process_stats()
 
+  stats_prpl_v = stats_dict[PRPL_V_STR]
+
   return render_to_response('display/index.html',{
       'os_name': simplejson.dumps(list(stats_dict[OS_NAME_STR].iteritems())),
       'app_bit': list(stats_dict[APP_BIT_STR].values()),
       'os_bit': list(stats_dict[OS_BIT_STR].values()),
       'arch_name': list(stats_dict[ARCH_NAME_STR].values()),
       'arch_pie': simplejson.dumps(list(stats_dict[ARCH_NAME_STR].iteritems())),
+      'purple_v_cat': simplejson.dumps(stats_dict[PRPL_V_STR].keys()),
+      'purple_v_dat': simplejson.dumps\
+          ({'name':'count', 'data':stats_dict[PRPL_V_STR].values()}),
     });


More information about the Commits mailing list