<commit_before># -*- coding: utf-8 -*-
#
# Author: <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import threading
import time

from kwapi.utils import cfg, log

LOG = log.getLogger(__name__)

collector_opts = [
    cfg.BoolOpt('signature_checking',
                required=True,
                ),
    cfg.IntOpt('cleaning_interval',
               required=True,
               ),
    cfg.MultiStrOpt('probes_endpoint',
                    required=True,
                    ),
    cfg.MultiStrOpt('watch_probe',
                    required=False,
                    ),
    cfg.StrOpt('driver_metering_secret',
               required=True,
               ),
]

cfg.CONF.register_opts(collector_opts)


class Record(dict):
    """Contains fields (timestamp, kwh, w) and a method to update
    consumption.

    """

    def __init__(self, timestamp, kwh, watts):
        """Initializes fields with the given arguments."""
        dict.__init__(self)
        self._dict = {}
        self['timestamp'] = timestamp
        self['kwh'] = kwh
        self['w'] = watts

    def add(self, watts):
        """Updates fields with consumption data."""
        currentTime = time.time()
        self['kwh'] += (currentTime - self['timestamp']) / 3600.0 * \
                       (watts / 1000.0)
        self['w'] = watts
        self['timestamp'] = currentTime


class Collector:
    """Collector gradually fills its database with received values from
    wattmeter drivers.

    """

    def __init__(self):
        """Initializes an empty database and start listening the endpoint."""
        LOG.info('Starting Collector')
        self.database = {}
        self.lock = threading.Lock()

    def add(self, probe, watts):
        """Creates (or updates) consumption data for this probe."""
        self.lock.acquire()
        if probe in self.database.keys():
            self.database[probe].add(watts)
        else:
            record = Record(timestamp=time.time(), kwh=0.0, watts=watts)
            self.database[probe] = record
        self.lock.release()

    def remove(self, probe):
        """Removes this probe from database."""
        self.lock.acquire()
        try:
            del self.database[probe]
            return True
        except KeyError:<commit_msg>api with network measures<commit_after># -*- coding: utf-8 -*-
#
# Author: <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import threading
import time

from kwapi.utils import cfg, log

LOG = log.getLogger(__name__)

collector_opts = [
    cfg.BoolOpt('signature_checking',
                required=True,
                ),
    cfg.IntOpt('cleaning_interval',
               required=True,
               ),
    cfg.MultiStrOpt('probes_endpoint',
                    required=True,
                    ),
    cfg.MultiStrOpt('watch_probe',
                    required=False,
                    ),
    cfg.StrOpt('driver_metering_secret',
               required=True,
               ),
]

cfg.CONF.register_opts(collector_opts)


class Record(dict):
    """Contains fields (timestamp, kwh, w) and a method to update
    consumption.

    """

    def __init__(self, timestamp, measure, data_type, params, avg):
        """Initializes fields with the given arguments."""
        dict.__init__(self)
        self._dict = {}
        self['timestamp'] = timestamp
        self['data_type'] = data_type
        if data_type == "ifOctets":
            self['measure'] = {}
            if not params['flow'] in self['measure']:
                self['measure'][params['flow']] = {}
            if not params['dest'] in self['measure'][params['flow']]:
                self['measure'][params['flow']][params['dest']] = {}
            self['measure'][params['flow']][params['dest']]['value'] = measure
            #self['measure'][params['flow']][params['dest']]['avg'] = avg
        else:
            self['avg'] = avg
            self['measure'] = measure
        

    def add(self, timestamp, measure, params):
        """Updates fields with consumption data."""
        currentTime = timestamp 
        if self['data_type'] == "ifOctets":
            if not params['flow'] in self['measure']:
                self['measure'][params['flow']] = {}
            if not params['dest'] in self['measure'][params['flow']]:
                self['measure'][params['flow']][params['dest']] = {}
            if not 'value' in self['measure'][params['flow']][params['dest']]:
                self['measure'][params['flow']][params['dest']]['value'] = 0
            #if not 'avg' in self['measure'][params['flow']][params['dest']]:
            #    self['measure'][params['flow']][params['dest']]['avg'] = 0.0
            #delta = measure - self['measure'][params['flow']][params['dest']]['value']
            self['measure'][params['flow']][params['dest']]['value'] = measure
            #self['measure'][params['flow']][params['dest']]['avg'] += (currentTime - self['timestamp']) / 3600.0 * (delta)
        else:
            self['avg'] += (currentTime - self['timestamp']) / 3600.0 * \
                           (measure / 1000.0)
            self['measure'] = measure
        self['timestamp'] = currentTime


class Collector:
    """Collector gradually fills its database with received values from
    wattmeter drivers.

    """

    def __init__(self):
        """Initializes an empty database and start listening the endpoint."""
        LOG.info('Starting Collector')
        self.database = {}
        self.lock = threading.Lock()

    def add(self, probe, name, timestamp, measure, params):
        """Creates (or updates) consumption data for this probe."""
        self.lock.acquire()
        if probe in self.database.keys():
            self.database[probe].add(timestamp, measure, params)
        else:
            record = Record(timestamp=timestamp, measure=measure, data_type=name, \
                            params=params, avg=0.0)
            self.database[probe] = record
        self.lock.release()

    def remove(self, probe):
        """Removes this probe from database."""
        self.lock.acquire()
        try:
            del self.database[probe]
            return True
        except KeyError:
{'fraction_non_alphanumeric': 0.13539356984478937, 'fraction_numerical': 0.00582039911308204, 'mean_word_length': 2.327339787920701, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7196120', 'n_tokens_mistral': 1963, 'n_tokens_neox': 1911, 'n_words': 536}