pgtoolkit/tools/longqueries.py
author Radek Brich <brich.radek@ifortuna.cz>
Tue, 06 May 2014 18:37:43 +0200
changeset 101 2a2d0d5df03b
parent 93 longqueries.py@b72591087495
permissions -rw-r--r--
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
101
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
     1
from pgtoolkit.toolbase import SimpleTool
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
     2
from pgtoolkit import pgstats
83
515fadd3d286 Add dependency on pycolib. Move common modules to pycolib. Add example table schema for meta DB.
Radek Brich <radek.brich@devl.cz>
parents: 73
diff changeset
     3
from pycolib.ansicolor import highlight, YELLOW, BOLD
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
101
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
     6
class LongQueriesTool(SimpleTool):
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
     7
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
     8
    """
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
     9
    List long running queries.
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
    10
    """
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
    11
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
    def __init__(self):
101
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
    13
        SimpleTool.__init__(self, name='longqueries')
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
    14
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
    15
    def specify_args(self):
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
    16
        SimpleTool.specify_args(self)
73
390376b9e70f Update longqueries tool: add --age parameter.
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    17
        self.parser.add_argument('--age', default='1m', help='How long must be the query running to be listed.')
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
    def main(self):
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
        stats = pgstats.PgStats(self.pgm.get_conn('target'))
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
73
390376b9e70f Update longqueries tool: add --age parameter.
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    22
        for ln in stats.list_long_queries(self.args.age):
44
4fe39c59c515 Update longqueries tool: add client IP, waiting attributes.
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
    23
            print(highlight(1),
4fe39c59c515 Update longqueries tool: add client IP, waiting attributes.
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
    24
                  'backend PID: ', ln['procpid'],
4fe39c59c515 Update longqueries tool: add client IP, waiting attributes.
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
    25
                  ', query_start: ', ln['query_start'].strftime('%F %T'),
4fe39c59c515 Update longqueries tool: add client IP, waiting attributes.
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
    26
                  ', client IP: ', ln['client_addr'],
4fe39c59c515 Update longqueries tool: add client IP, waiting attributes.
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
    27
                  ln['waiting'] and ', ' + highlight(1, YELLOW|BOLD) + 'waiting' or '',
4fe39c59c515 Update longqueries tool: add client IP, waiting attributes.
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
    28
                  highlight(0), sep='')
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
            print(ln['query'])
5
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    30
            print()
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
101
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
    33
cls = LongQueriesTool
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34