public ArrayList getLastTransactions()

in bigtable/use-cases/fraudDetection/src/main/java/bigtable/fraud/beam/utils/TransactionDetails.java [129:199]


  public ArrayList<TransactionDetails> getLastTransactions(
      final Result row, final long duration) {
    ArrayList<TransactionDetails> lastTransactions = new ArrayList<>();
    String[] headers = getHeaders();

    // Create ArrayList that will hold the cells when we read from CBT and
    // ignore the first element because it will hold the row key, and we
    // already know the row key (userID).
    ArrayList<List<Cell>> cells = new ArrayList<>();
    cells.add(null);
    for (int i = 1; i < headers.length; i++) {
        cells.add(row.getColumnCells(Bytes.toBytes(getColFamily()),
            Bytes.toBytes(headers[i])));
        if (cells.get(i).size() == 0) {
          return lastTransactions;
        }
    }

    // Iterate over all the transactions of that user that fit in the
    // timeMilliseconds range.
    // If we find a transaction that was declared as fraudulent,
    // we should ignore it.
    int transactionIteration = 0;
    boolean hasCells = true;
    while (hasCells) {
      // Build a historical transaction.
      ArrayList<String> historicalTransactionBuilder = new ArrayList<>();
      historicalTransactionBuilder.add(customerID);
      long transactionTimeMillisecond = -1;
      for (int header = 1; header < headers.length; header++) {
        // Populate the historical transaction.
        List<Cell> currentCells = cells.get(header);
        // Stop if we iterated over all transactions for that customer.
        if (currentCells.size() == transactionIteration + 1) {
          hasCells = false;
        }
        historicalTransactionBuilder.add(
            new String(currentCells.get(transactionIteration).getValueArray()));
        transactionTimeMillisecond = currentCells.get(transactionIteration)
            .getTimestamp();
      }

      // Create the historical transaction.
      TransactionDetails historicalTransaction =
          new TransactionDetails(
              UtilFunctions.arrayListToCommasString(
                  historicalTransactionBuilder));

      // Populate the historical transaction timestamp.
      if (transactionTimeMillisecond != -1) {
        historicalTransaction.setTimestampMillisecond(
            transactionTimeMillisecond);
      }

      // If we reach transactions from an older timestamp than what we want,
      // stop.
      if (historicalTransaction.getTimestampMillisecond() < duration) {
        break;
      }

      // If the transaction was legit, add it to the list of historical
      // transactions.
      if (!historicalTransaction.isFraud()) {
        lastTransactions.add(historicalTransaction);
      }

      // Go to the next historical transaction.
      transactionIteration++;
    }
    return lastTransactions;
  }