File size: 1,696 Bytes
469fa34
51cf475
 
469fa34
fafdf98
469fa34
fafdf98
469fa34
51cf475
 
 
 
 
 
 
 
 
388c28a
51cf475
f9e4e57
 
 
 
 
388c28a
f9e4e57
 
 
9619afc
f9e4e57
 
 
 
 
 
 
 
 
 
9619afc
 
 
 
 
388c28a
9619afc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
from datetime import datetime

# Garante a compatibilidade do caminho do log entre os ambientes
LOG_FILE_PATH = "/tmp/recommender.log"
if os.name == 'nt':
    LOG_FILE_PATH = "recommender.log"


def save_log(user_id: int, history: dict, response: str):
    log_entry = (
        f"[{datetime.now().isoformat(sep=' ', timespec='seconds')}]\n"
        f"Usuário: {user_id}\n"
        f"Histórico: {history}\n"
        f"Recomendação: {response.strip()}\n"
        f"{'-'*40}\n"
    )
    with open(LOG_FILE_PATH, "a", encoding="utf-8") as f:
        f.write(log_entry)


def get_user_history(user_id: int) -> dict:
    history = {}
    try:
        with open(LOG_FILE_PATH, "r", encoding="utf-8") as f:
            lines = f.readlines()

        current_user = None

        for line in lines:
            if line.startswith("Usuário:"):
                current_user = int(line.split(":")[1].strip())
            elif line.startswith("Histórico:") and current_user == user_id:
                history = eval(line.split(":", 1)[1].strip())

    except FileNotFoundError:
        pass

    return history


def get_all_users() -> dict:
    users = {}
    try:
        with open(LOG_FILE_PATH, "r", encoding="utf-8") as f:
            lines = f.readlines()

        current_user = None

        for i, line in enumerate(lines):

            if line.startswith("Usuário:"):
                current_user = int(line.split(":")[1].strip())
            elif line.startswith("Histórico:") and current_user is not None:
                history = eval(line.split(":", 1)[1].strip())
                users[current_user] = history

    except FileNotFoundError:
        pass

    return users