@@ -74,7 +74,7 @@ def record_unique_keys(self, uniques):
7474 :param uniques: unique keys disctionary
7575 :type uniques: Dictionary {'feature1': set(), 'feature2': set(), .. }
7676 """
77- bulk_mtks = self . _uniques_formatter (uniques )
77+ bulk_mtks = _uniques_formatter (uniques )
7878 try :
7979 inserted = self ._redis_client .rpush (self .MTK_QUEUE_KEY , * bulk_mtks )
8080 self ._expire_keys (self .MTK_QUEUE_KEY , self .MTK_KEY_DEFAULT_TTL , inserted , len (bulk_mtks ))
@@ -119,14 +119,82 @@ def _expire_keys(self, queue_key, key_default_ttl, total_keys, inserted):
119119 if total_keys == inserted :
120120 self ._redis_client .expire (queue_key , key_default_ttl )
121121
122- def _uniques_formatter (self , uniques ):
122+ class PluggableSenderAdapter (ImpressionsSenderAdapter ):
123+ """In Memory Impressions Sender Adapter class."""
124+
125+ MTK_QUEUE_KEY = 'SPLITIO.uniquekeys'
126+ MTK_KEY_DEFAULT_TTL = 3600
127+ IMP_COUNT_QUEUE_KEY = 'SPLITIO.impressions.count'
128+ IMP_COUNT_KEY_DEFAULT_TTL = 3600
129+
130+ def __init__ (self , adapter_client , prefix = None ):
123131 """
124- Format the unique keys dictionary array to a JSON body
132+ Initialize pluggable sender adapter instance
133+
134+ :param telemtry_http_client: instance of telemetry http api
135+ :type telemtry_http_client: splitio.api.telemetry.TelemetryAPI
136+ """
137+ self ._adapter_client = adapter_client
138+ self ._prefix = ""
139+ if prefix is not None :
140+ self ._prefix = prefix + "."
141+
142+ def record_unique_keys (self , uniques ):
143+ """
144+ post the unique keys to storage.
125145
126146 :param uniques: unique keys disctionary
127147 :type uniques: Dictionary {'feature1': set(), 'feature2': set(), .. }
148+ """
149+ bulk_mtks = _uniques_formatter (uniques )
150+ try :
151+ inserted = self ._adapter_client .push_items (self .MTK_QUEUE_KEY , * bulk_mtks )
152+ self ._expire_keys (self ._prefix + self .MTK_QUEUE_KEY , self .MTK_KEY_DEFAULT_TTL , inserted , len (bulk_mtks ))
153+ return True
154+ except RedisAdapterException :
155+ _LOGGER .error ('Something went wrong when trying to add mtks to storage adapter' )
156+ _LOGGER .error ('Error: ' , exc_info = True )
157+ return False
128158
129- :return: unique keys JSON array
130- :rtype: json
159+ def flush_counters (self , to_send ):
131160 """
132- return [json .dumps ({'f' : feature , 'ks' : list (keys )}) for feature , keys in uniques .items ()]
161+ post the impression counters to storage.
162+
163+ :param to_send: unique keys disctionary
164+ :type to_send: Dictionary {'feature1': set(), 'feature2': set(), .. }
165+ """
166+ try :
167+ resulted = 0
168+ for pf_count in to_send :
169+ resulted = self ._adapter_client .increment (self ._prefix + self .IMP_COUNT_QUEUE_KEY + "." + pf_count .feature + "::" + str (pf_count .timeframe ), pf_count .count )
170+ self ._expire_keys (self ._prefix + self .IMP_COUNT_QUEUE_KEY + "." + pf_count .feature + "::" + str (pf_count .timeframe ),
171+ self .IMP_COUNT_KEY_DEFAULT_TTL , resulted , pf_count .count )
172+ return True
173+ except RedisAdapterException :
174+ _LOGGER .error ('Something went wrong when trying to add counters to storage adapter' )
175+ _LOGGER .error ('Error: ' , exc_info = True )
176+ return False
177+
178+ def _expire_keys (self , queue_key , key_default_ttl , total_keys , inserted ):
179+ """
180+ Set expire
181+
182+ :param total_keys: length of keys.
183+ :type total_keys: int
184+ :param inserted: added keys.
185+ :type inserted: int
186+ """
187+ if total_keys == inserted :
188+ self ._adapter_client .expire (queue_key , key_default_ttl )
189+
190+ def _uniques_formatter (uniques ):
191+ """
192+ Format the unique keys dictionary array to a JSON body
193+
194+ :param uniques: unique keys disctionary
195+ :type uniques: Dictionary {'feature1': set(), 'feature2': set(), .. }
196+
197+ :return: unique keys JSON array
198+ :rtype: json
199+ """
200+ return [json .dumps ({'f' : feature , 'ks' : list (keys )}) for feature , keys in uniques .items ()]
0 commit comments