Beispiel #1
0
def desc(db, objectName, printDetails=True, printStats=False, sort=False):
    """Describes an object
    @param objectName: object to be described
    @return: header and resultset of definition as a tuple (header, definition)
    ==> This function should be split in two parts: one in pysqlOraObjects for object self description
    the other one here as a describe function that encapsulate pysqlOraObjects manipulation"""

    header = []
    result = []

    # Reads conf
    conf = PysqlConf.getConfig()
    unit = conf.get("unit") # Unit used to format data

    # Look for object type if given
    matchResult = re.match("(.*) \((.+)\)", objectName)
    if matchResult:
        oraObject = OraObject(objectName=matchResult.group(1),
                              objectType=matchResult.group(2))
    else:
        oraObject = OraObject(objectName=objectName)

    # Gets the object type and owner
    oraObjectSet = oraObject.guessInfos(db, interactive=True)


    if len(oraObjectSet) == 1:
        oraObject = oraObjectSet.pop()
    elif len(oraObjectSet) > 1:
        print CYAN + _("Got multiple result:") + "\n-" + RESET,
        print "\n- ".join([str(x) for x in oraObjectSet])
        # Looking for own object
        ownOraObjects = [o for o in oraObjectSet if o.getOwner() == db.getUsername().upper()]
        publicOraObjects = [o for o in oraObjectSet if o.getOwner() == "PUBLIC"]
        if len(ownOraObjects) == 1:
            oraObject = ownOraObjects.pop()
            print BOLD + RED + _("Defaulting to own object: %s") % oraObject + RESET
        # Looking for public objects
        elif len(publicOraObjects) == 1:
            oraObject = publicOraObjects.pop()
            print BOLD + RED + _("Defaulting to public object: %s") % oraObject + RESET
    else:
        # No result
        return ([], [])

    # Object or type unknown?
    if oraObject.getType() == "":
        return ([], [])

    # Tries to resolve synonym and describe the target
    if oraObject.getType() == "SYNONYM":
        oraObject = oraObject.getTarget(db)
        if oraObject.getType() == "SYNONYM":
            # cannot desc, too much synonym recursion
            return ([], [])

    # Guess object status
    oraObject.guessStatus(db)

    # Displays some information about the object
    if printDetails:
        print CYAN + _("Name") + "\t: " + oraObject.getName() + RESET
        print CYAN + _("Type") + "\t: " + oraObject.getType() + RESET
        print CYAN + _("Owner") + "\t: " + oraObject.getOwner() + RESET
        if oraObject.getStatus() in ("INVALID", "OFFLINE", "UNUSED"):
            print CYAN + _("Status") + "\t\t: " + BOLD + RED + oraObject.getStatus() + RESET
        else:
            print CYAN + _("Status") + "\t\t: " + oraObject.getStatus() + RESET
        if oraObject.getType() in ("TABLE", "TABLE PARTITION", "INDEX", "INDEX PARTITION"):
            try:
                print CYAN + _("Tablespace") + "\t: " + oraObject.getTablespace(db) + RESET
            except PysqlException:
                print CYAN + _("Tablespace") + "\t: " + _("<unable to get tablepsace name>") + RESET
            try:
                print CYAN + _("Partitioned?") + "\t: " + (oraObject.isPartitioned(db) and _("Yes") or _("No")) + RESET
            except PysqlException:
                print CYAN + _("Partitioned?") + "\t: " + _("<unable to get partition information>") + RESET
        if oraObject.getType() in ("TABLE", "TABLE PARTITION", "VIEW", "MATERIALIZED VIEW"):
            try:
                print CYAN + _("Comment") + "\t: " + oraObject.getComment(db) + RESET
            except PysqlException:
                print CYAN + _("Comment") + "\t: " + _("<unable to get comment>") + RESET
        if oraObject.getType() not in ("DATA FILE", "TABLESPACE", "USER"):
            try:
                print CYAN + _("Created on") + "\t: " + oraObject.getCreated(db) + RESET
            except PysqlException:
                print CYAN + _("Created on") + "\t: " + _("<unable to get date of creation>") + RESET
            try:
                print CYAN + _("Last DDL on") + "\t: " + oraObject.getLastDDL(db) + RESET
            except PysqlException:
                print CYAN + _("Last DDL on") + "\t: " + _("<unable to get date of last DDL modification>") + RESET

    # Displays some statistics about the object
    if printStats:
        if oraObject.getType() in ("TABLE", "TABLE PARTITION"):
            try:
                print ORANGE + _("Last analyzed on") + ": " + str(oraObject.getLastAnalyzed(db)) + RESET
            except PysqlException:
                print CYAN + _("Last analyzed on") + "\t: " + _("<unable to get date of last statistics computation>") + RESET
            try:
                print ORANGE + _("Nb rows") + "\t\t: " + str(oraObject.getNumRows(db)) + RESET
            except PysqlException:
                print CYAN + _("Nb rows") + "\t: " + _("<unable to get number of rows>") + RESET
            try:
                print ORANGE + _("Nb used blocks") + "\t: " + str(oraObject.getUsedBlocks(db)) + RESET
            except PysqlException:
                print ORANGE + _("Nb used blocks") + "\t: " + _("<unable to get number of used blocks>") + RESET
            try:
                print ORANGE + _("Avg row length") + "\t: " + str(oraObject.getAvgRowLength(db)) + RESET
            except PysqlException:
                print CYAN + _("Avg row length") + "\t: " + _("<unable to get average row length") + RESET

    # Evaluates object type (among the 24 defined)
    if oraObject.getType() in ("TABLE" , "TABLE PARTITION"):
        header = [_("Name"), _("Type"), _("Null?"), _("Comments"), _("Indexes")]
        columns = oraObject.getTableColumns(db, sort)

        # Gets indexed columns of the table
        indexedColumns = oraObject.getIndexedColumns(db)
        # Format index this way: index_name(index_position)
        #TODO: handle database encoding instead of using just str()
        indexedColumns = [[i[0], i[1] + "(" + str(i[2]) + ")"] for i in indexedColumns]
        for column in columns:
            column = list(column) # change tuple to list
            indexInfo = [i[1] for i in indexedColumns if i[0] == column[0]]
            column.append(", ".join(indexInfo))
            result.append(column)

    elif oraObject.getType() in ("VIEW", "MATERIALIZED VIEW"):
        header = [_("Name"), _("Type"), _("Null?"), _("Comments")]
        result = oraObject.getTableColumns(db)

    elif oraObject.getType() == "CONSUMER GROUP":
        raise PysqlNotImplemented()

    elif oraObject.getType() == "CONTEXT":
        raise PysqlNotImplemented()

    elif oraObject.getType() == "DATA FILE":
        header = [_("Tablespace"), _("Size (%s)") % unit.upper(), _("Free (%s)") % unit.upper(), _("%Used")]
        size = convert(oraObject.getAllocatedBytes(db), unit)
        free = convert(oraObject.getFreeBytes(db), unit)
        if size != 0:
            used = 100 - float(100 * free) / size
        else:
            used = 0
        if printDetails:
            print CYAN + _("Tablespace: ") + oraObject.getTablespace(db).getName() + RESET
        result = [[oraObject.getTablespace(db).getName(), round(size, 2), round(free, 2), round(used, 2)]]

    elif oraObject.getType() == "DATABASE LINK":
        header = [_("Target")]
        result = [[oraObject.getRemoteUser(db) + "@" + oraObject.getRemoteHost(db)]]

    elif oraObject.getType() == "DIRECTORY":
        header = [_("Path")]
        result = [[oraObject.getPath(db)]]

    elif oraObject.getType() == "EVALUATION CONTEXT":
        raise PysqlNotImplemented()

    elif oraObject.getType()in("FUNCTION", "PACKAGE", "PROCEDURE"):
        header = [_("#"), _("Source")]
        result = oraObject.getSource(db)

    elif oraObject.getType() == "INDEX":
        header = [_("Property"), _("Value")]
        result = oraObject.getProperties(db)

    elif oraObject.getType() == "INDEX PARTITION":
        raise PysqlNotImplemented()

    elif oraObject.getType() == "INDEXTYPE":
        raise PysqlNotImplemented()

    elif oraObject.getType() == "JAVA CLASS":
        raise PysqlNotImplemented()

    elif oraObject.getType() == "JAVA DATA":
        raise PysqlNotImplemented()

    elif oraObject.getType() == "JAVA RESOURCE":
        raise PysqlNotImplemented()

    elif oraObject.getType() == "LIBRARY":
        raise PysqlNotImplemented()

    elif oraObject.getType() == "OPERATOR":
        raise PysqlNotImplemented()

    elif oraObject.getType() == "PACKAGE BODY":
        raise PysqlNotImplemented()

    elif oraObject.getType() == "SEQUENCE":
        header = [_("Last"), _("Min"), _("Max"), _("Step")]
        result = [[oraObject.getLast(db), oraObject.getMin(db), oraObject.getMax(db), oraObject.getStep(db)]]

    elif oraObject.getType() == "TABLE PARTITION":
        raise PysqlNotImplemented()

    elif oraObject.getType() == "TABLESPACE":
        oraObject.updateDatafileList(db)
        header = [_("Datafile"), _("Size (%s)") % unit.upper(), _("Free (%s)") % unit.upper(), _("%Used")]
        result = [[]]
        totalSize = 0
        totalFree = 0
        totalUsed = 0
        for datafile in oraObject.getDatafiles():
            name = datafile.getName()
            size = convert(datafile.getAllocatedBytes(db), unit)
            free = convert(datafile.getFreeBytes(db), unit)
            if size != 0:
                used = 100 - float(100 * free) / size
            else:
                used = 0
            result.append([name, round(size, 2), round(free, 2), round(used, 2)])
            totalSize += size
            totalFree += free
        if totalSize != 0:
            totalUsed = 100 - float(100 * totalFree) / totalSize
        else:
            totalUsed = 0
        if len(oraObject.getDatafiles()) > 1:
            result[0] = ["> " + _("TOTAL"), round(totalSize, 2), round(totalFree, 2), round(totalUsed, 2)]
        else:
            result.pop(0)

    elif oraObject.getType() == "TRIGGER":
        oraObject.updateTable(db)
        header = [_("Status"), _("Table"), _("Type"), _("Event"), _("Body")]
        result = [[oraObject.getStatus(db), oraObject.getTable(db).getFullName(),
                 oraObject.getTriggerType(db), oraObject.getEvent(db),
                 oraObject.getBody(db).replace("\n", " ")]]

    elif oraObject.getType() == "USER":
        oraObject.updateTablespaceList(db)
        header = [_("Tablespace"), _("Default?"), _("#Tables"), _("#Indexes")]
        result = [[]]
        totalTables = 0
        totalIndexes = 0
        defaultTbs = oraObject.getDefaultTablespace(db)
        #tempTbs = oraObject.getTempTablespace(db)
        for tablespace in oraObject.getTablespaces():
            name = tablespace.getName()
            nbTables = oraObject.getNbTables(db, tablespace=tablespace.getName())
            nbIndexes = oraObject.getNbIndexes(db, tablespace=tablespace.getName())
            if name == defaultTbs:
                defstr = u"*"
            else:
                defstr = u""
            result.append([name, defstr, nbTables, nbIndexes])
            totalTables += nbTables
            totalIndexes += nbIndexes
        if len(oraObject.getTablespaces()) > 1:
            result[0] = ["> " + _("TOTAL"), u"", totalTables, totalIndexes]
        else:
            result.pop(0)
    else:
        raise PysqlException(_("Type not handled: %s") % oraObject.getType())
    return (header, result)
