Example usage for com.mongodb BasicDBObject getInt

List of usage examples for com.mongodb BasicDBObject getInt

Introduction

In this page you can find the example usage for com.mongodb BasicDBObject getInt.

Prototype

public int getInt(final String key) 

Source Link

Document

Returns the value of a field as an int .

Usage

From source file:com.streamreduce.core.transformer.message.AgentMessageTransformer.java

License:Apache License

/**
 * {@inheritDoc}/*w  w  w  .  j av a2 s .c om*/
 */
@Override
public String doTransform(Event event) {
    EventId eventId = event.getEventId();
    Map<String, Object> eventMetadata = event.getMetadata();
    String msg = "";

    switch (eventId) {
    case ACTIVITY:
        BasicDBObject payload = (BasicDBObject) eventMetadata.get("payload");
        StringBuilder sb = new StringBuilder();

        sb.append("Current system overview at ").append(eventMetadata.get("activityGenerated")) // Should we format this?
                .append("\n\n");

        sb.append("Uptime: ").append(payload.getString("uptime")).append("s\n").append("Disk usage:\n");

        BasicDBObject partitionsObj = (BasicDBObject) payload.get("partitions");
        Set<String> partitions = new TreeSet<>(partitionsObj.keySet());

        for (String key : partitions) {
            BasicDBObject partition = (BasicDBObject) partitionsObj.get(key);
            double totalAsKb = partition.getDouble("total");

            // Certain devices show as 0.00GB and should be pruned
            if (totalAsKb == 0) {
                continue;
            }

            double totalAsGB = MessageUtils.kbToGB(totalAsKb);
            double usedAsGB = MessageUtils.kbToGB(partition.getDouble("used"));
            double freeAsGB = MessageUtils.kbToGB(partition.getDouble("free"));

            sb.append("  ").append(key).append(": Total ").append(MessageUtils.roundAndTruncate(totalAsGB, 2))
                    .append("GB, Used ").append(MessageUtils.roundAndTruncate(usedAsGB, 2)).append("GB, Free ")
                    .append(MessageUtils.roundAndTruncate(freeAsGB, 2)).append("GB\n");
        }

        sb.append("Disk I/O:\n");

        BasicDBObject diskIO = (BasicDBObject) payload.get("disk_io");
        Set<String> disks = new TreeSet<>(diskIO.keySet());

        if (disks.size() == 0) {
            sb.append("  Unavailable\n");
        } else {
            for (String key : disks) {
                BasicDBObject disk = (BasicDBObject) diskIO.get(key);
                long reads = disk.getLong("read_count");
                long writes = disk.getLong("write_count");
                double gbRead = MessageUtils.kbToGB(disk.getLong("read_kbytes"));
                double gbWrite = MessageUtils.kbToGB(disk.getLong("write_kbytes"));
                long readSecs = disk.getLong("read_time");
                long writeSecs = disk.getLong("write_time");

                sb.append("  ").append(key).append(": Reads ").append(reads).append(", Writes ").append(writes)
                        .append(", GB Read ").append(MessageUtils.roundAndTruncate(gbRead, 2))
                        .append(", GB Written ").append(MessageUtils.roundAndTruncate(gbWrite, 2))
                        .append(", Read Time ").append(readSecs).append("s, Write Time ").append(writeSecs)
                        .append("s\n");
            }
        }

        sb.append("Network I/O:\n");

        BasicDBObject netIO = (BasicDBObject) payload.get("network_io");
        Set<String> nics = new TreeSet<>(netIO.keySet());
        int nicsDisplayed = 0;

        for (String key : nics) {
            BasicDBObject nic = (BasicDBObject) netIO.get(key);
            long packetsIn = nic.getInt("packets_in");
            long packetsOut = nic.getInt("packets_out");

            // Certain devices show 0 packets in/out and should be pruned
            if (packetsIn == 0 && packetsOut == 0) {
                continue;
            }

            double gbIn = MessageUtils.kbToGB(nic.getLong("kbytes_in"));
            double gbOut = MessageUtils.kbToGB(nic.getLong("kbytes_out"));

            sb.append("  ").append(key).append(": Packets In ").append(packetsIn).append(", Packets Out ")
                    .append(packetsOut).append(", GB In ").append(MessageUtils.roundAndTruncate(gbIn, 2))
                    .append(", GB Out ").append(MessageUtils.roundAndTruncate(gbOut, 2)).append("\n");

            nicsDisplayed++;
        }

        if (nicsDisplayed == 0) {
            sb.append("  Unavailable\n");
        }

        sb.append("Load: 1m ").append(MessageUtils.roundAndTruncate(payload.getDouble("load_avg_0"), 2))
                .append(", ").append("5m ")
                .append(MessageUtils.roundAndTruncate(payload.getDouble("load_avg_1"), 2)).append(", ")
                .append("15m ").append(MessageUtils.roundAndTruncate(payload.getDouble("load_avg_2"), 2))
                .append("\n");

        float gbTotalRAM = (float) MessageUtils.kbToGB(payload.getLong("phy_ram_total"));
        float gbUsedRAM = (float) MessageUtils.kbToGB(payload.getLong("phy_ram_used"));
        float gbFreeRAM = (float) MessageUtils.kbToGB(payload.getLong("phy_ram_free"));

        sb.append("Real Mem: Total ").append(MessageUtils.roundAndTruncate(gbTotalRAM, 2)).append("GB, Used ")
                .append(MessageUtils.roundAndTruncate(gbUsedRAM, 2)).append("GB, Free ")
                .append(MessageUtils.roundAndTruncate(gbFreeRAM, 2)).append("GB\n");

        double gbTotalVRAM = MessageUtils.kbToGB(payload.getLong("vir_ram_total"));
        double gbUsedVRAM = MessageUtils.kbToGB(payload.getLong("vir_ram_used"));
        double gbFreeVRAM = MessageUtils.kbToGB(payload.getLong("vir_ram_free"));

        sb.append("Virt Mem: Total ").append(MessageUtils.roundAndTruncate(gbTotalVRAM, 2)).append("GB, Used ")
                .append(MessageUtils.roundAndTruncate(gbUsedVRAM, 2)).append("GB, Free ")
                .append(MessageUtils.roundAndTruncate(gbFreeVRAM, 2)).append("GB\n");

        sb.append("Processes: ").append(payload.getInt("processes")).append("\n");

        sb.append("Users: Total ").append(payload.getInt("users_total")).append(", Unique ")
                .append(payload.getInt("users_unique")).append("\n");

        msg = sb.toString();
        break;
    default:
        super.doTransform(event);
        break;
    }
    return msg;
}

