Beispiel #1
0
class EditDynamicQuotaForm(GroupForm):
    def __init__(self, app, name, task):
        super(EditDynamicQuotaForm, self).__init__(app, name, task)

        self.field_tmpl = WidgetTemplate(self, "field_html")
        self.unclaimed_tmpl = WidgetTemplate(self, "unclaimed_html")

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

        self.quotas = ListParameter(app, "quotas", quota)
        self.add_parameter(self.quotas)

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

        self.chart = PriorityPieChart \
            (app, "chart", self.object, self.group_helper, self.group_leader)
        self.add_child(self.chart)

    def render_title(self, session):
        return "Edit Dynamic Group Quota"

    def render_submit_content(self, session):
        return "Change percentage"

    def render_form_class(self, session):
        return "priorityForm"

    def render_data_col_header(self, session):
        return "Percent"
    
    def render_all_group_values(self, session):
        group_leader = self.group_leader.get(session)
        groups = self.group_helper.get_siblings(session, group_leader)
        info = self.group_helper.get_config_for_groups(session, "GROUP_QUOTA_DYNAMIC", groups)
        values = []
        dynamic_groups = []
        try:
            dynamic_groups = ["%s" % x for x in groups if info[x]["GROUP_QUOTA_DYNAMIC"] != ""]
        except:
            log.error("Error when loading dynamic group values for quota editing.")
        for group in sorted(groups):
            if(group in dynamic_groups):
                values.append(self.render_quota_value(session, group))      
        
       # values.reverse()
        values.append(self.render_unclaimed_value(session, "Unclaimed"))
                
        return values

    def render_all_group_names(self, session):
        group_leader = self.group_leader.get(session)
        groups = self.group_helper.get_siblings(session, group_leader)
        info = self.group_helper.get_config_for_groups(session, "GROUP_QUOTA_DYNAMIC", groups)
        values = []
        dynamic_groups = []
        try:
            dynamic_groups = ["%s" % x for x in groups if info[x]["GROUP_QUOTA_DYNAMIC"] != ""]
        except:
            log.error("Error when loading dynamic group names for quota editing.")
        for group in sorted(groups):
            if(group in dynamic_groups):
                values.append(group)      
        
        values.append("Unclaimed")
                
        return values
    
    def render_groups(self, session):
        writer = Writer()
                
        group_leader = self.group_leader.get(session)
        groups = self.group_helper.get_siblings(session, group_leader)
        info = self.group_helper.get_config_for_groups(session, "GROUP_QUOTA_DYNAMIC", groups)
        dynamic_groups = []
        try:
            dynamic_groups = ["%s" % x for x in groups if info[x]["GROUP_QUOTA_DYNAMIC"] != ""]
        except:
            log.error("Error when loading dynamic groups for quota editing.")
        for group in sorted(groups):
            if(group in dynamic_groups):
                self.field_tmpl.render(writer, session, group)

        self.unclaimed_tmpl.render(writer, session, "Unclaimed")

        return writer.to_string()

    def render_quota_name(self, session, group):
        return self.quotas.path

    def render_quota_value(self, session, group):
        value = self.group_helper.get_config_value(session, group, "GROUP_QUOTA_DYNAMIC")
        try:
            return round(float(value) * 100.0, 2)
        except:
            return 0.0

    def render_unclaimed_value(self, session, group):
        group_leader = self.group_leader.get(session)
        groups = self.group_helper.get_siblings(session, group_leader)
        unclaimed = self.group_helper.get_unclaimed_dyn_quota \
            (session, groups)
        return round(float(unclaimed) * 100.0, 2)

    def render_chart_id(self, session):
        return self.chart.render_id(session)

    def _invoke_tasks(self, session):
        negotiator = self.negotiator.get(session)
        quotas = self.quotas.get(session)
        group_names = self.group_names.get(session)
        original_values = self.original_values.get(session)
        info = self.group_helper.get_config_info(session)

        changed = False
        for group, new_value, original_value in zip(group_names, quotas, original_values):
            if group == "Unclaimed":
                continue
            quota = self.check_quota(new_value, original_value)
            if quota:
                self.task.invoke(session, negotiator,
                                 "GROUP_QUOTA_DYNAMIC_" + group, quota)
                info[group]["GROUP_QUOTA_DYNAMIC"] = quota
                changed = True

        if changed:
            self.task.reconfig(negotiator)
            self.app.model.update_negotiator_config_value(negotiator)

    def process_submit(self, session):
        call_async(None, self._invoke_tasks, session)
        self.task.exit_with_redirect(session)

    def check_quota(self, quota, original):
        try:
            pri = float(quota)
        except:
            return None
        if pri < 0.0 or pri > 100.0:
            return '0'

        try:
            original = float(original)
        except:
            original = 0
        if pri == original:
            return None

        return str(pri / 100)
