Example usage for com.mongodb CommandResult getDouble

List of usage examples for com.mongodb CommandResult getDouble

Introduction

In this page you can find the example usage for com.mongodb CommandResult getDouble.

Prototype

public double getDouble(final String key) 

Source Link

Document

Returns the value of a field as a double .

Usage

From source file:com.hangum.tadpole.mongodb.core.editors.dbInfos.comosites.CollectionInformationComposite.java

License:Open Source License

/**
 *  ?? ./*from  www .j  a  v a  2 s.c  o  m*/
 */
public void initData(UserDBDAO userDB) {
    if (this.userDB == null)
        this.userDB = userDB;
    collectionList.clear();

    try {
        DB mongoDB = MongoConnectionManager.getInstance(userDB);

        for (String col : mongoDB.getCollectionNames()) {

            CommandResult commandResult = mongoDB.getCollection(col).getStats();
            //logger.debug(commandResult);

            MongoDBCollectionInfoDTO info = new MongoDBCollectionInfoDTO();
            info.setName(col);

            try {
                info.setCount(commandResult.getInt("count")); //$NON-NLS-1$
                info.setSize(commandResult.getInt("size")); //$NON-NLS-1$
                info.setStorage(commandResult.getInt("storageSize")); //$NON-NLS-1$
                info.setIndex(commandResult.getInt("totalIndexSize")); //$NON-NLS-1$
                try {
                    info.setAvgObj(commandResult.getDouble("avgObjSize")); //$NON-NLS-1$               
                } catch (NullPointerException npe) {
                    info.setAvgObj(0); //$NON-NLS-1$
                }
                info.setPadding(commandResult.getInt("paddingFactor")); //$NON-NLS-1$

                try {
                    info.setLastExtentSize(commandResult.getDouble("lastExtentSize")); //$NON-NLS-1$               
                } catch (NullPointerException npe) {
                    info.setLastExtentSize(0); //$NON-NLS-1$
                }

            } catch (Exception e) {
                logger.error("collection info error [" + col + "]", e); //$NON-NLS-1$ //$NON-NLS-2$
            }
            collectionList.add(info);
        }
        treeViewerCollections.setInput(collectionList);

        // summary  .
        double dblSize = 0, dblStorage = 0, dblIndex = 0;
        for (MongoDBCollectionInfoDTO info : collectionList) {
            dblSize += info.getSize();
            dblStorage += info.getStorage();
            dblIndex += info.getIndex();
        }
        lblCollection.setText(collectionList.size() + " Collections"); //$NON-NLS-1$
        lblSizes.setText("Size " + NumberFormatUtils.kbMbFormat(dblSize)); //$NON-NLS-1$
        lblStorages.setText("Storage " + NumberFormatUtils.kbMbFormat(dblStorage)); //$NON-NLS-1$
        lblIndex.setText("Index " + NumberFormatUtils.kbMbFormat(dblIndex)); //$NON-NLS-1$

    } catch (Exception e) {
        logger.error("mongodb collection infomtion init", e); //$NON-NLS-1$

        Status errStatus = new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e); //$NON-NLS-1$
        ExceptionDetailsErrorDialog.openError(null, "Error", "MongoDB Information", errStatus); //$NON-NLS-1$ //$NON-NLS-2$
    }
}

From source file:de.uni_koeln.spinfo.maalr.mongo.core.Database.java

License:Apache License

public DatabaseStatistics getStatistics() {
    DatabaseStatistics stats = new DatabaseStatistics();
    stats.setNumberOfEntries((int) entryCollection.getCount());
    Map<Verification, Integer> count = new HashMap<Verification, Integer>();
    DBCursor cursor = entryCollection.find();
    int entryCount = 0, lemmaCount = 0;
    Verification[] values = Verification.values();
    for (Verification verification : values) {
        count.put(verification, 0);/*from w  ww. ja  v  a  2  s.  co m*/
    }
    count.put(null, 0);
    while (cursor.hasNext()) {
        LexEntry entry = Converter.convertToLexEntry(cursor.next());
        List<LemmaVersion> history = entry.getVersionHistory();
        entryCount++;
        lemmaCount += history.size();
        for (LemmaVersion lemma : history) {
            Integer old = count.get(lemma.getVerification());
            count.put(lemma.getVerification(), old + 1);
        }
    }
    stats.setNumberOfApproved(count.get(Verification.ACCEPTED));
    stats.setNumberOfSuggestions(count.get(Verification.UNVERIFIED));
    stats.setNumberOfDeleted(count.get(Verification.REJECTED));
    stats.setNumberOfOutdated(count.get(Verification.OUTDATED));
    stats.setNumberOfUndefined(count.get(null));
    stats.setNumberOfEntries(entryCount);
    stats.setNumberOfLemmata(lemmaCount);
    /*
     * TODO: - Create a separate "System/Diagnostics"-Tab, containing a)
     * general information about - RAM - Free Disk Space - CPU - Network b)
     * DB-Statistics - general - entries - users
     * 
     * Check if this will work with sigar:
     * 
     * 
     * 
     * - Add green and red 'lamps' to each entry, each sub-statistic
     * (entries, users, general) - Add one lamp to the navigation bar, to
     * summarize the server state: should always be green.
     */

    CommandResult dbStats = entryCollection.getDB().getStats();

    NumberFormat nf = NumberFormat.getInstance();
    stats.addDBProperty("Server", dbStats.getString("serverUsed"));

    Double serverStatus = dbStats.getDouble("ok");
    if (Double.compare(1D, serverStatus) == 0) {
        stats.addDBProperty("Server Status", "ok");
    } else {
        stats.addDBProperty("Server Status", "not ok: " + serverStatus, Category.ERROR);
    }

    long collections = dbStats.getLong("collections");
    if (collections == 0) {
        stats.addDBProperty("Number of Collections", nf.format(dbStats.getLong("collections")), Category.ERROR);
    } else {
        stats.addDBProperty("Number of Collections", nf.format(dbStats.getLong("collections")));
    }
    stats.addDBProperty("Number of Indexes", nf.format(dbStats.getLong("indexes")));
    stats.addDBProperty("Average Object Size", nf.format(dbStats.getDouble("avgObjSize") / 1024) + " KB");
    stats.addDBProperty("Data Size", nf.format(dbStats.getLong("dataSize") / (1024 * 1024)) + " MB");
    stats.addDBProperty("Storage Size", nf.format(dbStats.getLong("storageSize") / (1024 * 1024)) + " MB");
    stats.addDBProperty("Index Size", nf.format(dbStats.getLong("indexSize") / (1024 * 1024)) + " MB");
    stats.addDBProperty("File Size", nf.format(dbStats.getLong("fileSize") / (1024 * 1024)) + " MB");

    BasicDBObject query;
    BasicDBList attributes;
    query = new BasicDBObject();
    attributes = new BasicDBList();
    DBObject lemmata = new BasicDBObject();
    lemmata.put(QUERY_VERSION_TIMESTAMP, new BasicDBObject("$gt", -1));
    attributes.add(lemmata);
    query.append("$and", attributes);
    // stats.setNumberOfLemmata((int) entryCollection.count(query));

    return stats;
}

