Example #1
0
    void Top::_appendToUsageMap( BSONObjBuilder& b, const UsageMap& map ) const {
        // pull all the names into a vector so we can sort them for the user

        vector<string> names;
        for ( UsageMap::const_iterator i = map.begin(); i != map.end(); ++i ) {
            names.push_back( i->first );
        }

        std::sort( names.begin(), names.end() );

        for ( size_t i=0; i<names.size(); i++ ) {
            BSONObjBuilder bb( b.subobjStart( names[i] ) );

            const CollectionData& coll = map.find(names[i])->second;

            _appendStatsEntry( b, "total", coll.total );

            _appendStatsEntry( b, "readLock", coll.readLock );
            _appendStatsEntry( b, "writeLock", coll.writeLock );

            _appendStatsEntry( b, "queries", coll.queries );
            _appendStatsEntry( b, "getmore", coll.getmore );
            _appendStatsEntry( b, "insert", coll.insert );
            _appendStatsEntry( b, "update", coll.update );
            _appendStatsEntry( b, "remove", coll.remove );
            _appendStatsEntry( b, "commands", coll.commands );

            bb.done();
        }
    }
Example #2
0
File: top.cpp Project: IlyaM/mongo
    void Top::append( BSONObjBuilder& b , const UsageMap& map ){
        for ( UsageMap::const_iterator i=map.begin(); i!=map.end(); i++ ){
            BSONObjBuilder bb( b.subobjStart( i->first.c_str() ) );
            
            const CollectionData& coll = i->second;
            
            append( b , "total" , coll.total );
            
            append( b , "readLock" , coll.readLock );
            append( b , "writeLock" , coll.writeLock );

            append( b , "queries" , coll.queries );
            append( b , "getmore" , coll.getmore );
            append( b , "insert" , coll.insert );
            append( b , "update" , coll.update );
            append( b , "remove" , coll.remove );

            bb.done();
        }
    }