Beispiel #2
0
def diskusage(db, userName, withIndexes=False, percent=True):
    """Extracts the physical storage of the current user as a picture based on Oracle statistics
The generation of the picture is powered by Graphviz (http://www.graphviz.org)
through the PyDot API (http://www.dkbza.org/pydot.html)
"""
    # Tries to import pydot module
    try:
        from pydot import find_graphviz, Dot, Subgraph, Cluster, Edge, Node
    except ImportError:
        message = _("Function not available because pydot module is not installed.\n\t")
        message += _("Go to http://dkbza.org/pydot.html to get it.")
        raise PysqlException(message)

    # Reads conf
    conf = PysqlConf.getConfig()
    unit = conf.get("unit") # Unit used to format data
    format = conf.get("graph_format") # Output format of the picture
    fontname = conf.get("graph_fontname") # Font used for table names
    fontsize = conf.get("graph_fontsize") # Font size for table names
    fontcolor = conf.get("graph_fontcolor") # Color of table and column names
    tablecolor = conf.get("graph_tablecolor") # Color of tables
    indexcolor = conf.get("graph_indexcolor") # Color of indexes
    bordercolor = conf.get("graph_bordercolor") # Color of borders

    # Gets picture generator
    prog = getProg(find_graphviz(), conf.get("graph_program"), "fdp")

