<commit_before>import threading
import psutil
import time

from dcm.agent import exceptions
import dcm.agent.utils as agent_util


class SystemStats(object):
    def __init__(self, name, hold_count, check_interval):
        self.interval = check_interval
        self.hold_count = hold_count
        self.name = name
        self._done = False
        self._stat_values = []
        self._cond = threading.Condition()
        self._t = threading.Thread(target=self.run)
        self._t.start()

    def run(self):
        while not self._done:
            self.poll()

    # this should only be called from below wait(), and thus locked
    def add_value(self, v):
        self._cond.acquire()
        try:
            self._stat_values.append(v)
            if len(self._stat_values) > self.hold_count:
                self._stat_values = self._stat_values[-self.hold_count:]
        finally:
            self._cond.release()

    def poll(self):
        self._cond.acquire()
        try:
            self._cond.wait(self.interval)
        finally:
            self._cond.release()

    def stop(self):
        self._cond.acquire()
        try:
            self._done = True
            self._cond.notify()
        finally:
            self._cond.release()
        self._t.join()

    def get_stats(self):
        return {'status': self._stat_values[:]}

    @agent_util.not_implemented_decorator
    def get_stats_type(self):
        pass


class CpuIdleSystemStats(SystemStats):
    def __init__(self, name, hold_count, check_interval):
        super(CpuIdleSystemStats, self).__init__(
            name, hold_count, check_interval)

    def poll(self):
        load = psutil.cpu_percent(self.interval)
        timestamp = time.time()
        self.add_value({'idle': load, 'timestamp': timestamp})

    def get_stats_type(self):
        return "cpu_idle_stat_array"


_g_stat_object_map = {
    "cpu-idle": CpuIdleSystemStats
}


_g_active_stats = {}
_g_lock = threading.Lock()


def start_new_system_stat(name, stat_type, hold_count, check_interval):
    if stat_type not in _g_stat_object_map:
        raise exceptions.AgentOptionValueException(
            "stat_type", stat_type, str(_g_stat_object_map.keys()))

    _g_lock.acquire()
    try:
        if name in _g_active_stats:
            raise exceptions.AgentOptionValueAlreadySetException(name)

        cls = _g_stat_object_map[stat_type]
        stat_obj = cls(name, hold_count, check_interval)
        _g_active_stats[name] = stat_obj
    finally:<commit_msg>Fixing the idle time reporting.<commit_after>import threading
import psutil
import time

from dcm.agent import exceptions
import dcm.agent.utils as agent_util


class SystemStats(object):
    def __init__(self, name, hold_count, check_interval):
        self.interval = check_interval
        self.hold_count = hold_count
        self.name = name
        self._done = False
        self._stat_values = []
        self._cond = threading.Condition()
        self._t = threading.Thread(target=self.run)
        self._t.start()

    def run(self):
        while not self._done:
            self.poll()

    # this should only be called from below wait(), and thus locked
    def add_value(self, v):
        self._cond.acquire()
        try:
            self._stat_values.append(v)
            if len(self._stat_values) > self.hold_count:
                self._stat_values = self._stat_values[-self.hold_count:]
        finally:
            self._cond.release()

    def poll(self):
        self._cond.acquire()
        try:
            self._cond.wait(self.interval)
        finally:
            self._cond.release()

    def stop(self):
        self._cond.acquire()
        try:
            self._done = True
            self._cond.notify()
        finally:
            self._cond.release()
        self._t.join()

    def get_stats(self):
        return {'status': self._stat_values[:]}

    @agent_util.not_implemented_decorator
    def get_stats_type(self):
        pass


class CpuIdleSystemStats(SystemStats):
    def __init__(self, name, hold_count, check_interval):
        super(CpuIdleSystemStats, self).__init__(
            name, hold_count, check_interval)

    def poll(self):
        load = psutil.cpu_percent(self.interval)
        timestamp = time.time()
        self.add_value({'timestamp': timestamp,
                        'cpu-idle': 1.0 - load})

    def get_stats_type(self):
        return "cpu_idle_stat_array"


_g_stat_object_map = {
    "cpu-idle": CpuIdleSystemStats
}


_g_active_stats = {}
_g_lock = threading.Lock()


def start_new_system_stat(name, stat_type, hold_count, check_interval):
    if stat_type not in _g_stat_object_map:
        raise exceptions.AgentOptionValueException(
            "stat_type", stat_type, str(_g_stat_object_map.keys()))

    _g_lock.acquire()
    try:
        if name in _g_active_stats:
            raise exceptions.AgentOptionValueAlreadySetException(name)

        cls = _g_stat_object_map[stat_type]
        stat_obj = cls(name, hold_count, check_interval)
        _g_active_stats[name] = stat_obj
    finally:
{'fraction_non_alphanumeric': 0.0964327714621717, 'fraction_numerical': 0.0003920031360250882, 'mean_word_length': 2.4042695130086726, 'pattern_counts': {'":': 2, '<': 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': '7459761', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1553, 'n_words': 314}