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_get_wrapup_times, mock_get_pause_intervals_in_range, mock_get_login_intervals_in_range, mock_insert_stats): agent_id_1 = 12 agent_id_2 = 13 input_stats = { agent_id_1: [ (dt(2012, 1, 1, 1, 5), dt(2012, 1, 1, 1, 15)), (dt(2012, 1, 1, 1, 20), dt(2012, 1, 1, 2, 20)), ], agent_id_2: [ (dt(2012, 1, 1, 1), dt(2012, 1, 1, 5)), ] } output_stats = { dt(2012, 1, 1, 1): { agent_id_1: {'login_time': timedelta(minutes=50), 'pause_time': timedelta(minutes=13)}, agent_id_2: {'login_time': ONE_HOUR, 'pause_time': timedelta(minutes=13)}, }, dt(2012, 1, 1, 2): { agent_id_1: {'login_time': timedelta(minutes=20), 'pause_time': timedelta(minutes=33)}, agent_id_2: {'login_time': ONE_HOUR, 'pause_time': timedelta(minutes=13)}, }, dt(2012, 1, 1, 3): { agent_id_2: {'login_time': ONE_HOUR, 'pause_time': ONE_HOUR}, }, dt(2012, 1, 1, 4): { agent_id_2: {'login_time': ONE_HOUR, 'pause_time': ONE_HOUR}, } } wrapups = { dt(2012, 1, 1, 3): { agent_id_1: {'wrapup_time': timedelta(seconds=15)} }, } start = dt(2012, 1, 1, 1) end = dt(2012, 1, 1, 4) mock_get_login_intervals_in_range.return_value = input_stats mock_get_pause_intervals_in_range.return_value = input_stats mock_get_wrapup_times.return_value = wrapups time_computer = Mock(agent.AgentTimeComputer) time_computer.compute_login_time_in_period.return_value = output_stats time_computer.compute_pause_time_in_period.return_value = output_stats agent.insert_periodic_stat(dao_sess, start, end) for period_start, agents_stats in output_stats.iteritems(): mock_insert_stats.assert_any_calls( agents_stats, period_start)
def test_insert_periodic_stat(self, mock_get_wrapup_times, mock_get_pause_intervals_in_range, mock_get_login_intervals_in_range, mock_insert_stats): agent_id_1 = 12 agent_id_2 = 13 input_stats = { agent_id_1: [ (dt(2012, 1, 1, 1, 5), dt(2012, 1, 1, 1, 15)), (dt(2012, 1, 1, 1, 20), dt(2012, 1, 1, 2, 20)), ], agent_id_2: [ (dt(2012, 1, 1, 1), dt(2012, 1, 1, 5)), ] } output_stats = { dt(2012, 1, 1, 1): { agent_id_1: { 'login_time': timedelta(minutes=50), 'pause_time': timedelta(minutes=13) }, agent_id_2: { 'login_time': ONE_HOUR, 'pause_time': timedelta(minutes=13) }, }, dt(2012, 1, 1, 2): { agent_id_1: { 'login_time': timedelta(minutes=20), 'pause_time': timedelta(minutes=33) }, agent_id_2: { 'login_time': ONE_HOUR, 'pause_time': timedelta(minutes=13) }, }, dt(2012, 1, 1, 3): { agent_id_2: { 'login_time': ONE_HOUR, 'pause_time': ONE_HOUR }, }, dt(2012, 1, 1, 4): { agent_id_2: { 'login_time': ONE_HOUR, 'pause_time': ONE_HOUR }, } } wrapups = { dt(2012, 1, 1, 3): { agent_id_1: { 'wrapup_time': timedelta(seconds=15) } }, } start = dt(2012, 1, 1, 1) end = dt(2012, 1, 1, 4) mock_get_login_intervals_in_range.return_value = input_stats mock_get_pause_intervals_in_range.return_value = input_stats mock_get_wrapup_times.return_value = wrapups time_computer = Mock(agent.AgentTimeComputer) time_computer.compute_login_time_in_period.return_value = output_stats time_computer.compute_pause_time_in_period.return_value = output_stats agent.insert_periodic_stat(dao_sess, start, end) for period_start, agents_stats in output_stats.iteritems(): mock_insert_stats.assert_any_calls(agents_stats, period_start)