-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessTemplateData.js
More file actions
160 lines (141 loc) · 5.84 KB
/
processTemplateData.js
File metadata and controls
160 lines (141 loc) · 5.84 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
// Shared logic for processing template data
// Works in both browser (main.js) and Node.js (mcp_server.mjs)
const GEN_TO_ALLOC = {
"aposmm": {
alloc_module: "persistent_aposmm_alloc",
alloc_function: "persistent_aposmm_alloc",
alloc_specs_user: ""
},
"default": {
alloc_module: "start_only_persistent",
alloc_function: "only_persistent_gens",
alloc_specs_user: 'user={"async_return": True},'
}
};
function processTemplateData(data, generatorSpecs = {}) {
// Set dimension, lb_array, ub_array
data.dimension = parseInt(data.dimension || 2);
data.lb_array = 'np.array([' + Array(data.dimension).fill(0.0).join(', ') + '])';
data.ub_array = 'np.array([' + Array(data.dimension).fill(3.0).join(', ') + '])';
// Custom gen_specs logic
const genModule = (data.gen_module || '').toLowerCase().trim();
const genFunc = (data.gen_function || '').toLowerCase().trim();
const combinedKey = genModule + '.' + genFunc;
let customSpec = null;
// Try direct match (case-insensitive, trimmed)
for (const key in generatorSpecs) {
if (key.toLowerCase().trim() === combinedKey) {
customSpec = generatorSpecs[key];
break;
}
}
// Store the custom spec for rendering
data._custom_spec = customSpec;
// GPU settings
data.auto_gpus = data.auto_gpus || false;
const rawGpus = data.gpus || 0;
data.num_gpus = rawGpus === "" || rawGpus === 0 ? 0 : parseInt(rawGpus);
data.gpus_line = (!data.auto_gpus && data.num_gpus > 0) ? `num_gpus=${data.num_gpus},` : "";
data.needs_mpich_gpu_support = data.auto_gpus || data.num_gpus > 0;
// Cluster settings
data.cluster_enabled = data.cluster_enable || false;
data.cluster_total_nodes = data.cluster_enabled ? (data.cluster_total_nodes || null) : null;
data.scheduler_type = data.cluster_enabled ? (data.scheduler_type || null) : null;
data.total_nodes = data.cluster_total_nodes; // For template compatibility
// Input handling
data.input_type = data.input_type || 'file';
data.input_usage = data.input_usage || 'directory';
data.input_usage_cmdline = (data.input_usage === "cmdline");
data.templated_enabled = data.templated_enable || false;
// Collect template variables (ensure it's an array)
const templateVars = Array.isArray(data.template_vars)
? data.template_vars.filter(v => v && v.trim() !== '')
: [];
// Set template data based on input type and templated settings
if (data.input_type === 'file') {
data.input_file = data.input_path;
data.input_file_basename = data.input_path ? data.input_path.split(/[\\/]/).pop() : null;
data.sim_input_dir = null;
data.templated_filename = null;
} else {
data.input_file = null;
data.input_file_basename = null;
data.sim_input_dir = data.input_path;
data.templated_filename = data.templated_enabled ? (data.templated_filename || null) : null;
}
// Determine input_filename for user/app_args
const inputFilename = (() => {
if (data.input_type === 'file' && data.input_path) {
return data.input_path.split(/[\\/]/).pop();
} else if (data.input_type === 'directory' && data.templated_enabled && data.templated_filename) {
return data.templated_filename;
}
return '';
})();
// Set input_filename when "in command line" is selected OR when there are templated values
const needsInputFilename = (data.input_usage === 'cmdline') || (data.templated_enabled && templateVars.length > 0);
if (needsInputFilename && inputFilename) {
data.input_filename = inputFilename;
} else {
delete data.input_filename;
}
// Only set input_names if there are templated values
if (data.templated_enabled && templateVars.length > 0) {
data.has_template_vars = true;
data.template_vars_list = templateVars.map(v => `"${v}"`).join(', ');
data.input_names = templateVars;
data.has_input_names = true;
} else {
data.has_template_vars = false;
data.template_vars_list = '';
delete data.input_names;
data.has_input_names = false;
}
// Allocation settings
if (data.gen_function && data.gen_function.toLowerCase().includes("aposmm")) {
const allocInfo = GEN_TO_ALLOC["aposmm"];
data.alloc_module = allocInfo.alloc_module;
data.alloc_function = allocInfo.alloc_function;
data.alloc_specs_user = allocInfo.alloc_specs_user;
} else {
const allocInfo = GEN_TO_ALLOC["default"];
data.alloc_module = allocInfo.alloc_module;
data.alloc_function = allocInfo.alloc_function;
data.alloc_specs_user = allocInfo.alloc_specs_user;
}
return data;
}
function renderCustomGenSpecs(data, mustacheRenderer) {
// Render custom gen_specs if it exists (identical logic for both browser and Node.js)
if (data._custom_spec) {
let customGenSpecsStr = null;
if (typeof data._custom_spec === 'string') {
customGenSpecsStr = data._custom_spec;
} else {
// If it's an object, would need prettyPrintPythonArgs, but generator_specs.json has strings
customGenSpecsStr = JSON.stringify(data._custom_spec);
}
data.custom_gen_specs = customGenSpecsStr ? mustacheRenderer(customGenSpecsStr, data) : null;
}
return data;
}
function getDefaultSetObjectiveCode(data) {
const outputFileName = data.output_file_name || `${data.app_ref || ''}.stat`;
return `def set_objective_value():
try:
data = np.loadtxt("${outputFileName}", ndmin=1)
return data[-1]
except Exception:
return np.nan`;
}
// Export for Node.js
if (typeof module !== 'undefined' && module.exports) {
module.exports = { processTemplateData, renderCustomGenSpecs, GEN_TO_ALLOC, getDefaultSetObjectiveCode };
}
// Make available globally for browser
if (typeof window !== 'undefined') {
window.processTemplateData = processTemplateData;
window.renderCustomGenSpecs = renderCustomGenSpecs;
window.GEN_TO_ALLOC = GEN_TO_ALLOC;
window.getDefaultSetObjectiveCode = getDefaultSetObjectiveCode;
}