feat: latest backup api endpoint - /api/backups/latest

This commit is contained in:
2025-11-01 16:53:32 +00:00
parent b2cc478c85
commit 703ab3b07d
2 changed files with 40 additions and 0 deletions
+17
View File
@@ -342,6 +342,23 @@ def delete_backup(backup_id):
return redirect(url_for('backups'))
@app.route('/api/backups/latest')
def api_latest_backups():
"""API endpoint to get the latest backup date and time for each instance."""
latest_backups = db.get_latest_backup_per_instance()
# Format the response
result = []
for item in latest_backups:
result.append({
'instance_id': item['instance_id'],
'instance_name': item['instance_name'],
'latest_backup': item['latest_backup'].isoformat() if item['latest_backup'] else None
})
return jsonify(result)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
+23
View File
@@ -279,4 +279,27 @@ class Database:
except Error as e:
logger.error(f"Error getting instance by ID: {e}")
return None
def get_latest_backup_per_instance(self) -> List[Dict[str, Any]]:
"""Get the latest backup date and time for each instance."""
try:
with self.get_connection() as conn:
cursor = conn.cursor(dictionary=True)
cursor.execute("""
SELECT
o.id as instance_id,
o.name as instance_name,
o.identifier as instance_identifier,
MAX(b.uploaded_at) as latest_backup
FROM opnsense_instances o
LEFT JOIN backups b ON o.id = b.instance_id
GROUP BY o.id, o.name, o.identifier
ORDER BY o.name
""")
results = cursor.fetchall()
cursor.close()
return results
except Error as e:
logger.error(f"Error getting latest backup per instance: {e}")
return []