Esempio n. 1
0
  public static List<String> queryMongoForSimilar(String courtId, String facets) throws Exception {
    // Query mongo for other facets with these values.
    System.out.println("Court id is: " + courtId);
    initializeMap();
    List<String> facetList = facetStringToListOfKeys(facets);
    // Facet list has the keys to get. Query mongo for the document with given courtId, then pull
    // the values of these keys.

    MongoConnection mc = new MongoConnection("supremeCourtDataDB.properties");
    MongoCollection<Document> collection = mc.getCollection();

    int courtIdInt = Integer.parseInt(courtId);
    Document d = collection.find(eq("courtId", courtIdInt)).first();
    System.out.println("Doc with this court id: " + d);

    Document queryDoc = new Document();
    // Build query based on given keys and their values in this doc.
    for (int i = 0; i < facetList.size(); i++) {
      String key = facetList.get(i);
      String tempString;
      int tempInt;
      try {
        tempString = d.getString(key);
        // queryJSON.put(key, tempString);
        queryDoc.put(key, tempString);
      } catch (ClassCastException c) {
        tempInt = d.getInteger(key);
        queryDoc.put(key, tempInt);
        // queryJSON.put(key, tempInt);
      }
    }

    List<String> resultList = new ArrayList<String>();

    System.out.println("Querying mongo.");
    FindIterable<Document> result = collection.find(queryDoc);

    for (Document doc : result) {
      String temp = Integer.toString(doc.getInteger("courtId"));
      resultList.add(temp);
    }

    System.out.println("Done!");
    return resultList;
  }
