- Added `migrate_to_sqlite.py` to handle migration of user stats and session logs from text files to an SQLite database. - The script reads XP, level, and total time from respective text files and saves them into the `user_stats` table in the SQLite database. - Session logs are parsed and logged into the `session_log` table. - Existing text files are deleted after successful migration. - Created a new SQLite database file `rhel_learning.db` with tables for questions, user stats, and session logs.
64 lines
2.0 KiB
Python
64 lines
2.0 KiB
Python
import os
|
|
from datetime import datetime
|
|
from main import (
|
|
XP_FILE, LEVEL_FILE, TOTAL_TIME_FILE, SESSION_LOG, DB_PATH, SUBSCRIPTION_END_DATE, UserStatsSqlite
|
|
)
|
|
|
|
def migrate_txt_to_sqlite():
|
|
migrated = False
|
|
xp = 0
|
|
level = 1
|
|
total_time = 0
|
|
if os.path.exists(XP_FILE):
|
|
try:
|
|
with open(XP_FILE) as f:
|
|
xp = int(f.read().strip() or 0)
|
|
except Exception:
|
|
pass
|
|
migrated = True
|
|
if os.path.exists(LEVEL_FILE):
|
|
try:
|
|
with open(LEVEL_FILE) as f:
|
|
level = int(f.read().strip() or 1)
|
|
except Exception:
|
|
pass
|
|
migrated = True
|
|
if os.path.exists(TOTAL_TIME_FILE):
|
|
try:
|
|
with open(TOTAL_TIME_FILE) as f:
|
|
total_time = int(f.read().strip() or 0)
|
|
except Exception:
|
|
pass
|
|
migrated = True
|
|
if migrated:
|
|
stats = UserStatsSqlite(DB_PATH, SUBSCRIPTION_END_DATE)
|
|
stats.save_stats(xp, level, total_time)
|
|
if os.path.exists(SESSION_LOG):
|
|
stats = UserStatsSqlite(DB_PATH, SUBSCRIPTION_END_DATE)
|
|
try:
|
|
with open(SESSION_LOG) as f:
|
|
for line in f:
|
|
line = line.strip()
|
|
if line:
|
|
import re
|
|
m = re.match(r'Session (\w+): (\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})', line)
|
|
if m:
|
|
status, timestamp = m.groups()
|
|
stats.log_session(status, duration=None)
|
|
except Exception:
|
|
pass
|
|
migrated = True
|
|
if migrated:
|
|
for f in [XP_FILE, LEVEL_FILE, TOTAL_TIME_FILE, SESSION_LOG]:
|
|
try:
|
|
os.remove(f)
|
|
except Exception:
|
|
pass
|
|
if migrated:
|
|
print("Migration complete. Data moved to SQLite and text files removed.")
|
|
else:
|
|
print("No migration needed. No text files found.")
|
|
|
|
if __name__ == "__main__":
|
|
migrate_txt_to_sqlite()
|