-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathforaging-metadata.js
More file actions
82 lines (71 loc) · 2.5 KB
/
Copy pathforaging-metadata.js
File metadata and controls
82 lines (71 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* foraging-metadata.js — Query helpers for the foraging_sessions DuckDB table.
*
* The table is registered from a parquet file on S3 (allen-data-views bucket)
* via cache_registry.json acorn definitions (centralized in lib/registry.js).
*/
import { ensureTable } from '../registry.js';
import { queryRows } from '../arrow.js';
const TABLE_NAME = 'foraging_sessions';
/**
* Ensure the foraging_sessions table is registered in DuckDB.
* Safe to call multiple times — uses singleton promise in registry.
*
* @param {import('@uwdata/mosaic-core').Coordinator} coordinator
*/
export async function ensureForagingTable(coordinator) {
await ensureTable(coordinator, TABLE_NAME);
}
/**
* Query foraging session metadata for a given subject + date.
*
* @param {import('@uwdata/mosaic-core').Coordinator} coordinator
* @param {string} subjectId
* @param {string} sessionDate - ISO date string (YYYY-MM-DD)
* @returns {Promise<object|null>} Session metadata row or null if not found.
*/
export async function queryForagingSession(coordinator, subjectId, sessionDate) {
try {
await ensureForagingTable(coordinator);
const safeId = subjectId.replace(/'/g, "''");
const safeDate = sessionDate.replace(/'/g, "''");
const rows = await queryRows(
coordinator,
`SELECT * FROM ${TABLE_NAME} WHERE subject_id = '${safeId}' AND session_date = '${safeDate}' LIMIT 1`,
);
return rows.length > 0 ? rows[0] : null;
} catch (err) {
console.warn('[ForagingMetadata] query failed:', err);
return null;
}
}
/**
* Query all foraging sessions for a given subject.
*
* @param {import('@uwdata/mosaic-core').Coordinator} coordinator
* @param {string} subjectId
* @returns {Promise<object[]>} Array of session metadata rows.
*/
export async function queryForagingSessionsForSubject(coordinator, subjectId) {
try {
await ensureForagingTable(coordinator);
const safeId = subjectId.replace(/'/g, "''");
const result = await coordinator.query(
`SELECT * FROM ${TABLE_NAME} WHERE subject_id = '${safeId}' ORDER BY session_date`,
);
if (!result || result.numRows === 0) return [];
const rows = [];
for (let i = 0; i < result.numRows; i++) {
const row = {};
for (const field of result.schema.fields) {
const col = result.getChild(field.name);
row[field.name] = col ? col.get(i) : null;
}
rows.push(row);
}
return rows;
} catch (err) {
console.warn('[ForagingMetadata] queryForSubject failed:', err);
return [];
}
}