Beispiel #2
0
class FlashFullPage(HtmlPage):
    def __init__(self, app, name):
        super(FlashFullPage, self).__init__(app, name)

        self.updater = Widget(app, "updater")
        self.updater.update_enabled = True
        self.add_child(self.updater)

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

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

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

        self.object = self.ObjectAttribute(app, "object")
        self.add_attribute(self.object)

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

        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.mode = Parameter(app, "mode")
        self.add_parameter(self.mode)

        self.chart_type = Parameter(app, "type")
        self.add_parameter(self.chart_type)

        self.flash_chart = self.GenericChart(app, "chart", self.object)
        self.add_child(self.flash_chart)

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

    def render_content(self, session):
        self.flash_chart.stats = self.stats.get(session)
        self.flash_chart.mode = self.mode.get(session)
        self.flash_chart.chart_type = self.chart_type.get(session)
        return self.flash_chart.render(session)

    class ObjectAttribute(Attribute):
        def get(self, session):
            obj = super(FlashFullPage.ObjectAttribute, self).get(session)
            if not obj:
                rpackage = self.widget.rosemary_package.get(session)
                rclass = self.widget.rosemary_class.get(session)
                signature = self.widget.object_signature.get(session)
                rosemary_package = self.app.model._packages_by_name[rpackage]
                rosemary_class = rosemary_package._classes_by_name[rclass]

                cursor = self.app.database.get_read_cursor()
                obj = rosemary_class.get_object_by_signature(cursor, signature)

                self.set(session, obj)

            return obj

    class GenericChart(StatFlashChart):
        def render_width(self, session):
            return self.parent.container_width.get(session)

        def render_height(self, session):
            return self.parent.container_height.get(session)

        def render_img_href(self, session):
            params = self.get_href_params(session)
            params.append("width=%i" % self.render_width(session))
            params.append("height=%i" % self.render_height(session))
            return escape_entity("%s?" % self.get_chart_name(session) + ";".join(params))

        def render_href(self, session):
            params = self.get_href_params(session)
            params.append("width=%i" % self.render_width(session))
            params.append("height=%i" % self.render_height(session))
            params.append("high=1")
            tp = self.parent.total_property.get(session)
            if tp:
                params.append("tp=%s" % tp)
            return escape_entity("%s?" % self.get_flash_name(session) + ";".join(params))
