void _append( BSONObjBuilder& b , const string& fieldName , const string& data ) { if ( _ignoreBlanks && data.size() == 0 ) return; if ( b.appendAsNumber( fieldName , data ) ) return; // TODO: other types? b.append ( fieldName , data ); }
INT32 _qgmPlInsert::_mergeObj( BSONObj &obj ) const { PD_TRACE_ENTRY( SDB__QGMPLINSERT__MERGEOBJ ) ; INT32 rc = SDB_OK ; BSONObjBuilder builder ; if ( _columns.size() != _values.size() ) { PD_LOG(PDERROR, "column's size does not suit value's size"); rc = SDB_INVALIDARG ; goto error ; } try { qgmOPFieldVec::const_iterator itr1 = _columns.begin() ; qgmOPFieldVec::const_iterator itr2 = _values.begin() ; for ( ; itr1 != _columns.end(); itr1++, itr2++ ) { if ( SQL_GRAMMAR::DIGITAL == itr2->type ) { builder.appendAsNumber( itr1->value.toString(), itr2->value.toString() ) ; } else if ( SQL_GRAMMAR::DATE == itr2->type ) { Date_t t ; UINT64 millis = 0 ; rc = utilStr2Date( itr2->value.toString().c_str(), millis ) ; if ( SDB_OK != rc ) { PD_LOG( PDDEBUG, "failed to parse to Date_t:%s", itr2->value.toString().c_str() ) ; rc = SDB_INVALIDARG ; goto error ; } t.millis = millis ; builder.appendDate( itr1->value.toString(), t ) ; } else { builder.append( itr1->value.toString(), itr2->value.toString()) ; } } obj = builder.obj() ; } catch ( std::exception &e ) { PD_LOG( PDERROR, "unexcepted err happened: %s", e.what() ) ; rc = SDB_SYS ; goto error ; } done: PD_TRACE_EXITRC( SDB__QGMPLINSERT__MERGEOBJ, rc ) ; return rc ; error: goto done ; }
// liberally cribbed from user_prio.cpp void plumage::stats::processAccountantStats(ClassAd* ad, ODSMongodbOps* ops, Date_t& ts) { // attr%d holders...sadly reverting back to MyString for convenience of formatstr MyString attrName, attrPrio, attrResUsed, attrWtResUsed, attrFactor, attrBeginUsage, attrAccUsage; MyString attrLastUsage, attrAcctGroup, attrIsAcctGroup; MyString attrConfigQuota, attrEffectiveQuota, attrSubtreeQuota, attrSurplusPolicy; // values string name, acctGroup, surplusPolicy; float priority, factor, wtResUsed, configQuota, effectiveQuota, subtreeQuota, accUsage = -1; int resUsed, beginUsage, lastUsage; resUsed = beginUsage = lastUsage = 0; bool isAcctGroup; DBClientConnection* conn = ops->m_db_conn; conn->ensureIndex(DB_STATS_SAMPLES_ACCOUNTANT, BSON( "ts" << -1 )); conn->ensureIndex(DB_STATS_SAMPLES_ACCOUNTANT, BSON( "lu" << -1 )); conn->ensureIndex(DB_STATS_SAMPLES_ACCOUNTANT, BSON( "n" << 1 )); unsigned long long acct_count = conn->count(DB_STATS_SAMPLES_ACCOUNTANT); // eventhough the Accountant doesn't forget // we don't care about stale submitters (default: last 24 hours) int cfg_last_usage = param_integer("ODS_ACCOUNTANT_LAST_USAGE", 60*60*24); int minLastUsageTime = time(0)-cfg_last_usage; int numElem = -1; ad->LookupInteger( "NumSubmittors", numElem ); for( int i=1; i<=numElem; i++) { priority=0; isAcctGroup = false; // skip stale records unless we have none attrLastUsage.formatstr("LastUsageTime%d", i ); ad->LookupInteger ( attrLastUsage.Value(), lastUsage ); if (lastUsage < minLastUsageTime && acct_count > 0) continue; // parse the horrid classad attrName.formatstr("Name%d", i ); attrPrio.formatstr("Priority%d", i ); attrResUsed.formatstr("ResourcesUsed%d", i ); attrWtResUsed.formatstr("WeightedResourcesUsed%d", i ); attrFactor.formatstr("PriorityFactor%d", i ); attrBeginUsage.formatstr("BeginUsageTime%d", i ); attrAccUsage.formatstr("WeightedAccumulatedUsage%d", i ); attrAcctGroup.formatstr("AccountingGroup%d", i); attrIsAcctGroup.formatstr("IsAccountingGroup%d", i); attrConfigQuota.formatstr("ConfigQuota%d", i); attrEffectiveQuota.formatstr("EffectiveQuota%d", i); attrSubtreeQuota.formatstr("SubtreeQuota%d", i); attrSurplusPolicy.formatstr("SurplusPolicy%d", i); ad->LookupString ( attrName.Value(), name ); ad->LookupFloat ( attrPrio.Value(), priority ); ad->LookupFloat ( attrFactor.Value(), factor ); ad->LookupFloat ( attrAccUsage.Value(), accUsage ); ad->LookupInteger ( attrBeginUsage.Value(), beginUsage ); ad->LookupInteger ( attrResUsed.Value(), resUsed ); ad->LookupBool ( attrIsAcctGroup.Value(), isAcctGroup); ad->LookupFloat ( attrConfigQuota.Value(), configQuota ); ad->LookupFloat ( attrEffectiveQuota.Value(), effectiveQuota ); ad->LookupFloat ( attrSubtreeQuota.Value(), subtreeQuota ); ad->LookupString ( attrSurplusPolicy.Value(), surplusPolicy ); if( !ad->LookupFloat( attrWtResUsed.Value(), wtResUsed ) ) { wtResUsed = resUsed; } if (!ad->LookupString(attrAcctGroup.Value(), acctGroup)) { acctGroup = "<none>"; } BSONObjBuilder bob; bob.appendDate("ts",ts); bob.append("n",name); bob.append("ag",acctGroup); bob.appendAsNumber("prio",formatReal(priority)); bob.appendAsNumber("fac",formatReal(factor)); bob.append("ru",resUsed); bob.append("wru",wtResUsed); // condor timestamps need massaging when going in the db bob.appendDate("bu",static_cast<unsigned long long>(beginUsage)*1000); bob.appendDate("lu",static_cast<unsigned long long>(lastUsage)*1000); bob.appendAsNumber("au",formatReal(accUsage)); bob.appendAsNumber("cq",formatReal(configQuota)); bob.appendAsNumber("eq",formatReal(effectiveQuota)); bob.appendAsNumber("sq",formatReal(subtreeQuota)); if (!surplusPolicy.empty()) bob.append("sp",surplusPolicy); conn->insert(DB_STATS_SAMPLES_ACCOUNTANT,bob.obj()); } }