-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsession_manager.py
More file actions
246 lines (191 loc) · 8.03 KB
/
session_manager.py
File metadata and controls
246 lines (191 loc) · 8.03 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env python3
"""Session management for storing and retrieving search results."""
import logging
import uuid
from typing import Dict, List, Optional
from datetime import datetime
from job_details_models import SearchSession
logger = logging.getLogger(__name__)
class SessionManager:
"""Manages search sessions for follow-up questions"""
def __init__(self, session_timeout: int = 3600):
"""
Initialize session manager
Args:
session_timeout: Session timeout in seconds (default: 1 hour)
"""
self.sessions: Dict[str, SearchSession] = {}
self.session_timeout = session_timeout
def create_session(self, results: List[Dict[str, str]], search_terms: List[str] = None, zip_code: str = "40210", radius: int = 5) -> str:
"""
Create a new search session
Args:
search_terms: List of search terms used
zip_code: ZIP code used for search
radius: Search radius in km
results: List of job results
Returns:
Session ID for the new session
"""
session_id = str(uuid.uuid4())
session = SearchSession(
session_id=session_id,
search_terms=search_terms if search_terms is not None else [],
zip_code=zip_code,
radius=radius,
results=results,
timestamp=datetime.now()
)
self.sessions[session_id] = session
self._cleanup_expired_sessions()
return session_id
def get_session(self, session_id: str) -> Optional[SearchSession]:
"""
Retrieve a session by ID
Args:
session_id: The session ID to retrieve
Returns:
SearchSession object or None if not found/expired
"""
if session_id not in self.sessions:
return None
session = self.sessions[session_id]
if session.is_expired(self.session_timeout):
del self.sessions[session_id]
return None
return session
def find_job_in_session(self, session_id: str, job_query: str) -> Optional[Dict[str, str]]:
"""
Find a specific job in a session based on user query
Args:
session_id: The session ID
job_query: User's job query (e.g., "AML Specialist")
Returns:
Job dictionary or None if not found
"""
session = self.get_session(session_id)
if not session:
return None
# Normalize query for matching
query_lower = job_query.lower().strip()
# Try exact title match first
for job in session.results:
if not isinstance(job, dict):
logger.warning("Skipping non-dictionary job entry: %r", job)
continue
title = job.get("title")
if not isinstance(title, str):
logger.warning("Job missing valid title; skipping entry: %r", job)
continue
if query_lower in title.lower():
return job
# Try company match
for job in session.results:
if not isinstance(job, dict):
logger.warning("Skipping non-dictionary job entry: %r", job)
continue
company = job.get("company")
if not isinstance(company, str):
logger.warning("Job missing valid company; skipping entry: %r", job)
continue
if query_lower in company.lower():
return job
# Try partial title match
query_words = query_lower.split()
for job in session.results:
if not isinstance(job, dict):
logger.warning("Skipping non-dictionary job entry: %r", job)
continue
title = job.get("title")
if not isinstance(title, str):
logger.warning("Job missing valid title; skipping entry: %r", job)
continue
title_words = title.lower().split()
if any(word in title_words for word in query_words):
return job
return None
def get_job_by_index(self, session_id: str, job_index: int) -> Optional[Dict[str, str]]:
"""Retrieve a job by its 1-based index within a session."""
session = self.get_session(session_id)
if not session:
return None
# Convert to zero-based index for list access
zero_based_index = job_index - 1
if zero_based_index < 0 or zero_based_index >= len(session.results):
return None
return session.results[zero_based_index]
def get_recent_session(self) -> Optional[SearchSession]:
"""
Get the most recent active session
Returns:
Most recent SearchSession or None if no active sessions
"""
self._cleanup_expired_sessions()
if not self.sessions:
return None
# Return the most recent session
return max(self.sessions.values(), key=lambda s: s.timestamp)
def _cleanup_expired_sessions(self):
"""Remove expired sessions"""
expired_sessions = [
session_id for session_id, session in self.sessions.items()
if session.is_expired(self.session_timeout)
]
for session_id in expired_sessions:
del self.sessions[session_id]
def get_session_summary(self, session_id: str) -> Optional[str]:
"""
Get a human-readable summary of a session
Args:
session_id: The session ID
Returns:
Summary string or None if session not found
"""
session = self.get_session(session_id)
if not session:
return None
summary = f"Search Session: {session_id[:8]}...\n"
search_terms_text = ', '.join(session.search_terms) if session.search_terms else "None"
summary += f"Search Terms: {search_terms_text}\n"
summary += f"Location: {session.zip_code} (±{session.radius}km)\n"
summary += f"Results: {len(session.results)} jobs found\n"
summary += f"Search Time: {session.timestamp.strftime('%Y-%m-%d %H:%M:%S')}"
return summary
def list_active_sessions(self) -> List[str]:
"""List all active session IDs"""
self._cleanup_expired_sessions()
return list(self.sessions.keys())
def get_active_session_overview(self) -> List[Dict[str, object]]:
"""Return lightweight metadata for each active session.
The overview is sorted by most recent session first so clients can
easily present the freshest results to the user. Each overview entry
contains human friendly values that are ready for display without
requiring additional processing downstream.
"""
self._cleanup_expired_sessions()
if not self.sessions:
return []
current_time = datetime.now()
overview: List[Dict[str, object]] = []
# Sort sessions by recency so that the freshest search appears first
for session in sorted(
self.sessions.values(), key=lambda s: s.timestamp, reverse=True
):
search_terms_text = (
", ".join(session.search_terms) if session.search_terms else "None"
)
overview.append(
{
"session_id": session.session_id,
"search_terms": search_terms_text,
"location": f"{session.zip_code} (±{session.radius}km)",
"result_count": len(session.results),
"age_seconds": max(
0,
int((current_time - session.timestamp).total_seconds()),
),
}
)
return overview
# Global session manager instance
session_manager = SessionManager()