Beispiel #3
0
class StatFlashPage(StatChartPage):
    # handles chart.json requests
    def __init__(self, app, name):
        super(StatFlashPage, self).__init__(app, name)

        # number of milliseconds since the last update
        self.elapsed = Parameter(app, "elapsed")
        self.elapsed.default = None
        self.add_parameter(self.elapsed)

        # the current y-axis max reported by the .swf
        self.axis_max = IntegerParameter(app, "amax")
        self.axis_max.default = 0;
        self.add_parameter(self.axis_max)

        # the max y value reported by the .swf
        self.vals_max = IntegerParameter(app, "vmax")
        self.vals_max.default = 0;
        self.add_parameter(self.vals_max)

        self.chart_type = Parameter(app, "type")
        self.chart_type.default = "area"
        self.add_parameter(self.chart_type)

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

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

        name = Parameter(app, "n")
        self.add_parameter(name)

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

        val = Parameter(app, "v")
        self.add_parameter(val)

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

        self.control_min = Parameter(app, "low")
        ten_min_of_day = 10.0 / (24.0 * 60.0)
        one_hour = 1 / 24.0
        self.control_min.default = str(1.0 - one_hour)
        self.add_parameter(self.control_min)

        self.control_max = Parameter(app, "high")
        self.control_max.default = None
        self.add_parameter(self.control_max)

        self.percent_property = Parameter(app, "tp")
        self.add_parameter(self.percent_property)
        
        self.user_selection = Parameter(app, "userc")
        self.user_selection.default = None
        self.add_parameter(self.user_selection)
        
        self.group_selection = Parameter(app, "groupc")
        self.group_selection.default = None
        self.add_parameter(self.group_selection)

    def get_content_type(self, session):
        return "text/plain"

    def do_render(self, session):
        adapter, stats = self.get_adapter_stats(session)

        chart = self.chart_factory(self.chart_type.get(session))
        return chart.create(session, adapter, stats)

    def chart_factory(self, chart_type):
        if chart_type == "area":
            chart_obj = AreaChart(self.app, chart_type, self)
        elif chart_type == "stacked":
            chart_obj = StackedAreaChart(self.app, chart_type, self)
            #chart_obj = StackedChart(self.app, chart_type, self)
        elif chart_type == "percent":
            chart_obj = PercentAreaChart(self.app, chart_type, self)
        elif chart_type == "pie":
            chart_obj = FlashPieChart(self.app, chart_type, self)
        elif chart_type == "reportarea":
            chart_obj = ReportAreaChart(self.app, chart_type, self)            
            

        return chart_obj
Beispiel #4
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)
Beispiel #5
0
class BindingAddForm(ObjectFrameTaskForm):
    def __init__(self, app, name, task):
        super(BindingAddForm, self).__init__(app, name, task)

        self.queue = Parameter(app, "queue")
        self.add_parameter(self.queue)
        self.q_field = QueueBindingField(app, "q_field", self.queue)
        self.add_field(self.q_field)

        self.exchange = Parameter(app, "exchange")
        self.add_parameter(self.exchange)
        self.x_field = ExchangeBindingField(app, "x_field", self.exchange)
        self.add_field(self.x_field)

        self.key = self.KeyField(app, "key")
        self.add_field(self.key)

        self.bindings = self.ExchangeBindings(app, "bindings", self.exchange)
        self.add_field(self.bindings)

    def init(self):
        super(BindingAddForm, self).init()
        self.form_params.add(self.queue)
        self.form_params.add(self.exchange)

    def render_title(self, session):
        return self.task.get_description(session)

    def render_key_id(self, session):
        return self.key.path

    def validate(self, session):
        super(BindingAddForm, self).validate(session)

        queue = self.queue.get(session)
        exchange = self.bindings.get_exchange(session)
        key = self.key.get(session)

        if not queue:
            error = FormError("A valid queue name is required")
            self.errors.add(session, error)
        if not exchange:
            error = FormError("A valid exchange name is required")
            self.errors.add(session, error)
        else:
            if not exchange.type == "fanout":
                if not key:
                    error = MissingValueError(self.key)
                    self.errors.add(session, error)

    def process_submit(self, session):
        self.validate(session)
        if not self.errors.get(session):
            queue = self.queue.get(session)
            exchange = self.exchange.get(session)
            if exchange == "Default exchange":
                exchange = ""
            key = self.key.get(session)
            arguments = self.bindings.get(session)
            obj = self.object.get(session)

            self.task.invoke(session, obj, queue, exchange, key, arguments)
            self.task.exit_with_redirect(session)

    class KeyField(StringField):
        def render_row_id(self, session):
            return "id='%s'" % self.path

        def render_title(self, session):
            return "Binding Key"

    class ExchangeBindings(ExchangeKeysField):
        def render_id(self, session, *args):
            cls = "ExchangeHiddenRow"
            exchange = self.get_exchange(session)
            if exchange and exchange.type == 'headers':
                cls = ""
            return cls and "%s\" class=\"%s" % (self.path, cls) or self.path

        def render_title(self, session):
            return "Arguments"