Changeset 2121

Show
Ignore:
Timestamp:
03/04/2008 23:32:46 (9 months ago)
Author:
chris
Message:

Make usage output (from bbstoreaccounts info and bbackupquery usage)
consistent and easier to read, with blocks, B/kB/MB/GB, % and an ASCII
bar graph.

Location:
box/trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • box/trunk/bin/bbackupquery/BackupQueries.cpp

    r2115 r2121  
    2828#include <set> 
    2929#include <limits> 
     30#include <iostream> 
     31#include <ostream> 
    3032 
    3133#include "BackupQueries.h" 
     
    21572159void BackupQueries::CommandUsageDisplayEntry(const char *Name, int64_t Size, int64_t HardLimit, int32_t BlockSize) 
    21582160{ 
    2159         // Calculate size in Mb 
    2160         double mb = (((double)Size) * ((double)BlockSize)) / ((double)(1024*1024)); 
    2161         int64_t percent = (Size * 100) / HardLimit; 
    2162  
    2163         // Bar graph 
    2164         char bar[41]; 
    2165         unsigned int b = (int)((Size * (sizeof(bar)-1)) / HardLimit); 
    2166         if(b > sizeof(bar)-1) {b = sizeof(bar)-1;} 
    2167         for(unsigned int l = 0; l < b; l++) 
    2168         { 
    2169                 bar[l] = '*'; 
    2170         } 
    2171         bar[b] = '\0'; 
    2172  
    2173         // Print the entryj 
    2174         ::printf("%14s %10.1fMb %3d%% %s\n", Name, mb, (int32_t)percent, bar); 
     2161        std::cout << FormatUsageLineStart(Name) << 
     2162                FormatUsageBar(Size, Size * BlockSize, HardLimit * BlockSize) << 
     2163                std::endl; 
    21752164} 
    21762165 
  • box/trunk/bin/bbstoreaccounts/bbstoreaccounts.cpp

    r1978 r2121  
    1414#include <sys/types.h> 
    1515#include <limits.h> 
     16 
     17#include <algorithm> 
     18#include <iostream> 
     19#include <ostream> 
    1620#include <vector> 
    17 #include <algorithm> 
    1821 
    1922#include "BoxPortsAndFiles.h" 
     
    6366} 
    6467 
    65 const char *BlockSizeToString(int64_t Blocks, int DiscSet) 
    66 { 
    67         // Not reentrant, nor can be used in the same function call twice, etc. 
    68         static char string[256]; 
    69          
    70         // Work out size in Mb. 
    71         double mb = (Blocks * BlockSizeOfDiscSet(DiscSet)) / (1024.0*1024.0); 
    72          
    73         // Format string 
    74 #ifdef WIN32 
    75         sprintf(string, "%I64d (%.2fMb)", Blocks, mb); 
    76 #else 
    77         sprintf(string, "%lld (%.2fMb)", Blocks, mb); 
    78 #endif 
    79          
    80         return string; 
     68std::string BlockSizeToString(int64_t Blocks, int64_t MaxBlocks, int DiscSet) 
     69{ 
     70        return FormatUsageBar(Blocks, Blocks * BlockSizeOfDiscSet(DiscSet), 
     71                MaxBlocks * BlockSizeOfDiscSet(DiscSet)); 
    8172} 
    8273 
     
    230221         
    231222        // Then print out lots of info 
    232         printf("                  Account ID: %08x\n", ID); 
    233         printf("              Last object ID: %lld\n", info->GetLastObjectIDUsed()); 
    234         printf("                 Blocks used: %s\n", BlockSizeToString(info->GetBlocksUsed(), discSet)); 
    235         printf("    Blocks used by old files: %s\n", BlockSizeToString(info->GetBlocksInOldFiles(), discSet)); 
    236         printf("Blocks used by deleted files: %s\n", BlockSizeToString(info->GetBlocksInDeletedFiles(), discSet)); 
    237         printf("  Blocks used by directories: %s\n", BlockSizeToString(info->GetBlocksInDirectories(), discSet)); 
    238         printf("            Block soft limit: %s\n", BlockSizeToString(info->GetBlocksSoftLimit(), discSet)); 
    239         printf("            Block hard limit: %s\n", BlockSizeToString(info->GetBlocksHardLimit(), discSet)); 
    240         printf("         Client store marker: %lld\n", info->GetClientStoreMarker()); 
     223        std::cout << FormatUsageLineStart("Account ID") << 
     224                BOX_FORMAT_ACCOUNT(ID) << std::endl; 
     225        std::cout << FormatUsageLineStart("Last object ID") << 
     226                BOX_FORMAT_OBJECTID(info->GetLastObjectIDUsed()) << std::endl; 
     227        std::cout << FormatUsageLineStart("Used") << 
     228                BlockSizeToString(info->GetBlocksUsed(), 
     229                        info->GetBlocksHardLimit(), discSet) << std::endl; 
     230        std::cout << FormatUsageLineStart("Old files") << 
     231                BlockSizeToString(info->GetBlocksInOldFiles(), 
     232                        info->GetBlocksHardLimit(), discSet) << std::endl; 
     233        std::cout << FormatUsageLineStart("Deleted files") << 
     234                BlockSizeToString(info->GetBlocksInDeletedFiles(), 
     235                        info->GetBlocksHardLimit(), discSet) << std::endl; 
     236        std::cout << FormatUsageLineStart("Directories") << 
     237                BlockSizeToString(info->GetBlocksInDirectories(), 
     238                        info->GetBlocksHardLimit(), discSet) << std::endl; 
     239        std::cout << FormatUsageLineStart("Soft limit") << 
     240                BlockSizeToString(info->GetBlocksSoftLimit(), 
     241                        info->GetBlocksHardLimit(), discSet) << std::endl; 
     242        std::cout << FormatUsageLineStart("Hard limit") << 
     243                BlockSizeToString(info->GetBlocksHardLimit(), 
     244                        info->GetBlocksHardLimit(), discSet) << std::endl; 
     245        std::cout << FormatUsageLineStart("Client store marker") << 
     246                info->GetLastObjectIDUsed() << std::endl; 
    241247         
    242248        return 0; 
  • box/trunk/lib/common/Utils.cpp

    r2119 r2121  
    160160} 
    161161 
    162  
    163  
    164  
     162std::string HumanReadableSize(int64_t Bytes) 
     163{ 
     164        double readableValue = Bytes; 
     165        std::string units = " B"; 
     166 
     167        if (readableValue > 1024) 
     168        { 
     169                readableValue /= 1024; 
     170                units = "kB"; 
     171        } 
     172   
     173        if (readableValue > 1024) 
     174        { 
     175                readableValue /= 1024; 
     176                units = "MB"; 
     177        } 
     178   
     179        if (readableValue > 1024) 
     180        { 
     181                readableValue /= 1024; 
     182                units = "GB"; 
     183        } 
     184   
     185        std::ostringstream result; 
     186        result << std::fixed << std::setprecision(2) << readableValue << 
     187                " " << units; 
     188        return result.str(); 
     189} 
     190 
     191std::string FormatUsageBar(int64_t Blocks, int64_t Bytes, int64_t Max) 
     192{ 
     193        std::ostringstream result; 
     194         
     195        // Bar graph 
     196        char bar[17]; 
     197        unsigned int b = (int)((Bytes * (sizeof(bar)-1)) / Max); 
     198        if(b > sizeof(bar)-1) {b = sizeof(bar)-1;} 
     199        for(unsigned int l = 0; l < b; l++) 
     200        { 
     201                bar[l] = '*'; 
     202        } 
     203        for(unsigned int l = b; l < sizeof(bar) - 1; l++) 
     204        { 
     205                bar[l] = ' '; 
     206        } 
     207        bar[sizeof(bar)-1] = '\0'; 
     208         
     209        result << std::fixed << 
     210                std::setw(10) << Blocks << " blocks, " << 
     211                std::setw(10) << HumanReadableSize(Bytes) << ", " <<  
     212                std::setw(3) << std::setprecision(0) << 
     213                ((Bytes*100)/Max) << "% |" << bar << "|"; 
     214         
     215        return result.str(); 
     216} 
     217 
     218std::string FormatUsageLineStart(const std::string& rName) 
     219{ 
     220        std::ostringstream result;       
     221        result << std::setw(20) << std::right << rName << ": "; 
     222        return result.str(); 
     223} 
  • box/trunk/lib/common/Utils.h

    r1920 r2121  
    3131}; 
    3232int ObjectExists(const std::string& rFilename); 
     33std::string HumanReadableSize(int64_t Bytes); 
     34std::string FormatUsageBar(int64_t Blocks, int64_t Bytes, int64_t Max); 
     35std::string FormatUsageLineStart(const std::string& rName); 
    3336 
    3437#include "MemLeakFindOff.h"