Ejemplo n.º 1
0
def query_list_to_table(queries,
                        maxqlen=120,
                        output=False,
                        normalise_numbers=True,
                        **outputoptions):
    """Convert a django query list (list of dict with keys time and sql) into a table3
    If output is non-False, output the table with the given options
    Specify print, "print", or a stream for output to be printed immediately
    """
    time = collections.defaultdict(list)
    for q in queries:
        query = q["sql"]
        if normalise_numbers:
            query = re.sub(r"\d+", "#", query)
        # print(query)
        time[query].append(float(q["time"]))
    t = ObjectTable(rows=time.items())
    t.add_column(lambda kv: len(kv[1]), "N")
    t.add_column(lambda kv: kv[0][:maxqlen], "Query")
    t.add_column(lambda kv: "%1.4f" % sum(kv[1]), "Cum.")
    t.add_column(lambda kv: "%1.4f" % (sum(kv[1]) / len(kv[1])), "Avg.")

    t = SortedTable(t, key=lambda row: row[2])

    if output:
        if "stream" not in outputoptions and output is not True:
            if output in (print, "print"):
                import sys
                outputoptions["stream"] = sys.stdout
            else:
                outputoptions["stream"] = output
        t.output(**outputoptions)
    return t
Ejemplo n.º 2
0
def query_list_to_table(queries, maxqlen=120, output=False, normalise_numbers=True,
                        **outputoptions):
    """Convert a django query list (list of dict with keys time and sql) into a table3
    If output is non-False, output the table with the given options
    Specify print, "print", or a stream for output to be printed immediately
    """
    time = collections.defaultdict(list)
    for q in queries:
        query = q["sql"]
        if normalise_numbers:
            query = re.sub(r"\d+", "#", query)
        # print(query)
        time[query].append(float(q["time"]))
    t = ObjectTable(rows=time.items())
    t.add_column(lambda kv: len(kv[1]), "N")
    t.add_column(lambda kv: kv[0][:maxqlen], "Query")
    t.add_column(lambda kv: "%1.4f" % sum(kv[1]), "Cum.")
    t.add_column(lambda kv: "%1.4f" % (sum(kv[1]) / len(kv[1])), "Avg.")

    t = SortedTable(t, key=lambda row: row[2])

    if output:
        if "stream" not in outputoptions and output is not True:
            if output in (print, "print"):
                import sys
                outputoptions["stream"] = sys.stdout
            else:
                outputoptions["stream"] = output
        t.output(**outputoptions)
    return t
Ejemplo n.º 3
0
    def test_object_table(self):
        """Does creating object tables work"""
        class Test(object):
            def __init__(self, a, b, c):
                self.a, self.b, self.c = a, b, c

        l = ObjectTable(
            rows=[Test(1, 2, 3),
                  Test("bla", None, 7),
                  Test(-1, -1, None)])
        l.add_column(lambda x: x.a, "de a")
        l.add_column("b")
        l.add_column(ObjectColumn("en de C", lambda x: x.c))

        result = tableoutput.table2unicode(l)
        # get rid of pesky unicode
        result = result.translate(
            dict((a, 65 + a % 26) for a in range(0x2500, 0x2600)))

        correct = '''OKKKKKKEKKKKEKKKKKKKKKR
L de a K b  K en de C L
ZIIIIIIQIIIIQIIIIIIIIIC
L 1    K 2  K 3       L
L bla  K    K 7       L
L -1   K -1 K         L
UKKKKKKHKKKKHKKKKKKKKKX'''
        self.assertEquals(_striplines(result), _striplines(correct.strip()))
Ejemplo n.º 4
0
    def test_object_table(self):
        """Does creating object tables work"""

        class Test(object):
            def __init__(self, a, b, c):
                self.a, self.b, self.c = a, b, c

        l = ObjectTable(rows=[Test(1, 2, 3), Test("bla", None, 7), Test(-1, -1, None)])
        l.add_column(lambda x: x.a, "de a")
        l.add_column("b")
        l.add_column(ObjectColumn("en de C", lambda x: x.c))

        result = tableoutput.table2unicode(l)
        # get rid of pesky unicode
        result = result.translate(dict((a, 65 + a % 26) for a in range(0x2500, 0x2600)))

        correct = '''OKKKKKKEKKKKEKKKKKKKKKR
L de a K b  K en de C L
ZIIIIIIQIIIIQIIIIIIIIIC
L 1    K 2  K 3       L
L bla  K    K 7       L
L -1   K -1 K         L
UKKKKKKHKKKKHKKKKKKKKKX'''
        self.assertEquals(_striplines(result), _striplines(correct.strip()))