File size: 1,248 Bytes
4402d23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import csv
import os
from datetime import datetime


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
LOGS_DIR = os.path.join(BASE_DIR, "logs")
NEGATIVE_MEMORY_FILE = os.path.join(LOGS_DIR, "negative_memory.csv")


def garantir_pasta_logs():
    os.makedirs(LOGS_DIR, exist_ok=True)


def inicializar_memoria_negativa():
    garantir_pasta_logs()

    if not os.path.exists(NEGATIVE_MEMORY_FILE):
        with open(NEGATIVE_MEMORY_FILE, "w", newline="", encoding="utf-8") as f:
            writer = csv.writer(f)
            writer.writerow([
                "timestamp",
                "query",
                "product_id",
                "product_name",
                "rating",
                "motivo"
            ])


def salvar_memoria_negativa(query, product_id, product_name, rating, motivo="feedback_negativo"):
    inicializar_memoria_negativa()

    with open(NEGATIVE_MEMORY_FILE, "a", newline="", encoding="utf-8") as f:
        writer = csv.writer(f)
        writer.writerow([
            datetime.now().isoformat(),
            query,
            product_id,
            product_name,
            rating,
            motivo
        ])

    return {"status": "ok"}