Esempio n. 2
0
  public Map<String, Booking> getAllBookings() {
    final Map<String, Booking> bookings = new LinkedHashMap<String, Booking>();
    try {
      final MongoDatabase mdb = MongoDBConnManager.getInstance().getConnection();
      final MongoCollection<Document> coll = mdb.getCollection(DBConstants.COLL_BOOKING);
      List<Document> aggregates = new ArrayList<Document>();

      Document join = new Document();
      Document sortCr = new Document();

      /*Document lookup = new Document();
      lookup.put("from", DBConstants.COLL_BOXES);
      lookup.put("localField", DBConstants.BOX_NAME);
      lookup.put("foreignField", DBConstants.BOX_NAME);
      lookup.put("as", "join_for_teamname");*/

      Document lookup2 = new Document();
      lookup2.put("from", DBConstants.COLL_USERS);
      lookup2.put("localField", DBConstants.EMAIL);
      lookup2.put("foreignField", DBConstants.EMAIL);
      lookup2.put("as", "join_for_username");

      // join.put("$lookup", lookup);
      join.put("$lookup", lookup2);
      sortCr.put("$sort", new Document(DBConstants.TEAM_NAME, 1));

      aggregates.add(join);
      aggregates.add(sortCr);

      final ArrayList<Document> bookingsFromDB =
          coll.aggregate(aggregates).into(new ArrayList<Document>());

      for (final Document document : bookingsFromDB) {
        final User user = new User();
        user.setUserName(
            ((Document) document.get("join_for_username", List.class).get(0))
                .getString(DBConstants.USER_NAME));
        user.setEmail(document.getString(DBConstants.EMAIL));
        user.setTeamName(
            ((Document) document.get("join_for_username", List.class).get(0))
                .getString(DBConstants.TEAM_NAME));
        user.setBookedDateNTime(document.getDate(DBConstants.BOOKED_DATE_N_TIME));
        user.setDateNTime(document.getDate(DBConstants.DATE_N_TIME));
        user.setEstimatedUsage(document.getInteger(DBConstants.ESTIMATED_USAGE));
        user.setBookingId(document.getString(DBConstants.BOOKING_ID));
        if (!bookings.containsKey(document.getString(DBConstants.BOX_NAME))) {
          Booking bkng = new Booking();
          bkng.setBoxName(document.getString(DBConstants.BOX_NAME));
          bkng.setBoxOwner(getBoxOwner(document.getString(DBConstants.BOX_NAME)));
          bkng.addUserToQueue(user);
          bookings.put(document.getString(DBConstants.BOX_NAME), bkng);
        } else {
          Booking bkng = bookings.get(document.getString(DBConstants.BOX_NAME));
          bkng.addUserToQueue(user);
        }
      }
      // sort users based on timestamps
      for (Booking bkng : bookings.values()) {
        Collections.sort(bkng.getUsersInQueue());
      }
    } catch (Exception e) {
      if (e instanceof com.mongodb.MongoTimeoutException) {
        throw new ApplicationException(MessagesEnum.MONGODB_IS_DOWN.getMessage(), e);
      }
      throw new ApplicationException(MessagesEnum.BOOKINGS_RETRIVAL_FAILED.getMessage(), e);
    }
    return bookings;
  }
  /**
   * 缓存每条汇总结果
   *
   * @param cacheMap
   * @param tuple
   */
  private void doCacheEventDetailInfo(
      Map<String, Document> cacheMap, Tuple2<String, Integer> tuple) {
    logger.debug("come into doCacheEventDetailInfo ==> " + tuple._1() + " <--> " + tuple._2());
    // 解析表名:字段名:timeValue:appId:appVersion:appChannel:appPlatform:eventId:paramKey:paramValue(tenantId)
    String keys[] = tuple._1().split(":");
    String tableName = keys[0];
    String fieldName = keys[1];
    String timeValue = keys[2];
    String appId = keys[3];
    String appVersion = keys[4];
    String appChannel = keys[5];
    String appPlatform = keys[6];
    String eventId = keys[7];
    String paramKey = keys[8];
    String paramValue = keys[9];
    String tenantId =
        (keys.length == 11 && keys[10] != null && !keys[10].trim().isEmpty()) ? (keys[10]) : "";
    String tenantIdKey =
        (keys.length == 11 && keys[10] != null && !keys[10].trim().isEmpty())
            ? (":" + keys[10])
            : ""; // 存在tenantId就加到keyStr中去

    Document eventDetailInfoDoc = null;

    String keyStr =
        tableName
            + ":"
            + timeValue
            + ":"
            + appId
            + ":"
            + appVersion
            + ":"
            + appChannel
            + ":"
            + appPlatform
            + ":"
            + eventId
            + ":"
            + paramKey
            + ":"
            + paramValue
            + tenantIdKey;
    // 如果缓存命中,使用缓存的对象
    if (cacheMap.containsKey(keyStr)) {
      eventDetailInfoDoc = cacheMap.get(keyStr);
    } else {
      eventDetailInfoDoc =
          eventDetailInfoDao.findOneBy(
              tableName,
              timeValue,
              appId,
              appVersion,
              appChannel,
              appPlatform,
              eventId,
              paramKey,
              paramValue,
              tenantId);
    }

    if (eventDetailInfoDoc == null) {
      EventDetailInfo eventDetailInfoTemp = new EventDetailInfo();
      eventDetailInfoTemp.setEventId(eventId);
      eventDetailInfoTemp.setTimeValue(timeValue);
      eventDetailInfoTemp.setAppId(appId);
      if (tenantId != null && !tenantId.trim().isEmpty()) {
        eventDetailInfoTemp.setTenantId(tenantId);
      }
      Gson gson = new Gson();
      eventDetailInfoDoc = Document.parse(gson.toJson(eventDetailInfoTemp));
      ObjectId objId = new ObjectId();
      eventDetailInfoDoc.put("_id", objId);
      eventDetailInfoDoc.put("appVersion", appVersion);
      eventDetailInfoDoc.put("appChannel", appChannel);
      eventDetailInfoDoc.put("appPlatform", appPlatform);
      eventDetailInfoDoc.put("paramKey", paramKey);
      eventDetailInfoDoc.put("paramValue", paramValue);
    }
    if (eventDetailInfoDoc.get(fieldName) == null) {
      eventDetailInfoDoc.put(fieldName, (long) tuple._2());
    } else {
      long fieldValue = 0;
      try {
        fieldValue = eventDetailInfoDoc.getLong(fieldName);
      } catch (ClassCastException e) {
        fieldValue = (long) eventDetailInfoDoc.getInteger(fieldName);
      }
      eventDetailInfoDoc.put(fieldName, (long) (fieldValue + tuple._2()));
    }
    cacheMap.put(keyStr, eventDetailInfoDoc);
    return;
  }
