def applyByNode(requestContext, seriesList, nodeNum, templateFunction,
                newName=None):
    """
    Takes a seriesList and applies some complicated function (described by
    a string), replacing templates with unique prefixes of keys from the
    seriesList (the key is all nodes up to the index given as `nodeNum`).

    If the `newName` parameter is provided, the name of the resulting series
    will be given by that parameter, with any "%" characters replaced by the
    unique prefix.

    Example::

        &target=applyByNode(servers.*.disk.bytes_free,1,
                "divideSeries(%.disk.bytes_free,sumSeries(%.disk.bytes_*))")

    Would find all series which match `servers.*.disk.bytes_free`, then trim
    them down to unique series up to the node given by nodeNum, then fill them
    into the template function provided (replacing % by the prefixes).

    """
    from .app import evaluateTarget
    prefixes = set()
    for series in seriesList:
        prefix = '.'.join(series.name.split('.')[:nodeNum + 1])
        prefixes.add(prefix)
    results = []
    for prefix in sorted(prefixes):
        target = templateFunction.replace('%', prefix)
        for resultSeries in evaluateTarget(requestContext, target):
            if newName:
                resultSeries.name = newName.replace('%', prefix)
            resultSeries.pathExpression = prefix
            resultSeries.start = series.start
            resultSeries.end = series.end
            results.append(resultSeries)
    return results
Takes a seriesList and applies some complicated function (described by
    a string), replacing templates with unique prefixes of keys from the
    seriesList (the key is all nodes up to the index given as `nodeNum`).

    If the `newName` parameter is provided, the name of the resulting series
    will be given by that parameter, with any "%" characters replaced by the
    unique prefix.

    Example::

        &target=applyByNode(servers.*.disk.bytes_free,1,
                "divideSeries(%.disk.bytes_free,sumSeries(%.disk.bytes_*))")

    Would find all series which match `servers.*.disk.bytes_free`, then trim
    them down to unique series up to the node given by nodeNum, then fill them
    into the template function provided (replacing % by the prefixes).