You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.5 KiB

from __future__ import absolute_import
from util.constants import (
DAYS_DIFFERENCE_THRESHOLD,
LENS_DIFFERENCE_THRESHOLD,
SECONDS_PER_DAY,
PART_OR_WHOLE,
DRIFT_TYPE,
START_TIME,
END_TIME
)
from logger import get_logger
logger = get_logger(__name__)
def compare_drift(existing_drift_dict, new_drift_dict):
"""
Compare two drift dictionaries to determine if they are considered the same based on defined rules.
Parameters:
existing_drift_dict (Dictionary): The existing drift segment containing drift information, defined in util/csc_output.py.
This function will use the following attributes.
{
PART_OR_WHOLE (str): Enum [1: whole drift, 2: part drift (accumulated drift)],
DRIFT_TYPE (str): Enum ["LEVEL", "TREND"],
START_TIME (int): we assume this field is always epoch time,
END_TIME (int): we assume this field is always epoch time,
}
new_drift_dict (Dictionary): The new detected drift containing drift information.
The attributes is same as existing_drift_dict.
Returns:
bool: True if the drifts are considered the same, False otherwise.
"""
# Rule-based comparisons
if existing_drift_dict[DRIFT_TYPE] != new_drift_dict[DRIFT_TYPE]:
logger.debug(f"Type mismatch: {existing_drift_dict[DRIFT_TYPE]} != {new_drift_dict[DRIFT_TYPE]}")
return False
if existing_drift_dict[PART_OR_WHOLE] != new_drift_dict[PART_OR_WHOLE]:
info = f"Finished drift mismatch: {existing_drift_dict[PART_OR_WHOLE]} != {new_drift_dict[PART_OR_WHOLE]}"
logger.debug(info)
return False
start_day_diff = abs((existing_drift_dict[START_TIME] - new_drift_dict[START_TIME]) / SECONDS_PER_DAY)
if start_day_diff > DAYS_DIFFERENCE_THRESHOLD:
info = f"Start time difference is larger than threshold: {existing_drift_dict[START_TIME]} and {new_drift_dict[START_TIME]}"
logger.debug(info)
return False
existing_diff_days = int((existing_drift_dict[END_TIME] - existing_drift_dict[START_TIME]) / SECONDS_PER_DAY)
new_diff_days = int((new_drift_dict[END_TIME] - new_drift_dict[START_TIME]) / SECONDS_PER_DAY)
lens_diff = abs(existing_diff_days - new_diff_days)
if lens_diff > LENS_DIFFERENCE_THRESHOLD:
logger.debug(f"Length difference is larger than threshold: {lens_diff}")
return False
logger.debug(f"Drift {existing_drift_dict} and {new_drift_dict} matches")
return True