Update to 0.9.3 #43

Merged
serversdown merged 9 commits from dev into main 2026-03-28 13:49:01 -04:00
4 changed files with 231 additions and 59 deletions
Showing only changes of commit ac48fb2977 - Show all commits

View File

@@ -355,8 +355,11 @@ async def nrl_detail_page(
).first() ).first()
assigned_unit = None assigned_unit = None
assigned_modem = None
if assignment: if assignment:
assigned_unit = db.query(RosterUnit).filter_by(id=assignment.unit_id).first() assigned_unit = db.query(RosterUnit).filter_by(id=assignment.unit_id).first()
if assigned_unit and assigned_unit.deployed_with_modem_id:
assigned_modem = db.query(RosterUnit).filter_by(id=assigned_unit.deployed_with_modem_id).first()
# Get session count # Get session count
session_count = db.query(MonitoringSession).filter_by(location_id=location_id).count() session_count = db.query(MonitoringSession).filter_by(location_id=location_id).count()
@@ -393,6 +396,7 @@ async def nrl_detail_page(
"location": location, "location": location,
"assignment": assignment, "assignment": assignment,
"assigned_unit": assigned_unit, "assigned_unit": assigned_unit,
"assigned_modem": assigned_modem,
"session_count": session_count, "session_count": session_count,
"file_count": file_count, "file_count": file_count,
"active_session": active_session, "active_session": active_session,

View File

@@ -353,18 +353,18 @@ async def assign_unit_to_location(
detail=f"Unit type '{unit.device_type}' does not match location type '{location.location_type}'", detail=f"Unit type '{unit.device_type}' does not match location type '{location.location_type}'",
) )
# Check if location already has an active assignment # Check if location already has an active assignment (active = assigned_until IS NULL)
existing_assignment = db.query(UnitAssignment).filter( existing_assignment = db.query(UnitAssignment).filter(
and_( and_(
UnitAssignment.location_id == location_id, UnitAssignment.location_id == location_id,
UnitAssignment.status == "active", UnitAssignment.assigned_until == None,
) )
).first() ).first()
if existing_assignment: if existing_assignment:
raise HTTPException( raise HTTPException(
status_code=400, status_code=400,
detail=f"Location already has an active unit assignment ({existing_assignment.unit_id}). Unassign first.", detail=f"Location already has an active unit assignment ({existing_assignment.unit_id}). Use swap to replace it.",
) )
# Create new assignment # Create new assignment
@@ -433,10 +433,120 @@ async def unassign_unit(
return {"success": True, "message": "Unit unassigned successfully"} return {"success": True, "message": "Unit unassigned successfully"}
@router.post("/locations/{location_id}/swap")
async def swap_unit_on_location(
project_id: str,
location_id: str,
request: Request,
db: Session = Depends(get_db),
):
"""
Swap the unit assigned to a vibration monitoring location.
Ends the current active assignment (if any), creates a new one,
and optionally updates modem pairing on the seismograph.
Works for first-time assignments too (no current assignment = just create).
"""
location = db.query(MonitoringLocation).filter_by(
id=location_id,
project_id=project_id,
).first()
if not location:
raise HTTPException(status_code=404, detail="Location not found")
form_data = await request.form()
unit_id = form_data.get("unit_id")
modem_id = form_data.get("modem_id") or None
notes = form_data.get("notes") or None
if not unit_id:
raise HTTPException(status_code=400, detail="unit_id is required")
# Validate new unit
unit = db.query(RosterUnit).filter_by(id=unit_id).first()
if not unit:
raise HTTPException(status_code=404, detail="Unit not found")
expected_device_type = "slm" if location.location_type == "sound" else "seismograph"
if unit.device_type != expected_device_type:
raise HTTPException(
status_code=400,
detail=f"Unit type '{unit.device_type}' does not match location type '{location.location_type}'",
)
# End current active assignment if one exists (active = assigned_until IS NULL)
current = db.query(UnitAssignment).filter(
and_(
UnitAssignment.location_id == location_id,
UnitAssignment.assigned_until == None,
)
).first()
if current:
current.assigned_until = datetime.utcnow()
current.status = "completed"
# Create new assignment
new_assignment = UnitAssignment(
id=str(uuid.uuid4()),
unit_id=unit_id,
location_id=location_id,
project_id=project_id,
device_type=unit.device_type,
assigned_until=None,
status="active",
notes=notes,
)
db.add(new_assignment)
# Update modem pairing on the seismograph if modem provided
if modem_id:
modem = db.query(RosterUnit).filter_by(id=modem_id, device_type="modem").first()
if not modem:
raise HTTPException(status_code=404, detail=f"Modem '{modem_id}' not found")
unit.deployed_with_modem_id = modem_id
modem.deployed_with_unit_id = unit_id
else:
# Clear modem pairing if not provided
unit.deployed_with_modem_id = None
db.commit()
return JSONResponse({
"success": True,
"assignment_id": new_assignment.id,
"message": f"Unit '{unit_id}' assigned to '{location.name}'" + (f" with modem '{modem_id}'" if modem_id else ""),
})
# ============================================================================ # ============================================================================
# Available Units for Assignment # Available Units for Assignment
# ============================================================================ # ============================================================================
@router.get("/available-modems", response_class=JSONResponse)
async def get_available_modems(
project_id: str,
db: Session = Depends(get_db),
):
"""
Get all deployed, non-retired modems for the modem assignment dropdown.
"""
modems = db.query(RosterUnit).filter(
and_(
RosterUnit.device_type == "modem",
RosterUnit.deployed == True,
RosterUnit.retired == False,
)
).order_by(RosterUnit.id).all()
return [
{
"id": m.id,
"hardware_model": m.hardware_model,
"ip_address": m.ip_address,
}
for m in modems
]
@router.get("/available-units", response_class=JSONResponse) @router.get("/available-units", response_class=JSONResponse)
async def get_available_units( async def get_available_units(
project_id: str, project_id: str,

View File

@@ -838,6 +838,7 @@ async function loadProjectDetails() {
// Update tab labels and visibility based on project type // Update tab labels and visibility based on project type
const isSoundProject = projectTypeId === 'sound_monitoring'; const isSoundProject = projectTypeId === 'sound_monitoring';
const isVibrationProject = projectTypeId === 'vibration_monitoring';
if (isSoundProject) { if (isSoundProject) {
document.getElementById('locations-tab-label').textContent = 'NRLs'; document.getElementById('locations-tab-label').textContent = 'NRLs';
document.getElementById('locations-header').textContent = 'Noise Recording Locations'; document.getElementById('locations-header').textContent = 'Noise Recording Locations';
@@ -848,9 +849,9 @@ async function loadProjectDetails() {
const isRemote = mode === 'remote'; const isRemote = mode === 'remote';
document.getElementById('sessions-tab-btn').classList.toggle('hidden', !isSoundProject); document.getElementById('sessions-tab-btn').classList.toggle('hidden', !isSoundProject);
document.getElementById('data-tab-btn').classList.toggle('hidden', !isSoundProject); document.getElementById('data-tab-btn').classList.toggle('hidden', !isSoundProject);
// Schedules and Assigned Units are remote-only (manual projects collect data by hand) // Schedules and Assigned Units: hidden for vibration; for sound, only show if remote
document.getElementById('schedules-tab-btn')?.classList.toggle('hidden', isSoundProject && !isRemote); document.getElementById('schedules-tab-btn')?.classList.toggle('hidden', isVibrationProject || (isSoundProject && !isRemote));
document.getElementById('units-tab-btn')?.classList.toggle('hidden', isSoundProject && !isRemote); document.getElementById('units-tab-btn')?.classList.toggle('hidden', isVibrationProject || (isSoundProject && !isRemote));
// FTP browser within Data Files tab // FTP browser within Data Files tab
document.getElementById('ftp-browser')?.classList.toggle('hidden', !isRemote); document.getElementById('ftp-browser')?.classList.toggle('hidden', !isRemote);

View File

@@ -37,7 +37,7 @@
{{ location.name }} {{ location.name }}
</h1> </h1>
<p class="text-gray-600 dark:text-gray-400 mt-1"> <p class="text-gray-600 dark:text-gray-400 mt-1">
Monitoring Location {{ project.name }} Monitoring Location &bull; {{ project.name }}
</p> </p>
</div> </div>
<div class="flex gap-2"> <div class="flex gap-2">
@@ -116,20 +116,36 @@
<h2 class="text-xl font-semibold text-gray-900 dark:text-white mb-4">Unit Assignment</h2> <h2 class="text-xl font-semibold text-gray-900 dark:text-white mb-4">Unit Assignment</h2>
{% if assigned_unit %} {% if assigned_unit %}
<div class="space-y-4"> <div class="space-y-4">
<!-- Seismograph row -->
<div> <div>
<div class="text-sm text-gray-600 dark:text-gray-400">Assigned Unit</div> <div class="text-sm text-gray-600 dark:text-gray-400">Seismograph</div>
<div class="text-lg font-medium text-gray-900 dark:text-white"> <div class="text-lg font-medium text-gray-900 dark:text-white">
<a href="/unit/{{ assigned_unit.id }}" class="text-seismo-orange hover:text-seismo-navy"> <a href="/unit/{{ assigned_unit.id }}" class="text-seismo-orange hover:text-seismo-navy">
{{ assigned_unit.id }} {{ assigned_unit.id }}
</a> </a>
</div> </div>
{% if assigned_unit.unit_type %}
<div class="text-xs text-gray-500 dark:text-gray-400 mt-0.5">{{ assigned_unit.unit_type }}</div>
{% endif %}
</div> </div>
{% if assigned_unit.device_type %} <!-- Modem row -->
<div> <div>
<div class="text-sm text-gray-600 dark:text-gray-400">Device Type</div> <div class="text-sm text-gray-600 dark:text-gray-400">Modem</div>
<div class="text-gray-900 dark:text-white">{{ assigned_unit.device_type|capitalize }}</div> {% if assigned_modem %}
<div class="text-lg font-medium text-gray-900 dark:text-white">
<a href="/unit/{{ assigned_modem.id }}" class="text-seismo-orange hover:text-seismo-navy">
{{ assigned_modem.id }}
</a>
</div>
{% if assigned_modem.hardware_model or assigned_modem.ip_address %}
<div class="text-xs text-gray-500 dark:text-gray-400 mt-0.5">
{{ assigned_modem.hardware_model or '' }}{% if assigned_modem.hardware_model and assigned_modem.ip_address %} &bull; {% endif %}{{ assigned_modem.ip_address or '' }}
</div> </div>
{% endif %} {% endif %}
{% else %}
<div class="text-sm text-gray-400 dark:text-gray-500 italic">No modem paired</div>
{% endif %}
</div>
{% if assignment %} {% if assignment %}
<div> <div>
<div class="text-sm text-gray-600 dark:text-gray-400">Assigned Since</div> <div class="text-sm text-gray-600 dark:text-gray-400">Assigned Since</div>
@@ -142,10 +158,14 @@
</div> </div>
{% endif %} {% endif %}
{% endif %} {% endif %}
<div class="pt-2"> <div class="pt-2 flex gap-2 flex-wrap">
<button onclick="openSwapModal()"
class="px-4 py-2 bg-seismo-orange text-white rounded-lg hover:bg-seismo-navy transition-colors text-sm">
Swap Unit / Modem
</button>
<button onclick="unassignUnit('{{ assignment.id }}')" <button onclick="unassignUnit('{{ assignment.id }}')"
class="px-4 py-2 bg-amber-100 text-amber-800 dark:bg-amber-900/30 dark:text-amber-300 rounded-lg hover:bg-amber-200 dark:hover:bg-amber-900/50 transition-colors"> class="px-4 py-2 bg-amber-100 text-amber-800 dark:bg-amber-900/30 dark:text-amber-300 rounded-lg hover:bg-amber-200 dark:hover:bg-amber-900/50 transition-colors text-sm">
Unassign Unit Unassign
</button> </button>
</div> </div>
</div> </div>
@@ -155,7 +175,7 @@
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 13V6a2 2 0 00-2-2H6a2 2 0 00-2 2v7m16 0v5a2 2 0 01-2 2H6a2 2 0 01-2-2v-5m16 0h-2.586a1 1 0 00-.707.293l-2.414 2.414a1 1 0 01-.707.293h-3.172a1 1 0 01-.707-.293l-2.414-2.414A1 1 0 006.586 13H4"></path> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 13V6a2 2 0 00-2-2H6a2 2 0 00-2 2v7m16 0v5a2 2 0 01-2 2H6a2 2 0 01-2-2v-5m16 0h-2.586a1 1 0 00-.707.293l-2.414 2.414a1 1 0 01-.707.293h-3.172a1 1 0 01-.707-.293l-2.414-2.414A1 1 0 006.586 13H4"></path>
</svg> </svg>
<p class="text-gray-500 dark:text-gray-400 mb-4">No unit currently assigned</p> <p class="text-gray-500 dark:text-gray-400 mb-4">No unit currently assigned</p>
<button onclick="openAssignModal()" <button onclick="openSwapModal()"
class="px-4 py-2 bg-seismo-orange text-white rounded-lg hover:bg-seismo-navy transition-colors"> class="px-4 py-2 bg-seismo-orange text-white rounded-lg hover:bg-seismo-navy transition-colors">
Assign a Unit Assign a Unit
</button> </button>
@@ -214,47 +234,55 @@
</div> </div>
</div> </div>
<!-- Assign Unit Modal --> <!-- Assign / Swap Modal -->
<div id="assign-modal" class="hidden fixed inset-0 bg-black bg-opacity-50 z-50 flex items-center justify-center"> <div id="swap-modal" class="hidden fixed inset-0 bg-black bg-opacity-50 z-50 flex items-center justify-center">
<div class="bg-white dark:bg-slate-800 rounded-xl shadow-2xl w-full max-w-2xl max-h-[90vh] overflow-y-auto m-4"> <div class="bg-white dark:bg-slate-800 rounded-xl shadow-2xl w-full max-w-2xl max-h-[90vh] overflow-y-auto m-4">
<div class="p-6 border-b border-gray-200 dark:border-gray-700 flex items-center justify-between"> <div class="p-6 border-b border-gray-200 dark:border-gray-700 flex items-center justify-between">
<div> <div>
<h2 class="text-2xl font-bold text-gray-900 dark:text-white">Assign Unit</h2> <h2 id="swap-modal-title" class="text-2xl font-bold text-gray-900 dark:text-white">Assign Unit</h2>
<p class="text-gray-600 dark:text-gray-400 mt-1">Attach a seismograph to this location</p> <p class="text-gray-600 dark:text-gray-400 mt-1">Select a seismograph and optionally a modem for this location</p>
</div> </div>
<button onclick="closeAssignModal()" class="text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200"> <button onclick="closeSwapModal()" class="text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200">
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"> <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
</svg> </svg>
</button> </button>
</div> </div>
<form id="assign-form" class="p-6 space-y-4"> <form id="swap-form" class="p-6 space-y-4">
<div> <div>
<label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Available Units</label> <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Seismograph <span class="text-red-500">*</span></label>
<select id="assign-unit-id" name="unit_id" <select id="swap-unit-id" name="unit_id"
class="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white" required> class="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white" required>
<option value="">Loading units...</option> <option value="">Loading units...</option>
</select> </select>
<p id="assign-empty" class="hidden text-xs text-gray-500 mt-2">No available seismographs for this project.</p> <p id="swap-units-empty" class="hidden text-xs text-gray-500 mt-1">No available seismographs.</p>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Modem <span class="text-xs text-gray-400">(optional)</span></label>
<select id="swap-modem-id" name="modem_id"
class="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white">
<option value="">No modem</option>
</select>
</div> </div>
<div> <div>
<label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Notes</label> <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Notes</label>
<textarea id="assign-notes" name="notes" rows="2" <textarea id="swap-notes" name="notes" rows="2"
class="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white"></textarea> class="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white"></textarea>
</div> </div>
<div id="assign-error" class="hidden text-sm text-red-600"></div> <div id="swap-error" class="hidden text-sm text-red-600"></div>
<div class="flex justify-end gap-3 pt-2"> <div class="flex justify-end gap-3 pt-2">
<button type="button" onclick="closeAssignModal()" <button type="button" onclick="closeSwapModal()"
class="px-6 py-2 border border-gray-300 dark:border-gray-600 rounded-lg text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700"> class="px-6 py-2 border border-gray-300 dark:border-gray-600 rounded-lg text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700">
Cancel Cancel
</button> </button>
<button type="submit" <button type="submit" id="swap-submit-btn"
class="px-6 py-2 bg-seismo-orange hover:bg-seismo-navy text-white rounded-lg font-medium"> class="px-6 py-2 bg-seismo-orange hover:bg-seismo-navy text-white rounded-lg font-medium">
Assign Unit Assign
</button> </button>
</div> </div>
</form> </form>
@@ -264,6 +292,7 @@
<script> <script>
const projectId = "{{ project_id }}"; const projectId = "{{ project_id }}";
const locationId = "{{ location_id }}"; const locationId = "{{ location_id }}";
const hasAssignment = {{ 'true' if assigned_unit else 'false' }};
// Tab switching // Tab switching
function switchTab(tabName) { function switchTab(tabName) {
@@ -314,60 +343,89 @@ document.getElementById('location-settings-form').addEventListener('submit', asy
} }
}); });
// Assign modal // Swap / Assign modal
function openAssignModal() { async function openSwapModal() {
document.getElementById('assign-modal').classList.remove('hidden'); document.getElementById('swap-modal').classList.remove('hidden');
loadAvailableUnits(); document.getElementById('swap-modal-title').textContent = hasAssignment ? 'Swap Unit / Modem' : 'Assign Unit';
document.getElementById('swap-submit-btn').textContent = hasAssignment ? 'Swap' : 'Assign';
document.getElementById('swap-error').classList.add('hidden');
document.getElementById('swap-notes').value = '';
await Promise.all([loadSwapUnits(), loadSwapModems()]);
} }
function closeAssignModal() { function closeSwapModal() {
document.getElementById('assign-modal').classList.add('hidden'); document.getElementById('swap-modal').classList.add('hidden');
} }
async function loadAvailableUnits() { async function loadSwapUnits() {
try { try {
const response = await fetch(`/api/projects/${projectId}/available-units?location_type=vibration`); const response = await fetch(`/api/projects/${projectId}/available-units?location_type=vibration`);
if (!response.ok) throw new Error('Failed to load available units'); if (!response.ok) throw new Error('Failed to load units');
const data = await response.json(); const data = await response.json();
const select = document.getElementById('assign-unit-id'); const select = document.getElementById('swap-unit-id');
select.innerHTML = '<option value="">Select a unit</option>'; select.innerHTML = '<option value="">Select a seismograph</option>';
if (!data.length) { if (!data.length) {
document.getElementById('assign-empty').classList.remove('hidden'); document.getElementById('swap-units-empty').classList.remove('hidden');
return; } else {
document.getElementById('swap-units-empty').classList.add('hidden');
} }
data.forEach(unit => { data.forEach(unit => {
const option = document.createElement('option'); const option = document.createElement('option');
option.value = unit.id; option.value = unit.id;
option.textContent = `${unit.id}${unit.model || unit.device_type}`; option.textContent = unit.id + (unit.model ? ` \u2022 ${unit.model}` : '') + (unit.location ? ` \u2014 ${unit.location}` : '');
select.appendChild(option); select.appendChild(option);
}); });
} catch (err) { } catch (err) {
const errorEl = document.getElementById('assign-error'); document.getElementById('swap-error').textContent = 'Failed to load seismographs.';
errorEl.textContent = err.message || 'Failed to load units.'; document.getElementById('swap-error').classList.remove('hidden');
errorEl.classList.remove('hidden');
} }
} }
document.getElementById('assign-form').addEventListener('submit', async function(e) { async function loadSwapModems() {
try {
const response = await fetch(`/api/projects/${projectId}/available-modems`);
if (!response.ok) throw new Error('Failed to load modems');
const data = await response.json();
const select = document.getElementById('swap-modem-id');
select.innerHTML = '<option value="">No modem</option>';
data.forEach(modem => {
const option = document.createElement('option');
option.value = modem.id;
let label = modem.id;
if (modem.hardware_model) label += ` \u2022 ${modem.hardware_model}`;
if (modem.ip_address) label += ` \u2014 ${modem.ip_address}`;
option.textContent = label;
select.appendChild(option);
});
} catch (err) {
// Modem list failure is non-fatal — just leave blank
console.warn('Failed to load modems:', err);
}
}
document.getElementById('swap-form').addEventListener('submit', async function(e) {
e.preventDefault(); e.preventDefault();
const unitId = document.getElementById('assign-unit-id').value; const unitId = document.getElementById('swap-unit-id').value;
const notes = document.getElementById('assign-notes').value.trim(); const modemId = document.getElementById('swap-modem-id').value;
const notes = document.getElementById('swap-notes').value.trim();
if (!unitId) { if (!unitId) {
document.getElementById('assign-error').textContent = 'Select a unit to assign.'; document.getElementById('swap-error').textContent = 'Please select a seismograph.';
document.getElementById('assign-error').classList.remove('hidden'); document.getElementById('swap-error').classList.remove('hidden');
return; return;
} }
try { try {
const formData = new FormData(); const formData = new FormData();
formData.append('unit_id', unitId); formData.append('unit_id', unitId);
formData.append('notes', notes); if (modemId) formData.append('modem_id', modemId);
if (notes) formData.append('notes', notes);
const response = await fetch(`/api/projects/${projectId}/locations/${locationId}/assign`, { const response = await fetch(`/api/projects/${projectId}/locations/${locationId}/swap`, {
method: 'POST', method: 'POST',
body: formData body: formData
}); });
@@ -379,9 +437,8 @@ document.getElementById('assign-form').addEventListener('submit', async function
window.location.reload(); window.location.reload();
} catch (err) { } catch (err) {
const errorEl = document.getElementById('assign-error'); document.getElementById('swap-error').textContent = err.message || 'Failed to assign unit.';
errorEl.textContent = err.message || 'Failed to assign unit.'; document.getElementById('swap-error').classList.remove('hidden');
errorEl.classList.remove('hidden');
} }
}); });
@@ -405,11 +462,11 @@ async function unassignUnit(assignmentId) {
} }
document.addEventListener('keydown', function(e) { document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') closeAssignModal(); if (e.key === 'Escape') closeSwapModal();
}); });
document.getElementById('assign-modal')?.addEventListener('click', function(e) { document.getElementById('swap-modal')?.addEventListener('click', function(e) {
if (e.target === this) closeAssignModal(); if (e.target === this) closeSwapModal();
}); });
</script> </script>
{% endblock %} {% endblock %}