def retrieve_user_mobility_data_single_user(dbcc, user_id, current_date, predicted_date):
    history_date = calculate_mp_history_date(current_date)

    current_week_date_range = support.unix_weeks_timestamp_to_date_range(
        support.date_to_unix_weeks_timestamp(current_date)
    )
    history_week_date_range = support.unix_weeks_timestamp_to_date_range(
        support.date_to_unix_weeks_timestamp(history_date)
    )
    begin_of_history_week = history_week_date_range.min_range
    end_of_current_week = current_week_date_range.max_range

    current_day = support.calc_day(current_date)
    predicted_day = support.calc_day(predicted_date)
    applicable_days = support.difference_of_days(current_day, predicted_day)

    con = dbcc.retrieve_connection(db_layout.MPFetched.database_name)
    select = "*"
    table = db_layout.MPFetched.table_name
    layout_dict = dict(db_layout.MPFetched.layout)
    user_id_sql = layout_dict["user_id"].format_to_db_value(user_id)
    end_date_sql = layout_dict["date"].format_to_db_value(end_of_current_week)
    start_date_sql = layout_dict["date"].format_to_db_value(begin_of_history_week)
    applicable_days_sql = map(layout_dict["day"].format_to_db_value, applicable_days)

    applicable_days_query = " AND (day = " + " OR day = ".join(applicable_days_sql) + ")"

    where_query = (
        "WHERE user_id = "
        + user_id_sql
        + " AND date <= "
        + end_date_sql
        + " AND date >= "
        + start_date_sql
        + applicable_days_query
    )

    err.log_error(err.INFO, "Retrieving data for MP between " + str(history_date) + " and " + str(current_date))
    mysql_rows = con.retrieve(select, table, where_query)

    result = []
    for mysql_row in mysql_rows:
        result.append(db_layout.MPFetched.object_from_mysql_row(dbcc, mysql_row))

    return result
Пример #2
0
def retrieve_user_mobility_data_single_user(dbcc, user_id, current_date,
                                            predicted_date):
    history_date = calculate_mp_history_date(current_date)

    current_week_date_range = support.unix_weeks_timestamp_to_date_range(
        support.date_to_unix_weeks_timestamp(current_date))
    history_week_date_range = support.unix_weeks_timestamp_to_date_range(
        support.date_to_unix_weeks_timestamp(history_date))
    begin_of_history_week = history_week_date_range.min_range
    end_of_current_week = current_week_date_range.max_range

    current_day = support.calc_day(current_date)
    predicted_day = support.calc_day(predicted_date)
    applicable_days = support.difference_of_days(current_day, predicted_day)

    con = dbcc.retrieve_connection(db_layout.MPFetched.database_name)
    select = "*"
    table = db_layout.MPFetched.table_name
    layout_dict = dict(db_layout.MPFetched.layout)
    user_id_sql = layout_dict["user_id"].format_to_db_value(user_id)
    end_date_sql = layout_dict["date"].format_to_db_value(end_of_current_week)
    start_date_sql = layout_dict["date"].format_to_db_value(
        begin_of_history_week)
    applicable_days_sql = map(layout_dict["day"].format_to_db_value,
                              applicable_days)

    applicable_days_query = " AND (day = " + " OR day = ".join(
        applicable_days_sql) + ")"

    where_query = ("WHERE user_id = " + user_id_sql + " AND date <= " +
                   end_date_sql + " AND date >= " + start_date_sql +
                   applicable_days_query)

    err.log_error(
        err.INFO, "Retrieving data for MP between " + str(history_date) +
        " and " + str(current_date))
    mysql_rows = con.retrieve(select, table, where_query)

    result = []
    for mysql_row in mysql_rows:
        result.append(
            db_layout.MPFetched.object_from_mysql_row(dbcc, mysql_row))

    return result
