def test_hours_with_calls(self): start = datetime(2012, 1, 1, tzinfo=UTC) end = datetime(2012, 6, 30, 23, 59, 59, 999999) res = [ h for h in queue_log_dao.hours_with_calls(self.session, start, end) ] self.assertEqual(res, []) def _insert_at(t): queue_log_dao.insert_entry(t, 'hours', 'queue', 'agent', 'event') _insert_at('2011-12-31 12:55:22.123123') _insert_at('2012-01-01 08:45:23.2345') _insert_at('2012-06-30 23:59:59.999999') _insert_at('2012-07-01 00:00:00.000000') expected = [ datetime(2012, 1, 1, 8, tzinfo=UTC), datetime(2012, 6, 30, 23, tzinfo=UTC) ] res = [ h for h in queue_log_dao.hours_with_calls(self.session, start, end) ] self.assertEqual(res, expected)
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_hours_with_calls(self): start = datetime(2012, 01, 01) end = datetime(2012, 6, 30, 23, 59, 59, 999999) res = [h for h in queue_log_dao.hours_with_calls(self.session, start, end)] self.assertEqual(res, []) def _insert_at(t): queue_log_dao.insert_entry(t, 'hours', 'queue', 'agent', 'event') _insert_at('2011-12-31 12:55:22.123123') _insert_at('2012-01-01 08:45:23.2345') _insert_at('2012-06-30 23:59:59.999999') _insert_at('2012-07-01 00:00:00.000000') expected = [ datetime(2012, 1, 1, 8), datetime(2012, 6, 30, 23) ] res = [h for h in queue_log_dao.hours_with_calls(self.session, start, end)] self.assertEqual(res, expected)
def update_db(config, end_date, start_date=None): if start_date is None: try: with session_scope() as dao_sess: start = get_start_time(dao_sess) except RuntimeError: return else: start = datetime.datetime.strptime(start_date, '%Y-%m-%dT%H:%M:%S%z') end = datetime.datetime.strptime(end_date, '%Y-%m-%dT%H:%M:%S%z') auth_client = AuthClient(**config['auth']) token_data = auth_client.token.new(expiration=300) confd_client = ConfdClient(**config['confd']) confd_client.set_token(token_data['token']) logger.info('Getting objects from wazo-confd...') confd_queues = confd_client.queues.list(recurse=True) confd_agents = confd_client.agents.list(recurse=True) master_tenant = token_data['metadata']['tenant_uuid'] logger.info('Filling cache into DB') logger.info('Start Time: %s, End time: %s', start, end) with session_scope() as dao_sess: insert_missing_queues(dao_sess, start, end, confd_queues['items'], master_tenant) insert_missing_agents(dao_sess, confd_agents['items']) dao_sess.flush() queue.remove_between(dao_sess, start, end) agent.remove_after_start(dao_sess, start) queue.fill_simple_calls(dao_sess, start, end) dao_sess.flush() logger.info('Inserting agent periodic stat') agent.insert_periodic_stat(dao_sess, start, end) logger.info('Inserting queue periodic stat') 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)