mirror of
https://git.jami.net/savoirfairelinux/jami-daemon.git
synced 2025-08-12 22:09:25 +08:00
[#1214] Save and load history according to the limit timestamp + unit tests
This commit is contained in:
@ -21,6 +21,7 @@
|
||||
#include <historymanager.h>
|
||||
#include <errno.h>
|
||||
#include <cc++/file.h>
|
||||
#include <time.h>
|
||||
|
||||
HistoryManager::HistoryManager () : _history_loaded (false), _history_path (""){
|
||||
|
||||
@ -32,13 +33,13 @@ HistoryManager::~HistoryManager () {
|
||||
_history_items.clear ();
|
||||
}
|
||||
|
||||
int HistoryManager::load_history (std::string path)
|
||||
int HistoryManager::load_history (int limit, std::string path)
|
||||
{
|
||||
Conf::ConfigTree history_list;
|
||||
|
||||
create_history_path (path);
|
||||
load_history_from_file (&history_list);
|
||||
return load_history_items_map (&history_list);
|
||||
return load_history_items_map (&history_list, limit);
|
||||
}
|
||||
|
||||
bool HistoryManager::save_history (void)
|
||||
@ -59,7 +60,7 @@ bool HistoryManager::load_history_from_file (Conf::ConfigTree *history_list)
|
||||
return exist;
|
||||
}
|
||||
|
||||
int HistoryManager::load_history_items_map (Conf::ConfigTree *history_list)
|
||||
int HistoryManager::load_history_items_map (Conf::ConfigTree *history_list, int limit)
|
||||
{
|
||||
|
||||
short nb_items = 0;
|
||||
@ -68,6 +69,13 @@ int HistoryManager::load_history_items_map (Conf::ConfigTree *history_list)
|
||||
Conf::TokenList::iterator iter;
|
||||
std::string number, name, accountID, timestamp_start, timestamp_stop;
|
||||
CallType type;
|
||||
int history_limit;
|
||||
time_t current_timestamp;
|
||||
|
||||
// We want to save only the items recent enough (ie compared to CONFIG_HISTORY_LIMIT)
|
||||
// Get the current timestamp
|
||||
(void) time (¤t_timestamp);
|
||||
history_limit = get_unix_timestamp_equivalent (limit);
|
||||
|
||||
sections = history_list->getSections();
|
||||
iter = sections.begin();
|
||||
@ -81,12 +89,15 @@ int HistoryManager::load_history_items_map (Conf::ConfigTree *history_list)
|
||||
accountID = getConfigString (*iter, "accountid", history_list);
|
||||
timestamp_start = *iter;
|
||||
|
||||
item = new HistoryItem (timestamp_start, type, timestamp_stop, name, number, accountID);
|
||||
add_new_history_entry (item);
|
||||
nb_items ++;
|
||||
// Make a check on the start timestamp to know it is loadable according to CONFIG_HISTORY_LIMIT
|
||||
if ( atoi (timestamp_start.c_str ()) >= ((int) current_timestamp - history_limit))
|
||||
{
|
||||
item = new HistoryItem (timestamp_start, type, timestamp_stop, name, number, accountID);
|
||||
add_new_history_entry (item);
|
||||
nb_items ++;
|
||||
}
|
||||
|
||||
iter ++;
|
||||
|
||||
}
|
||||
|
||||
return nb_items;
|
||||
@ -200,21 +211,30 @@ std::map <std::string, std::string> HistoryManager::get_history_serialized (void
|
||||
}
|
||||
|
||||
|
||||
int HistoryManager::set_serialized_history (std::map <std::string, std::string> history)
|
||||
int HistoryManager::set_serialized_history (std::map <std::string, std::string> history, int limit)
|
||||
{
|
||||
std::map <std::string, std::string>::iterator iter;
|
||||
HistoryItem *new_item;
|
||||
int items_added = 0;
|
||||
int history_limit;
|
||||
time_t current_timestamp;
|
||||
|
||||
// Clear the existing history
|
||||
_history_items.clear ();
|
||||
|
||||
// We want to save only the items recent enough (ie compared to CONFIG_HISTORY_LIMIT)
|
||||
// Get the current timestamp
|
||||
(void) time (¤t_timestamp);
|
||||
history_limit = get_unix_timestamp_equivalent (limit);
|
||||
iter = history.begin ();
|
||||
while (iter != history.end ())
|
||||
{
|
||||
new_item = new HistoryItem (iter->first, iter->second);
|
||||
add_new_history_entry (new_item);
|
||||
items_added ++;
|
||||
if (atoi (iter->first.c_str ()) >= ((int) current_timestamp - history_limit))
|
||||
{
|
||||
new_item = new HistoryItem (iter->first, iter->second);
|
||||
add_new_history_entry (new_item);
|
||||
items_added ++;
|
||||
}
|
||||
iter ++;
|
||||
}
|
||||
|
||||
|
@ -25,6 +25,8 @@
|
||||
#include <global.h>
|
||||
#include <user_cfg.h>
|
||||
|
||||
#define DAY_UNIX_TIMESTAMP 86400 // Number of seconds in one day: 60 x 60 x 24
|
||||
|
||||
typedef std::map <std::string, HistoryItem*> HistoryItemMap;
|
||||
|
||||
class HistoryManager {
|
||||
@ -45,7 +47,7 @@ class HistoryManager {
|
||||
*
|
||||
*@return int The number of history items succesfully loaded
|
||||
*/
|
||||
int load_history (std::string path="");
|
||||
int load_history (int limit, std::string path="");
|
||||
|
||||
/**
|
||||
*@return bool True if the history has been successfully saved in the file
|
||||
@ -60,7 +62,7 @@ class HistoryManager {
|
||||
/*
|
||||
* @return int The number of history items loaded
|
||||
*/
|
||||
int load_history_items_map (Conf::ConfigTree *history_list);
|
||||
int load_history_items_map (Conf::ConfigTree *history_list, int limit);
|
||||
|
||||
/*
|
||||
* Inverse method, ie save a data structure containing the history into a file
|
||||
@ -92,10 +94,13 @@ class HistoryManager {
|
||||
|
||||
std::map <std::string, std::string> get_history_serialized (void);
|
||||
|
||||
int set_serialized_history (std::map <std::string, std::string> history);
|
||||
int set_serialized_history (std::map <std::string, std::string> history, int limit);
|
||||
|
||||
private:
|
||||
|
||||
inline int get_unix_timestamp_equivalent (int days)
|
||||
{
|
||||
return days * DAY_UNIX_TIMESTAMP;
|
||||
}
|
||||
|
||||
int getConfigInt(const std::string& section, const std::string& name, Conf::ConfigTree *history_list);
|
||||
std::string getConfigString(const std::string& section, const std::string& name, Conf::ConfigTree *history_list);
|
||||
|
@ -147,7 +147,7 @@ ManagerImpl::init()
|
||||
|
||||
|
||||
// Load the history
|
||||
_history->load_history ();
|
||||
_history->load_history (getConfigInt (PREFERENCES, CONFIG_HISTORY_LIMIT));
|
||||
}
|
||||
|
||||
void ManagerImpl::terminate()
|
||||
@ -2899,7 +2899,7 @@ std::map<std::string, std::string> ManagerImpl::send_history_to_client (void)
|
||||
|
||||
void ManagerImpl::receive_history_from_client (std::map<std::string, std::string> history)
|
||||
{
|
||||
_history->set_serialized_history (history);
|
||||
_history->set_serialized_history (history, Manager::instance().getConfigInt (PREFERENCES, CONFIG_HISTORY_LIMIT));
|
||||
_history->save_history ();
|
||||
}
|
||||
|
||||
|
@ -30,6 +30,7 @@ using std::endl;
|
||||
#define HISTORY_SAMPLE "history-sample"
|
||||
#define HISTORY_SAMPLE_SIZE 3
|
||||
#define CONFIG_SAMPLE "sflphonedrc-sample"
|
||||
#define HUGE_HISTORY_LIMIT 20000
|
||||
|
||||
void HistoryTest::setUp(){
|
||||
// Instanciate the cleaner singleton
|
||||
@ -69,7 +70,7 @@ void HistoryTest::test_load_history_items_map ()
|
||||
|
||||
history->set_history_path (HISTORY_SAMPLE);
|
||||
history->load_history_from_file (&history_list);
|
||||
nb_items = history->load_history_items_map (&history_list);
|
||||
nb_items = history->load_history_items_map (&history_list, HUGE_HISTORY_LIMIT);
|
||||
CPPUNIT_ASSERT (nb_items == HISTORY_SAMPLE_SIZE);
|
||||
CPPUNIT_ASSERT (history->get_history_size () == HISTORY_SAMPLE_SIZE);
|
||||
}
|
||||
@ -82,7 +83,7 @@ void HistoryTest::test_save_history_items_map ()
|
||||
|
||||
history->set_history_path (HISTORY_SAMPLE);
|
||||
history->load_history_from_file (&history_list);
|
||||
nb_items_loaded = history->load_history_items_map (&history_list);
|
||||
nb_items_loaded = history->load_history_items_map (&history_list, HUGE_HISTORY_LIMIT);
|
||||
nb_items_saved = history->save_history_items_map (&history_list2);
|
||||
CPPUNIT_ASSERT (nb_items_loaded == nb_items_saved);
|
||||
}
|
||||
@ -96,7 +97,7 @@ void HistoryTest::test_save_history_to_file ()
|
||||
|
||||
history->set_history_path (HISTORY_SAMPLE);
|
||||
history->load_history_from_file (&history_list);
|
||||
history->load_history_items_map (&history_list);
|
||||
history->load_history_items_map (&history_list, HUGE_HISTORY_LIMIT);
|
||||
history->save_history_items_map (&history_list2);
|
||||
CPPUNIT_ASSERT (history->save_history_to_file (&history_list2));
|
||||
}
|
||||
@ -111,7 +112,7 @@ void HistoryTest::test_get_history_serialized ()
|
||||
Manager::instance().initConfigFile(true, CONFIG_SAMPLE);
|
||||
Manager::instance().loadAccountMap ();
|
||||
|
||||
CPPUNIT_ASSERT (history->load_history (HISTORY_SAMPLE) == HISTORY_SAMPLE_SIZE);
|
||||
CPPUNIT_ASSERT (history->load_history (HUGE_HISTORY_LIMIT, HISTORY_SAMPLE) == HISTORY_SAMPLE_SIZE);
|
||||
res = history->get_history_serialized ();
|
||||
CPPUNIT_ASSERT (res.size()==HISTORY_SAMPLE_SIZE);
|
||||
|
||||
@ -142,8 +143,9 @@ void HistoryTest::test_set_serialized_history ()
|
||||
map_test["747638685"] = "2|136|Emmanuel Milou|747638765|Account:1239059899";
|
||||
map_test["775354456"] = "1|5143848557|empty|775354987|Account:43789459478";
|
||||
|
||||
CPPUNIT_ASSERT (history->load_history (HISTORY_SAMPLE) == HISTORY_SAMPLE_SIZE);
|
||||
CPPUNIT_ASSERT (history->set_serialized_history (map_test) == 3);
|
||||
CPPUNIT_ASSERT (history->load_history (HUGE_HISTORY_LIMIT, HISTORY_SAMPLE) == HISTORY_SAMPLE_SIZE);
|
||||
// We use a large history limit to be able to interpret results
|
||||
CPPUNIT_ASSERT (history->set_serialized_history (map_test, HUGE_HISTORY_LIMIT) == 3);
|
||||
CPPUNIT_ASSERT (history->get_history_size () == 3);
|
||||
|
||||
map_test.clear ();
|
||||
@ -165,6 +167,39 @@ void HistoryTest::test_set_serialized_history ()
|
||||
CPPUNIT_ASSERT (history->save_history_to_file (&history_list));
|
||||
}
|
||||
|
||||
void HistoryTest::test_set_serialized_history_with_limit ()
|
||||
{
|
||||
// We build a map to have an efficient test
|
||||
std::map <std::string, std::string> map_test;
|
||||
std::string tmp;
|
||||
Conf::ConfigTree history_list;
|
||||
time_t current, day=86400; // One day in unix timestamp
|
||||
std::stringstream current_1, current_2, current_3;
|
||||
|
||||
(void) time (¤t);
|
||||
current_1 << (current - 2*day) << std::endl;
|
||||
current_2 << (current - 5*day) << std::endl;
|
||||
current_3 << (current - 11*day) << std::endl;
|
||||
|
||||
map_test[current_1.str ()] = "0|514-276-5468|Savoir-faire Linux|144562458|empty";
|
||||
map_test[current_2.str ()] = "2|136|Emmanuel Milou|747638765|Account:1239059899";
|
||||
map_test[current_3.str ()] = "1|5143848557|empty|775354987|Account:43789459478";
|
||||
|
||||
CPPUNIT_ASSERT (history->load_history (HUGE_HISTORY_LIMIT, HISTORY_SAMPLE) == HISTORY_SAMPLE_SIZE);
|
||||
// We use different value of history limit
|
||||
// 10 days - the last entry should not be saved
|
||||
CPPUNIT_ASSERT (history->set_serialized_history (map_test, 10) == 2);
|
||||
CPPUNIT_ASSERT (history->get_history_size () == 2);
|
||||
|
||||
// 4 days - the two last entries should not be saved
|
||||
CPPUNIT_ASSERT (history->set_serialized_history (map_test, 4) == 1);
|
||||
CPPUNIT_ASSERT (history->get_history_size () == 1);
|
||||
|
||||
// 1 day - no entry should not be saved
|
||||
CPPUNIT_ASSERT (history->set_serialized_history (map_test, 1) == 0);
|
||||
CPPUNIT_ASSERT (history->get_history_size () == 0);
|
||||
}
|
||||
|
||||
void HistoryTest::tearDown(){
|
||||
// Delete the history object
|
||||
delete history; history=0;
|
||||
|
@ -49,6 +49,7 @@ class HistoryTest : public CppUnit::TestCase {
|
||||
CPPUNIT_TEST (test_save_history_to_file);
|
||||
CPPUNIT_TEST (test_get_history_serialized);
|
||||
CPPUNIT_TEST (test_set_serialized_history);
|
||||
CPPUNIT_TEST (test_set_serialized_history_with_limit);
|
||||
CPPUNIT_TEST_SUITE_END ();
|
||||
|
||||
public:
|
||||
@ -73,6 +74,8 @@ class HistoryTest : public CppUnit::TestCase {
|
||||
void test_get_history_serialized ();
|
||||
|
||||
void test_set_serialized_history ();
|
||||
|
||||
void test_set_serialized_history_with_limit ();
|
||||
|
||||
/*
|
||||
* Code factoring - Common resources can be released here.
|
||||
|
Reference in New Issue
Block a user