Пример #1
0
def _handler_ttroute(da, item, action_log):
    """

    :type da: pyticas_tetres.da.route.TTRouteDataAccess
    :type item: pyticas_tetres.ttypes.TTRouteInfo
    :type action_log: pyticas_tetres.ttypes.ActionLogInfo
    """
    # 1. calculate travel time
    # 2. categorize (all)
    try:
        from pyticas_tetres.util.traffic_file_checker import has_traffic_files
        start = datetime.date(cfg.DATA_ARCHIVE_START_YEAR, 1, 1)
        last_day = datetime.date.today() - datetime.timedelta(days=cfg.DAILY_JOB_OFFSET_DAYS)
        start_date_str, end_date_str = start.strftime('%Y-%m-%d'), last_day.strftime('%Y-%m-%d')
        if not has_traffic_files(start_date_str, end_date_str):
            return False, "Missing traffic files for the given time range from {} to {}.".format(start_date_str, end_date_str)
    except Exception as e:
        getLogger(__name__).warning(
            'Exception occured while checking if traffic files exist during handling travel time routes. Error: {}'.format(
                e))
    daily_periods = _get_all_daily_periods()
    cnt = 0
    for prd in daily_periods:
        try:
            inserted_ids = traveltime.calculate_a_route(prd, item)
            if inserted_ids:
                categorization.categorize(item, prd)
                cnt += len(inserted_ids)
        except Exception as ex:
            getLogger(__name__).warning(
                'Exception occured when handling route changes : %s' % tb.traceback(ex, f_print=False))

    return cnt > 0
Пример #2
0
def _handler_ttroute(da, item, action_log):
    """

    :type da: pyticas_tetres.da.route.TTRouteDataAccess
    :type item: pyticas_tetres.ttypes.TTRouteInfo
    :type action_log: pyticas_tetres.ttypes.ActionLogInfo
    """
    # 1. calculate travel time
    # 2. categorize (all)
    # faverolles 1/16/2020 NOTE: always starts at datetime.today
    daily_periods = _get_all_daily_periods()
    cnt = 0
    try:
        for prd in daily_periods:
            inserted_ids = traveltime.calculate_a_route(prd, item)
            if inserted_ids:
                categorization.categorize(item, prd)

            if inserted_ids is not False:
                cnt += len(inserted_ids)

        return cnt > 0
    except Exception as ex:
        getLogger(__name__).warning(
            'Exception occured when handling route changes : %s' %
            tb.traceback(ex, f_print=False))
        return False
def _perform_calculation_of_tt(ttri, prd):
    """

    :type ttri: pyticas_tetres.ttypes.TTRouteInfo
    :type prd: pyticas.ttypes.Period
    """
    inserted_ids = traveltime.calculate_a_route(prd, ttri)
    if inserted_ids:
        categorization.categorize(ttri, prd)
Пример #4
0
def _worker_process_to_calculate_tt_and_categorize(idx, queue, lck, data_path,
                                                   db_info):
    from pyticas_tetres.db.tetres import conn
    from pyticas.infra import Infra
    from pyticas.tool import tb
    from pyticas_tetres.rengine.cats import weather, incident, snowmgmt, specialevent, workzone

    logger = getLogger(__name__)
    # initialize
    logger.debug('[TT-Categorization Worker %d] starting...' % (idx))
    ticas.initialize(data_path)
    infra = Infra.get_infra()
    conn.connect(db_info)

    categorizers = [weather, incident, workzone, specialevent, snowmgmt]
    da_route = TTRouteDataAccess()
    logger.debug('[TT-Categorization Worker %d] is ready' % (idx))
    while True:
        ttr_id, prd, num, total = queue.get()
        if prd is None:
            da_route.close_session()
            exit(1)
        try:
            ttri = da_route.get_by_id(ttr_id)
            if not ttri:
                logger.debug(
                    '[TT-Categorization Worker %d] route is not found (%s)' %
                    (idx, ttr_id))
                continue
            logger.debug(
                '[TT-Categorization Worker %d] (%d/%d) %s (id=%s) at %s' %
                (idx, num, total, ttri.name, ttri.id, prd.get_date_string()))
            is_inserted = traveltime.calculate_a_route(
                prd, ttri, dbsession=da_route.get_session(), lock=lck)
            if not is_inserted:
                logger.warning(
                    '[TT-Categorization Worker %d]  - fail to add travel time data'
                    % idx)

            tt_da = TravelTimeDataAccess(prd.start_date.year)
            tt_data_list = tt_da.list_by_period(ttri.id, prd)
            tt_da.close_session()

            for cidx, categorizer in enumerate(categorizers):
                n_inserted = categorizer.categorize(ttri,
                                                    prd,
                                                    tt_data_list,
                                                    lock=lck)

            gc.collect()

        except Exception as ex:
            tb.traceback(ex)
            continue
Пример #5
0
def _calculate_tt_for_routes(start_date, end_date, ttr_ids):
    stime = datetime.time(0, 0, 0, 0)
    etime = datetime.time(23, 55, 0, 0)
    da_route = TTRouteDataAccess()

    daily_periods = period.create_periods(start_date,
                                          end_date,
                                          stime,
                                          etime,
                                          cfg.TT_DATA_INTERVAL,
                                          target_days=[0, 1, 2, 3, 4, 5, 6],
                                          remove_holiday=False)
    for pidx, prd in enumerate(daily_periods):
        for ridx, ttr_id in enumerate(ttr_ids):
            ttri = da_route.get_by_id(ttr_id)
            is_inserted = traveltime.calculate_a_route(
                prd, ttri, dbsession=da_route.get_session())
Пример #6
0
def _worker_process_to_calculate_tt_only(idx, queue, lck, data_path, db_info,
                                         **kwargs):
    from pyticas_tetres.db.tetres import conn
    from pyticas.infra import Infra
    from pyticas.tool import tb

    logger = getLogger(__name__)
    # initialize
    logger.debug('[TT-Categorization Worker %d] starting...' % (idx))
    ticas.initialize(data_path)
    infra = Infra.get_infra()
    conn.connect(db_info)

    da_route = TTRouteDataAccess()
    logger.debug('[TT-Categorization Worker %d] is ready' % (idx))
    while True:
        ttr_id, prd, num, total = queue.get()
        if prd is None:
            da_route.close_session()
            exit(1)
        try:
            ttri = da_route.get_by_id(ttr_id)
            if not ttri:
                logger.debug(
                    '[TT-Categorization Worker %d] route is not found (%s)' %
                    (idx, ttr_id))
                continue
            logger.debug(
                '[TT-Categorization Worker %d] (%d/%d) %s (id=%s) at %s' %
                (idx, num, total, ttri.name, ttri.id, prd.get_date_string()))
            is_inserted = traveltime.calculate_a_route(
                prd, ttri, dbsession=da_route.get_session(), lock=lck)
            if not is_inserted:
                logger.warning(
                    '[TT-Categorization Worker %d]  - fail to add travel time data'
                    % idx)

            gc.collect()

        except Exception as ex:
            tb.traceback(ex)
            continue