def check_user_mobility_data_single_user_integrity(grouped_by_date_grid_by_minute_of_day_mp_fetched, current_date):
    missing_ranges_date_time_date_time = []

    current_unix_days = support.date_to_unix_days_timestamp(current_date)
    history_unix_days = current_unix_days - config.MP_USE_DATA_FROM_LAST_DAYS
    current_unix_week = int(current_unix_days / common_config.DAYS_IN_A_WEEK)
    history_unix_week = int(history_unix_days / common_config.DAYS_IN_A_WEEK)

    # Find missing weeks
    all_weeks = map(
        lambda x: support.date_to_unix_weeks_timestamp(x), grouped_by_date_grid_by_minute_of_day_mp_fetched.keys()
    )

    missing_ranges_unix_weeks = support.reduce_to_missing_ranges(
        all_weeks, lambda i: i, 1, history_unix_week, current_unix_week
    )

    missing_ranges_date = map(
        lambda x: support.Range(
            support.unix_weeks_timestamp_to_date_range(x.min_range).min_range,
            support.unix_weeks_timestamp_to_date_range(x.max_range).max_range,
            support.Range.RESOLUTION_DATETIME_DAY,
        ),
        missing_ranges_unix_weeks,
    )

    # Add the missing days
    begin_of_day_time = datetime.time(0, 0, 0)
    end_of_day_time = datetime.time(23, 59, 59)
    for missing_range_date in missing_ranges_date:
        start_date_time = datetime.datetime.combine(missing_range_date.min_range, begin_of_day_time)
        end_date_time = datetime.datetime.combine(missing_range_date.max_range, end_of_day_time)

        missing_ranges_date_time_date_time.append(
            support.Range(start_date_time, end_date_time, support.Range.RESOLUTION_DATETIME_MINUTE)
        )

    # Calculate the missing time ranges in each day that already exists
    for date, grid_by_minute_of_day_mp_fetched in grouped_by_date_grid_by_minute_of_day_mp_fetched.items():
        missing_ranges_in_minutes = support.reduce_to_missing_ranges_already_grid(
            grid_by_minute_of_day_mp_fetched, 1, 0, common_config.MINUTES_IN_A_DAY - 1
        )

        missing_ranges_in_time = map(
            lambda missing_range: support.Range(
                support.calc_time_from_minute_of_day(missing_range.min_range),
                support.calc_time_from_minute_of_day(missing_range.max_range),
                support.Range.RESOLUTION_DATETIME_SECOND,
            ),
            missing_ranges_in_minutes,
        )

        # Add the missing times for a certain day
        for missing_range_in_time in missing_ranges_in_time:
            start_date_time = datetime.datetime.combine(date, missing_range_in_time.min_range)
            end_date_time = datetime.datetime.combine(date, missing_range_in_time.max_range)

            missing_ranges_date_time_date_time.append(
                support.Range(start_date_time, end_date_time, support.Range.RESOLUTION_DATETIME_MINUTE)
            )

    return missing_ranges_date_time_date_time
Пример #4
0
def check_user_mobility_data_single_user_integrity(
        grouped_by_date_grid_by_minute_of_day_mp_fetched, current_date):
    missing_ranges_date_time_date_time = []

    current_unix_days = support.date_to_unix_days_timestamp(current_date)
    history_unix_days = current_unix_days - config.MP_USE_DATA_FROM_LAST_DAYS
    current_unix_week = int(current_unix_days / common_config.DAYS_IN_A_WEEK)
    history_unix_week = int(history_unix_days / common_config.DAYS_IN_A_WEEK)

    #Find missing weeks
    all_weeks = map(lambda x: support.date_to_unix_weeks_timestamp(x),
                    grouped_by_date_grid_by_minute_of_day_mp_fetched.keys())

    missing_ranges_unix_weeks = support.reduce_to_missing_ranges(
        all_weeks, lambda i: i, 1, history_unix_week, current_unix_week)

    missing_ranges_date = map(
        lambda x: support.Range(
            support.unix_weeks_timestamp_to_date_range(x.min_range).min_range,
            support.unix_weeks_timestamp_to_date_range(x.max_range).max_range,
            support.Range.RESOLUTION_DATETIME_DAY), missing_ranges_unix_weeks)

    #Add the missing days
    begin_of_day_time = datetime.time(0, 0, 0)
    end_of_day_time = datetime.time(23, 59, 59)
    for missing_range_date in missing_ranges_date:
        start_date_time = datetime.datetime.combine(
            missing_range_date.min_range, begin_of_day_time)
        end_date_time = datetime.datetime.combine(missing_range_date.max_range,
                                                  end_of_day_time)

        missing_ranges_date_time_date_time.append(
            support.Range(start_date_time, end_date_time,
                          support.Range.RESOLUTION_DATETIME_MINUTE))

    #Calculate the missing time ranges in each day that already exists
    for date, grid_by_minute_of_day_mp_fetched in grouped_by_date_grid_by_minute_of_day_mp_fetched.items(
    ):
        missing_ranges_in_minutes = support.reduce_to_missing_ranges_already_grid(
            grid_by_minute_of_day_mp_fetched, 1, 0,
            common_config.MINUTES_IN_A_DAY - 1)

        missing_ranges_in_time = map(
            lambda missing_range: support.Range(
                support.calc_time_from_minute_of_day(missing_range.min_range),
                support.calc_time_from_minute_of_day(missing_range.max_range),
                support.Range.RESOLUTION_DATETIME_SECOND),
            missing_ranges_in_minutes)

        #Add the missing times for a certain day
        for missing_range_in_time in missing_ranges_in_time:
            start_date_time = datetime.datetime.combine(
                date, missing_range_in_time.min_range)
            end_date_time = datetime.datetime.combine(
                date, missing_range_in_time.max_range)

            missing_ranges_date_time_date_time.append(
                support.Range(start_date_time, end_date_time,
                              support.Range.RESOLUTION_DATETIME_MINUTE))

    return missing_ranges_date_time_date_time