def update_db(end_date, start_date=None): if start_date is None: try: start = get_start_time() except RuntimeError: return else: start = datetime.datetime.strptime(start_date, '%Y-%m-%dT%H:%M:%S') end = datetime.datetime.strptime(end_date, '%Y-%m-%dT%H:%M:%S') logger.info('Filling cache into DB') logger.info('Start Time: %s, End time: %s', start, end) try: dao_sess.begin() insert_missing_queues(start, end) insert_missing_agents() dao_sess.commit() dao_sess.begin() queue.remove_between(dao_sess, start, end) agent.remove_after_start(dao_sess, start) queue.fill_simple_calls(dao_sess, start, end) dao_sess.commit() agent.insert_periodic_stat(dao_sess, start, end) for period_start in queue_log_dao.hours_with_calls(dao_sess, start, end): period_end = period_start + datetime.timedelta(hours=1) - datetime.timedelta(microseconds=1) queue.fill_calls(dao_sess, period_start, period_end) queue.insert_periodic_stat(dao_sess, period_start, period_end) except (IntegrityError, KeyboardInterrupt): _clean_up_after_error()
def test_remove_between_no_calls(self, mock_stat_queue_periodic_remove_after, mock_find_all_callid_between_date, mock_remove_callids): start = datetime.datetime(2012, 1, 1) end = datetime.datetime(2012, 1, 1, 23, 59, 59) callids = [] mock_find_all_callid_between_date.return_value = callids queue.remove_between(dao_sess, start, end) mock_find_all_callid_between_date.assert_called_once_with(dao_sess, start, end) self.assertEqual(mock_remove_callids.call_count, 0) mock_stat_queue_periodic_remove_after.assert_called_once_with(dao_sess, start)
def test_remove_between(self, mock_stat_queue_periodic_remove_after, mock_find_all_callid_between_date, mock_remove_callids): start = datetime.datetime(2012, 1, 1) end = datetime.datetime(2012, 1, 1, 23, 59, 59) callids = [1, 2, 3] mock_find_all_callid_between_date.return_value = callids queue.remove_between(dao_sess, start, end) mock_find_all_callid_between_date.assert_called_once_with( dao_sess, start, end) mock_remove_callids.assert_called_once_with(dao_sess, callids) mock_stat_queue_periodic_remove_after.assert_called_once_with( dao_sess, start)