From source file:org.graylog2.system.stats.mongo.MongoProbe.java

License:Open Source License

public MongoStats mongoStats() {
    final List<ServerAddress> serverAddresses = mongoClient.getServerAddressList();
    final List<HostAndPort> servers = Lists.newArrayListWithCapacity(serverAddresses.size());
    for (ServerAddress serverAddress : serverAddresses) {
        servers.add(HostAndPort.fromParts(serverAddress.getHost(), serverAddress.getPort()));
    }/*from   w  ww.j av a2  s  . co m*/

    final DatabaseStats dbStats;
    final CommandResult dbStatsResult = db.command("dbStats");
    if (dbStatsResult.ok()) {
        final BasicDBObject extentFreeListMap = (BasicDBObject) dbStatsResult.get("extentFreeList");
        final DatabaseStats.ExtentFreeList extentFreeList = DatabaseStats.ExtentFreeList
                .create(extentFreeListMap.getInt("num"), extentFreeListMap.getInt("totalSize"));

        final BasicDBObject dataFileVersionMap = (BasicDBObject) dbStatsResult.get("dataFileVersion");
        final DatabaseStats.DataFileVersion dataFileVersion = DatabaseStats.DataFileVersion
                .create(dataFileVersionMap.getInt("major"), dataFileVersionMap.getInt("minor"));

        dbStats = DatabaseStats.create(dbStatsResult.getString("db"), dbStatsResult.getLong("collections"),
                dbStatsResult.getLong("objects"), dbStatsResult.getDouble("avgObjSize"),
                dbStatsResult.getLong("dataSize"), dbStatsResult.getLong("storageSize"),
                dbStatsResult.getLong("numExtents"), dbStatsResult.getLong("indexes"),
                dbStatsResult.getLong("indexSize"), dbStatsResult.getLong("fileSize"),
                dbStatsResult.getLong("nsSizeMB"), extentFreeList, dataFileVersion);
    } else {
        dbStats = null;
    }

    final ServerStatus serverStatus;
    final CommandResult serverStatusResult = adminDb.command("serverStatus");
    if (serverStatusResult.ok()) {
        final BasicDBObject connectionsMap = (BasicDBObject) serverStatusResult.get("connections");
        final ServerStatus.Connections connections = ServerStatus.Connections.create(
                connectionsMap.getInt("current"), connectionsMap.getInt("available"),
                connectionsMap.getLong("totalCreated"));

        final BasicDBObject networkMap = (BasicDBObject) serverStatusResult.get("network");
        final ServerStatus.Network network = ServerStatus.Network.create(networkMap.getInt("bytesIn"),
                networkMap.getInt("bytesOut"), networkMap.getInt("numRequests"));

        final BasicDBObject memoryMap = (BasicDBObject) serverStatusResult.get("mem");
        final ServerStatus.Memory memory = ServerStatus.Memory.create(memoryMap.getInt("bits"),
                memoryMap.getInt("resident"), memoryMap.getInt("virtual"), memoryMap.getBoolean("supported"),
                memoryMap.getInt("mapped"), memoryMap.getInt("mappedWithJournal"));

        serverStatus = ServerStatus.create(serverStatusResult.getString("host"),
                serverStatusResult.getString("version"), serverStatusResult.getString("process"),
                serverStatusResult.getLong("pid"), serverStatusResult.getInt("uptime"),
                serverStatusResult.getLong("uptimeMillis"), serverStatusResult.getInt("uptimeEstimate"),
                new DateTime(serverStatusResult.getDate("localTime")), connections, network, memory);
    } else {
        serverStatus = null;
    }

    // TODO Collection stats? http://docs.mongodb.org/manual/reference/command/collStats/

    return MongoStats.create(servers, buildInfo, hostInfo, serverStatus, dbStats);
}