<commit_before># Copyright 2011-2013 <NAME>
# Copyright 2011-2013 <NAME>
#
# 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.

from sts.replay_event import *
import time
from collections import Counter
import operator
import logging

log = logging.getLogger("event_scheduler")

def format_time(time):
  mins = int(time/60)
  secs = int(time % 60)
  ms = int( (time * 1000) % 1000)
  return "%02d:%02d.%03d" % (mins, secs, ms)

class EventSchedulerStats(object):
  def __init__(self):
    # Coarse grained event classes -> match counts
    self.event2matched = Counter()
    # ControlMessageReceive packet classes -> match counts
    self.msgrecv2matched = Counter()
    # ControlMessageSend packet classes -> match counts
    self.msgsend2matched = Counter()
    # Coarse grained event classes -> timeout counts
    self.event2timeouts = Counter()
    # ControlMessageReceive packet classes -> timeout counts
    self.msgrecv2timeouts = Counter()
    # ControlMessageSend packet classes -> timeout counts
    self.msgsend2timeouts = Counter()
    self.replay_start = None
    self.record_start = None

  def start_replay(self, event):
    self.replay_start = time.time()
    self.record_start = event.time.as_float()

  def time(self, event):
    return format_time(time.time() - self.replay_start) + " " + \
           format_time(event.time.as_float() - self.record_start)

  def event_matched(self, event):
    msg.replay_event_success(self.time(event) + " Successfully matched event "+str(event))
    self.event2matched[event.__class__.__name__] += 1
    if event.__class__.__name__ == "ControlMessageReceive":
      pkt_class = event.get_packet().__class__.__name__
      self.msgrecv2matched[pkt_class] += 1
    if event.__class__.__name__ == "ControlMessageSend":
      pkt_class = event.get_packet().__class__.__name__
      self.msgsend2matched[pkt_class] += 1

  def event_timed_out(self, event):
    msg.replay_event_timeout(self.time(event) + " Event timed out "+str(event))
    self.event2timeouts[event.__class__.__name__] += 1
    if event.__class__.__name__ == "ControlMessageReceive":
      pkt_class = event.get_packet().__class__.__name__
      self.msgrecv2timeouts[pkt_class] += 1
    if event.__class__.__name__ == "ControlMessageSend":
      pkt_class = event.get_packet().__class__.__name__
      self.msgsend2timeouts[pkt_class] += 1

  def sorted_match_counts(self):
    for e, count in sorted(self.event2matched.items(),
                           key=operator.itemgetter(1)):
      yield (e, count)

  def sorted_timeout_counts(self):
    for e, count in sorted(self.event2timeouts.items(),
                           key=operator.itemgetter(1)):
      yield (e, count,)

  def get_matches_dict(self):
    d = dict(self.event2matched)
    if 'ControlMessageReceive' in d:
      total = d['ControlMessageReceive']
      d['ControlMessageReceive'] = dict(self.msgrecv2matched)
      d['ControlMessageReceive']['total'] = total
    if 'ControlMessageSend' in d:
      total = d['ControlMessageSend']
      d['ControlMessageSend'] = dict(self.msgsend2matched)
      d['ControlMessageSend']['total'] = total
    return d

  def get_timeouts_dict(self):
    d = dict(event2timouts)
    if 'ControlMessageReceive' in d:
      total = d['ControlMessageReceive']
      d['ControlMessageReceive'] = dict(self.msgrecv2timeouts)
      d['ControlMessageReceive']['total'] = total
    if 'ControlMessageSend' in d:
      total = d['ControlMessageSend']
      d['ControlMessageSend'] = dict(self.msgsend2timeouts)
      d['ControlMessageSend']['total'] = total
    return d<commit_msg>Fix typo<commit_after># Copyright 2011-2013 <NAME>
# Copyright 2011-2013 <NAME>
#
# 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.

from sts.replay_event import *
import time
from collections import Counter
import operator
import logging

log = logging.getLogger("event_scheduler")

def format_time(time):
  mins = int(time/60)
  secs = int(time % 60)
  ms = int( (time * 1000) % 1000)
  return "%02d:%02d.%03d" % (mins, secs, ms)

class EventSchedulerStats(object):
  def __init__(self):
    # Coarse grained event classes -> match counts
    self.event2matched = Counter()
    # ControlMessageReceive packet classes -> match counts
    self.msgrecv2matched = Counter()
    # ControlMessageSend packet classes -> match counts
    self.msgsend2matched = Counter()
    # Coarse grained event classes -> timeout counts
    self.event2timeouts = Counter()
    # ControlMessageReceive packet classes -> timeout counts
    self.msgrecv2timeouts = Counter()
    # ControlMessageSend packet classes -> timeout counts
    self.msgsend2timeouts = Counter()
    self.replay_start = None
    self.record_start = None

  def start_replay(self, event):
    self.replay_start = time.time()
    self.record_start = event.time.as_float()

  def time(self, event):
    return format_time(time.time() - self.replay_start) + " " + \
           format_time(event.time.as_float() - self.record_start)

  def event_matched(self, event):
    msg.replay_event_success(self.time(event) + " Successfully matched event "+str(event))
    self.event2matched[event.__class__.__name__] += 1
    if event.__class__.__name__ == "ControlMessageReceive":
      pkt_class = event.get_packet().__class__.__name__
      self.msgrecv2matched[pkt_class] += 1
    if event.__class__.__name__ == "ControlMessageSend":
      pkt_class = event.get_packet().__class__.__name__
      self.msgsend2matched[pkt_class] += 1

  def event_timed_out(self, event):
    msg.replay_event_timeout(self.time(event) + " Event timed out "+str(event))
    self.event2timeouts[event.__class__.__name__] += 1
    if event.__class__.__name__ == "ControlMessageReceive":
      pkt_class = event.get_packet().__class__.__name__
      self.msgrecv2timeouts[pkt_class] += 1
    if event.__class__.__name__ == "ControlMessageSend":
      pkt_class = event.get_packet().__class__.__name__
      self.msgsend2timeouts[pkt_class] += 1

  def sorted_match_counts(self):
    for e, count in sorted(self.event2matched.items(),
                           key=operator.itemgetter(1)):
      yield (e, count)

  def sorted_timeout_counts(self):
    for e, count in sorted(self.event2timeouts.items(),
                           key=operator.itemgetter(1)):
      yield (e, count,)

  def get_matches_dict(self):
    d = dict(self.event2matched)
    if 'ControlMessageReceive' in d:
      total = d['ControlMessageReceive']
      d['ControlMessageReceive'] = dict(self.msgrecv2matched)
      d['ControlMessageReceive']['total'] = total
    if 'ControlMessageSend' in d:
      total = d['ControlMessageSend']
      d['ControlMessageSend'] = dict(self.msgsend2matched)
      d['ControlMessageSend']['total'] = total
    return d

  def get_timeouts_dict(self):
    d = dict(self.event2timouts)
    if 'ControlMessageReceive' in d:
      total = d['ControlMessageReceive']
      d['ControlMessageReceive'] = dict(self.msgrecv2timeouts)
      d['ControlMessageReceive']['total'] = total
    if 'ControlMessageSend' in d:
      total = d['ControlMessageSend']
      d['ControlMessageSend'] = dict(self.msgsend2timeouts)
      d['ControlMessageSend']['total'] = total
    return d
{'fraction_non_alphanumeric': 0.10935972629521017, 'fraction_numerical': 0.016129032258064516, 'mean_word_length': 3.76703552708212, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 19, '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': '4105917', 'n_tokens_mistral': 2563, 'n_tokens_neox': 2436, 'n_words': 612}