Skip to content

Commit

Permalink
Bug 797945 - Imap bayes conversion runs many times
Browse files Browse the repository at this point in the history
When the Imap Editor is loaded, the bayes KVP entries are tested to see
if they are required to be converted to flat entries. This involves a
function that scans every account and if it finds entries that need
changing, the change is made and book property
GNC_FEATURE_GUID_FLAT_BAYESIAN is added so further scans are not made.

As the Imap Editor loops over the account list also, if there are no
bayes KVP changes then the conversion function gets called for every
account in the list. This can also happen to a lesser degree when doing
imports.

To stop this a flag is set once the convert to flat function has been
run so it only runs once and only lasts for the session.
  • Loading branch information
Bob-IT committed Sep 18, 2020
1 parent 0e9f368 commit 416e234
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions gnucash/gnome-utils/gnc-file.c
Expand Up @@ -1140,6 +1140,10 @@ gnc_file_open_file (GtkWindow *parent, const char * newfile, gboolean open_reado
if (!gnc_file_query_save (parent, TRUE))
return FALSE;

/* Reset the flag that indicates the conversion of the bayes KVP
* entries has been run */
gnc_account_reset_convert_bayes_to_flat ();

return gnc_post_file_open (parent, newfile, open_readonly);
}

Expand Down
4 changes: 4 additions & 0 deletions gnucash/gnome/gnc-plugin-basic-commands.c
Expand Up @@ -449,6 +449,10 @@ gnc_main_window_cmd_file_open (GtkAction *action, GncMainWindowActionData *data)
if (!gnc_main_window_all_finish_pending())
return;

/* Reset the flag that indicates the conversion of the bayes KVP
* entries has been run */
gnc_account_reset_convert_bayes_to_flat ();

gnc_window_set_progressbar_window (GNC_WINDOW(data->window));
#ifdef HAVE_DBI_DBI_H
gnc_ui_file_access_for_open (GTK_WINDOW (data->window));
Expand Down
14 changes: 13 additions & 1 deletion libgnucash/engine/Account.cpp
Expand Up @@ -55,6 +55,9 @@ static QofLogModule log_module = GNC_MOD_ACCOUNT;
/* The Canonical Account Separator. Pre-Initialized. */
static gchar account_separator[8] = ".";
static gunichar account_uc_separator = ':';

static bool imap_convert_bayes_to_flat_run = false;

/* Predefined KVP paths */
static const std::string KEY_ASSOC_INCOME_ACCOUNT("ofx/associated-income-account");
static const std::string KEY_RECONCILE_INFO("reconcile-info");
Expand Down Expand Up @@ -5602,6 +5605,12 @@ imap_convert_bayes_to_flat (QofBook * book)
return ret;
}

void
gnc_account_reset_convert_bayes_to_flat (void)
{
imap_convert_bayes_to_flat_run = false;
}

/*
* Here we check to see the state of import map data.
*
Expand All @@ -5616,10 +5625,13 @@ imap_convert_bayes_to_flat (QofBook * book)
static void
check_import_map_data (QofBook *book)
{
if (gnc_features_check_used (book, GNC_FEATURE_GUID_FLAT_BAYESIAN))
if (gnc_features_check_used (book, GNC_FEATURE_GUID_FLAT_BAYESIAN) ||
imap_convert_bayes_to_flat_run)
return;

/* This function will set GNC_FEATURE_GUID_FLAT_BAYESIAN if necessary.*/
imap_convert_bayes_to_flat (book);
imap_convert_bayes_to_flat_run = true;
}

static constexpr double threshold = .90 * probability_factor; /* 90% */
Expand Down
5 changes: 5 additions & 0 deletions libgnucash/engine/Account.h
Expand Up @@ -1504,6 +1504,11 @@ void gnc_account_delete_map_entry (Account *acc, char *head, char *category,
*/
void gnc_account_delete_all_bayes_maps (Account *acc);

/** Reset the flag that indicates the function imap_convert_bayes_to_flat
* has been run
*/
void gnc_account_reset_convert_bayes_to_flat (void);

/** @} */


Expand Down
2 changes: 2 additions & 0 deletions libgnucash/engine/test/gtest-import-map.cpp
Expand Up @@ -345,6 +345,8 @@ TEST_F(ImapBayesTest, ConvertBayesData)
root->set_path({IMAP_FRAME_BAYES, sausage, "Expense#Drink"}, val3);
root->set_path({IMAP_FRAME_BAYES, foo, "Expense#Food"}, new KvpValue{*val2});
root->set_path({IMAP_FRAME_BAYES, salt, acct1_guid}, new KvpValue{*val1});
/* make sure to reset the conversion has been run flag */
gnc_account_reset_convert_bayes_to_flat ();
/*Calling into the imap functions should trigger a conversion.*/
gnc_account_imap_add_account_bayes(t_imap, t_list5, t_expense_account2); //pork and sausage; account Food
// convert from 'Asset-Bank' to 'Asset-Bank' guid
Expand Down

0 comments on commit 416e234

Please sign in to comment.