Is there an efficient way of getting the last transaction date for an account?

Bob B's Avatar

Bob B

18 Aug, 2021 05:42 PM

I'm displaying a list of all the securities in all the investment accounts (about 1000 active and inactive). Producing the list takes milliseconds but getting the last transaction date takes over 7 seconds for the lot. Is there a more efficient/clever way than the following:

TxnSet txns = book.getTransactionSet().getTransactionsForAccount(secAcct);
txns.sortByField(AccountUtil.DATE);
int latestDate = txns.getLastTxn().getDateInt();

Thanks for any suggestions.

  1. 1 Posted by Stuart Beesley ... on 18 Aug, 2021 07:08 PM

    Stuart Beesley - JUST A FELLOW USER and Toolbox ‘guy’'s Avatar

    Probably not very helpful, but I see the internal MD code does this..:

    TxnSet acctTxns = book.getTransactionSet().getTransactionsForAccount(account);
    int lastDate = acctTxns.getLastTxn().getDateInt();

    Which is identical to your code... So I somehow suspect that there is no particularly faster way..?

    What about getting all txns (perhaps for all Investment Accounts) and iterating over the whole lot yourself and building a small table of last dates and then querying that.. Perhaps one call to getTransactions for all txns is faster than many calls per account..?

  2. 2 Posted by Bob B on 18 Aug, 2021 09:44 PM

    Bob B's Avatar

    Thanks I'll give that a try and report back.

  3. 3 Posted by Bob B on 18 Aug, 2021 10:26 PM

    Bob B's Avatar

    WOW Stuart that was a BRILLIANT suggestion. The timings went from over 7 seconds to about 0.013 seconds to generate the map of Account, LastTxDate Thanks so much.

    private static void generateLastDateMap() {
            if (lastDateMap != null) {return;}
            long start = System.nanoTime();
            lastDateMap = new HashMap<Account, Integer>();
            TransactionSet set = book.getTransactionSet();
            TxnSet txns = set.getAllTxns();
            txns.sortByField(AccountUtil.DATE);
            for (AbstractTxn txn : txns) {
                if (txn.getAccount().getAccountType() == AccountType.SECURITY) {
                    lastDateMap.put(txn.getAccount(), txn.getDateInt());}
                    // since this is a map, any new entry with the same key will replace
                    // the prior one and the last one will be the latest date because txns
                    //  are sorted by date
                }

        long end = System.nanoTime();
        Helpers.printTimings(start, end, &quot;generateLastDateMap&quot;);
    }</code>
    
    
    
    
  4. 4 Posted by Stuart Beesley ... on 18 Aug, 2021 10:59 PM

    Stuart Beesley - JUST A FELLOW USER and Toolbox ‘guy’'s Avatar

    👍 +1 😃

  5. Maddy closed this discussion on 20 Aug, 2021 01:57 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac