A database in TSV text files: data you can read, fast inserts, and an optional Rust speed-up.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-tsv.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-tsv.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-tsv.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Add launch announcement for dbbasic-tsv v1.0.1 1c9b0b8 1 year, 1 month ago History
storage.py · 1.7 KB · 70 lines
1"""
2API endpoint: GET /admin/api/storage
3Returns storage statistics for TSV files
4"""
5
6import json
7from pathlib import Path
8
9
10def GET(request):
11 """Get storage statistics"""
12 data_dir = Path("data")
13
14 if not data_dir.exists():
15 return json_response({
16 'total_mb': 0,
17 'used_mb': 0,
18 'tables': []
19 })
20
21 tables = []
22 total_bytes = 0
23
24 for tsv_file in data_dir.glob("*.tsv"):
25 try:
26 stat = tsv_file.stat()
27 size_kb = stat.st_size / 1024
28
29 # Format size
30 if size_kb < 1:
31 size_str = f"{stat.st_size} B"
32 elif size_kb < 1024:
33 size_str = f"{size_kb:.1f} KB"
34 else:
35 size_str = f"{size_kb/1024:.1f} MB"
36
37 tables.append({
38 'name': tsv_file.stem,
39 'size': size_str,
40 'size_bytes': stat.st_size
41 })
42
43 total_bytes += stat.st_size
44 except Exception as e:
45 print(f"Error reading {tsv_file}: {e}")
46 continue
47
48 # Sort by size (largest first)
49 tables.sort(key=lambda t: t['size_bytes'], reverse=True)
50
51 used_mb = total_bytes / (1024 * 1024)
52
53 # Estimate total available (could be from disk stats in real impl)
54 # For now, just show a reasonable default
55 total_mb = max(1000, used_mb * 2)
56
57 return json_response({
58 'total_mb': round(total_mb, 1),
59 'used_mb': round(used_mb, 2),
60 'tables': tables
61 })
62
63
64def json_response(data, status=200):
65 """Helper to return JSON response"""
66 return {
67 'status': status,
68 'headers': {'Content-Type': 'application/json'},
69 'body': json.dumps(data)
70 }