Example #3
0
void ApplicationUsage::getFormattedString(std::string& str, const UsageMap& um,unsigned int widthOfOutput,bool showDefaults,const UsageMap& ud)
{

    unsigned int maxNumCharsInOptions = 0;
    ApplicationUsage::UsageMap::const_iterator citr;
    for(citr=um.begin();
        citr!=um.end();
        ++citr)
    {
        maxNumCharsInOptions = maximum(maxNumCharsInOptions,(unsigned int)citr->first.length());
    }


    unsigned int fullWidth = widthOfOutput;
    unsigned int optionPos = 2;
    unsigned int explanationPos = optionPos+maxNumCharsInOptions+2;

    double ratioOfExplanationToOutputWidth = float(explanationPos)/float(widthOfOutput);
    double maxRatioOfExplanationToOutputWidth = 0.25f;

    if (ratioOfExplanationToOutputWidth > maxRatioOfExplanationToOutputWidth)
    {
        explanationPos = static_cast<unsigned int>(maxRatioOfExplanationToOutputWidth*float(widthOfOutput));
    }

    unsigned int defaultPos = 0;
    if (showDefaults)
    {
        defaultPos = explanationPos;
        explanationPos = optionPos+8;
    }
    unsigned int explanationWidth = fullWidth-explanationPos;

    std::string line;
    
    for(citr=um.begin();
        citr!=um.end();
        ++citr)
    {
        line.assign(fullWidth,' ');
        line.replace(optionPos,citr->first.length(),citr->first);
        unsigned int currentEndPos = optionPos + citr->first.length();

        if (showDefaults)
        {

            UsageMap::const_iterator ditr = ud.find(citr->first);
            if (ditr != ud.end())
            {
                if (currentEndPos+1>=defaultPos)
                {
                    str += line; str += "\n";
                    line.assign(fullWidth,' ');
                }

                line.replace(defaultPos, std::string::npos, "");
                if (ditr->second != "")
                {
                    line += "[";
                    line += ditr->second;
                    line += "]";
                }
                str += line;
                str += "\n";
                line.assign(fullWidth,' ');

                currentEndPos =  0;
            }
        }
        
        const std::string& explanation = citr->second;
        std::string::size_type pos = 0;
        std::string::size_type offset = 0;
        bool firstInLine = true;
        if (!explanation.empty())
        {

            if (currentEndPos+1>explanationPos)
            {
                str += line; str += "\n";
                line.assign(fullWidth,' ');
            }

            while (pos<explanation.length())
            {
                if (firstInLine) offset = 0;

                // skip any leading white space.
                while (pos<explanation.length() && explanation[pos]==' ')
                {
                    if (firstInLine) ++offset;
                    ++pos;
                }

                firstInLine = false;

                std::string::size_type width = minimum((std::string::size_type)(explanation.length()-pos),(std::string::size_type)(explanationWidth-offset));
                std::string::size_type slashn_pos = explanation.find('\n',pos);

                unsigned int extraSkip = 0;
                bool concatinated = false;
                if (slashn_pos!=std::string::npos)
                {
                    if (slashn_pos<pos+width)
                    {
                        width = slashn_pos-pos;
                        ++extraSkip;
                        firstInLine = true;
                    }
                    else if (slashn_pos==pos+width) 
                    {
                        ++extraSkip;
                        firstInLine = true;
                    }
                }

                if (pos+width<explanation.length())
                {
                    // now reduce width until we get a space or a return
                    // so that we ensure that whole words are printed.
                    while (width>0 && 
                           explanation[pos+width]!=' ' && 
                           explanation[pos+width]!='\n') --width;

                    if (width==0)
                    {
                        // word must be longer than a whole line so will need
                        // to concatenate it.
                        width = explanationWidth-1;
                        concatinated = true;
                    }
                }

                line.replace(explanationPos+offset,explanationWidth, explanation, pos, width);

                if (concatinated) { str += line; str += "-\n"; }
                else { str += line; str += "\n"; }

                // move to the next line of output.
                line.assign(fullWidth,' ');

                pos += width+extraSkip;

            }
        }
        else
        {
            str += line; str += "\n";
        }
    }
}
void rspfApplicationUsage::getFormatedString(rspfString& str, const UsageMap& um,unsigned int widthOfOutput)
{

    unsigned int maxNumCharsInOptions = 0;
    rspfApplicationUsage::UsageMap::const_iterator citr;
    for(citr=um.begin();
        citr!=um.end();
        ++citr)
    {
        maxNumCharsInOptions = rspf::max(maxNumCharsInOptions,(unsigned int)citr->first.length());
    }
    
    unsigned int fullWidth = widthOfOutput;
    unsigned int optionPos = 2;
    unsigned int explanationPos = 2+maxNumCharsInOptions+2;
    unsigned int explanationWidth = fullWidth-explanationPos;

    rspfString line;
    
    for(citr=um.begin();
        citr!=um.end();
        ++citr)
    {
        line.assign(fullWidth,' ');
        line.replace(optionPos,citr->first.length(),citr->first);
        
        const rspfString& explanation = citr->second;
        std::string::size_type pos = 0;
        std::string::size_type offset = 0;
        bool firstInLine = true;
        while (pos<explanation.length())
        {
            if (firstInLine) offset = 0;
                    
            // skip any leading white space.
            while (pos<explanation.length() && *(explanation.begin()+pos)==' ')
            {
                if (firstInLine) ++offset;
                ++pos;
            }
            
            firstInLine = false;
        
            std::string::size_type width = rspf::min((rspf_int64)(explanation.length()-pos),
                                                      (rspf_int64)(explanationWidth-offset));
            std::string::size_type slashn_pos = explanation.find('\n',pos);
            
            unsigned int extraSkip = 0;
            bool concatinated = false;
            if (slashn_pos!=std::string::npos)
            {
                if (slashn_pos<pos+width)
                {
                    width = slashn_pos-pos;
                    ++extraSkip;
                    firstInLine = true;
                }
                else if (slashn_pos==pos+width) 
                {
                    ++extraSkip;
                    firstInLine = true;
                }
            }
            
            if (pos+width<explanation.length())
            {
                // now reduce width until we get a space or a return
                // so that we ensure that whole words are printed.
                while (width>0 && 
                       *(explanation.begin()+(pos+width))!=' ' && 
                       *(explanation.begin()+(pos+width))!='\n') --width;
                       
                if (width==0)
                {
                    // word must be longer than a whole line so will need
                    // to concatinate it.
                    width = explanationWidth-1;
                    concatinated = true;
                }
            }

            line.replace(explanationPos+offset,explanationWidth, explanation, pos, width);

            if (concatinated) { str += line; str += "-\n"; }
            else { str += line; str += "\n"; }
            
            // move to the next line of output.
            line.assign(fullWidth,' ');
            
            pos += width+extraSkip;

            
        }
                
    }
}