Beispiel #1
0
  public static Long getCounter(String key, int days) throws BackendServiceException {
    if (days <= 0) {
      throw new IllegalArgumentException("days param must be greater than 0");
    }
    Calendar cal = Calendar.getInstance();
    List<TPairKeyDate> keys = new ArrayList<TPairKeyDate>(days);

    for (int i = 0; i < days; i++) {
      TPairKeyDate keyDate = new TPairKeyDate();
      keyDate.setKey(key);
      keyDate.setDate(StrNCounter64Client.buildTDate(cal, DateTimeBitField.DAY_IN_MONTH));

      keys.add(keyDate);
      cal.add(Calendar.DAY_OF_MONTH, -1);
    }

    TListDataByDateResult result = CounterClient.multiGetByDate(keys);

    if (result.getError() < 0
        && result.getError() != -ECode.NOT_EXIST.getValue()
        && result.getError() != -ECode.OVER_FLOW.getValue()) {
      throw new BackendServiceException(
          MsgBuilder.format(
              "CounterClient.mmultiGetCache, key[??], days[??] responses error[??]",
              key,
              days,
              result.getError()));
    }

    Long counter = 0L;
    List<Long> values;
    for (TPairKeyDateValueResult t : result.getDataList()) {
      if (t.getValueResult().getError() >= 0) {
        values = t.getValueResult().getValue();
        if (values != null && values.size() > 0) {
          counter += values.get(0);
        }
      }
    }
    return counter;
  }
Beispiel #2
0
  public static Map<String, Long> getCounters(Map<String, EventMsg> map)
      throws BackendServiceException {
    List<TPairKeyDate> keys = new ArrayList<TPairKeyDate>();
    for (Map.Entry<String, EventMsg> keyMsg : map.entrySet()) {
      EventMsg msg = keyMsg.getValue();
      Calendar cal = Calendar.getInstance();
      int days;
      if (msg.getPeriodType() == PeriodType.DAY) {
        days = 1;
      } else {
        long endDate =
            msg.getEndDate() < System.currentTimeMillis()
                ? msg.getEndDate()
                : System.currentTimeMillis();
        cal.setTimeInMillis(endDate);
        days = Utils.getDays(msg.getStartDate(), endDate);
      }
      LOGGER.info(
          MsgBuilder.format(
              "getCounters counter[??] timer[??], days[??]",
              keyMsg.getKey(),
              cal.getTimeInMillis(),
              days));

      for (int i = 0; i < days; i++) {
        TPairKeyDate keyDate = new TPairKeyDate();
        keyDate.setKey(keyMsg.getKey());
        keyDate.setDate(StrNCounter64Client.buildTDate(cal, DateTimeBitField.DAY_IN_MONTH));
        LOGGER.warn(MsgBuilder.format("find date[??]", cal.getTime()));

        keys.add(keyDate);
        cal.add(Calendar.DAY_OF_MONTH, -1);
      }
    }
    TListDataByDateResult multiGetByDateResult = CounterClient.multiGetByDate(keys);

    if (multiGetByDateResult.getError() < 0) {
      if (multiGetByDateResult.getError() != -9) {
        LOGGER.warn(
            MsgBuilder.format(
                "CounterClient.multiGetByDate responses error[??]",
                multiGetByDateResult.getError()));
      }
    }
    Map<String, Long> result = new ConcurrentHashMap<String, Long>();
    Iterator<TPairKeyDateValueResult> dataListIterator = multiGetByDateResult.getDataListIterator();
    while (dataListIterator.hasNext()) {
      TPairKeyDateValueResult next = dataListIterator.next();
      String key = next.getKeyDate().getKey();
      Long value = result.get(key);
      if (value == null) {
        value = 0L;
      }
      if (next.getValueResult().getError() < 0) {
        if (next.getValueResult().getError() != -9
            && next.getValueResult().getError() != -ECode.OVER_FLOW.getValue()) {
          LOGGER.warn(
              MsgBuilder.format(
                  "CounterClient.multiGetByDate responses error[??]",
                  next.getValueResult().getError()));
        }
      } else {
        for (long count : next.getValueResult().getValue()) {
          value += count;
          LOGGER.info(next.getKeyDate().getDate() + " - " + count);
        }
      }
      result.put(key, value);
    }
    return result;
  }