feat/sort-instances #69

Merged
josh merged 10 commits from feat/sort-instances into dev 2026-03-29 08:46:15 -04:00
4 changed files with 27 additions and 4 deletions
Showing only changes of commit b6ca460ac6 - Show all commits

View File

@@ -48,6 +48,13 @@
<select id="filter-stack" onchange="filterInstances()">
<option value="">all stacks</option>
</select>
<select id="sort-field" onchange="filterInstances()">
<option value="name">name</option>
<option value="vmid">vmid</option>
<option value="updated_at">last updated</option>
<option value="created_at">last created</option>
</select>
<button id="sort-dir" class="btn" data-dir="asc" onclick="toggleSortDir()" title="reverse sort"></button>
<div class="toolbar-right">
<button class="btn primary" onclick="openNewModal()">+ new instance</button>
</div>

View File

@@ -100,11 +100,21 @@ function setStateFilter(state) {
filterInstances();
}
function toggleSortDir() {
const btn = document.getElementById('sort-dir');
const next = btn.dataset.dir === 'asc' ? 'desc' : 'asc';
btn.dataset.dir = next;
btn.textContent = next === 'asc' ? '↑' : '↓';
filterInstances();
}
async function filterInstances() {
const search = document.getElementById('search-input').value;
const state = document.getElementById('filter-state').value;
const stack = document.getElementById('filter-stack').value;
const instances = await getInstances({ search, state, stack });
const sort = document.getElementById('sort-field').value;
const order = document.getElementById('sort-dir').dataset.dir || 'asc';
const instances = await getInstances({ search, state, stack, sort, order });
const grid = document.getElementById('instance-grid');
if (!instances.length) {

View File

@@ -133,6 +133,8 @@ function seedJobs() {
// ── Queries ───────────────────────────────────────────────────────────────────
const VALID_SORTS = ['name', 'vmid', 'updated_at', 'created_at'];
export function getInstances(filters = {}) {
const parts = ['SELECT * FROM instances WHERE 1=1'];
const params = {};
@@ -142,7 +144,11 @@ export function getInstances(filters = {}) {
}
if (filters.state) { parts.push('AND state = @state'); params.state = filters.state; }
if (filters.stack) { parts.push('AND stack = @stack'); params.stack = filters.stack; }
parts.push('ORDER BY name ASC');
const sortField = VALID_SORTS.includes(filters.sort) ? filters.sort : 'name';
const sortOrder = filters.order === 'desc' ? 'DESC' : 'ASC';
// id is a stable tiebreaker for timestamp fields (datetime precision is 1 s)
const tiebreaker = (sortField === 'created_at' || sortField === 'updated_at') ? `, id ${sortOrder}` : '';
parts.push(`ORDER BY ${sortField} ${sortOrder}${tiebreaker}`);
return db.prepare(parts.join(' ')).all(params);
}

View File

@@ -69,8 +69,8 @@ router.get('/instances/stacks', (_req, res) => {
// GET /api/instances
router.get('/instances', (req, res) => {
const { search, state, stack } = req.query;
res.json(getInstances({ search, state, stack }));
const { search, state, stack, sort, order } = req.query;
res.json(getInstances({ search, state, stack, sort, order }));
});
// GET /api/instances/:vmid/history