-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
151 lines (123 loc) · 5.1 KB
/
app.py
File metadata and controls
151 lines (123 loc) · 5.1 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
# app.py (updated)
from flask import Flask, render_template, request, redirect
import json
import os
import glob
import sqlite3
from datetime import datetime
app = Flask(__name__)
def get_config():
with open('config.json') as f:
return json.load(f)
def init_db():
config = get_config()
conn = sqlite3.connect(config['database'])
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS codex_entries
(id INTEGER PRIMARY KEY AUTOINCREMENT,
region TEXT NOT NULL,
system TEXT NOT NULL,
body_id INTEGER NOT NULL,
name TEXT NOT NULL,
count INTEGER DEFAULT 1,
UNIQUE(region, system, body_id, name))''')
c.execute('''CREATE TABLE IF NOT EXISTS processed_files
(path TEXT PRIMARY KEY,
last_modified REAL NOT NULL)''')
conn.commit()
conn.close()
def scan_logs(full_rescan=False):
config = get_config()
conn = sqlite3.connect(config['database'])
c = conn.cursor()
if full_rescan:
c.execute('DELETE FROM codex_entries')
c.execute('DELETE FROM processed_files')
conn.commit()
processed_files = {row[0]: row[1] for row in c.execute('SELECT path, last_modified FROM processed_files')}
log_files = sorted(glob.glob(os.path.join(config['log_path'], 'Journal*.log')),
key=os.path.getmtime)
for path in log_files:
try:
mtime = os.path.getmtime(path)
if not full_rescan and path in processed_files and processed_files[path] >= mtime:
continue
with open(path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if not line:
continue
try:
entry = json.loads(line)
if entry.get('event') != 'CodexEntry':
continue
if entry.get('Category') != '$Codex_Category_Biology;':
continue
# Validate required fields
required_fields = [
'Region_Localised', 'System',
'BodyID', 'Name_Localised'
]
if not all(entry.get(field) for field in required_fields):
continue
# Insert/update entry
c.execute('''INSERT INTO codex_entries
(region, system, body_id, name)
VALUES (?, ?, ?, ?)
ON CONFLICT(region, system, body_id, name)
DO UPDATE SET count = count + 1''',
(entry['Region_Localised'],
entry['System'],
entry['BodyID'],
entry['Name_Localised']))
except (json.JSONDecodeError, KeyError) as e:
print(f"Error processing line in {path}: {e}")
continue
c.execute('''INSERT OR REPLACE INTO processed_files
VALUES (?, ?)''', (path, mtime))
conn.commit()
except Exception as e:
print(f"Error processing file {path}: {e}")
conn.rollback()
conn.close()
# ... rest of the Flask routes remain the same ...
@app.route('/')
def index():
conn = sqlite3.connect(get_config()['database'])
c = conn.cursor()
# Group by region > system > body
hierarchy = {}
c.execute('''SELECT region, system, body_id, name, SUM(count)
FROM codex_entries
GROUP BY region, system, body_id, name
ORDER BY region, system, body_id''')
for row in c.fetchall():
region, system, body_id, name, count = row
if region not in hierarchy:
hierarchy[region] = {}
if system not in hierarchy[region]:
hierarchy[region][system] = {}
if body_id not in hierarchy[region][system]:
hierarchy[region][system][body_id] = []
hierarchy[region][system][body_id].append((name, count))
# Get totals
totals = {}
c.execute('SELECT name, SUM(count) FROM codex_entries GROUP BY name')
totals = dict(c.fetchall())
conn.close()
return render_template('index.html', hierarchy=hierarchy, totals=totals)
@app.route('/scan', methods=['POST'])
def scan():
scan_logs(full_rescan=False)
return redirect('/')
@app.route('/rescan', methods=['POST'])
def rescan():
scan_logs(full_rescan=True)
return redirect('/')
#if __name__ == '__main__':
# init_db()
# app.run(debug=True, host='0.0.0.0')
# -----
if __name__ == '__main__':
init_db()
app.run(debug=True, host='0.0.0.0')