-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathappstats_client.py
53 lines (43 loc) · 1.47 KB
/
appstats_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import json
import logging
import threading
from time import time
from collections import defaultdict, Counter
from requests import Session, RequestException
log = logging.getLogger(__name__)
lock = threading.Lock()
class AppStatsClient(object):
limit = 100 # records
interval = 60 # seconds
timeout = 1 # timeout in seconds to submit data
def __init__(self, url, app_id):
self.url = url
self.app_id = app_id
self._acc = defaultdict(Counter)
self._last_sent = time()
self._session = Session(
headers = {'Content-Type': 'application/json'},
timeout = self.timeout,
)
self._req_count = 0
def add(self, name, counts):
with lock:
self._acc[name]['NUMBER'] += 1
for counter in counts:
self._acc[name][counter] += counts[counter]
self._req_count += 1
elapsed = time() - self._last_sent
if elapsed >= self.interval or self._req_count >= self.limit:
self.submit()
def submit(self):
data = json.dumps({self.app_id: self._acc})
try:
self._session.post(self.url, data=data)
except RequestException, e:
log.debug('Error during data submission: %s' % e)
else:
log.debug('Successfully submitted app stats')
finally:
self._last_sent = time()
self._acc = defaultdict(Counter)
self._req_count = 0