File size: 3,058 Bytes
59d9f71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from pathlib import Path
from datetime import datetime
import pandas as pd

# <--- DEBUG START --->
print("\n>>> DEBUG: [Import] decision_engine/logging.py loaded")
# <--- DEBUG END --->

# -------------------------
# Paths
# -------------------------

BASE_DIR = Path(__file__).resolve().parent
LOG_DIR = BASE_DIR / "logs"
LOG_FILE = LOG_DIR / "decision_logs.csv"

COLUMNS = [
    "request_id",
    "timestamp",
    "raw_query",
    "situations",
    "constraints",
    "solutions_shown",
    "solution_chosen",
    "worked",
    "feedback"
]


# -------------------------
# Helpers
# -------------------------

def _ensure_log_file():
    LOG_DIR.mkdir(parents=True, exist_ok=True)

    if not LOG_FILE.exists():
        df = pd.DataFrame(columns=COLUMNS)
        df.to_csv(LOG_FILE, index=False)


# -------------------------
# Decision logging (initial)
# -------------------------

def log_decision(

    request_id: str,

    raw_query: str,

    situations: dict,

    constraints: dict,

    solutions_shown: list,

    solution_chosen: str = None,

    worked: bool = None,

    feedback: str = None

):
    """

    Logs initial decision output.

    """
    # <--- DEBUG START --->
    print(f">>> DEBUG: [Logging] Writing decision log for ReqID: {request_id}")
    # <--- DEBUG END --->

    try:
        _ensure_log_file()

        row = {
            "request_id":request_id,
            "timestamp": datetime.utcnow().isoformat(),
            "raw_query": raw_query,
            "situations": str(situations),
            "constraints": str(constraints),
            "solutions_shown": str(solutions_shown),
            "solution_chosen": solution_chosen,
            "worked": worked,
            "feedback": feedback
        }

        pd.DataFrame([row]).to_csv(
            LOG_FILE,
            mode="a",
            header=False,
            index=False
        )

    except Exception as e:
        print(f">>> DEBUG: [Logging] Error writing log: {e}")


# -------------------------
# Feedback logging (NEW)
# -------------------------

def log_feedback(

    request_id:str,

    solution_chosen: str,

    worked: bool,

    feedback: str = None

):
    """

    Updates the most recent log row with teacher feedback.

    """
    # <--- DEBUG START --->
    print(f">>> DEBUG: [Logging] Updating feedback for ReqID: {request_id}")
    # <--- DEBUG END --->

    try:
        if not LOG_FILE.exists():
            return

        df = pd.read_csv(LOG_FILE)

        if df.empty:
            return

        match = df["request_id"] == request_id
        if not match.any():
            return

        # Update the row
        idx = df.index[match][0]
        df.at[idx, "solution_chosen"] = solution_chosen
        df.at[idx, "worked"] = worked
        df.at[idx, "feedback"] = feedback

        df.to_csv(LOG_FILE, index=False)

    except Exception as e:
        print(f">>> DEBUG: [Logging] Error updating feedback: {e}")