# First step: objects library building
    # Tablespaces
    if userName == db.getUsername().upper():
        tablespaces = db.executeAll(diskusageSql["Tablespaces"])
    else:
        tablespaces = db.executeAll(diskusageSql["TablespacesFromOwner"], [userName])
    tbsBytes = 0
    tbsList = []
    for tablespace in tablespaces:
        tablespaceName = unicode(tablespace[0])

        # Tables from current tablespace
        if userName == db.getUsername().upper():
            tables = db.executeAll(diskusageSql["TablesFromTbs"], [tablespaceName])
        else:
            tables = db.executeAll(diskusageSql["TablesFromOwnerAndTbs"], [userName, tablespaceName])
        tabList = []
        print CYAN + _("Extracting %3d tables from tablespace %s") % (len(tables), tablespaceName) + RESET
        for table in tables:
            tableName = table[0]
            if table[1] is None:
                print RED + _("""Warning: table "%s" removed because no statistics have been found""") \
                           % (tablespaceName + "/" + tableName) + RESET
                continue
            if table[1] == 0:
                print RED + _("""Warning: table "%s" removed because it is empty""") \
                           % (tablespaceName + "/" + tableName) + RESET
                continue
            numRows = int(table[1])
            avgRowLen = float(table[2])
            bytes = int(table[3])
            tbsBytes += bytes
            tabList += [[tableName, bytes, numRows, avgRowLen]]

        if withIndexes:
            # Indexes from current tablespace
            if userName == db.getUsername().upper():
                indexes = db.executeAll(diskusageSql["IndexesFromTbs"], [tablespaceName])
            else:
                indexes = db.executeAll(diskusageSql["IndexesFromOwnerAndTbs"], [userName, tablespaceName])
            idxList = []
            print CYAN + _("Extracting %3d indexes from tablespace %s") % (len(indexes), tablespaceName) + RESET
            for index in indexes:
                indexName = index[0]
                if index[1] is None:
                    print RED + _("""Warning: index "%s" removed because no statistics have been found""") \
                            % (tablespaceName + "/" + indexName) + RESET
                    continue
                if index[1] == 0:
                    print RED + _("""Warning: index "%s" removed because it is empty""") \
                            % (tablespaceName + "/" + indexName) + RESET
                    continue
                numRows = int(index[1])
                distinctKeys = int(index[2])
                bytes = int(index[3])
                tabName = str(index[4])
                tbsBytes += bytes
                idxList += [[indexName, bytes, numRows, distinctKeys, tabName]]
        else:
            print CYAN + _("Not extracting indexes from tablespace %s (ignored)") % (tablespaceName) + RESET
            idxList = []
        tbsList += [[tablespaceName, tbsBytes, tabList, idxList]]

