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_insert_periodic_stat(self, mock_insert_stats, mock_get_periodic_stats): s = datetime.datetime(2012, 1, 1) e = datetime.datetime(2012, 1, 31, 23, 59, 59, 999999) t1 = s t2 = s + datetime.timedelta(hours=1) stat1 = {'stats': 1234} stat2 = {'stats': 5555} fake_stats = {t1: stat1, t2: stat2} mock_get_periodic_stats.return_value = fake_stats queue.insert_periodic_stat(dao_sess, s, e) mock_insert_stats.assert_any_call(dao_sess, stat1, t1) mock_insert_stats.assert_any_call(dao_sess, stat2, t2) self.assertEqual(mock_insert_stats.call_count, 2)