Skip to content

Instantly share code, notes, and snippets.

@compwron
Created May 8, 2020 22:46
Show Gist options
  • Save compwron/7b52ab54750678e37f36970e6e57c25d to your computer and use it in GitHub Desktop.
Save compwron/7b52ab54750678e37f36970e6e57c25d to your computer and use it in GitHub Desktop.

Revisions

  1. compwron created this gist May 8, 2020.
    15 changes: 15 additions & 0 deletions query-counter.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@

    # USAGE:
    # def test_foo:
    # stmts = []
    # def catch_queries(conn, cursor, statement, a, b, c):
    # stmts.append(statement)
    # assert most_frequent_queries(...) == ??
    def most_frequent_queries(queries, query_count, query_type, query_print_length):
    select_sql_statement_groupings = {}
    for statement in queries:
    if statement.startswith(query_type):
    select_sql_statement_groupings.setdefault(statement, 0)
    select_sql_statement_groupings[statement] += 1
    return list(map(lambda x: (x[0][:query_print_length], x[1]), list(OrderedDict(
    sorted(select_sql_statement_groupings.items(), key=lambda x: x[1], reverse=True)).items())[:query_count]))