Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 70 additions & 22 deletions src/lib/components/dashboard/DashboardDeviceCards.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
getDashboardDeviceNextRefreshDelayMs,
refreshDashboardDevice
} from './dashboard-device-refresh';
import { resolveDeviceTypeConfig } from './dashboard-device-data';
import {
mapDashboardPrimaryDataToDevice,
mergeDashboardDevices,
resolveDeviceTypeConfig
} from './dashboard-device-data';
import { ApiService } from '$lib/api/api.service';
import { reactiveNow } from '$lib/utils/reactive-now.svelte';
import { listDashboardDevices, type DashboardDeviceFilters } from './device-table';
Expand Down Expand Up @@ -55,6 +59,9 @@
let expandedDetailRowsByDevEui = $state<Record<string, CwSensorCardDetailRow[] | null>>({});
let isExpandingLocationWindow = $state(false);
let monitoredCardRefreshIds: string[] = [];
// Dedupe guard for the missing-primary-data backfill effect below; a plain
// Set is fine because we never want to render from it.
const backfilledDevEuis = new Set<string>();

let search = $derived(page.url.searchParams.get('search') ?? '');
let filterKey = $derived(
Expand Down Expand Up @@ -149,6 +156,23 @@
}
});

// Backfill visible cards whose device came back from the bulk primary-data
// endpoint without any reading. Scoped to visible cards so we don't spray
// requests across hundreds of never-seen devices.
$effect(() => {
if (!app.accessToken) return;

for (const card of visibleLocationCards) {
for (const sensor of card.sensors) {
if (backfilledDevEuis.has(sensor.devEui)) continue;
if (sensor.sourceDevice.has_primary_data !== false) continue;

backfilledDevEuis.add(sensor.devEui);
void fetchAndMergeLatestDeviceData(sensor.devEui);
}
}
});

$effect(() => {
if (!app.accessToken) {
cardRefreshAlarms.clear();
Expand Down Expand Up @@ -365,20 +389,10 @@
}
}

async function handleSensorExpand(sensor: DashboardSensorCardEntry) {
if (!app.accessToken || sensor.sourceDevice.has_primary_data === false) {
return;
}

const { devEui, sourceDevice } = sensor;

// Skip if already fetched or currently loading
if (devEui in expandedDetailRowsByDevEui) {
return;
}

// Mark as loading
expandedDetailRowsByDevEui[devEui] = null;
async function fetchAndMergeLatestDeviceData(
devEui: string
): Promise<Record<string, unknown> | null> {
if (!app.accessToken) return null;

try {
const api = new ApiService({ authToken: app.accessToken });
Expand All @@ -391,17 +405,51 @@
typeof v === 'string' && v.trim() !== '' && Number.isFinite(Number(v)) ? Number(v) : v
])
);
const typeConfig = resolveDeviceTypeConfig(sourceDevice, app.deviceTypeLookup);
expandedDetailRowsByDevEui[devEui] = buildDeviceExpandedDetailRows(
freshData,
typeConfig,
sensor.sensor.label
);

// Merge into app.devices so the card header's primary/secondary
// values re-derive — otherwise the card keeps whatever (possibly
// empty) reading the bulk primary-data endpoint returned at page load.
const freshDevice = mapDashboardPrimaryDataToDevice(freshData);
app.devices = mergeDashboardDevices(app.devices, [freshDevice]);

return freshData;
} catch (err) {
console.error(`Failed to load expanded data for ${devEui}:`, err);
console.error(`Failed to load latest data for ${devEui}:`, err);
return null;
}
}

async function handleSensorExpand(sensor: DashboardSensorCardEntry) {
if (!app.accessToken) {
return;
}

const { devEui } = sensor;

// Skip if already fetched or currently loading
if (devEui in expandedDetailRowsByDevEui) {
return;
}

// Mark as loading
expandedDetailRowsByDevEui[devEui] = null;

const freshData = await fetchAndMergeLatestDeviceData(devEui);
if (!freshData) {
// Remove loading marker so the user can retry by collapsing and re-expanding
delete expandedDetailRowsByDevEui[devEui];
return;
}

backfilledDevEuis.add(devEui);

const liveDevice = resolveLiveDashboardDevice(sensor.sourceDevice) ?? sensor.sourceDevice;
const typeConfig = resolveDeviceTypeConfig(liveDevice, app.deviceTypeLookup);
expandedDetailRowsByDevEui[devEui] = buildDeviceExpandedDetailRows(
freshData,
typeConfig,
sensor.sensor.label
);
}

async function refreshSingleDevice(device: IDevice | null | undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ describe('DashboardDeviceCards', () => {
name: 'Canopy',
location_name: 'Grow Room',
group: 'air',
created_at: '2026-04-09T10:00:18.000Z',
co2: 901,
humidity: 60.1,
temperature_c: 25.4,
created_at: '2026-04-09T09:50:00.000Z',
co2: 820,
humidity: 56,
temperature_c: 24.5,
location_id: 1
}));
const { component } = renderDashboardDeviceCards();
Expand Down
13 changes: 9 additions & 4 deletions src/routes/reports/[report_id]/edit/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,15 +308,20 @@ function sanitizeDataProcessingScheduleEntries(
): CreateReportDataProcessingScheduleRequest[] | undefined {
if (!Array.isArray(value)) return undefined;

// Validate HH:mm format
const isValidTime = (t: unknown): t is string => typeof t === 'string' && /^\d{2}:\d{2}$/.test(t);
// Accept both HH:mm and HH:mm:ss; normalize to HH:mm.
const TIME_PATTERN = /^(\d{2}:\d{2})(?::\d{2})?$/;
const normalizeTime = (t: unknown): string | undefined => {
if (typeof t !== 'string') return undefined;
const match = TIME_PATTERN.exec(t.trim());
return match ? match[1] : undefined;
};

const entries = value
.filter(isRecord)
.map((entry) => {
const dayOfWeek = readOptionalInteger(entry.day_of_week);
const startTime = isValidTime(entry.start_time) ? entry.start_time : undefined;
const endTime = isValidTime(entry.end_time) ? entry.end_time : undefined;
const startTime = normalizeTime(entry.start_time);
const endTime = normalizeTime(entry.end_time);

if (dayOfWeek === undefined || !startTime || !endTime) {
return null;
Expand Down
10 changes: 3 additions & 7 deletions src/routes/reports/[report_id]/edit/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -594,16 +594,12 @@
}

function addDataProcessingSchedule() {
fields.report_data_processing_schedules = [
...fields.report_data_processing_schedules,
createEmptyDataProcessingSchedule()
];
fields.report_data_processing_schedules.push(createEmptyDataProcessingSchedule());
}

function removeDataProcessingSchedule(key: string) {
fields.report_data_processing_schedules = fields.report_data_processing_schedules.filter(
(s) => s.key !== key
);
const idx = fields.report_data_processing_schedules.findIndex((s) => s.key === key);
if (idx >= 0) fields.report_data_processing_schedules.splice(idx, 1);
}

function addRecipient() {
Expand Down
Loading