-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
381 lines (304 loc) · 12.3 KB
/
api.py
File metadata and controls
381 lines (304 loc) · 12.3 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
379
380
381
#!/usr/bin/env python3
"""
Task Management Dashboard API
Flask API to serve task data from PostgreSQL database to the UI
"""
from flask import Flask, jsonify, request, send_from_directory
import psycopg2
import psycopg2.extras
import os
from datetime import datetime, timedelta
import json
app = Flask(__name__, static_folder='.')
# Database connection parameters from environment variables
DB_HOST = os.environ.get("PT_DB_HOST", "localhost")
DB_PORT = int(os.environ.get("PT_DB_PORT", 5433))
DB_NAME = os.environ.get("PT_DB_NAME", "financial_analysis")
DB_USER = os.environ.get("PT_DB_USER", "finance_user")
DB_PASSWORD = os.environ.get("PT_DB_PASSWORD", "secure_finance_password")
def get_db_connection():
"""Get a connection to the PostgreSQL database."""
conn = psycopg2.connect(
host=DB_HOST,
port=DB_PORT,
database=DB_NAME,
user=DB_USER,
password=DB_PASSWORD
)
return conn
def get_projects_data():
"""Get list of all projects with task counts - standalone function."""
conn = get_db_connection()
cursor = conn.cursor()
# Get distinct projects with task counts
cursor.execute("""
SELECT
project,
COUNT(*) as total_tasks,
SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END) as completed_tasks,
SUM(CASE WHEN status = 'in-progress' THEN 1 ELSE 0 END) as in_progress_tasks,
SUM(CASE WHEN status = 'todo' OR status = 'new' THEN 1 ELSE 0 END) as todo_tasks
FROM project_tracker
GROUP BY project
ORDER BY total_tasks DESC
""")
rows = cursor.fetchall()
col_names = [desc[0] for desc in cursor.description]
conn.close()
projects = []
for row in rows:
# Convert tuple to dict using column names
row_dict = {col_names[i]: row[i] for i in range(len(col_names))}
projects.append(row_dict)
return projects
@app.route('/')
def index():
"""Serve the main dashboard page."""
return send_from_directory('.', 'dashboard.html')
@app.route('/shared-dashboard.js')
def serve_shared_dashboard_js():
"""Serve the shared dashboard JavaScript file."""
import os
# Get the directory where this script is located
script_dir = os.path.dirname(os.path.abspath(__file__))
js_file_path = os.path.join(script_dir, 'shared-dashboard.js')
try:
with open(js_file_path, 'r') as f:
js_content = f.read()
return js_content, 200, {'Content-Type': 'application/javascript'}
except FileNotFoundError:
return "Shared dashboard file not found", 404
@app.route('/api/projects')
def get_projects():
"""Get list of all projects with task counts."""
try:
projects = get_projects_data()
return jsonify(projects)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/project/<project_name>')
def get_project_details(project_name):
"""Get detailed information for a specific project."""
try:
conn = get_db_connection()
cursor = conn.cursor()
# Get task counts by status
cursor.execute("""
SELECT
status,
COUNT(*) as count
FROM project_tracker
WHERE project = %s
GROUP BY status
""", (project_name,))
status_rows = cursor.fetchall()
status_counts = {row[0]: row[1] for row in status_rows}
# Get task counts by priority
cursor.execute("""
SELECT
priority,
COUNT(*) as count
FROM project_tracker
WHERE project = %s
GROUP BY priority
""", (project_name,))
priority_rows = cursor.fetchall()
priority_counts = {row[0]: row[1] for row in priority_rows}
# Get task counts by category
cursor.execute("""
SELECT
category,
COUNT(*) as count
FROM project_tracker
WHERE project = %s
GROUP BY category
""", (project_name,))
category_rows = cursor.fetchall()
category_counts = {row[0]: row[1] for row in category_rows}
# Get recent tasks
cursor.execute("""
SELECT
id, title, status, priority, category, created_date, updated_date, tags
FROM project_tracker
WHERE project = %s
ORDER BY updated_date DESC
LIMIT 10
""", (project_name,))
recent_rows = cursor.fetchall()
recent_col_names = [desc[0] for desc in cursor.description]
recent_tasks = []
for row in recent_rows:
# Convert tuple to dict using column names
row_dict = {recent_col_names[i]: row[i] for i in range(len(recent_col_names))}
# Convert datetime objects to strings
if isinstance(row_dict.get('created_date'), (datetime)):
row_dict['created_date'] = row_dict['created_date'].isoformat()
if isinstance(row_dict.get('updated_date'), (datetime)):
row_dict['updated_date'] = row_dict['updated_date'].isoformat()
# Convert tags from JSON string to object if needed
if isinstance(row_dict.get('tags'), str):
try:
row_dict['tags'] = json.loads(row_dict['tags'])
except:
pass
recent_tasks.append(row_dict)
conn.close()
# Calculate total tasks
total_tasks = sum(status_counts.values())
return jsonify({
"project": project_name,
"statusCounts": status_counts,
"priorityCounts": priority_counts,
"categoryCounts": category_counts,
"recentTasks": recent_tasks,
"totalTasks": total_tasks
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/project/<project_name>/task/<task_id>', methods=['PUT'])
def update_task_status(project_name, task_id):
"""Update task status in the database."""
try:
data = request.get_json()
if not data:
return jsonify({"error": "No data provided"}), 400
new_status = data.get('status')
new_title = data.get('title')
new_description = data.get('description')
new_priority = data.get('priority')
updated_date = data.get('updated_date', datetime.now().strftime('%Y-%m-%d'))
conn = get_db_connection()
cursor = conn.cursor()
update_fields = []
update_values = []
if new_status:
update_fields.append("status = %s")
update_values.append(new_status)
if new_title:
update_fields.append("title = %s")
update_values.append(new_title)
if new_description:
update_fields.append("description = %s")
update_values.append(new_description)
if new_priority:
update_fields.append("priority = %s")
update_values.append(new_priority)
update_fields.append("updated_date = %s")
update_values.append(updated_date)
if not update_fields:
return jsonify({"error": "No valid fields to update"}), 400
query = f"UPDATE project_tracker SET {', '.join(update_fields)} WHERE project = %s AND id = %s RETURNING id"
cursor.execute(query, update_values + [project_name, task_id])
result = cursor.fetchone()
if not result:
conn.close()
return jsonify({"error": "Task not found"}), 404
conn.commit()
cursor.close()
conn.close()
return jsonify({
"message": f"Task {task_id} updated successfully",
"task_id": task_id
}), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/project/<project_name>/task/<task_id>', methods=['GET'])
def get_task_details(project_name, task_id):
"""Get details for a specific task."""
try:
conn = get_db_connection()
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cursor.execute("""
SELECT id, project, title, description, status, priority, category, created_date, updated_date, tags
FROM project_tracker
WHERE project = %s AND id = %s
""", (project_name, task_id))
task = cursor.fetchone()
conn.close()
if not task:
return jsonify({"error": "Task not found"}), 404
# Convert datetime objects to strings
if isinstance(task.get('created_date'), datetime):
task['created_date'] = task['created_date'].isoformat()
if isinstance(task.get('updated_date'), datetime):
task['updated_date'] = task['updated_date'].isoformat()
# Convert tags from JSON string to object if needed
if isinstance(task.get('tags'), str):
try:
task['tags'] = json.loads(task['tags'])
except:
pass
return jsonify(task)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/project/<project_name>/task', methods=['POST'])
def create_task(project_name):
"""Create a new task in the database."""
try:
data = request.get_json()
required_fields = ['title', 'description', 'status', 'priority', 'category']
if not all(field in data for field in required_fields):
return jsonify({"error": f"Missing required fields: {', '.join(required_fields)}"}), 400
title = data['title']
description = data['description']
status = data['status']
priority = data['priority']
category = data['category']
created_date = data.get('created_date', datetime.now().strftime('%Y-%m-%d'))
updated_date = data.get('updated_date', datetime.now().strftime('%Y-%m-%d'))
tags = json.dumps(data.get('tags', []))
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("""
INSERT INTO project_tracker (project, title, description, status, priority, category, created_date, updated_date, tags)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
RETURNING id
""", (project_name, title, description, status, priority, category, created_date, updated_date, tags))
new_task_id = cursor.fetchone()[0]
conn.commit()
cursor.close()
conn.close()
return jsonify({
"message": "Task created successfully",
"task_id": new_task_id,
"project": project_name
}), 201
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/stats')
def get_global_stats():
"""Get global statistics across all projects."""
try:
conn = get_db_connection()
cursor = conn.cursor()
# Get total task count
cursor.execute("SELECT COUNT(*) FROM project_tracker")
total_tasks = cursor.fetchone()[0]
# Get status distribution
cursor.execute("""
SELECT status, COUNT(*)
FROM project_tracker
GROUP BY status
""")
status_distribution = {row[0]: row[1] for row in cursor.fetchall()}
# Get priority distribution
cursor.execute("""
SELECT priority, COUNT(*)
FROM project_tracker
GROUP BY priority
""")
priority_distribution = {row[0]: row[1] for row in cursor.fetchall()}
# Get project count
cursor.execute("SELECT COUNT(DISTINCT project) FROM project_tracker")
project_count = cursor.fetchone()[0]
conn.close()
return jsonify({
"totalTasks": total_tasks,
"projectCount": project_count,
"statusDistribution": status_distribution,
"priorityDistribution": priority_distribution
})
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)