Ejemplo n.º 1
0
def stats():
    def mb(x):
        return int(round(x/2**20.))
    info={}
    info['minsizes'] = ['0','1','10','100','1000','10000','100000']
    info['minsize'] = request.args.get('minsize','1').strip()
    if not info['minsize'] in info['minsizes']:
        info['minsizes'] = '1'
    info['groupby'] = 'db' if request.args.get('groupby','').strip().lower() == 'db' else ''
    info['sortby'] = request.args.get('sortby','size').strip().lower()
    if not info['sortby'] in ['size', 'objects', 'name']:
        info['sortby'] = 'size'
    nobjects = size = dataSize = indexSize = 0
    dbSize = defaultdict(int)
    dbObjects = defaultdict(int)
    stats = {}
    table_sizes = db.table_sizes()
    def split_db(tablename):
        i = tablename.find('_')
        if i == -1:
            return '', tablename
        else:
            return tablename[:i], tablename[i+1:]
    for tablename, sizes in table_sizes.items():
        dname, name = split_db(tablename)
        dbSize[dname] += sizes['total_bytes']
        dbObjects[dname] += sizes['nrows']
    for tablename, sizes in table_sizes.items():
        tsize = sizes['total_bytes']
        size += tsize
        csize = mb(tsize)
        nobjects += sizes['nrows']
        indexSize += sizes['index_bytes']
        if csize >= int(info['minsize']):
            dname, name = split_db(tablename)
            if tablename not in db.tablenames:
                link = tablename
            else:
                link = '<a href = "' + url_for(".api_query", table=tablename) + '">' + tablename + '</a>'
            if not sizes['toast_bytes']:
                sizes['toast_bytes'] = 0 
            if sizes['nrows']:
                avg_size = int(round(float(sizes['table_bytes'] + sizes['toast_bytes'] + sizes['extra_bytes']) / sizes['nrows']))
            else:
                avg_size = 0
            stats[tablename] = {
                'db':dname, 'table':link, 'dbSize':dbSize[dname], 'dbObjects':dbObjects[dname],
                'size': csize, 'avgObjSize':avg_size,
                'indexSize':mb(sizes['index_bytes']), 'dataSize':mb(sizes['table_bytes'] + sizes['toast_bytes'] + sizes['extra_bytes']),
                'countsSize':mb(sizes['counts_bytes']), 'statsSize':mb(sizes['stats_bytes']),
                'nrows': sizes['nrows'], 'nstats': sizes['nstats'], 'ncounts': sizes['ncounts']}
    dataSize = size - indexSize
    info['ntables'] = len(table_sizes)
    info['nobjects'] = nobjects
    info['size'] = mb(size)
    info['dataSize'] = mb(dataSize)
    info['indexSize'] = mb(indexSize)
    if info['sortby'] == 'name':
        sortedkeys = sorted(list(stats))
    elif info['sortby'] == 'objects' and info['groupby'] == 'db':
        sortedkeys = sorted(list(stats),key=lambda x: (-stats[x]['dbObjects'],stats[x]['db'],-stats[x]['nrows'],stats[x]['table']))
    elif info['sortby'] == 'objects':
        sortedkeys = sorted(list(stats),key=lambda x: (-stats[x]['nrows'],stats[x]['db'],stats[x]['table']))
    elif info['sortby'] == 'size' and info['groupby'] == 'db':
        sortedkeys = sorted(list(stats),key=lambda x: (-stats[x]['dbSize'],stats[x]['db'],-stats[x]['size'],stats[x]['table']))
    else:
        sortedkeys = sorted(list(stats),key=lambda x: (-stats[x]['size'],stats[x]['db'],stats[x]['table']))
    info['stats'] = [stats[key] for key in sortedkeys]
    return render_template('api-stats.html', info=info)
Ejemplo n.º 2
0
def stats():
    def mb(x):
        return int(round(x/2**20.))
    info={}
    info['minsizes'] = ['0','1','10','100','1000','10000','100000']
    info['minsize'] = request.args.get('minsize','1').strip()
    if not info['minsize'] in info['minsizes']:
        info['minsizes'] = '1'
    info['groupby'] = 'db' if request.args.get('groupby','').strip().lower() == 'db' else ''
    info['sortby'] = request.args.get('sortby','size').strip().lower()
    if not info['sortby'] in ['size', 'objects', 'name']:
        info['sortby'] = 'size'
    nobjects = size = dataSize = indexSize = 0
    dbSize = defaultdict(int)
    dbObjects = defaultdict(int)
    stats = {}
    table_sizes = db.table_sizes()
    def split_db(tablename):
        i = tablename.find('_')
        if i == -1:
            return '', tablename
        else:
            return tablename[:i], tablename[i+1:]
    for tablename, sizes in table_sizes.items():
        dname, name = split_db(tablename)
        dbSize[dname] += sizes['total_bytes']
        dbObjects[dname] += sizes['nrows']
    for tablename, sizes in table_sizes.items():
        tsize = sizes['total_bytes']
        size += tsize
        csize = mb(tsize)
        nobjects += sizes['nrows']
        indexSize += sizes['index_bytes']
        if csize >= int(info['minsize']):
            dname, name = split_db(tablename)
            if tablename not in db.tablenames:
                link = tablename
            else:
                link = '<a href = "' + url_for(".api_query", table=tablename) + '">' + tablename + '</a>'
            if not sizes['toast_bytes']:
                sizes['toast_bytes'] = 0 
            if sizes['nrows']:
                avg_size = int(round(float(sizes['table_bytes'] + sizes['toast_bytes'] + sizes['extra_bytes']) / sizes['nrows']))
            else:
                avg_size = 0
            stats[tablename] = {
                'db':dname, 'table':link, 'dbSize':dbSize[dname], 'dbObjects':dbObjects[dname],
                'size': csize, 'avgObjSize':avg_size,
                'indexSize':mb(sizes['index_bytes']), 'dataSize':mb(sizes['table_bytes'] + sizes['toast_bytes'] + sizes['extra_bytes']),
                'countsSize':mb(sizes['counts_bytes']), 'statsSize':mb(sizes['stats_bytes']),
                'nrows': sizes['nrows'], 'nstats': sizes['nstats'], 'ncounts': sizes['ncounts']}
    dataSize = size - indexSize
    info['ntables'] = len(table_sizes)
    info['nobjects'] = nobjects
    info['size'] = mb(size)
    info['dataSize'] = mb(dataSize)
    info['indexSize'] = mb(indexSize)
    if info['sortby'] == 'name':
        sortedkeys = sorted(list(stats))
    elif info['sortby'] == 'objects' and info['groupby'] == 'db':
        sortedkeys = sorted(list(stats),key=lambda x: (-stats[x]['dbObjects'],stats[x]['db'],-stats[x]['nrows'],stats[x]['table']))
    elif info['sortby'] == 'objects':
        sortedkeys = sorted(list(stats),key=lambda x: (-stats[x]['nrows'],stats[x]['db'],stats[x]['table']))
    elif info['sortby'] == 'size' and info['groupby'] == 'db':
        sortedkeys = sorted(list(stats),key=lambda x: (-stats[x]['dbSize'],stats[x]['db'],-stats[x]['size'],stats[x]['table']))
    else:
        sortedkeys = sorted(list(stats),key=lambda x: (-stats[x]['size'],stats[x]['db'],stats[x]['table']))
    info['stats'] = [stats[key] for key in sortedkeys]
    return render_template('api-stats.html', info=info)