Пример #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()
Пример #2
0
    def __init__(self, app, name, exchange):
        super(ExchangeKeysField, self).__init__(app, name)

        self.title = "Initial bindings"
        self.exchange = exchange

        name = StringParameter(app, "name")
        self.names = ListParameter(app, "names", name)
        self.add_parameter(self.names)

        value = StringParameter(app, "value")
        self.values = ListParameter(app, "values", value)
        self.add_parameter(self.values)

        self.count = IntegerParameter(app, "count")
        self.count.default = 3
        self.add_parameter(self.count)

        self.inputs_container_tmpl = WidgetTemplate(self, "input_container_html")
        self.inputs_tmpl = WidgetTemplate(self, "inputs_html")
Пример #3
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)
Пример #4
0
class ExchangeKeysField(FormField):
    def __init__(self, app, name, exchange):
        super(ExchangeKeysField, self).__init__(app, name)

        self.title = "Initial bindings"
        self.exchange = exchange

        name = StringParameter(app, "name")
        self.names = ListParameter(app, "names", name)
        self.add_parameter(self.names)

        value = StringParameter(app, "value")
        self.values = ListParameter(app, "values", value)
        self.add_parameter(self.values)

        self.count = IntegerParameter(app, "count")
        self.count.default = 3
        self.add_parameter(self.count)

        self.inputs_container_tmpl = WidgetTemplate(self, "input_container_html")
        self.inputs_tmpl = WidgetTemplate(self, "inputs_html")

    def init(self):
        """ we added parameters directly to the FormField instead
            of adding FormInputs. XXX should this logic be moved up to FormField? """
        super(ExchangeKeysField, self).init()
        for param in self.parameters:
            self.form.form_params.add(param)

    def render_title(self, session):
        return self.title

    def render_input_fields(self, session, *args):
        count = self.count.get(session)
        writer = Writer()
        for i in range(count):
            self.inputs_tmpl.render(writer, session, i)
        return writer.to_string()

    def render_inputs(self, session, *args):
        writer = Writer()
        self.inputs_container_tmpl.render(writer, session, *args)
        return writer.to_string()

    def render_n_name(self, session, i):
        return self.names.path

    def render_v_name(self, session, i):
        return self.values.path

    def render_n_value(self, session, i):
        names = self.names.get(session)
        return len(names) > i and xml_escape(names[i]) or ""

    def render_v_value(self, session, i):
        values = self.values.get(session)
        return len(values) > i and xml_escape(values[i]) or ""

    def get_exchange(self, session):
        exchange_string = self.exchange.get(session)
        if exchange_string:
            if exchange_string == "Default exchange":
                exchange_string = ""
            obj = self.form.object.get(session)
            cls = self.app.model.org_apache_qpid_broker.Exchange
            vhostid = obj._class._name == "Vhost" and obj._id or obj._vhostRef_id
            exchanges = cls.get_selection(session.cursor, name=exchange_string, _vhostRef_id=vhostid)
            if len(exchanges):
                return exchanges[0]
        return None

    def get(self, session):
        ret_dict = dict()
        exchange = self.get_exchange(session)
        if exchange:
            if exchange.type == "headers" or exchange.type == "xml":
                names = self.names.get(session)
                values = self.values.get(session)

                for name, value in zip(names, values):
                    if name:
                        ret_dict[name] = value
        return ret_dict

    def validate(self, session):
        exchange = self.get_exchange(session)
        names = self.names.get(session)
        values = self.values.get(session)

        if exchange:
            if exchange.type == "headers":
                if not "x-match" in names:
                    error = FormError("x-match argument is required for this exchange")
                    self.form.errors.add(session, error)

                for i in range(len(names)):
                    if names[i]:
                        if names[i] == "x-match":
                            if not values[i] == "all" and not values[i] == "any":
                                error = FormError("Argument name x-match must have a value of <i>all</i> or <i>any</i>")
                                self.form.errors.add(session, error)
                        else:
                            if not values[i]:
                                error = FormError("Missing argument value for name: %s" % names[i])
                                self.form.errors.add(session, error)

                for i in range(len(values)):
                    if values[i]:
                        if not names[i]:
                            error = FormError("Missing argument name for value: %s" % values[i])
                            self.form.errors.add(session, error)

            elif exchange.type == "xml":
                if not "xquery" in names:
                    error = FormError("xquery argument is required for this exchange")
                    self.form.errors.add(session, error)