-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
284 lines (246 loc) · 7.95 KB
/
database.py
File metadata and controls
284 lines (246 loc) · 7.95 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import sqlite3
import json
from datetime import datetime
from config import Config
def get_db():
"""Get database connection"""
conn = sqlite3.connect(Config.DATABASE)
conn.row_factory = sqlite3.Row
return conn
def init_db():
"""Initialize database tables"""
conn = get_db()
cursor = conn.cursor()
# Users table
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# Interview sessions table
cursor.execute('''
CREATE TABLE IF NOT EXISTS interview_sessions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
domain TEXT,
experience_level TEXT,
resume_text TEXT,
job_description TEXT,
start_time TIMESTAMP,
end_time TIMESTAMP,
total_score REAL,
feedback_summary TEXT,
FOREIGN KEY (user_id) REFERENCES users (id)
)
''')
# Questions table
cursor.execute('''
CREATE TABLE IF NOT EXISTS questions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id INTEGER,
question_text TEXT,
question_type TEXT,
difficulty TEXT,
category TEXT,
time_allocated INTEGER,
FOREIGN KEY (session_id) REFERENCES interview_sessions (id)
)
''')
# Answers table
cursor.execute('''
CREATE TABLE IF NOT EXISTS answers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
question_id INTEGER,
session_id INTEGER,
answer_text TEXT,
transcript TEXT,
duration INTEGER,
grammar_score REAL,
relevance_score REAL,
confidence_score REAL,
star_score REAL,
filler_words_count INTEGER,
feedback TEXT,
cross_question_asked BOOLEAN DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (question_id) REFERENCES questions (id),
FOREIGN KEY (session_id) REFERENCES interview_sessions (id)
)
''')
# Coding tests table
cursor.execute('''
CREATE TABLE IF NOT EXISTS coding_tests (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id INTEGER,
problem_statement TEXT,
language TEXT DEFAULT 'python',
user_code TEXT,
test_cases_passed INTEGER,
total_test_cases INTEGER,
efficiency_score REAL,
clarity_score REAL,
logic_score REAL,
feedback TEXT,
time_taken INTEGER,
FOREIGN KEY (session_id) REFERENCES interview_sessions (id)
)
''')
# Performance history table
cursor.execute('''
CREATE TABLE IF NOT EXISTS performance_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
session_id INTEGER,
date DATE,
overall_score REAL,
communication_score REAL,
technical_score REAL,
coding_score REAL,
confidence_score REAL,
areas_to_improve TEXT,
FOREIGN KEY (user_id) REFERENCES users (id),
FOREIGN KEY (session_id) REFERENCES interview_sessions (id)
)
''')
conn.commit()
conn.close()
def save_interview_session(session_data):
"""Save interview session data"""
conn = get_db()
cursor = conn.cursor()
cursor.execute('''
INSERT INTO interview_sessions
(user_id, domain, experience_level, resume_text, job_description, start_time)
VALUES (?, ?, ?, ?, ?, ?)
''', (
session_data['user_id'],
session_data['domain'],
session_data['experience_level'],
session_data['resume_text'],
session_data['job_description'],
datetime.now()
))
session_id = cursor.lastrowid
conn.commit()
conn.close()
return session_id
def save_question(session_id, question_data):
"""Save generated question"""
conn = get_db()
cursor = conn.cursor()
cursor.execute('''
INSERT INTO questions
(session_id, question_text, question_type, difficulty, category, time_allocated)
VALUES (?, ?, ?, ?, ?, ?)
''', (
session_id,
question_data['question_text'],
question_data['question_type'],
question_data['difficulty'],
question_data['category'],
question_data['time_allocated']
))
question_id = cursor.lastrowid
conn.commit()
conn.close()
return question_id
def save_answer(answer_data):
"""Save answer with analysis"""
conn = get_db()
cursor = conn.cursor()
cursor.execute('''
INSERT INTO answers
(question_id, session_id, answer_text, transcript, duration,
grammar_score, relevance_score, confidence_score, star_score,
filler_words_count, feedback, cross_question_asked)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
answer_data['question_id'],
answer_data['session_id'],
answer_data['answer_text'],
answer_data['transcript'],
answer_data['duration'],
answer_data['grammar_score'],
answer_data['relevance_score'],
answer_data['confidence_score'],
answer_data['star_score'],
answer_data['filler_words_count'],
answer_data['feedback'],
answer_data.get('cross_question_asked', False)
))
answer_id = cursor.lastrowid
conn.commit()
conn.close()
return answer_id
def save_coding_test(test_data):
"""Save coding test results"""
conn = get_db()
cursor = conn.cursor()
cursor.execute('''
INSERT INTO coding_tests
(session_id, problem_statement, language, user_code,
test_cases_passed, total_test_cases, efficiency_score,
clarity_score, logic_score, feedback, time_taken)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
test_data['session_id'],
test_data['problem_statement'],
test_data['language'],
test_data['user_code'],
test_data['test_cases_passed'],
test_data['total_test_cases'],
test_data['efficiency_score'],
test_data['clarity_score'],
test_data['logic_score'],
test_data['feedback'],
test_data['time_taken']
))
test_id = cursor.lastrowid
conn.commit()
conn.close()
return test_id
def get_session_performance(session_id):
"""Get performance data for a session"""
conn = get_db()
cursor = conn.cursor()
# Get answers for this session
cursor.execute('''
SELECT * FROM answers
WHERE session_id = ?
''', (session_id,))
answers = [dict(row) for row in cursor.fetchall()]
# Get coding tests
cursor.execute('''
SELECT * FROM coding_tests
WHERE session_id = ?
''', (session_id,))
coding_tests = [dict(row) for row in cursor.fetchall()]
# Get session info
cursor.execute('''
SELECT * FROM interview_sessions
WHERE id = ?
''', (session_id,))
session_row = cursor.fetchone()
session = dict(session_row) if session_row else None
conn.close()
return {
'session': session,
'answers': answers,
'coding_tests': coding_tests
}
def get_user_history(user_id):
"""Get user's performance history"""
conn = get_db()
cursor = conn.cursor()
cursor.execute('''
SELECT ph.*, is.domain, is.experience_level
FROM performance_history ph
JOIN interview_sessions is ON ph.session_id = is.id
WHERE ph.user_id = ?
ORDER BY ph.date DESC
''', (user_id,))
history = [dict(row) for row in cursor.fetchall()]
conn.close()
return history