Esempio n. 4
0
  /**
   * 缓存每条汇总结果
   *
   * @param cacheMap
   * @param tuple
   */
  private void doCacheEventDetailInfo(
      Map<String, Document> cacheMap, Tuple2<String, Integer> tuple) {
    logger.debug("come into doCacheEventDetailInfo ==> " + tuple._1() + " <--> " + tuple._2());
    // 解析表名:字段名1、字段名2:字段名1值:timeValue:appId:appVersion(tenantId)
    String keys[] = tuple._1().split(":");
    String tableName = keys[0];
    String fieldName1 = keys[1];
    String fieldName2 = keys[2];
    String fieldName1Value = keys[3];
    String timeValue = keys[4];
    String appId = keys[5];
    String appVersion = keys[6];
    String tenantId =
        (keys.length == 8 && keys[7] != null && !keys[7].trim().isEmpty()) ? (keys[7]) : "";
    String tenantIdKey =
        (keys.length == 8 && keys[7] != null && !keys[7].trim().isEmpty())
            ? (":" + keys[7])
            : ""; // 存在tenantId就加到keyStr中去

    Document versionDetailDoc = null;

    String newUserFromChannal = null;
    String updateUserFromChannal = null;
    String updateUserFromVersion = null;
    if ("newUserFromChannal".equals(fieldName1)) {
      newUserFromChannal = fieldName1Value;
    }
    if ("updateUserFromChannal".equals(fieldName1)) {
      updateUserFromChannal = fieldName1Value;
    }
    if ("updateUserFromVersion".equals(fieldName1)) {
      updateUserFromVersion = fieldName1Value;
    }
    String keyStr =
        tableName
            + ":"
            + timeValue
            + ":"
            + appId
            + ":"
            + appVersion
            + ":"
            + fieldName1
            + ":"
            + fieldName1Value
            + tenantIdKey;
    // 如果缓存命中,使用缓存的对象
    if (cacheMap.containsKey(keyStr)) {
      versionDetailDoc = cacheMap.get(keyStr);
    } else {
      versionDetailDoc =
          versionDetailDao.findOneBy(
              tableName,
              timeValue,
              appId,
              appVersion,
              newUserFromChannal,
              updateUserFromChannal,
              updateUserFromVersion,
              tenantId);
    }

    if (versionDetailDoc == null) {
      VersionDetail versionDetail = new VersionDetail();
      versionDetail.setAppId(appId);
      versionDetail.setAppVersion(appVersion);
      versionDetail.setTimeValue(timeValue);
      if (tenantId != null && !tenantId.trim().isEmpty()) {
        versionDetail.setTenantId(tenantId);
      }
      Gson gson = new Gson();
      versionDetailDoc = Document.parse(gson.toJson(versionDetail));
      ObjectId objId = new ObjectId();
      versionDetailDoc.put("_id", objId);
      versionDetailDoc.put(fieldName1, fieldName1Value);
    }
    if (versionDetailDoc.get(fieldName2) == null) {
      versionDetailDoc.put(fieldName2, tuple._2());
    } else {
      long fieldValue = 0;
      try {
        fieldValue = versionDetailDoc.getLong(fieldName2);
      } catch (ClassCastException e) {
        fieldValue = (long) versionDetailDoc.getInteger(fieldName2);
      }

      versionDetailDoc.put(fieldName2, (long) (fieldValue + tuple._2()));
    }
    cacheMap.put(keyStr, versionDetailDoc);
    return;
  }
  public static void run() {
    try {

      logger.debug("Getting time line from Twitter");
      ArrayList<JSONObject> ResponsesFromTwitter = TwitterMethods.getTimeLineAsJsons(twitter);
      if (ResponsesFromTwitter != null) {
        logger.debug("There are " + ResponsesFromTwitter.size() + " tweets to be processed");
        for (JSONObject jsonObject : ResponsesFromTwitter) {

          // logger.debug("Processing a new twitter object ");
          if (!jsonObject.isNull("in_reply_to_status_id_str")) {
            logger.debug("Found a reply tweet " + jsonObject);
            String in_reply_to_status_id_str = jsonObject.getString("in_reply_to_status_id_str");
            String reply = jsonObject.getString("text");
            String in_reply_to_screen_name = jsonObject.getString("in_reply_to_screen_name");
            String taskResponse = reply.replaceAll("@" + in_reply_to_screen_name, "");

            // // store the reply id
            // String id_str = jsonObject.getString("id_str");

            // store the use screen name
            JSONObject userJson = jsonObject.getJSONObject("user");

            // store the replier user name
            String screen_name = userJson.getString("screen_name");

            logger.debug("Checking if the reply has already being stored");
            // Document taskRun = getTaskRunsFromMongoDB(id_str);
            // if (taskRun == null) {

            logger.debug("Looking for the original tweet for the reply");
            JSONObject orgTweet =
                TwitterMethods.getTweetByID(String.valueOf(in_reply_to_status_id_str), twitter);
            // loop through tweets till you find the orginal
            // tweet
            if (orgTweet != null) {
              while (!orgTweet.isNull("in_reply_to_status_id_str")) {
                orgTweet =
                    TwitterMethods.getTweetByID(
                        orgTweet.getString("in_reply_to_status_id_str"), twitter);
              }
              logger.debug("Original tweet was found");

              String orgTweetText = orgTweet.getString("text");
              Pattern pattern = Pattern.compile("(#t[0-9]+)");
              Matcher matcher = pattern.matcher(orgTweetText);
              String taskID = "";
              if (matcher.find()) {
                logger.debug("Found a taskID in the orginal tweet");
                taskID = matcher.group(1).replaceAll("#t", "");
                Integer intTaskID = Integer.valueOf(taskID);

                // cache taskIDs
                if (!cachedTaskIDsAndProjectsIDs.containsKey(intTaskID)) {
                  logger.debug("TaskID is not in the cache");
                  logger.debug("Retriving Task id from Collection: " + Config.taskCollection);
                  Document doc = MongodbMethods.getTaskFromMongoDB(intTaskID);
                  if (doc != null) {
                    int project_id = doc.getInteger("project_id");
                    cachedTaskIDsAndProjectsIDs.put(intTaskID, project_id);
                    if (insertTaskRun(taskResponse, intTaskID, project_id, screen_name, SOURCE)) {
                      logger.debug("Task run was completely processed");
                    } else {
                      logger.error("Failed to process the task run");
                    }
                  } else {
                    logger.error("Couldn't find task with ID " + taskID);
                    // TODO: Remove tweets that do not have
                    // records in MongoDB
                  }
                } else {
                  logger.debug("Task ID was found in the cache");
                  insertTaskRun(
                      taskResponse,
                      intTaskID,
                      cachedTaskIDsAndProjectsIDs.get(intTaskID),
                      screen_name,
                      SOURCE);
                }

              } else {
                logger.error(
                    "reply: \\"
                        + reply
                        + " was not being identified with an associated task in the original text: \\"
                        + orgTweetText);
              }
            }
          }
          // else {
          // logger.debug("This is not a reply tweet");
          //
          // }

        }
      } else {
        logger.info("Time line was null");
      }

    } catch (Exception e) {
      logger.error("Error ", e);
    }
  }