Ejemplo n.º 1
0
    def __init__(self, app, name):
        super(StatChartPage, self).__init__(app, name)

        self.rosemary_class = Parameter(app, "rcls")
        self.add_parameter(self.rosemary_class)

        self.rosemary_package = Parameter(app, "rpkg")
        self.add_parameter(self.rosemary_package)

        param = StringParameter(app, "sigparam")
        self.object_signature = ListParameter(app, "signature", param)
        self.add_parameter(self.object_signature)

        param = Parameter(app, "param")
        self.stats = ListParameter(app, "stat", param)
        self.add_parameter(self.stats)

        self.mode = Parameter(app, "mode")
        self.add_parameter(self.mode)

        # In seconds
        self.duration = IntegerParameter(app, "duration")
        self.duration.default = 600
        self.add_parameter(self.duration)

        self.interval = IntegerParameter(app, "interval")
        self.interval.default = -1
        self.add_parameter(self.interval)

        self.method = Parameter(app, "method")
        self.method.default = "avg"
        self.add_parameter(self.method)

        self.samples = BooleanParameter(app, "samples")
        self.add_parameter(self.samples)

        self.container_width = IntegerParameter(app, "width")
        self.container_width.default = 360
        self.add_parameter(self.container_width)

        self.container_height = IntegerParameter(app, "height")
        self.container_height.default = 100
        self.add_parameter(self.container_height)

        self.total_property = Parameter(app, "tp")
        self.add_parameter(self.total_property)

        self.type = Parameter(app, "type")
        self.add_parameter(self.type)
        
        self.maxsamp = IntegerParameter(app, "maxsamp")
        self.maxsamp.default = 0
        self.add_parameter(self.maxsamp)

        self.cache = ImageCache()
Ejemplo n.º 2
0
class StatChartPage(Page):
    # handles stats.png request
    def __init__(self, app, name):
        super(StatChartPage, self).__init__(app, name)

        self.rosemary_class = Parameter(app, "rcls")
        self.add_parameter(self.rosemary_class)

        self.rosemary_package = Parameter(app, "rpkg")
        self.add_parameter(self.rosemary_package)

        param = StringParameter(app, "sigparam")
        self.object_signature = ListParameter(app, "signature", param)
        self.add_parameter(self.object_signature)

        param = Parameter(app, "param")
        self.stats = ListParameter(app, "stat", param)
        self.add_parameter(self.stats)

        self.mode = Parameter(app, "mode")
        self.add_parameter(self.mode)

        # In seconds
        self.duration = IntegerParameter(app, "duration")
        self.duration.default = 600
        self.add_parameter(self.duration)

        self.interval = IntegerParameter(app, "interval")
        self.interval.default = -1
        self.add_parameter(self.interval)

        self.method = Parameter(app, "method")
        self.method.default = "avg"
        self.add_parameter(self.method)

        self.samples = BooleanParameter(app, "samples")
        self.add_parameter(self.samples)

        self.container_width = IntegerParameter(app, "width")
        self.container_width.default = 360
        self.add_parameter(self.container_width)

        self.container_height = IntegerParameter(app, "height")
        self.container_height.default = 100
        self.add_parameter(self.container_height)

        self.total_property = Parameter(app, "tp")
        self.add_parameter(self.total_property)

        self.type = Parameter(app, "type")
        self.add_parameter(self.type)
        
        self.maxsamp = IntegerParameter(app, "maxsamp")
        self.maxsamp.default = 0
        self.add_parameter(self.maxsamp)

        self.cache = ImageCache()

    def get_content_type(self, session):
        return self.samples.get(session) and "text/plain" or "image/png"

    def get_object_property(self, session, property):
        rpackage = self.rosemary_package.get(session)
        rclass = self.rosemary_class.get(session)
        signature = self.object_signature.get(session)

        rosemary_package = self.app.model._packages_by_name[rpackage]
        cls = rosemary_package._classes_by_name[rclass]
        cursor = self.app.database.get_read_cursor()
        object = cls.get_object_by_signature(cursor, signature)

        return object.get_value(property)

    def get_adapter_stats(self, session):
        rpackage = self.rosemary_package.get(session)
        rclass = self.rosemary_class.get(session)
        rosemary_package = self.app.model._packages_by_name[rpackage]
        rosemary_class = rosemary_package._classes_by_name[rclass]

        signature = self.object_signature.get(session)
        
        stats = [getattr(rosemary_class, x) for x in self.stats.get(session)]
        adapters = dict()
        for stat in stats:
            filters = list()
            if self.user_selection.get(session) is not None and self.user_selection.get(session).lower() != "<none>" and self.user_selection.get(session) != "": 
                filters.append(self.UserFilter(rosemary_class, self.user_selection.get(session)))
            if self.group_selection.get(session) is not None and self.group_selection.get(session).lower() != "<none>" and self.group_selection.get(session) != "": 
                filters.append(self.UserFilter(rosemary_class, self.group_selection.get(session)))
            adapters[stat] = SamplesSqlAdapter(self.app, rosemary_class, signature, session, filters)
            
            
        return (adapters, stats)
    
    class UserFilter(SqlFilter):
        def __init__(self, cls, value):
    
            table = cls.sql_samples_table
    
            fmt = "(%s like '%s')"
            args = (table.user.identifier, value)
    
            self.text = fmt % args
    
        def emit(self):
            return self.text
    # builds an object that is similar to a RosemaryStatistic that can 
    # be used later in the chart rendering to give values to the chart items
    class Pseudostat(object):
        def __init__(self, statname):
            name, title = statname.split('|')
            self.name = name
            self.title = title
            self.short_title = None

    def get_cache_control(self, session):
        return "no-cache"

    def gen_filename(self, session):
        return session.marshal()

    def get_cached(self, session, recent):
        filename = self.gen_filename(session)
        chart, args = self.cache.find_recent(filename, 30)
        if args:
            last_recent, samples, xy = args
            if recent == last_recent:
                return chart

    def get_cached_samples(self, session, recent):
        branch = session.branch()
        self.samples.set(branch, False)
        filename = self.gen_filename(branch)
        chart, args = self.cache.get_current(filename)
        if args:
            return (args['samples'], args['title_xy'])

    def cache_it(self, session, chart, args):
        filename = self.gen_filename(session)
        writer = self.cache.create_cache_file(filename, args)
        chart.write(writer)

    def get_interval(self, session, duration, width):
        interval = self.interval.get(session)
        if interval != -1:
            return interval
        else:
            max_samples = int(width * 1.5)
            return max(int(duration / max_samples), 1)

    def render_samples(self, session, recent):
        c = {(1,0,0): "red", (0,0,1): "blue", (0,1,0): "green"}
        cached_samples, title_xy = self.get_cached_samples(session, recent)
        if cached_samples:
            rets = dict()
            for stat in cached_samples:
                ret = dict()
                ret["color"] = c[cached_samples[stat][1]]
                ret["points"] = cached_samples[stat][0]
                ret["xy"] = title_xy[cached_samples[stat][1]]
                rets[stat.name] = ret
            return str(rets)