-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
378 lines (312 loc) · 11.9 KB
/
database.py
File metadata and controls
378 lines (312 loc) · 11.9 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import sqlite3
import os
from datetime import datetime
from pathlib import Path
import threading
class ConversionDatabase:
"""
Database manager for tracking file conversions
Stores conversion statistics in SQLite database
"""
def __init__(self, db_path="conversion_stats.db"):
"""Initialize database connection and create tables if needed"""
self.db_path = db_path
self.lock = threading.Lock()
self._create_tables()
def _get_connection(self):
"""Get database connection"""
return sqlite3.connect(self.db_path, check_same_thread=False)
def _create_tables(self):
"""Create database tables if they don't exist"""
with self.lock:
conn = self._get_connection()
cursor = conn.cursor()
# Table for individual conversions
cursor.execute('''
CREATE TABLE IF NOT EXISTS conversions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
filename TEXT NOT NULL,
file_size INTEGER NOT NULL,
from_format TEXT NOT NULL,
to_format TEXT NOT NULL,
conversion_type TEXT NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
success BOOLEAN DEFAULT 1
)
''')
# Table for overall statistics (singleton table)
cursor.execute('''
CREATE TABLE IF NOT EXISTS statistics (
id INTEGER PRIMARY KEY CHECK (id = 1),
total_files INTEGER DEFAULT 0,
total_size_bytes INTEGER DEFAULT 0,
last_updated DATETIME DEFAULT CURRENT_TIMESTAMP
)
''')
# Initialize statistics if empty
cursor.execute('SELECT COUNT(*) FROM statistics')
if cursor.fetchone()[0] == 0:
cursor.execute('''
INSERT INTO statistics (id, total_files, total_size_bytes)
VALUES (1, 0, 0)
''')
conn.commit()
conn.close()
def add_conversion(self, filename, file_size, from_format, to_format, conversion_type="document", success=True):
"""
Add a new conversion record to the database
Args:
filename (str): Name of the file converted
file_size (int): Size of the file in bytes
from_format (str): Source format (e.g., 'DOCX', 'PDF')
to_format (str): Target format (e.g., 'PDF', 'DOCX')
conversion_type (str): Type of conversion ('document', 'media', 'developer')
success (bool): Whether conversion was successful
Returns:
int: ID of the inserted record
"""
with self.lock:
conn = self._get_connection()
cursor = conn.cursor()
# Insert conversion record
cursor.execute('''
INSERT INTO conversions
(filename, file_size, from_format, to_format, conversion_type, success)
VALUES (?, ?, ?, ?, ?, ?)
''', (filename, file_size, from_format, to_format, conversion_type, success))
conversion_id = cursor.lastrowid
# Update statistics if successful
if success:
cursor.execute('''
UPDATE statistics
SET total_files = total_files + 1,
total_size_bytes = total_size_bytes + ?,
last_updated = CURRENT_TIMESTAMP
WHERE id = 1
''', (file_size,))
conn.commit()
conn.close()
return conversion_id
def get_statistics(self):
"""
Get overall conversion statistics
Returns:
dict: Dictionary containing total_files, total_size_bytes, total_size_tb, last_updated
"""
with self.lock:
conn = self._get_connection()
cursor = conn.cursor()
cursor.execute('''
SELECT total_files, total_size_bytes, last_updated
FROM statistics
WHERE id = 1
''')
row = cursor.fetchone()
conn.close()
if row:
total_files, total_size_bytes, last_updated = row
# Convert bytes to TB
total_size_tb = total_size_bytes / (1024 ** 4) # 1 TB = 1024^4 bytes
return {
'total_files': total_files,
'total_size_bytes': total_size_bytes,
'total_size_tb': round(total_size_tb, 2),
'last_updated': last_updated
}
return {
'total_files': 0,
'total_size_bytes': 0,
'total_size_tb': 0.0,
'last_updated': None
}
def get_conversion_history(self, limit=100, conversion_type=None):
"""
Get recent conversion history
Args:
limit (int): Maximum number of records to return
conversion_type (str): Filter by conversion type (optional)
Returns:
list: List of conversion records
"""
with self.lock:
conn = self._get_connection()
cursor = conn.cursor()
if conversion_type:
cursor.execute('''
SELECT id, filename, file_size, from_format, to_format,
conversion_type, timestamp, success
FROM conversions
WHERE conversion_type = ?
ORDER BY timestamp DESC
LIMIT ?
''', (conversion_type, limit))
else:
cursor.execute('''
SELECT id, filename, file_size, from_format, to_format,
conversion_type, timestamp, success
FROM conversions
ORDER BY timestamp DESC
LIMIT ?
''', (limit,))
rows = cursor.fetchall()
conn.close()
conversions = []
for row in rows:
conversions.append({
'id': row[0],
'filename': row[1],
'file_size': row[2],
'from_format': row[3],
'to_format': row[4],
'conversion_type': row[5],
'timestamp': row[6],
'success': bool(row[7])
})
return conversions
def get_conversion_stats_by_type(self):
"""
Get conversion statistics grouped by conversion type
Returns:
dict: Statistics for each conversion type
"""
with self.lock:
conn = self._get_connection()
cursor = conn.cursor()
cursor.execute('''
SELECT conversion_type,
COUNT(*) as count,
SUM(file_size) as total_size
FROM conversions
WHERE success = 1
GROUP BY conversion_type
''')
rows = cursor.fetchall()
conn.close()
stats = {}
for row in rows:
conversion_type, count, total_size = row
stats[conversion_type] = {
'count': count,
'total_size_bytes': total_size or 0,
'total_size_mb': round((total_size or 0) / (1024 ** 2), 2)
}
return stats
def get_popular_conversions(self, limit=10):
"""
Get most popular conversion types
Args:
limit (int): Number of top conversions to return
Returns:
list: List of popular conversions with counts
"""
with self.lock:
conn = self._get_connection()
cursor = conn.cursor()
cursor.execute('''
SELECT from_format, to_format, COUNT(*) as count
FROM conversions
WHERE success = 1
GROUP BY from_format, to_format
ORDER BY count DESC
LIMIT ?
''', (limit,))
rows = cursor.fetchall()
conn.close()
popular = []
for row in rows:
popular.append({
'from_format': row[0],
'to_format': row[1],
'count': row[2]
})
return popular
def reset_statistics(self):
"""
Reset all statistics (USE WITH CAUTION!)
This will delete all conversion records
"""
with self.lock:
conn = self._get_connection()
cursor = conn.cursor()
cursor.execute('DELETE FROM conversions')
cursor.execute('''
UPDATE statistics
SET total_files = 0,
total_size_bytes = 0,
last_updated = CURRENT_TIMESTAMP
WHERE id = 1
''')
conn.commit()
conn.close()
def get_formatted_stats(self):
"""
Get formatted statistics for display
Returns:
dict: Formatted statistics with human-readable numbers
"""
stats = self.get_statistics()
# Format numbers with commas
total_files_formatted = f"{stats['total_files']:,}"
# SMART SIZE FORMATTING - KB or MB
total_bytes = stats['total_size_bytes']
if total_bytes < 1024 * 1024: # Less than 1 MB
# Show in KB
total_kb = total_bytes / 1024
size_formatted = f"{total_kb:,.1f} KB"
else:
# Show in MB
total_mb = total_bytes / (1024 ** 2)
size_formatted = f"{total_mb:,.1f} MB"
return {
'total_files': stats['total_files'],
'total_files_formatted': total_files_formatted,
'total_size_bytes': total_bytes,
'size_formatted': size_formatted,
'last_updated': stats['last_updated']
}
# Singleton instance
_db_instance = None
def get_database():
"""
Get singleton database instance
Returns:
ConversionDatabase: Database instance
"""
global _db_instance
if _db_instance is None:
_db_instance = ConversionDatabase()
return _db_instance
# Helper functions for easy use
def track_conversion(filename, file_size, from_format, to_format, conversion_type="document", success=True):
"""
Track a file conversion
Args:
filename (str): Name of the file
file_size (int): Size in bytes
from_format (str): Source format
to_format (str): Target format
conversion_type (str): Type of conversion
success (bool): Whether conversion succeeded
"""
db = get_database()
return db.add_conversion(filename, file_size, from_format, to_format, conversion_type, success)
def get_stats():
"""
Get current statistics
Returns:
dict: Current statistics
"""
db = get_database()
return db.get_formatted_stats()
def get_file_size(file):
"""
Get size of uploaded file
Args:
file: Streamlit UploadedFile object
Returns:
int: File size in bytes
"""
file.seek(0, os.SEEK_END)
size = file.tell()
file.seek(0)
return size