<commit_before>from threading import Thread, Event

import requests
import time
import logging

logger = logging.getLogger(__name__)


class PandoraStatsPoller(Thread):
    def __init__(self, port):
        super(PandoraStatsPoller, self).__init__()
        self._stop = Event()
        self.buffer = []
        self.port = port

    def run(self):
        last_ts = int(time.time() - 1)

        while not self._stop.is_set():
            curr_ts = int(time.time())
            if curr_ts > last_ts:
                last_ts = curr_ts
                try:
                    pandora_stat = requests.get(
                        "http://localhost:{port}/debug/vars".format(port=self.port), timeout=0.9
                    ).json()
                    data = {
                        'ts': last_ts - 1,
                        'metrics': {
                            'instances': pandora_stat.get("engine_ActiveRequests"),
                            'reqps': pandora_stat.get("engine_ReqPS"),
                        }
                    }
                except (requests.ConnectionError, requests.HTTPError, requests.exceptions.Timeout):
                    logger.debug("Pandora expvar http interface is unavailable", exc_info=True)
                    data = {
                        'ts': last_ts - 1,
                        'metrics': {<commit_msg>try to avoid crashing on available pandora<commit_after>from threading import Thread, Event

import requests
import time
import logging

logger = logging.getLogger(__name__)


class PandoraStatsPoller(Thread):
    def __init__(self, port):
        super(PandoraStatsPoller, self).__init__()
        self._stop = Event()
        self.buffer = []
        self.port = port

    def run(self):
        last_ts = int(time.time() - 1)

        while not self._stop.is_set():
            curr_ts = int(time.time())
            if curr_ts > last_ts:
                last_ts = curr_ts
                try:
                    pandora_stat = requests.get(
                        "http://localhost:{port}/debug/vars".format(port=self.port), timeout=0.9
                    ).json()
                    data = {
                        'ts': last_ts - 1,
                        'metrics': {
                            'instances': pandora_stat.get("engine_ActiveRequests"),
                            'reqps': pandora_stat.get("engine_ReqPS"),
                        }
                    }
                except (requests.ConnectionError, requests.HTTPError, requests.exceptions.Timeout, ValueError):
                    logger.debug("Pandora expvar http interface is unavailable", exc_info=True)
                    data = {
                        'ts': last_ts - 1,
                        'metrics': {
{'fraction_non_alphanumeric': 0.10112359550561797, 'fraction_numerical': 0.003624501631025734, 'mean_word_length': 1.391681109185442, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4807083', 'n_tokens_mistral': 737, 'n_tokens_neox': 704, 'n_words': 149}