File size: 1,972 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import csv
import os
from datetime import datetime

from .memory import salvar_memoria_negativa


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


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


def inicializar_arquivo_feedback():
    garantir_pasta_logs()

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


def salvar_feedback(query, product_id, product_name, rating=None, is_helpful=None):
    inicializar_arquivo_feedback()

    with open(FEEDBACK_FILE, mode="a", newline="", encoding="utf-8") as f:
        writer = csv.writer(f)
        writer.writerow([
            datetime.now().isoformat(),
            query,
            product_id,
            product_name,
            rating if rating is not None else "",
            is_helpful if is_helpful is not None else ""
        ])

    # Regra simples para criar memória negativa
    if rating is not None and rating <= 2:
        salvar_memoria_negativa(
            query=query,
            product_id=product_id,
            product_name=product_name,
            rating=rating,
            motivo="rating_baixo"
        )

    if is_helpful is False:
        salvar_memoria_negativa(
            query=query,
            product_id=product_id,
            product_name=product_name,
            rating=rating if rating is not None else "",
            motivo="nao_foi_util"
        )

    return {
        "status": "ok",
        "message": "Feedback salvo com sucesso."
    }