@@ -66,6 +66,8 @@ def __init__(self, redis_client):
6666 :type telemtry_http_client: splitio.api.telemetry.TelemetryAPI
6767 """
6868 self ._redis_client = redis_client
69+ self .pipe = self ._redis_client .pipeline ()
70+
6971
7072 def record_unique_keys (self , uniques ):
7173 """
@@ -88,13 +90,17 @@ def flush_counters(self, to_send):
8890 """
8991 post the impression counters to redis.
9092
91- :param uniques : unique keys disctionary
92- :type uniques : Dictionary {'feature1': set(), 'feature2': set(), .. }
93+ :param to_send : unique keys disctionary
94+ :type to_send : Dictionary {'feature1': set(), 'feature2': set(), .. }
9395 """
94- bulk_counts = self ._build_counters (to_send )
9596 try :
96- inserted = self ._redis_client .rpush (self .IMP_COUNT_QUEUE_KEY , * bulk_counts )
97- self ._expire_keys (self .IMP_COUNT_QUEUE_KEY , self .IMP_COUNT_KEY_DEFAULT_TTL , inserted , len (bulk_counts ))
97+ resulted = 0
98+ counted = 0
99+ for pf_count in to_send :
100+ self .pipe .hincrby (self .IMP_COUNT_QUEUE_KEY , pf_count .feature + "::" + str (pf_count .timeframe ), pf_count .count )
101+ counted += pf_count .count
102+ resulted = sum (self .pipe .execute ())
103+ self ._expire_keys (self .IMP_COUNT_QUEUE_KEY , self .IMP_COUNT_KEY_DEFAULT_TTL , resulted , counted )
98104 return True
99105 except RedisAdapterException :
100106 _LOGGER .error ('Something went wrong when trying to add counters to redis' )
@@ -124,23 +130,3 @@ def _uniques_formatter(self, uniques):
124130 :rtype: json
125131 """
126132 return [json .dumps ({'f' : feature , 'ks' : list (keys )}) for feature , keys in uniques .items ()]
127-
128- def _build_counters (self , counters ):
129- """
130- Build an impression bulk formatted as the API expects it.
131-
132- :param counters: List of impression counters per feature.
133- :type counters: list[splitio.engine.impressions.Counter.CountPerFeature]
134-
135- :return: dict with list of impression count dtos
136- :rtype: dict
137- """
138- return json .dumps ({
139- 'pf' : [
140- {
141- 'f' : pf_count .feature ,
142- 'm' : pf_count .timeframe ,
143- 'rc' : pf_count .count
144- } for pf_count in counters
145- ]
146- })
0 commit comments