From source file:com.tml.pathummoto.Dao.BillDao.java

public ArrayList<Bill> searchBill(String no) {

    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("Bill");
    System.out.println("Collection user selected successfully");

    BasicDBObject whereQuery = new BasicDBObject();
    whereQuery.put("vehicleNo", no);
    DBCursor cursor = coll.find(whereQuery);

    ArrayList<Bill> bills = new ArrayList<Bill>();

    while (cursor.hasNext()) {
        BasicDBObject doc = (BasicDBObject) cursor.next();

        if (doc != null) {
            Bill bill = new Bill();
            bill.setDate(doc.getDate("Date"));
            bill.setKm(doc.getInt("km"));
            bill.setPayment(doc.getInt("payment"));
            bill.setService(doc.getInt("service"));
            bill.setTotalCost(doc.getInt("total"));
            String array = "";
            int partSize = doc.getInt("partsSize");
            for (int i = 0; i < partSize; i++) {
                String parts = "part" + i;
                String quentity = "quant" + i;
                array = array + doc.getString(parts) + " : " + doc.getInt(quentity) + "\n";

            }//from  ww  w  .j  av  a2s.  c  o m
            bill.setParts(array);
            bills.add(bill);
        }
    }
    return bills;
}

From source file:com.tml.pathummoto.Dao.CustomDao.java

public Customer searchCustomer(String no) {
    Customer customer = new Customer();
    // To connect to mongodb server
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("customer");
    System.out.println("Collection user selected successfully");

    BasicDBObject whereQuery = new BasicDBObject();
    whereQuery.put("_id", no);
    DBCursor cursor = coll.find(whereQuery);
    while (cursor.hasNext()) {
        System.out.println(cursor.next());
    }/*w  w  w .j  a  v  a  2  s .c  o  m*/
    BasicDBObject doc = (BasicDBObject) cursor.curr();
    System.out.println("doc" + doc);
    if (doc != null) {
        customer.setVehicleNo(doc.getString("_id"));
        customer.setName(doc.getString("name"));
        customer.setPayment(doc.getInt("payment"));
        customer.setFreeServiceNo(doc.getInt("freeServiceNo"));
        customer.setServiceNo(doc.getInt("serviceNo"));
        customer.setDateOfDelivery(doc.getDate("dateOfDelivery"));
        customer.setLastKm(doc.getInt("lastKm"));

    }

    return customer;
}

From source file:com.tml.pathummoto.Dao.PartDao.java

public ArrayList searchpart3(String modelName, String PartType) {
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("part");
    System.out.println("Collection user selected successfully");
    System.out.println(modelName);
    System.out.println(PartType);
    BasicDBObject whereQuery = new BasicDBObject();
    whereQuery.put("Model Name", modelName);
    whereQuery.put("Part Type", PartType);
    DBCursor cursor = coll.find(whereQuery);
    ArrayList<Part> arr = new ArrayList<Part>();
    while (cursor.hasNext()) {
        BasicDBObject obj = (BasicDBObject) cursor.next();
        Part part = new Part(obj.getString("Part Name"), obj.getString("_id"), obj.getString("Part Type"),
                obj.getInt("quant"));
        // System.out.println(obj.getString("Image No").toString());
        arr.add(part);/*from   w w  w.j  a  v a  2  s  .  c o  m*/

    }
    return (arr);

}

