-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_processor.js
More file actions
278 lines (233 loc) · 9.57 KB
/
task_processor.js
File metadata and controls
278 lines (233 loc) · 9.57 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
* Task Processor
* Handles loading and processing task definitions for the database editor
*/
function validateTask(task, taskName) {
const errors = [];
if (!task.name) errors.push('Missing required field: name');
if (!task.main_table) errors.push('Missing required field: main_table');
if (!task.record_identifier) errors.push('Missing required field: record_identifier');
if (!task.column_groups) errors.push('Missing required field: column_groups');
if (!task.fields) errors.push('Missing required field: fields');
if (task.column_groups && task.record_identifier) {
const recordGroup = task.column_groups[task.record_identifier];
if (!recordGroup) {
errors.push(`Record identifier group '${task.record_identifier}' not found in column_groups`);
} else {
if (!recordGroup.columns || !Array.isArray(recordGroup.columns) || recordGroup.columns.length === 0) {
errors.push(`Record identifier group '${task.record_identifier}' must have at least one column`);
}
if (!recordGroup.values || !recordGroup.values.type) {
errors.push(`Record identifier group '${task.record_identifier}' missing values.type`);
}
}
}
if (task.fields && task.column_groups) {
task.fields.forEach(fieldName => {
if (!task.column_groups[fieldName]) {
errors.push(`Field group '${fieldName}' not found in column_groups`);
}
});
}
if (errors.length > 0) {
throw new Error(`Invalid task definition '${taskName}':\n - ${errors.join('\n - ')}`);
}
}
export async function loadTaskDefinitions(config) {
try {
const taskPromises = config.tasks.map(async (taskName) => {
const response = await fetch(`${config.tasksDirectory}/${taskName}.json`);
const task = await response.json();
validateTask(task, taskName);
return task;
});
return await Promise.all(taskPromises);
} catch (error) {
console.error('Error loading tasks:', error);
throw error;
}
}
export function asArray(value) {
return Array.isArray(value) ? value : [value];
}
export function getRecordSelector(task) {
return task.column_groups[task.record_identifier].columns[0];
}
export function buildSelectRecordQuery(task, recordId) {
const selector = getRecordSelector(task);
return {
sql: `SELECT * FROM ${task.main_table} WHERE ${selector} = ?`,
params: [recordId]
};
}
export function buildUpdateQuery(task, column, recordId, value) {
const selector = getRecordSelector(task);
return {
sql: `UPDATE ${task.main_table} SET ${column} = ? WHERE ${selector} = ?`,
params: [value, recordId]
};
}
function parseColumnName(columnName, mainTable) {
if (columnName.includes('.')) {
const parts = columnName.split('.');
return { table: parts[0], column: parts[1] };
}
return { table: mainTable, column: columnName };
}
// Cache for enum groups to avoid rebuilding on every query
const enumGroupsCache = new WeakMap();
function getEnumGroups(task) {
if (enumGroupsCache.has(task)) {
return enumGroupsCache.get(task);
}
const enumGroups = {};
Object.entries(task.column_groups).forEach(([name, group]) => {
if (group.values.type === 'enum') {
group.columns.forEach(col => {
enumGroups[col] = group.values.options;
});
}
});
enumGroupsCache.set(task, enumGroups);
return enumGroups;
}
export function buildRecordQuery(task) {
const recordGroup = task.column_groups[task.record_identifier];
if (!recordGroup) {
throw new Error(`Record identifier group '${task.record_identifier}' not found`);
}
const selectorColumn = recordGroup.columns[0];
// If no display columns, simple query
if (!recordGroup.display) {
return `SELECT ${selectorColumn}, * FROM ${task.main_table}`;
}
const displayCols = asArray(recordGroup.display);
const hasJoin = recordGroup.values.type === 'join';
const enumGroups = getEnumGroups(task);
// Build select for each display column
const selects = displayCols.map((qualifiedCol, i) => {
const { table, column } = parseColumnName(qualifiedCol, task.main_table);
const tableAlias = hasJoin && table !== task.main_table ? 'fk' : 't';
const aliasName = `_display_${i}`;
if (enumGroups[column]) {
const cases = Object.entries(enumGroups[column])
.map(([value, label]) => `WHEN ${tableAlias}.${column} = ${value} THEN '${label.replace(/'/g, "''")}'`)
.join(' ');
return `CASE ${cases} ELSE ${tableAlias}.${column} END as ${aliasName}`;
}
return `${tableAlias}.${column} as ${aliasName}`;
});
const allSelects = selects.join(', ');
if (hasJoin) {
const join = recordGroup.values;
return `SELECT t.${selectorColumn}, t.*, ${allSelects}
FROM ${task.main_table} t
LEFT JOIN ${join.table} fk ON t.${join.local_key} = fk.${join.foreign_key}`;
} else {
return `SELECT t.${selectorColumn}, t.*, ${allSelects}
FROM ${task.main_table} t`;
}
}
export function findDisplayIndices(task, columns) {
const recordGroup = task.column_groups[task.record_identifier];
if (!recordGroup || !recordGroup.display) return [];
const displayCols = asArray(recordGroup.display);
return displayCols.map((col, i) => columns.indexOf(`_display_${i}`));
}
export function findBestDisplayColumn(columns, idIndex) {
for (let i = 0; i < columns.length; i++) {
const col = columns[i].toLowerCase();
if (col.includes('firstlast')) return i;
if (col.includes('name')) return i;
}
return idIndex;
}
export function formatRecordLabel(task, row, columns, idIndex, displayIndices) {
if (displayIndices.length > 0) {
const labelParts = displayIndices
.map(idx => row[idx])
.filter(v => v != null);
return labelParts.join(', ');
}
const displayIndex = findBestDisplayColumn(columns, idIndex);
if (displayIndex !== idIndex) {
return `${row[displayIndex]}`;
}
return `ID: ${row[idIndex]}`;
}
export function generateLabel(columnName) {
let label = columnName;
const prefixes = ['gene_sz_', 'gene_i_', 'gene_f_', 'gene_b_',
'charac_i_', 'value_i_', 'value_f_',
'fkID', 'fk'];
for (const prefix of prefixes) {
if (label.startsWith(prefix)) {
label = label.substring(prefix.length);
break;
}
}
label = label.replace(/_(i|f|sz|b)_/, '_');
label = label.replace(/_/g, ' ');
label = label.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join(' ');
return label;
}
export function buildFieldDefinitions(task, db, row) {
const fields = [];
task.fields.forEach(groupName => {
const group = task.column_groups[groupName];
if (!group) {
console.warn(`Field group '${groupName}' not found`);
return;
}
const columns = group.columns;
const valueSpec = group.values;
columns.forEach(column => {
const field = {
column: column,
label: generateLabel(column),
currentValue: row[column],
type: valueSpec.type
};
switch (valueSpec.type) {
case 'readonly':
field.options = null;
break;
case 'enum':
field.options = Object.entries(valueSpec.options).map(([key, display]) => ({
value: isNaN(key) ? key : Number(key),
display: display
}));
break;
case 'query':
const queryResult = db.exec(valueSpec.sql);
field.options = queryResult.length > 0 ? queryResult[0].values.map(queryRow => queryRow[0]) : [];
break;
case 'list':
field.options = valueSpec.options;
// Check if consecutive integers for slider
const isConsecutive = field.options.length > 0 &&
field.options.every((val, idx) => {
if (typeof val !== 'number' || !Number.isInteger(val)) return false;
if (idx === 0) return true;
return val === field.options[idx - 1] + 1;
});
field.isConsecutiveIntegers = isConsecutive;
break;
case 'join':
// For join fields, use first display column or foreign key as fallback
const displayCol = group.display ? (Array.isArray(group.display) ? group.display[0] : group.display) : valueSpec.foreign_key;
const { column: displayColumn } = parseColumnName(displayCol, task.main_table);
const joinResult = db.exec(`SELECT ${valueSpec.foreign_key}, ${displayColumn} FROM ${valueSpec.table} ORDER BY ${displayColumn}`);
field.options = joinResult.length > 0 ? joinResult[0].values.map(fkRow => ({
value: fkRow[0],
display: fkRow[1]
})) : [];
break;
}
fields.push(field);
});
});
return fields;
}