<commit_before>import time
import boto3

dynamodb = boto3.client('dynamodb', 'eu-west-1')


class Tracker:
    def __init__(self, plugin_name, update=None, u_id=None):
        self.plugin = plugin_name

        # Events, for example, uber-make-order, uber-cancel-order
        self.events = []
        # Timers, for example, uber-make-order-request,  vinci-filter-download
        self.timers = []

        if update:
            self.u_id = update.u_id
        else:
            self.u_id = u_id

        self.timer_start = {}

    def event(self, event_type, params=None):
        self.events.append({
            'type': event_type,
            'plugin_name': self.plugin,
            'time': time.time(),
            'params': params,
            'u_id': self.u_id
        })


    def timer_start(self, timer_name):
        self.timer_start[timer_name] = time.time()

    def fix_timer(self, timer_name, params=None):
        finish_time = time.time()
        self.timers.append({'name': timer_name,
                            'start_time': self.timer_start[timer_name],
                            'finish_time': finish_time,
                            'time': finish_time - self.timer_start[timer_name],
                            'params': params,
                            'plugin_name': self.plugin,
                            'u_id': self.u_id})

    def send(self):
        for event in self.events:
            dynamodb.put_item(
                TableName='LeonardBotEvent',
                Item=prepare_to_dynamo(event)
            )
        for timer in self.timers:
            dynamodb.put_item(
                TableName='LeonardBotTimer',
                Item=prepare_to_dynamo(timer)
            )



def track_message(message, handler, tracker=None):
    dynamodb.put_item(
        TableName='LeonardBotUserMessage',
        Item={
            'id': {'N': str(message.message_id)},
            'time': {'N': str(time.mktime(message.date.timetuple()))},
            'proceed_time': {'N': str(time.time())},
            'text': {'S': message.text},
            'user_id': {'N': str(message.from_user.id)},
            'handler': {'S': handler}
        }
    )

    if tracker:
        tracker.send()


def prepare_to_dynamo(item):
    dynamo_item = {}
    for (key, value) in item.items():
        if value == '':
            continue
        if type(value) == str:
            dynamo_item[key] = {'S': value}
        elif type(value) in [int, float]:
            dynamo_item[key] = {'N': str(value)}
        elif type(value) == dict:
            dynamo_item[key] = {'M': prepare_to_dynamo(value)}<commit_msg>Fix tracking message with empty handler<commit_after>import time
import boto3

dynamodb = boto3.client('dynamodb', 'eu-west-1')


class Tracker:
    def __init__(self, plugin_name, update=None, u_id=None):
        self.plugin = plugin_name

        # Events, for example, uber-make-order, uber-cancel-order
        self.events = []
        # Timers, for example, uber-make-order-request,  vinci-filter-download
        self.timers = []

        if update:
            self.u_id = update.u_id
        else:
            self.u_id = u_id

        self.timer_start = {}

    def event(self, event_type, params=None):
        self.events.append({
            'type': event_type,
            'plugin_name': self.plugin,
            'time': time.time(),
            'params': params,
            'u_id': self.u_id
        })


    def timer_start(self, timer_name):
        self.timer_start[timer_name] = time.time()

    def fix_timer(self, timer_name, params=None):
        finish_time = time.time()
        self.timers.append({'name': timer_name,
                            'start_time': self.timer_start[timer_name],
                            'finish_time': finish_time,
                            'time': finish_time - self.timer_start[timer_name],
                            'params': params,
                            'plugin_name': self.plugin,
                            'u_id': self.u_id})

    def send(self):
        for event in self.events:
            dynamodb.put_item(
                TableName='LeonardBotEvent',
                Item=prepare_to_dynamo(event)
            )
        for timer in self.timers:
            dynamodb.put_item(
                TableName='LeonardBotTimer',
                Item=prepare_to_dynamo(timer)
            )



def track_message(message, handler, tracker=None):
    dynamodb.put_item(
        TableName='LeonardBotUserMessage',
        Item={
            'id': {'N': str(message.message_id)},
            'time': {'N': str(time.mktime(message.date.timetuple()))},
            'proceed_time': {'N': str(time.time())},
            'text': {'S': message.text},
            'user_id': {'N': str(message.from_user.id)},
            'handler': {'S': handler or 'registration'}
        }
    )

    if tracker:
        tracker.send()


def prepare_to_dynamo(item):
    dynamo_item = {}
    for (key, value) in item.items():
        if value == '':
            continue
        if type(value) == str:
            dynamo_item[key] = {'S': value}
        elif type(value) in [int, float]:
            dynamo_item[key] = {'N': str(value)}
        elif type(value) == dict:
            dynamo_item[key] = {'M': prepare_to_dynamo(value)}
{'fraction_non_alphanumeric': 0.12989457831325302, 'fraction_numerical': 0.0011295180722891566, 'mean_word_length': 1.77007299270073, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '609402', 'n_tokens_mistral': 1656, 'n_tokens_neox': 1618, 'n_words': 310}