Example #1
0
def cache_customevent_trend(ids=[]):
    """
    Runs the rawdata gatherer for the specific custom events.
    Intended to be run mainly but the BibSched daemon interface.

    For a specific id, all possible timespans' rawdata is gathered.

    @param ids: The custom event ids that are subject to caching.
    @type ids: []
    """
    args = {}
    timespans = _get_timespans()

    for id in ids:
        args['id'] = id
        args['cols'] = []

        for i in range(len(timespans)):
            # Get timespans parameters
            args['timespan'] = timespans[i][0]
            args.update({ 't_start': timespans[i][2], 't_end': timespans[i][3], 'granularity': timespans[i][4],
                          't_format': timespans[i][5], 'xtic_format': timespans[i][6] })

            # Create unique filename for this combination of parameters
            filename = "webstat_customevent_%(id)s_%(timespan)s" \
                        % { 'id': re.subn("[^\w]", "_", id)[0], 'timespan': re.subn("[^\w]", "_", args['timespan'])[0] }

            # Create closure of gatherer function in case cache needs to be refreshed
            gatherer = lambda: get_customevent_trend(args)

            # Get data file from cache, ALWAYS REFRESH DATA!
            _get_file_using_cache(filename, gatherer, True).read()

    return True
Example #2
0
def cache_customevent_trend(ids=[]):
    """
    Runs the rawdata gatherer for the specific custom events.
    Intended to be run mainly but the BibSched daemon interface.

    For a specific id, all possible timespans' rawdata is gathered.

    @param ids: The custom event ids that are subject to caching.
    @type ids: []
    """
    args = {}
    timespans = _get_timespans()

    for id in ids:
        args['id'] = id
        args['cols'] = []

        for i in range(len(timespans)):
            # Get timespans parameters
            args['timespan'] = timespans[i][0]
            args.update({
                't_start': timespans[i][2],
                't_end': timespans[i][3],
                'granularity': timespans[i][4],
                't_format': timespans[i][5],
                'xtic_format': timespans[i][6]
            })

            # Create unique filename for this combination of parameters
            filename = "webstat_customevent_%(id)s_%(timespan)s" \
                        % { 'id': re.subn("[^\w]", "_", id)[0], 'timespan': re.subn("[^\w]", "_", args['timespan'])[0] }

            # Create closure of gatherer function in case cache needs to be refreshed
            gatherer = lambda: get_customevent_trend(args)

            # Get data file from cache, ALWAYS REFRESH DATA!
            _get_file_using_cache(filename, gatherer, True).read()

    return True
