Console: internal changes
This commit is contained in:
parent
c9cd87b142
commit
34e8277d15
@ -1,4 +1,4 @@
|
||||
#!/usr/bin/env python
|
||||
##!/usr/bin/env python
|
||||
|
||||
# Copyright (C) 2007, Eduardo Silva (edsiper@gmail.com).
|
||||
#
|
||||
@ -21,18 +21,17 @@ import sys
|
||||
import gtk
|
||||
import string
|
||||
import gobject
|
||||
import drwarea
|
||||
|
||||
import procmem
|
||||
|
||||
class CPU_Usage:
|
||||
|
||||
CPU_HZ = 0
|
||||
last_jiffies = 0
|
||||
times = 0
|
||||
_CPU_HZ = 0
|
||||
_last_jiffies = 0
|
||||
_times = 0
|
||||
|
||||
def __init__(self):
|
||||
self.CPU_hz = os.sysconf(2)
|
||||
self._CPU_HZ = os.sysconf(2)
|
||||
|
||||
def _get_CPU_data(self):
|
||||
# Uptime info
|
||||
@ -56,13 +55,13 @@ class CPU_Usage:
|
||||
|
||||
used_jiffies = (int(cpu_info[2]) + int(cpu_info[3]) + int(cpu_info[4]))
|
||||
|
||||
if self.times ==0:
|
||||
self.last_jiffies = used_jiffies
|
||||
self.times +=1
|
||||
return True
|
||||
if self._times ==0:
|
||||
self._last_jiffies = used_jiffies
|
||||
self._times +=1
|
||||
return 0
|
||||
|
||||
new_ujiffies = (used_jiffies - self.last_jiffies)
|
||||
new_ajiffies = ((self.frequency/1000) * self.CPU_hz)
|
||||
new_ujiffies = (used_jiffies - self._last_jiffies)
|
||||
new_ajiffies = ((self.frequency/1000) * self._CPU_HZ)
|
||||
|
||||
if new_ajiffies <= 0:
|
||||
pcpu = 0.0
|
||||
@ -72,38 +71,28 @@ class CPU_Usage:
|
||||
if pcpu >100:
|
||||
pcpu = 100
|
||||
|
||||
self.times +=1
|
||||
self.last_jiffies = used_jiffies
|
||||
self._times +=1
|
||||
self._last_jiffies = used_jiffies
|
||||
|
||||
return pcpu
|
||||
|
||||
class XO_CPU(gtk.Frame):
|
||||
|
||||
context = None
|
||||
frequency_timer = 1
|
||||
graph_offset = 7
|
||||
_frequency_timer = 1
|
||||
|
||||
def __init__(self):
|
||||
gtk.Frame.__init__(self, 'System CPU Usage')
|
||||
self.set_border_width(10)
|
||||
|
||||
self._updated = False
|
||||
self.drw_width = (gtk.gdk.screen_width() * 99 / 100) - 50
|
||||
self.drw_height = (gtk.gdk.screen_height() * 15 / 100) - 20
|
||||
width = (gtk.gdk.screen_width() * 99 / 100) - 50
|
||||
height = (gtk.gdk.screen_height() * 15 / 100) - 20
|
||||
|
||||
self.y_cpu = self.drw_height - self.graph_offset
|
||||
self._cpu = 0
|
||||
self._cpu_buffer = [0]
|
||||
|
||||
self._drawingarea = gtk.DrawingArea()
|
||||
self._drawingarea.set_size_request(self.drw_width, self.drw_height)
|
||||
self._drawingarea.connect("expose-event", self.do_expose)
|
||||
|
||||
self.dat = drwarea.Drawing_Area_Tools(self._drawingarea)
|
||||
self._graphic = HorizontalGraphic()
|
||||
self._graphic.set_size_request(width, height)
|
||||
|
||||
fixed = gtk.Fixed()
|
||||
fixed.set_border_width(10)
|
||||
fixed.add(self._drawingarea)
|
||||
fixed.add(self._graphic)
|
||||
|
||||
self.add(fixed)
|
||||
|
||||
@ -113,144 +102,152 @@ class XO_CPU(gtk.Frame):
|
||||
gobject.timeout_add(self._DRW_CPU.frequency, self._update_cpu_usage)
|
||||
|
||||
def _update_cpu_usage(self):
|
||||
|
||||
redraw_all = False
|
||||
|
||||
# end of the drawing area
|
||||
if ((self.frequency_timer + 1)*self.graph_offset) >= (self.drw_width - self.graph_offset):
|
||||
self.frequency_timer = 1
|
||||
self._cpu_buffer = [self._cpu_buffer[-1]]
|
||||
redraw_all = True
|
||||
length = 1
|
||||
else:
|
||||
length = len(self._cpu_buffer) - 1
|
||||
|
||||
self._cpu = self._DRW_CPU._get_CPU_usage()
|
||||
self._cpu_buffer.append(self._cpu)
|
||||
|
||||
self._updated = True
|
||||
|
||||
if redraw_all:
|
||||
area_x = 0
|
||||
area_width = self.drw_width
|
||||
else:
|
||||
area_x = (length*self.graph_offset)
|
||||
area_width = self.graph_offset*2
|
||||
|
||||
self._drawingarea.queue_draw_area(area_x, 0, area_width, self.drw_height - 5)
|
||||
self.frequency_timer += 1
|
||||
|
||||
#print "Sending: " + str(self._cpu)
|
||||
self.set_label('System CPU Usage: ' + str(self._cpu) + '%')
|
||||
self._graphic.draw_value(self._cpu)
|
||||
return True
|
||||
|
||||
def _get_y_cpu(self, pcpu):
|
||||
class HorizontalGraphic(gtk.DrawingArea):
|
||||
_MARGIN = 5
|
||||
_LINE_WIDTH = 2
|
||||
_GRAPH_OFFSET = 7
|
||||
_range_x = []
|
||||
_range_y = []
|
||||
_frequency_timer = 0
|
||||
|
||||
height = (self.dat.range_y['to']) - (self.dat.range_y['from'])
|
||||
def __init__(self):
|
||||
gtk.DrawingArea.__init__(self)
|
||||
self.connect('expose-event', self.do_expose)
|
||||
self.connect('size-allocate', self._change_size_cb)
|
||||
|
||||
# Get percent of cpu usage
|
||||
y_value = (height - ((pcpu*height)/100) + 4)
|
||||
|
||||
return int(y_value)
|
||||
self._buffer = [0]
|
||||
|
||||
def do_expose(self, widget, event):
|
||||
context = widget.window.cairo_create()
|
||||
|
||||
context.rectangle(0, 0, self.dat.width - 1, self.dat.height - 1)
|
||||
context.rectangle(0, 0, self._width - 1, self._height - 1)
|
||||
context.set_source_rgb (0,0,0)
|
||||
context.fill_preserve()
|
||||
context.set_line_width(self._LINE_WIDTH)
|
||||
|
||||
if event.area.x == 0:
|
||||
draw_all = True
|
||||
|
||||
self._draw_border_lines(context)
|
||||
context.stroke()
|
||||
else:
|
||||
draw_all = False
|
||||
|
||||
context.rectangle(event.area.x, event.area.y, event.area.width, event.area.height)
|
||||
context.clip()
|
||||
|
||||
# Drawing horizontal and vertical border lines
|
||||
self.dat.draw_border_lines(context)
|
||||
|
||||
# Drawing grid
|
||||
line_margin = self.dat.margin
|
||||
context.set_source_rgb(1, 1, 1)
|
||||
context.set_line_width(1)
|
||||
#self.draw_grid(context, event, line_margin + 1, line_margin + 1, self.dat.width - line_margin - 2, self.dat.height - line_margin - 2)
|
||||
|
||||
self._draw_buffer(event, widget, context, draw_all)
|
||||
|
||||
cpu_label = str(round(self._cpu, 4))
|
||||
self.set_label('System CPU Usage: ' + cpu_label + ' %')
|
||||
context.stroke()
|
||||
|
||||
self._updated = False
|
||||
return False
|
||||
|
||||
# Draw a grid
|
||||
def draw_grid(self, context, event, init_x, init_y, end_x, end_y):
|
||||
def draw_value(self, percent):
|
||||
redraw_all = False
|
||||
|
||||
x_range = (end_x - init_x) + 5
|
||||
y_range = (end_y - init_y) + 1
|
||||
if (len(self._buffer) + 1) *self._GRAPH_OFFSET >= self._width:
|
||||
redraw_all = True
|
||||
self._buffer = [self._buffer[-1]]
|
||||
length = 1
|
||||
else:
|
||||
length = len(self._buffer) - 1
|
||||
|
||||
current_y = init_y
|
||||
context.set_line_width(0.3)
|
||||
self._buffer.append(percent)
|
||||
self._updated = True
|
||||
|
||||
#for y in range(y_range):
|
||||
for y in range(0, y_range, 20):
|
||||
if (y%20) == 0:
|
||||
context.move_to(init_x, y)
|
||||
context.line_to(end_x, y)
|
||||
if redraw_all:
|
||||
area_x = 0
|
||||
area_y = 0
|
||||
height = self._height
|
||||
width = self._width
|
||||
else:
|
||||
area_x = (length*self._GRAPH_OFFSET)
|
||||
area_y = self._graph_y
|
||||
width = self._GRAPH_OFFSET * 2
|
||||
height = self._graph_height
|
||||
|
||||
for x in range(0, x_range, 20):
|
||||
if (x%20) == 0:
|
||||
context.move_to(x, init_y)
|
||||
context.line_to(x, end_y)
|
||||
self.queue_draw_area(area_x, area_y, width, height)
|
||||
self._frequency_timer += 1
|
||||
|
||||
context.stroke()
|
||||
return True
|
||||
|
||||
def check_context(self, event, offset, length, freq):
|
||||
print "CONTEXT ALLOWED - from: " + str(event.area.x) + " to: " + str(event.area.x+event.area.width)
|
||||
def _draw_border_lines(self, context):
|
||||
context.set_source_rgb(1, 1, 1)
|
||||
self._draw_line(context, self._MARGIN, self._MARGIN, self._MARGIN, self._height - self._MARGIN)
|
||||
self._draw_line(context, self._MARGIN, self._height - self._MARGIN - 1, self._width - self._MARGIN, self._height - self._MARGIN - 1)
|
||||
|
||||
if event.area.x != (freq*self.graph_offset):
|
||||
print "************************"
|
||||
print " ERROR DRAWING CONTEXT"
|
||||
print " ---> Area X: " + str(event.area.x) + " To X: " + str(freq*self.graph_offset)
|
||||
print "************************"
|
||||
def _draw_line(self, context, from_x, from_y, to_x, to_y):
|
||||
#print ""
|
||||
#print "DRAWING LINE"
|
||||
#print "from: " + str(from_x) + "," + str(from_y) + "," + str(to_x) + "," + str(to_y)
|
||||
|
||||
context.move_to(from_x, from_y)
|
||||
context.line_to(to_x, to_y)
|
||||
|
||||
def _draw_buffer(self, event, drwarea, context, draw_all=True):
|
||||
buffer_offset = 0
|
||||
freq = 1 # Frequency timer
|
||||
|
||||
length = len(self._cpu_buffer)
|
||||
length = len(self._buffer)
|
||||
|
||||
if length == 0:
|
||||
return
|
||||
|
||||
# Context properties
|
||||
context.set_line_width(2)
|
||||
context.set_line_width(self._LINE_WIDTH)
|
||||
context.set_source_rgb(0,1,0)
|
||||
|
||||
if draw_all == True:
|
||||
buffer_offset = 0
|
||||
freq = 0
|
||||
else:
|
||||
freq = buffer_offset = (event.area.x/self.graph_offset)
|
||||
freq = buffer_offset = (event.area.x/self._GRAPH_OFFSET)
|
||||
|
||||
for pcpu in self._cpu_buffer[buffer_offset:length]:
|
||||
for percent in self._buffer[buffer_offset:length]:
|
||||
if buffer_offset == 0:
|
||||
from_y = self.drw_height - self.graph_offset
|
||||
from_x = self.graph_offset
|
||||
from_y = self._height - self._GRAPH_OFFSET
|
||||
from_x = self._GRAPH_OFFSET
|
||||
else:
|
||||
from_y = self._get_y_cpu(self._cpu_buffer[buffer_offset-1])
|
||||
from_x = (freq * self.graph_offset)
|
||||
from_y = self._get_y(self._buffer[buffer_offset-1])
|
||||
from_x = (freq * self._GRAPH_OFFSET)
|
||||
|
||||
to_x = (freq+1) * self._GRAPH_OFFSET
|
||||
to_y = self._get_y(percent)
|
||||
|
||||
to_x = (freq+1) * self.graph_offset
|
||||
to_y = self._get_y_cpu(pcpu)
|
||||
|
||||
# Debug context, just for development
|
||||
#self.check_context(event, buffer_offset, length, freq)
|
||||
|
||||
self.dat.draw_line(context, from_x, from_y, to_x, to_y)
|
||||
self._draw_line(context, from_x, from_y, to_x, to_y)
|
||||
buffer_offset+=1
|
||||
freq+=1
|
||||
|
||||
context.stroke()
|
||||
|
||||
def _get_y(self, percent):
|
||||
y_value = self._GRAPH_OFFSET + (self._graph_height - ((percent*self._graph_height)/100))
|
||||
return int(y_value)
|
||||
|
||||
def _change_size_cb(self, widget, allocation):
|
||||
self._width = allocation.width
|
||||
self._height = allocation.height
|
||||
|
||||
self._graph_x = self._MARGIN + self._LINE_WIDTH
|
||||
self._graph_y = self._MARGIN + self._LINE_WIDTH
|
||||
self._graph_width = self._width - (self._MARGIN + self._LINE_WIDTH)
|
||||
self._graph_height = self._height - ((self._MARGIN + self._LINE_WIDTH)*2)
|
||||
|
||||
|
||||
# Just for test >:)
|
||||
#graph = HorizontalGraphic()
|
||||
#graph.set_size_request(400,200)
|
||||
"""
|
||||
window = gtk.Window()
|
||||
window.set_size_request(500, 250)
|
||||
window.add(XO_CPU())
|
||||
window.show_all()
|
||||
gtk.main()
|
||||
"""
|
||||
|
@ -39,9 +39,6 @@ class Drawing_Area_Tools:
|
||||
self.width = drwarea_size[0]
|
||||
self.height = drwarea_size[1]
|
||||
|
||||
# print "width %i" % self.width
|
||||
# print "height %i" % self.height
|
||||
|
||||
self.range_x = {'from': self.margin+2, 'to': self.width - (self.margin+2)}
|
||||
self.range_y = {'from': self.margin+2, 'to': self.height - (self.margin+2)}
|
||||
|
||||
@ -54,4 +51,3 @@ class Drawing_Area_Tools:
|
||||
self.draw_line(context, self.margin, self.margin, self.margin, self.height - self.margin)
|
||||
self.draw_line(context, self.margin, self.height - self.margin - 1, self.width - self.margin, self.height - self.margin - 1)
|
||||
context.stroke()
|
||||
|
Loading…
Reference in New Issue
Block a user