From source file:com.tml.pathummoto.Dao.PartDao.java

public ArrayList searchpart4(String modelName, String PartType) {
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    //         // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("part");
    System.out.println("Collection user selected successfully");
    System.out.println(modelName);
    System.out.println(PartType);
    BasicDBObject whereQuery = new BasicDBObject();
    whereQuery.put("Model Name", modelName);
    whereQuery.put("Part Type", PartType);
    DBCursor cursor = coll.find(whereQuery);
    ArrayList<Part> arr = new ArrayList<Part>();
    while (cursor.hasNext()) {
        BasicDBObject obj = (BasicDBObject) cursor.next();
        Part part;/*www .  j a  v a2 s.  co  m*/
        part = new Part(obj.getString("Quantity"), obj.getString("_id"), obj.getInt("quant"));

        arr.add(part);

    }
    return (arr);

}

From source file:controllers.DadosBean.java

public void guardarDadosPC() {

    contratacao.setPid(Integer.parseInt(mySessionBean.getPid()));
    String muser = mySessionBean.getUser();

    //verificar se o registo j existe
    BasicDBObject res = (BasicDBObject) conn.getDBCollection()
            .findOne(new BasicDBObject("pid", contratacao.getPid()));
    BasicDBObject o = CPParsers.contratacaoToJson(contratacao);

    if (res == null) {
        //faz insert
        String user = mySessionBean.getUser();
        o.put("user", muser);
        o.put("updated", new Date());
        conn.getDBCollection().insert(o);
    } else {/*from   ww  w . j a  v  a 2 s .com*/
        //faz backup
        conn.setDBCollection("collProcContratacao_Backup");
        res.put("_idOld", res.getObjectId("_id"));
        res.removeField("_id");
        conn.getDBCollection().insert(res);
        //faz update
        conn.setDBCollection("collProcContratacao");

        o.put("user", muser);
        o.put("updated", new Date());
        o.removeField("_id");
        conn.getDBCollection().update(new BasicDBObject("pid", res.getInt("pid")), o);
    }

    APPEUtils.printMessage("Dados guardados com sucesso!");

}

From source file:controllers.FilterController.java

License:Apache License

public static Statistics getStatisticsFromResult(BasicDBObject aggregation) {
    if (aggregation == null)
        return null;

    final DecimalFormat df = new DecimalFormat("#.##");
    Statistics stats = new Statistics();
    stats.setCount(aggregation.getInt("count") + " objects");
    stats.setSize(df.format(aggregation.getLong("sum") / 1024D / 1024) + " MB");
    stats.setAvg(df.format(aggregation.getDouble("avg") / 1024 / 1024) + " MB");
    stats.setMin(aggregation.getLong("min") + " B");
    stats.setMax(df.format(aggregation.getLong("max") / 1024D / 1024) + " MB");
    stats.setSd(df.format(aggregation.getDouble("stddev") / 1024 / 1024) + " MB");
    stats.setVar(df.format(aggregation.getDouble("variance") / 1024 / 1024 / 1024 / 1024) + " MB");
    // because of sd^2
    return stats;
}

From source file:DataAccess.Entity.Restaurant.java

public Restaurant(BasicDBObject dBObjectRs) {
    this.name = dBObjectRs.getString("name");
    this.address = dBObjectRs.getString("direccion");
    this.phone = dBObjectRs.getInt("phone");
}

From source file:entity.Annotation.java

public Annotation(BasicDBObject dbo) {
    this.objectID = dbo.getObjectId("_id").toString();
    this.namespace = dbo.getString("namespace");
    this.content = dbo.getString("content");
    this.selector = dbo.getString("selector");
    this.title = dbo.getString("title");
    this.resource = dbo.getString("resource");
    this.resourceType = dbo.getString("resourceType");
    this.outterRelative = dbo.getString("outterRelative");
    this.addedTime = dbo.getLong("addedTime");
    this.fontColor = dbo.getString("fontColor");
    this.fontType = dbo.getString("fontType");
    this.permission = dbo.getInt("permission");
    this.originalAnnoID = dbo.getString("originalAnnoID");
    this.versionNum = dbo.getInt("versionNum");
    this.forkFromID = dbo.getString("forkFromID");
}

From source file:example.springdata.mongodb.util.MongosSystemForTestFactory.java

License:Apache License

private boolean isReplicaSetStarted(BasicDBObject setting) {
    if (setting.get("members") == null) {
        return false;
    }/*from  w  ww  .  j  a va 2 s . c  o  m*/

    BasicDBList members = (BasicDBList) setting.get("members");
    for (Object m : members.toArray()) {
        BasicDBObject member = (BasicDBObject) m;
        logger.info(member.toString());
        int state = member.getInt("state");
        logger.info("state: {}", state);
        // 1 - PRIMARY, 2 - SECONDARY, 7 - ARBITER
        if (state != 1 && state != 2 && state != 7) {
            return false;
        }
    }
    return true;
}