Example #3
0
def perform_display_customevent(ids=[], args={}, req=None, ln=CFG_SITE_LANG):
    """
    Display custom events using a certain output type over the given time span.

    @param ids: The ids for the custom events that are to be displayed.
    @type ids: [str]

    @param args: { param name: argument value }
    @type args: { str: str }

    @param req: The Apache request object, necessary for export redirect.
    @type req:
    """
    # Get all the option lists: { parameter name: [(argument internal name, argument full name)]}
    cols_dict = _get_customevent_cols()
    cols_dict['__header'] = 'Argument'
    cols_dict['__none'] = []
    options = { 'ids': ('Custom event', _get_customevents()),
                'timespan': ('Time span', _get_timespans()),
                'format': ('Output format', _get_formats(True)),
                'cols': cols_dict }

    # Build a dictionary for the selected parameters: { parameter name: argument internal name }
    choosed = { 'ids': args['ids'], 'timespan': args['timespan'], 'format': args['format']}
    # Calculate cols
    index = []
    for key in args.keys():
        if key[:4] == 'cols':
            index.append(key[4:])
    index.sort()
    choosed['cols'] = [ zip([""]+args['bool'+i], args['cols'+i], args['col_value'+i])
                        for i in index ]
    # Send to template to prepare event customization FORM box
    out = TEMPLATES.tmpl_customevent_box(options, choosed, ln=ln)

    # Arguments OK?

    # Make sure extraparams are valid, if any
    for param in ['ids', 'timespan', 'format']:
        legalvalues = [x[0] for x in options[param][1]]

        if type(args[param]) is list:
            # If the argument is a list, like the content of 'ids' every value has to be checked
            if len(args[param]) == 0:
                return out + TEMPLATES.tmpl_error('Please specify a valid value for parameter "%s".' % options[param][0], ln=ln)
            for arg in args[param]:
                if not arg in legalvalues:
                    return out + TEMPLATES.tmpl_error('Please specify a valid value for parameter "%s".' % options[param][0], ln=ln)
        else:
            if not args[param] in legalvalues:
                return out + TEMPLATES.tmpl_error('Please specify a valid value for parameter "%s".' % options[param][0], ln=ln)

    # Fetch time parameters from repository
    _, t_fullname, t_start, t_end, granularity, t_format, xtic_format = \
        options['timespan'][1][[x[0] for x in options['timespan'][1]].index(choosed['timespan'])]
    args_req = { 't_start': t_start, 't_end': t_end, 'granularity': granularity,
                't_format': t_format, 'xtic_format': xtic_format }

    data_unmerged = []

    # ASCII dump data is different from the standard formats
    if choosed['format'] == 'asciidump':
        for i in [ str(j) for j in range(len(ids))]:
            args['bool'+i].insert(0, "")
            args_req['cols'+i] = zip(args['bool'+i], args['cols'+i], args['col_value'+i])
        filename = "webstat_customevent_" + re.subn("[^\w]", "", ''.join(ids) + "_" + choosed['timespan'] + "_" + '-'.join([ ':'.join(col) for col in [ args['cols'+str(i)] for i in range(len(ids))]]) + "_asciidump")[0]
        args_req['ids'] = ids
        gatherer = lambda: get_customevent_dump(args_req)
        data = eval(_get_file_using_cache(filename, gatherer).read())
    else:
        for id, i in [ (ids[i], str(i)) for i in range(len(ids))]:
            # Calculate cols
            args_req['cols'] = choosed['cols'][int(i)]

            # Get unique name for the rawdata file (wash arguments!)
            filename = "webstat_customevent_" + re.subn("[^\w]", "", id + "_" + choosed['timespan'] + "_" + '-'.join([ ':'.join(col) for col in args_req['cols']]))[0]

            # Add the current id to the gatherer's arguments
            args_req['id'] = id

            # Prepare raw data gatherer, if cache needs refreshing.
            gatherer = lambda: get_customevent_trend(args_req)

            # Determine if this particular file is due for scheduling cacheing, in that case we must not
            # allow refreshing of the rawdata.
            allow_refresh = not _is_scheduled_for_cacheing(id)

            # Get file from cache, and evaluate it to trend data
            data_unmerged.append(eval(_get_file_using_cache(filename, gatherer, allow_refresh=allow_refresh).read()))

        # Merge data from the unmerged trends into the final destination
        data = [(x[0][0], tuple([y[1] for y in x])) for x in zip(*data_unmerged)]

    # If type indicates an export, run the export function and we're done
    if _is_type_export(args['format']):
        _get_export_closure(args['format'])(data, req)
        return out

    # Get full names, for those that have them
    names = []
    events = _get_customevents()
    for id in ids:
        temp = events[[x[0] for x in events].index(id)]
        if temp[1] != None:
            names.append(temp[1])
        else:
            names.append(temp[0])

    # Generate a filename for the graph
    filename = "tmp_webstat_customevent_" + ''.join([re.subn("[^\w]", "", id)[0] for id in ids]) + "_" + choosed['timespan']

    settings = { "title": 'Custom event',
                 "xlabel": t_fullname + ' (' + granularity + ')',
                 "ylabel": "Action quantity",
                 "xtic_format": xtic_format,
                 "format": choosed['format'],
                 "multiple": (type(ids) is list) and names or [] }

    return out + _perform_display_event(data, os.path.basename(filename), settings, ln=ln)