# Second step: objects drawing
    graph = Dot(label=userName, overlap="false", splines="true")

    for tbs in tbsList:
        tbsName = tbs[0]
        tbsBytes = tbs[1]
        tabList = tbs[2]
        idxList = tbs[3]
        subGraph = Subgraph("cluster_" + tbsName, bgcolor="palegreen", \
                          fontname=fontname, fontsize=str(fontsize - 1), \
                          label="%s\\n(%d %s)" % (tbsName, convert(tbsBytes, unit), unit.upper()))
        graph.add_subgraph(subGraph)

        print CYAN + _("Displaying %3d tables for tablespace %s") % (len(tabList), tbsName) + RESET
        for tab in tabList:
            name = tab[0]
            bytes = tab[1]
            numRows = tab[2]      # unused
            avgRowLen = tab[3]    # unused

            # Mathematics at work
            width = 0.2
            height = 0.2
            if percent:
                height += 10 * round(float(bytes) / tbsBytes, 4)
                label = "%s\\n(%.2f %s)" % (name, round(100 * float(bytes) / tbsBytes, 2), "%")

            else:
                height += round(sqrt(bytes) / 8192, 3)
                width += round(sqrt(bytes) / 8192, 3)
                label = "%s\\n(%3d %s)" % (name, convert(bytes, unit), unit.upper())
            subGraph.add_node(Node(name, label=label, shape="box", style="filled", \
                                   color="none", fillcolor=tablecolor, \
                                   fontname=fontname, fontcolor=fontcolor, fixedsize="false", \
                                   fontsize=str(fontsize - 2 - floor((len(label) - 7) / 15)), \
                                   nodesep="0.01", height=str(height), width=str(max(width, 1))))

        print CYAN + _("Displaying %3d indexes for tablespace %s") % (len(idxList), tbsName) + RESET
        for idx in idxList:
            name = idx[0]
            bytes = idx[1]
            numRows = idx[2]      # unused
            distinctKeys = idx[3] # unused
            tabName = idx[4]      # unused

            # Mathematics at work again)
            width = 0.2
            height = 0.2
            if percent:
                height += 10 * round(float(bytes) / tbsBytes, 4)
                label = "%s\\n(%.2f %s)" % (name, round(100 * float(bytes) / tbsBytes, 2), "%")
            else:
                height += round(sqrt(bytes) / 8192, 3)
                width += round(sqrt(bytes) / 8192, 3)
                label = "%s\\n(%3d %s)" % (name, convert(bytes, unit), unit.upper())

            subGraph.add_node(Node(name, label=label, shape="box", style="filled", \
                                color="none", fillcolor=indexcolor, \
                                fontname=fontname, fontcolor=fontcolor, fixedsize="false", \
                                fontsize=str(fontsize - 2 - floor((len(label) - 7) / 15)), \
                                nodesep="0.01", height=str(height), width=str(max(width, 1))))
            #Moving index near by its table (unused because it widens the graph)
            #subGraph.add_edge(Edge(src=name, dst=tabName, constraint="false", style="invis"))

    filename = "du_" + userName + "." + format
    generateImage(graph, filename, prog, format)
    viewImage(filename)