Skip to content

Commit d86dabb

Browse files
committed
fix: batch fiber photometry data pull to deal with asset duplicates
1 parent 85b0974 commit d86dabb

1 file changed

Lines changed: 38 additions & 24 deletions

File tree

web/src/fiber_photometry/view.js

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -508,11 +508,22 @@ export function createFiberPhotometryView(coord) {
508508
const t0 = performance.now();
509509
console.log('[FibPhot] start');
510510

511-
ensureTable(coord, 'platform_fib')
512-
.then(() => {
513-
console.log(`[FibPhot] platform_fib registered +${(performance.now()-t0).toFixed(0)}ms`);
514-
console.log(`[FibPhot] starting JOIN query (platform_fib × asset_basics) +${(performance.now()-t0).toFixed(0)}ms`);
515-
return queryRows(coord,
511+
const BATCH_SIZE = 500;
512+
const sqlStr = (s) => "'" + String(s).replace(/'/g, "''") + "'";
513+
514+
(async () => {
515+
await ensureTable(coord, 'platform_fib');
516+
console.log(`[FibPhot] platform_fib registered +${(performance.now()-t0).toFixed(0)}ms`);
517+
518+
const nameRows = await queryRows(coord, 'SELECT DISTINCT asset_name FROM platform_fib');
519+
const assetNames = nameRows.map((r) => r.asset_name).filter((n) => n != null);
520+
console.log(`[FibPhot] ${assetNames.length} distinct assets +${(performance.now()-t0).toFixed(0)}ms`);
521+
522+
const longRows = [];
523+
for (let i = 0; i < assetNames.length; i += BATCH_SIZE) {
524+
const batch = assetNames.slice(i, i + BATCH_SIZE);
525+
const inList = batch.map(sqlStr).join(', ');
526+
const rows = await queryRows(coord,
516527
`SELECT f.asset_name, f.fiber, f.channel, f.targeted_structure, f.intended_measurement,
517528
b.subject_id, b.project_name, b.acquisition_start_time,
518529
b.data_level, b.modalities_str AS modalities, b.genotype, b.location,
@@ -527,27 +538,30 @@ export function createFiberPhotometryView(coord) {
527538
array_to_string(investigators_normalized, ', ') AS investigators_str,
528539
array_to_string(experimenters_normalized, ', ') AS experimenters_str
529540
FROM asset_basics
530-
) b ON b.name = f.asset_name`,
541+
WHERE name IN (${inList})
542+
QUALIFY row_number() OVER (PARTITION BY name ORDER BY _last_modified DESC) = 1
543+
) b ON b.name = f.asset_name
544+
WHERE f.asset_name IN (${inList})`,
531545
);
532-
})
533-
.then((longRows) => {
534-
console.log(`[FibPhot] JOIN query returned +${(performance.now()-t0).toFixed(0)}ms`);
535-
console.log(`[FibPhot] result → array (${longRows.length} long rows) +${(performance.now()-t0).toFixed(0)}ms`);
536-
const wideRows = pivotLongFormRows(longRows);
537-
wideRows.sort((a, b) => {
538-
const av = a.acquisition_start_time ?? '';
539-
const bv = b.acquisition_start_time ?? '';
540-
return String(bv).localeCompare(String(av)) || String(a.asset_name ?? '').localeCompare(String(b.asset_name ?? ''));
541-
});
542-
console.log(`[FibPhot] pivot → wide (${wideRows.length} assets) +${(performance.now()-t0).toFixed(0)}ms`);
543-
loadingEl.remove();
544-
buildPage(wideRows);
545-
console.log(`[FibPhot] page built +${(performance.now()-t0).toFixed(0)}ms`);
546-
})
547-
.catch((err) => {
548-
loadingEl.className = 'loading-message error';
549-
loadingEl.textContent = `Failed to load Fiber Photometry data: ${err?.message ?? err}`;
546+
for (const r of rows) longRows.push(r);
547+
console.log(`[FibPhot] batch ${i / BATCH_SIZE + 1} (${longRows.length} rows) +${(performance.now()-t0).toFixed(0)}ms`);
548+
}
549+
550+
console.log(`[FibPhot] JOIN queries done +${(performance.now()-t0).toFixed(0)}ms`);
551+
const wideRows = pivotLongFormRows(longRows);
552+
wideRows.sort((a, b) => {
553+
const av = a.acquisition_start_time ?? '';
554+
const bv = b.acquisition_start_time ?? '';
555+
return String(bv).localeCompare(String(av)) || String(a.asset_name ?? '').localeCompare(String(b.asset_name ?? ''));
550556
});
557+
console.log(`[FibPhot] pivot → wide (${wideRows.length} assets) +${(performance.now()-t0).toFixed(0)}ms`);
558+
loadingEl.remove();
559+
buildPage(wideRows);
560+
console.log(`[FibPhot] page built +${(performance.now()-t0).toFixed(0)}ms`);
561+
})().catch((err) => {
562+
loadingEl.className = 'loading-message error';
563+
loadingEl.textContent = `Failed to load Fiber Photometry data: ${err?.message ?? err}`;
564+
});
551565

552566
function buildPage(allRows) {
553567
const channelCols = detectChannelColumns(allRows);

0 commit comments

Comments
 (0)