-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
213 lines (182 loc) · 6.64 KB
/
Copy pathserver.py
File metadata and controls
213 lines (182 loc) · 6.64 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
#!/usr/bin/env python3
"""
Project Dashboard Web Server
Simple Flask app to manage projects, time tracking, and protection
"""
from flask import Flask, render_template, jsonify, request
import subprocess
import json
import os
from datetime import datetime
import csv
app = Flask(__name__)
PROJECTS_DIR = "/mnt/c/projects"
DASHBOARD_DIR = os.path.expanduser("~/.project-dashboard")
ACTIVE_FILE = os.path.join(DASHBOARD_DIR, "active.txt")
TIME_LOG = os.path.expanduser("~/.time-tracker/time_log.csv")
TIME_TRACKER = "/mnt/c/projects/time-tracker/track.sh"
PROTECT_CMD = "/mnt/c/projects/project-hub/claude-proof/protect_enhanced.sh"
# Ensure directories exist
os.makedirs(DASHBOARD_DIR, exist_ok=True)
def get_projects():
"""Get list of all projects"""
projects = []
try:
for item in os.listdir(PROJECTS_DIR):
path = os.path.join(PROJECTS_DIR, item)
if os.path.isdir(path) and not item.startswith('.'):
projects.append({
'name': item,
'path': path,
'time_today': get_project_time_today(item),
'total_time': get_project_total_time(item),
'protected': is_protected(item)
})
except Exception as e:
print(f"Error getting projects: {e}")
return sorted(projects, key=lambda x: x['name'])
def get_active_project():
"""Get currently active project"""
try:
if os.path.exists(ACTIVE_FILE):
with open(ACTIVE_FILE, 'r') as f:
return f.read().strip()
except Exception as e:
print(f"Error getting active project: {e}")
return None
def get_project_time_today(project_name):
"""Get time spent on project today"""
try:
if not os.path.exists(TIME_LOG):
return "0h 0m"
today = datetime.now().strftime("%Y-%m-%d")
total_minutes = 0
with open(TIME_LOG, 'r') as f:
reader = csv.DictReader(f)
for row in reader:
if row['project'] == project_name and row['date'] == today:
total_minutes += int(row['duration_minutes'])
hours = total_minutes // 60
minutes = total_minutes % 60
return f"{hours}h {minutes}m"
except Exception as e:
print(f"Error getting time: {e}")
return "0h 0m"
def get_project_total_time(project_name):
"""Get total time spent on project"""
try:
if not os.path.exists(TIME_LOG):
return "0h 0m"
total_minutes = 0
with open(TIME_LOG, 'r') as f:
reader = csv.DictReader(f)
for row in reader:
if row['project'] == project_name:
total_minutes += int(row['duration_minutes'])
hours = total_minutes // 60
minutes = total_minutes % 60
return f"{hours}h {minutes}m"
except Exception as e:
return "0h 0m"
def is_protected(project_name):
"""Check if project is protected"""
try:
result = subprocess.run(
[PROTECT_CMD, "verify", f"{PROJECTS_DIR}/{project_name}"],
capture_output=True,
text=True
)
return "CLAUDE PROOF protected" in result.stdout
except:
return False
def get_current_session():
"""Get current time tracking session"""
try:
result = subprocess.run(
[TIME_TRACKER, "status"],
capture_output=True,
text=True
)
if "Active Session" in result.stdout:
lines = result.stdout.split('\n')
session = {}
for line in lines:
if "Project:" in line:
session['project'] = line.split(":")[1].strip()
elif "Started:" in line:
session['started'] = line.split("Started:")[1].strip()
elif "Elapsed:" in line:
session['elapsed'] = line.split("Elapsed:")[1].strip()
return session
return None
except Exception as e:
print(f"Error getting session: {e}")
return None
@app.route('/')
def index():
"""Main dashboard page"""
return render_template('dashboard.html')
@app.route('/api/projects')
def api_projects():
"""API: Get all projects"""
projects = get_projects()
active = get_active_project()
session = get_current_session()
return jsonify({
'projects': projects,
'active': active,
'session': session
})
@app.route('/api/activate', methods=['POST'])
def api_activate():
"""API: Activate a project"""
data = request.json
project_name = data.get('project')
if not project_name:
return jsonify({'success': False, 'error': 'Project name required'})
try:
# Stop current if active
current = get_active_project()
if current:
subprocess.run([TIME_TRACKER, "stop"])
# Activate new project
with open(ACTIVE_FILE, 'w') as f:
f.write(project_name)
# Start time tracking
subprocess.run([TIME_TRACKER, "start", project_name, "Activated via web dashboard"])
return jsonify({'success': True, 'message': f'Activated: {project_name}'})
except Exception as e:
return jsonify({'success': False, 'error': str(e)})
@app.route('/api/stop', methods=['POST'])
def api_stop():
"""API: Stop current project"""
try:
subprocess.run([TIME_TRACKER, "stop"])
if os.path.exists(ACTIVE_FILE):
os.remove(ACTIVE_FILE)
return jsonify({'success': True, 'message': 'Project stopped'})
except Exception as e:
return jsonify({'success': False, 'error': str(e)})
@app.route('/api/protect', methods=['POST'])
def api_protect():
"""API: Protect current project"""
data = request.json
description = data.get('description', 'Protected via web dashboard')
active = get_active_project()
if not active:
return jsonify({'success': False, 'error': 'No active project'})
try:
project_path = os.path.join(PROJECTS_DIR, active)
result = subprocess.run(
[PROTECT_CMD, "protect", project_path, description],
capture_output=True,
text=True
)
return jsonify({'success': True, 'message': 'Project protected', 'output': result.stdout})
except Exception as e:
return jsonify({'success': False, 'error': str(e)})
if __name__ == '__main__':
print("🚀 Starting Project Dashboard Web Server")
print("📍 Open: http://localhost:5000")
print("⏹️ Stop: Ctrl+C")
app.run(debug=True, host='0.0.0.0', port=5000)