Author: eallen
Date: 2009-05-13 11:57:14 -0400 (Wed, 13 May 2009)
New Revision: 3377
Modified:
mgmt/trunk/cumin/python/cumin/model.py
mgmt/trunk/cumin/python/cumin/stat.py
Log:
Cache line and stacked bar graphs
Modified: mgmt/trunk/cumin/python/cumin/model.py
===================================================================
--- mgmt/trunk/cumin/python/cumin/model.py 2009-05-11 20:23:02 UTC (rev 3376)
+++ mgmt/trunk/cumin/python/cumin/model.py 2009-05-13 15:57:14 UTC (rev 3377)
@@ -363,6 +363,13 @@
def init(self):
pass
+ def recent(self, object):
+ stats = object.stats
+
+ stats = stats.orderBy("-qmfUpdateTime")
+ if stats.count():
+ return stats[0].qmfUpdateTime
+
def samples(self, object, secs):
stats = object.stats
Modified: mgmt/trunk/cumin/python/cumin/stat.py
===================================================================
--- mgmt/trunk/cumin/python/cumin/stat.py 2009-05-11 20:23:02 UTC (rev 3376)
+++ mgmt/trunk/cumin/python/cumin/stat.py 2009-05-13 15:57:14 UTC (rev 3377)
@@ -8,7 +8,8 @@
from parameters import *
from util import *
from formats import *
-import tempfile
+#import tempfile
+import cStringIO
from datetime import timedelta, datetime
from visualizations import CuminVisualization
@@ -183,23 +184,21 @@
self.__files = dict() # {name: {"time": time_created, "file":
file object, "cookie": (cookie values)}}
def find_recent(self, name, max_age):
- for cname in self.__files:
- if cname == name:
- age = timedelta(seconds=max_age)
- now = datetime.now()
- then = self.__files[cname]["time"]
- if now - then < age:
- file = self.__files[cname]["file"]
- file.seek(0)
- return (file.read(), self.__files[cname]["cookie"])
- else:
- break
+ if name in self.__files:
+ age = timedelta(seconds=max_age)
+ now = datetime.now()
+ then = self.__files[name]["time"]
+ if now - then < age:
+ file = self.__files[name]["file"]
+ file.seek(0)
+ return (file.read(), self.__files[name]["cookie"])
return (None, None)
def create_cache_file(self, name, args):
if name not in self.__files:
- file = tempfile.TemporaryFile()
+ #file = tempfile.TemporaryFile()
+ file = cStringIO.StringIO()
else:
file = self.__files[name]["file"]
file.seek(0)
@@ -305,18 +304,16 @@
return writer.to_string()
def get_cached(self, session, zl):
- filename = self.__gen_filename(session, zl)
+ filename = self.gen_filename(session, zl)
return self.cache.find_recent(filename, self.max_png_age)
def cache_it(self, session, zl, map, args):
- filename = self.__gen_filename(session, zl)
+ filename = self.gen_filename(session, zl)
writer = self.cache.create_cache_file(filename, args)
map.write(writer)
- def __gen_filename(self, session, zl):
- cls = self.class_.get(session)
- id = self.id.get(session)
- return "-".join((cls, str(id), str(zl)))
+ def gen_filename(self, session):
+ return session.marshal()
def set_cookie(self, session, args):
cookie = "|".join((str(x) for x in args))
@@ -346,6 +343,8 @@
self.duration.default = 600
self.add_parameter(self.duration)
+ self.cache = ImageCache()
+
def get_content_type(self, session):
return "image/png"
@@ -357,12 +356,34 @@
id = self.id.get(session)
return (cls.get(id),)
+ def gen_filename(self, session):
+ return session.marshal()
+
+ def get_cached(self, session, recent):
+ filename = self.gen_filename(session)
+ chart, last_recent = self.cache.find_recent(filename, 3)
+ if recent == last_recent:
+ return chart
+
+ def cache_it(self, session, chart, recent):
+ filename = self.gen_filename(session)
+ writer = self.cache.create_cache_file(filename, recent)
+ chart.write(writer)
+
def do_render(self, session, object):
- chart = TimeSeriesChart(360, 100)
-
cls = self.class_.get(session)
stats = [getattr(cls, x) for x in self.stats.get(session)]
+ if len(stats):
+ stat = stats[0]
+ recent = stat.recent(object)
+ if recent:
+ cached_png = self.get_cached(session, recent)
+ if cached_png:
+ return cached_png
+
+ chart = TimeSeriesChart(360, 100)
+
samples = dict()
values = dict()
@@ -439,11 +460,12 @@
chart.plot_legend(titles, colors)
+ self.cache_it(session, chart, recent)
+
writer = Writer()
chart.write(writer)
return writer.to_string()
-from random import *
class StatStackedPage(StatChartPage):
def __init__(self, app, name):
super(StatStackedPage, self).__init__(app, name)
@@ -459,6 +481,15 @@
def do_render(self, session, object):
cls = self.class_.get(session)
stats = [getattr(cls, x) for x in self.stats.get(session)]
+
+ if len(stats):
+ stat = stats[0]
+ recent = stat.recent(object)
+ if recent:
+ cached_png = self.get_cached(session, recent)
+ if cached_png:
+ return cached_png
+
legend_height = len(stats) * 16 + 16
chart = StackedValueChart(self.container_width.get(session),
@@ -525,6 +556,8 @@
chart.plot_legend(reversed(titles), reversed(colors[:len(titles)]))
+ self.cache_it(session, chart, recent)
+
writer = Writer()
chart.write(writer)
return writer.to_string()