Example #4
0
def perform_display_customevent(ids=[], args={}, req=None, ln=CFG_SITE_LANG):
    """
    Display custom events using a certain output type over the given time span.

    @param ids: The ids for the custom events that are to be displayed.
    @type ids: [str]

    @param args: { param name: argument value }
    @type args: { str: str }

    @param req: The Apache request object, necessary for export redirect.
    @type req:
    """
    # Get all the option lists: { parameter name: [(argument internal name, argument full name)]}
    cols_dict = _get_customevent_cols()
    cols_dict['__header'] = 'Argument'
    cols_dict['__none'] = []
    options = {
        'ids': ('Custom event', _get_customevents()),
        'timespan': ('Time span', _get_timespans()),
        'format': ('Output format', _get_formats(True)),
        'cols': cols_dict
    }

    # Build a dictionary for the selected parameters: { parameter name: argument internal name }
    choosed = {
        'ids': args['ids'],
        'timespan': args['timespan'],
        'format': args['format']
    }
    # Calculate cols
    index = []
    for key in args.keys():
        if key[:4] == 'cols':
            index.append(key[4:])
    index.sort()
    choosed['cols'] = [
        zip([""] + args['bool' + i], args['cols' + i], args['col_value' + i])
        for i in index
    ]
    # Send to template to prepare event customization FORM box
    out = TEMPLATES.tmpl_customevent_box(options, choosed, ln=ln)

    # Arguments OK?

    # Make sure extraparams are valid, if any
    for param in ['ids', 'timespan', 'format']:
        legalvalues = [x[0] for x in options[param][1]]

        if type(args[param]) is list:
            # If the argument is a list, like the content of 'ids' every value has to be checked
            if len(args[param]) == 0:
                return out + TEMPLATES.tmpl_error(
                    'Please specify a valid value for parameter "%s".' %
                    options[param][0],
                    ln=ln)
            for arg in args[param]:
                if not arg in legalvalues:
                    return out + TEMPLATES.tmpl_error(
                        'Please specify a valid value for parameter "%s".' %
                        options[param][0],
                        ln=ln)
        else:
            if not args[param] in legalvalues:
                return out + TEMPLATES.tmpl_error(
                    'Please specify a valid value for parameter "%s".' %
                    options[param][0],
                    ln=ln)

    # Fetch time parameters from repository
    _, t_fullname, t_start, t_end, granularity, t_format, xtic_format = \
        options['timespan'][1][[x[0] for x in options['timespan'][1]].index(choosed['timespan'])]
    args_req = {
        't_start': t_start,
        't_end': t_end,
        'granularity': granularity,
        't_format': t_format,
        'xtic_format': xtic_format
    }

    data_unmerged = []

    # ASCII dump data is different from the standard formats
    if choosed['format'] == 'asciidump':
        for i in [str(j) for j in range(len(ids))]:
            args['bool' + i].insert(0, "")
            args_req['cols' + i] = zip(args['bool' + i], args['cols' + i],
                                       args['col_value' + i])
        filename = "webstat_customevent_" + re.subn(
            "[^\w]", "",
            ''.join(ids) + "_" + choosed['timespan'] + "_" + '-'.join([
                ':'.join(col)
                for col in [args['cols' + str(i)] for i in range(len(ids))]
            ]) + "_asciidump")[0]
        args_req['ids'] = ids
        gatherer = lambda: get_customevent_dump(args_req)
        data = eval(_get_file_using_cache(filename, gatherer).read())
    else:
        for id, i in [(ids[i], str(i)) for i in range(len(ids))]:
            # Calculate cols
            args_req['cols'] = choosed['cols'][int(i)]

            # Get unique name for the rawdata file (wash arguments!)
            filename = "webstat_customevent_" + re.subn(
                "[^\w]", "", id + "_" + choosed['timespan'] + "_" +
                '-'.join([':'.join(col) for col in args_req['cols']]))[0]

            # Add the current id to the gatherer's arguments
            args_req['id'] = id

            # Prepare raw data gatherer, if cache needs refreshing.
            gatherer = lambda: get_customevent_trend(args_req)

            # Determine if this particular file is due for scheduling cacheing, in that case we must not
            # allow refreshing of the rawdata.
            allow_refresh = not _is_scheduled_for_cacheing(id)

            # Get file from cache, and evaluate it to trend data
            data_unmerged.append(
                eval(
                    _get_file_using_cache(filename,
                                          gatherer,
                                          allow_refresh=allow_refresh).read()))

        # Merge data from the unmerged trends into the final destination
        data = [(x[0][0], tuple([y[1] for y in x]))
                for x in zip(*data_unmerged)]

    # If type indicates an export, run the export function and we're done
    if _is_type_export(args['format']):
        _get_export_closure(args['format'])(data, req)
        return out

    # Get full names, for those that have them
    names = []
    events = _get_customevents()
    for id in ids:
        temp = events[[x[0] for x in events].index(id)]
        if temp[1] != None:
            names.append(temp[1])
        else:
            names.append(temp[0])

    # Generate a filename for the graph
    filename = "tmp_webstat_customevent_" + ''.join(
        [re.subn("[^\w]", "", id)[0]
         for id in ids]) + "_" + choosed['timespan']

    settings = {
        "title": 'Custom event',
        "xlabel": t_fullname + ' (' + granularity + ')',
        "ylabel": "Action quantity",
        "xtic_format": xtic_format,
        "format": choosed['format'],
        "multiple": (type(ids) is list) and names or []
    }

    return out + _perform_display_event(
        data, os.path.basename(filename), settings, ln=ln)