Permalink
Browse files

optimized memory widget

  • Loading branch information...
1 parent ea3ad9a commit 4f897dd4325db4c1a9943124e90a65aa1018819c @Eyjafjallajokull committed Jan 25, 2013
Showing with 19 additions and 19 deletions.
  1. +15 −5 widgets/memory/main.js
  2. +4 −14 widgets/memory/main.py
@@ -2,15 +2,25 @@
var w = $.extend({}, Widget);
w.mainTpl = '<div class="memoryLoad"></div><div class="memorySums"></div>';
-
+
+ function parse(data) {
+ for (var prop in data) {
+ data[prop] = parseInt(data[prop].replace(/[^\d]/g, ''));
+ }
+ return data;
+ }
+
w.render = function(data) {
+ data = parse(data)
+
+ data.used = data.total-data.free-data.buffers-data.cached;
var load = data.used/data.total;
load = Math.round(load*100);
this.$('.memoryLoad').text(load+' %');
- this.$('.memorySums').html('Total: '+Math.round(data.total/102.4)+
- 'M Used: '+Math.round(data.used/102.4)+
- 'M<br> Buffers: '+Math.round(data.buffers/102.4)+
- 'M Cached: '+Math.round(data.cached/102.4) + 'M');
+ this.$('.memorySums').html('Total: '+Math.round(data.total/1024)+
+ 'M Used: '+Math.round(data.used/1024)+
+ 'M<br> Buffers: '+Math.round(data.buffers/1024)+
+ 'M Cached: '+Math.round(data.cached/1024) + 'M');
var c = Math.round(100 * load / 100)+100;
load = (100-load) / 100;
@@ -3,19 +3,9 @@
class MemoryWidget(BaseWidget):
tags = [ 'system' ]
def collectData(self):
- meminfo = file('/proc/meminfo', 'r')
- total = meminfo.readline()[0:-4]
- free = meminfo.readline()[0:-4]
- buffers = meminfo.readline()[0:-4]
- cached = meminfo.readline()[0:-4]
- meminfo.close()
-
- total = int(total[total.rfind(' ')+1:-1])
- free = int(free[free.rfind(' ')+1:-1])
- buffers = int(buffers[buffers.rfind(' ')+1:-1])
- cached = int(cached[cached.rfind(' ')+1:-1])
- return {'total': total,
+ meminfo = self.runSystemCommand('head -4 /proc/meminfo').split('\n')[0:4]
+ (total, free, buffers, cached) = meminfo
+ return {'total': total,
'free': free,
'buffers': buffers,
- 'cached': cached,
- 'used': total-free-buffers-cached }
+ 'cached': cached }

0 comments on commit 4f897dd

Please sign in to comment.