<commit_before>import json
import logging
import threading
from time import time

from requests import Session, RequestException


log = logging.getLogger(__name__)

lock = threading.Lock()


class AppStatsClient(object):
    limit = 100 # records
    interval = 100 # seconds
    timeout = 1 # timeout in seconds to submit data

    def __init__(self, app_id, url):
        self.url = url
        self.app_id = app_id
        self._acc = {}
        self._last_sent = time()
        self._session = Session(
            headers = {'Content-Type': 'application/json'},
            timeout = self.timeout,
        )

    def add(self, name, counts):
        with lock:
            acc = self._acc.setdefault(name, {'NUMBER': 0})
            acc['NUMBER'] += 1
            for counter in counts:
                acc.setdefault(counter, 0)
                acc[counter] += counts[counter]

            elapsed = time() - self._last_sent
            if elapsed >= self.interval or len(self._acc) >= self.limit:
                self.submit()

    def submit(self):
        data = {self.app_id: self._acc}
        data = json.dumps(data)
        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 = {}<commit_msg>Copied client from uaprom<commit_after>import json
import logging
import threading
from time import time

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 = {}
        self._last_sent = time()
        self._session = Session(
            headers = {'Content-Type': 'application/json'},
            timeout = self.timeout,
        )

    def add(self, name, counts):
        with lock:
            acc = self._acc.setdefault(name, {'NUMBER': 0})
            acc['NUMBER'] += 1
            for counter in counts:
                acc.setdefault(counter, 0)
                acc[counter] += counts[counter]

            elapsed = time() - self._last_sent
            if elapsed >= self.interval or len(self._acc) >= 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 = {}
{'fraction_non_alphanumeric': 0.10567986230636833, 'fraction_numerical': 0.006540447504302926, 'mean_word_length': 1.9323915237134208, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2802522', 'n_tokens_mistral': 825, 'n_tokens_neox': 787, 'n_words': 223}