Skip to content

Commit a57c841

Browse files
fix ZeroDivisionError when no records exist to push
1 parent 8e17271 commit a57c841

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

stitchclient/client.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def moving_average_bytes_per_record(self):
112112
num_records += stats.num_records
113113
num_bytes += stats.num_bytes
114114

115-
return num_bytes // num_records
115+
return num_bytes // num_records if num_records else None
116116

117117
def push(self, message, callback_arg=None):
118118
"""message should be a dict recognized by the Stitch Import API.
@@ -155,9 +155,12 @@ def _send_batch(self, batch):
155155
for body, callback_args in partition_batch(batch, self.max_batch_size_bytes):
156156
self._send(body, callback_args)
157157

158-
self.target_messages_per_batch = min(self.max_messages_per_batch,
159-
0.8 * (self.max_batch_size_bytes / self.moving_average_bytes_per_record()))
160-
158+
if self.moving_average_bytes_per_record():
159+
self.target_messages_per_batch = \
160+
min(self.max_messages_per_batch,
161+
0.8 * (self.max_batch_size_bytes / self.moving_average_bytes_per_record()))
162+
else:
163+
self.target_messages_per_batch = self.max_messages_per_batch
161164

162165
def _stitch_request(self, body):
163166
headers = {'Authorization': 'Bearer {}'.format(self.token),

0 commit comments

Comments
 (0)