|
| 1 | +import abc |
| 2 | + |
| 3 | +from splitio.engine.manager import Observer, truncate_impressions_time, Counter, truncate_time |
| 4 | +from splitio.engine.unique_keys_tracker import UniqueKeysTracker |
| 5 | +from splitio import util |
| 6 | + |
| 7 | +_IMPRESSION_OBSERVER_CACHE_SIZE = 500000 |
| 8 | +_UNIQUE_KEYS_CACHE_SIZE = 30000 |
| 9 | + |
| 10 | +class BaseStrategy(object, metaclass=abc.ABCMeta): |
| 11 | + """Strategy interface.""" |
| 12 | + |
| 13 | + @abc.abstractmethod |
| 14 | + def process_impressions(self): |
| 15 | + """ |
| 16 | + Return a list(impressions) object |
| 17 | +
|
| 18 | + """ |
| 19 | + pass |
| 20 | + |
| 21 | +class StrategyDebugMode(BaseStrategy): |
| 22 | + """Debug mode strategy.""" |
| 23 | + |
| 24 | + def __init__(self): |
| 25 | + """ |
| 26 | + Construct a strategy instance for debug mode. |
| 27 | +
|
| 28 | + """ |
| 29 | + self._observer = Observer(_IMPRESSION_OBSERVER_CACHE_SIZE) |
| 30 | + |
| 31 | + def process_impressions(self, impressions): |
| 32 | + """ |
| 33 | + Process impressions. |
| 34 | +
|
| 35 | + Impressions are analyzed to see if they've been seen before. |
| 36 | +
|
| 37 | + :param impressions: List of impression objects with attributes |
| 38 | + :type impressions: list[tuple[splitio.models.impression.Impression, dict]] |
| 39 | +
|
| 40 | + :returns: Observed list of impressions |
| 41 | + :rtype: list[tuple[splitio.models.impression.Impression, dict]] |
| 42 | + """ |
| 43 | + imps = [(self._observer.test_and_set(imp), attrs) for imp, attrs in impressions] |
| 44 | + return [i for i, _ in imps], imps |
| 45 | + |
| 46 | +class StrategyNoneMode(BaseStrategy): |
| 47 | + """Debug mode strategy.""" |
| 48 | + |
| 49 | + def __init__(self, counter): |
| 50 | + """ |
| 51 | + Construct a strategy instance for none mode. |
| 52 | +
|
| 53 | + """ |
| 54 | + self._counter = counter |
| 55 | + self._unique_keys_tracker = UniqueKeysTracker(_UNIQUE_KEYS_CACHE_SIZE) |
| 56 | + |
| 57 | + def process_impressions(self, impressions): |
| 58 | + """ |
| 59 | + Process impressions. |
| 60 | +
|
| 61 | + Impressions are analyzed to see if they've been seen before and counted. |
| 62 | + Unique keys tracking are updated. |
| 63 | +
|
| 64 | + :param impressions: List of impression objects with attributes |
| 65 | + :type impressions: list[tuple[splitio.models.impression.Impression, dict]] |
| 66 | +
|
| 67 | + :returns: Empty list, no impressions to post |
| 68 | + :rtype: list[] |
| 69 | + """ |
| 70 | + self._counter.track([imp for imp, _ in impressions]) |
| 71 | + for i, _ in impressions: |
| 72 | + self._unique_keys_tracker.track(i.matching_key, i.feature_name) |
| 73 | + return [], impressions |
| 74 | + |
| 75 | + def get_unique_keys_tracker(self): |
| 76 | + return self._unique_keys_tracker |
| 77 | + |
| 78 | +class StrategyOptimizedMode(BaseStrategy): |
| 79 | + """Optimized mode strategy.""" |
| 80 | + |
| 81 | + def __init__(self, counter): |
| 82 | + """ |
| 83 | + Construct a strategy instance for optimized mode. |
| 84 | +
|
| 85 | + """ |
| 86 | + self._observer = Observer(_IMPRESSION_OBSERVER_CACHE_SIZE) |
| 87 | + self._counter = counter |
| 88 | + |
| 89 | + def process_impressions(self, impressions): |
| 90 | + """ |
| 91 | + Process impressions. |
| 92 | +
|
| 93 | + Impressions are analyzed to see if they've been seen before and counted. |
| 94 | +
|
| 95 | + :param impressions: List of impression objects with attributes |
| 96 | + :type impressions: list[tuple[splitio.models.impression.Impression, dict]] |
| 97 | +
|
| 98 | + :returns: Observed list of impressions |
| 99 | + :rtype: list[tuple[splitio.models.impression.Impression, dict]] |
| 100 | + """ |
| 101 | + imps = [(self._observer.test_and_set(imp), attrs) for imp, attrs in impressions] |
| 102 | + self._counter.track([imp for imp, _ in imps]) |
| 103 | + this_hour = truncate_time(util.utctime_ms()) |
| 104 | + return [i for i, _ in imps if i.previous_time is None or i.previous_time < this_hour], imps |
0 commit comments