-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.inc
304 lines (266 loc) · 8.5 KB
/
run.inc
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
module_load_include('inc', 'boa', 'examples');
function _get_datasets() {
$query = get_input_query();
$query->addExpression('IF(enabled = 1, name, CONCAT(\'[admin] \', name))', 'name');
$query->orderBy('id', 'DESC');
return $query->execute();
}
function boa_run_form($form, &$form_state) {
global $examples, $example_options;
global $base_path;
global $user;
drupal_add_js($base_path . 'sites/all/libraries/ace/src/ace.js');
drupal_add_js($base_path . 'sites/all/libraries/ace/src/ext-language_tools.js');
drupal_add_js($base_path . 'sites/all/libraries/ace/src/theme-crimson_editor.js');
drupal_add_js($base_path . 'sites/all/libraries/ace/src/mode-boa.js');
$form['examples'] = array(
'#type' => 'select',
'#title' => t('Run an Example'),
'#ajax' => array(
'callback' => 'boa_run_form_callback',
'wrapper' => 'sourcecode',
'progress' => array(
'type' => 'throbber'
),
'effect' => 'fade',
),
'#options' => $example_options,
'#default_value' => 0,
);
$form['source'] = array(
'#type' => 'textarea',
'#prefix' => '<div id="sourcecode">',
'#cols' => 50,
'#title' => t('Boa Source Code'),
);
$form['parent'] = array(
'#type' => 'hidden',
);
if (isset($_GET['example']))
$form['examples']['#default_value'] = $_GET['example'];
if (isset($_GET['edit'])) {
if (!boa_public_access($_GET['edit']) && !isValidJob($_GET['edit'])) {
invalidJob($_GET['edit']);
} else {
$query = db_select('boa_jobs', 'j');
$query->fields('j', array('source', 'input'));
$query->condition('id', $_GET['edit']);
if (!isValidJob($_GET['edit']))
$query->condition('public', 1);
$result = $query->execute();
$record = $result->fetchObject();
$form['source']['#default_value'] = $record->source;
$form['input']['#default_value'] = $record->input;
$form['parent']['#default_value'] = $_GET['edit'];
}
}
if (isset($_GET['source']))
$form['source']['#default_value'] = $_GET['source'];
$source = !empty($form_state['values']['examples']) ? $examples[$form_state['values']['examples']] : (!empty($form['source']['#default_value']) ? $form['source']['#default_value'] : "");
$form['source']['#suffix'] = '<div style="position: relative; width: 100%;" id="source"></div></div>';
$form['#attributes']['onsubmit'] = "jQuery('textarea[name=\"source\"]').val(jQuery('source').getSession().getValue());";
$form['ace'] = array(
'#markup' => <<<JAVASCRIPT
<script type="text/javascript">
<!--//--><![CDATA[//><!--
var BoaMode = require("ace/mode/boa").Mode;
ace.require("ace/ext/language_tools");
function setupAce() {
var editor = ace.edit("source");
editor.getSession().setMode(new BoaMode());
editor.setTheme("ace/theme/crimson_editor");
editor.renderer.setShowGutter(true);
editor.setShowPrintMargin(false);
editor.setShowInvisibles(false);
var textarea = jQuery('textarea[name="source"]');
editor.getSession().setValue(textarea.val());
textarea.parent().hide();
textarea.change(function(){
editor.getSession().setValue(textarea.val());
});
editor.getSession().on('change', function(){
textarea.val(editor.getSession().getValue());
});
editor.setOptions({
enableBasicAutocompletion: true,
enableSnippets: true,
enableLiveAutocompletion: true,
minLines: 10,
maxLines: 1000
});
}
setupAce();
jQuery(document).ajaxComplete(function(){
setupAce();
});
//--><!]]>
</script>
<style type="text/css" media="all">
.ace_sb {
overflow-y: auto !important;
}
#source {
position: relative;
width: 100%;
}
</style>
JAVASCRIPT
);
$form['input'] = array(
'#type' => 'select',
'#title' => t('Input Dataset (use the SMALL dataset when testing queries!)'),
'#default_value' => isset($form['input']['#default_value']) ? $form['input']['#default_value'] : (isset($_GET['input']) ? (int)$_GET['input'] : 0)
);
$result = _get_datasets();
foreach ($result as $record)
$form['input']['#options'][$record->id] = $record->name;
if (is_boa_admin()) {
$form['releaseonly'] = array(
'#type' => 'checkbox',
'#title' => t('Show Released Datasets Only'),
);
$form['checkfilter'] = array(
'#markup' => <<<JAVASCRIPT
<script type="text/javascript">
<!--//--><![CDATA[//><!--
var datasetCache = [];
function filterAdmin() {
var opt, sel = document.getElementsByName('input')[0];
if (!document.getElementsByName('releaseonly')[0].checked) {
restoreDatasets();
} else {
// Loop backwards through options as removing them modifies the next
// to be visited if go forwards
for (var i=sel.options.length-1; i>=0; i--) {
opt = sel.options[i];
if (opt.text.toLowerCase().indexOf('[admin] ') == 0){
sel.removeChild(opt)
}
}
}
sel.selectedIndex = 0;
}
function restoreDatasets() {
var sel = document.getElementsByName('input')[0];
sel.options.length = 0;
for (var i = 0, iLen = datasetCache.length; i < iLen; i++) {
sel.appendChild(datasetCache[i]);
}
}
window.onload = function() {
var sel = document.getElementsByName('input')[0];
for (var i = 0, iLen = sel.options.length; i < iLen; i++) {
datasetCache.push(sel.options[i]);
}
var check = document.getElementsByName('releaseonly')[0];
check.onclick = filterAdmin;
}
//--><!]]>
</script>
JAVASCRIPT
);
}
$form['run'] = array(
'#type' => 'submit',
'#value' => t('Run Program'),
);
$form['note'] = array(
'#markup' => t('<span style="font-size: 0.8em;">NOTE: All data submitted to this site is subject to our <a href="/policies/privacy.php" target="_blank">privacy policy</a>.</span>'),
);
return $form;
}
function boa_run_form_callback($form, $form_state) {
global $examples;
$form['source']['#value'] = '';
if (!empty($form_state['values']['examples']))
$form['source']['#value'] = htmlspecialchars_decode($examples[$form_state['values']['examples']]);
return $form['source'];
}
function boa_run_form_submit($form, &$form_state) {
global $user;
if (!is_valid_input($form_state['values']['input']))
invalid_input();
$source = trim($form_state['values']['source']);
if (empty($source)) {
drupal_set_message(t('Boa program is empty!'), 'error');
drupal_goto('boa/run', array('query' => array('source' => $form_state['values']['source'])));
drupal_exit();
}
$running = job_run_count($user->uid);
$query = db_insert('boa_jobs');
$tz = date_default_timezone_get();
date_default_timezone_set('America/Chicago');
$query->fields(array(
'submitted' => date('Y-m-d G:i:s'),
'uid' => $user->uid,
'parent_id' => isset($form_state['values']['parent']) ? (int)$form_state['values']['parent'] : NULL,
'source' => $source,
'input' => $form_state['values']['input'],
));
date_default_timezone_set($tz);
$id = $query->execute();
$query = db_insert('boa_output');
$query->fields(array(
'id' => $id,
'length' => 0,
'web_result' => '',
));
$query->execute();
drupal_set_message(t('Your job was submitted. Please note it can take several minutes to get results, depending on server load.'));
if ($running > 0)
drupal_set_message(t('You have other job(s) running. This job will queue until those jobs finish or are stopped.'));
if (job_run_count() > 3)
drupal_set_message(t('Server load is currently high. It will take some time for this job to finish.'));
drupal_goto("boa/job/$id");
drupal_exit();
}
function _run_rpc($source, $input) {
global $user;
if (!is_valid_input($input))
invalid_input();
$query = db_insert('boa_jobs');
$tz = date_default_timezone_get();
date_default_timezone_set('America/Chicago');
$query->fields(array(
'submitted' => date('Y-m-d G:i:s'),
'uid' => $user->uid,
'source' => trim($source),
'input' => $input,
));
date_default_timezone_set($tz);
$id = $query->execute();
$query = db_insert('boa_output');
$query->fields(array(
'id' => $id,
'length' => 0,
'web_result' => '',
));
$query->execute();
$query = db_select('boa_jobs', 'j');
$query->join('boa_input', 'i', 'j.input = i.id');
$query->fields('j', array('submitted', 'compiler_status', 'hadoop_status'));
$query->fields('i', array('name'));
$query->addField('i', 'id', 'inputid');
$query->condition('j.id', $id);
$result = $query->execute();
$rows = array();
while ($data = $result->fetchObject()) {
fakeRunning($data);
$rows[] = array(
'id' => $id,
'submitted' => $data->submitted . ' ' . strftime('%Z'),
'input' => array('id' => $data->inputid, 'name' => $data->name),
'compiler_status' => showStatus($data->compiler_status),
'hadoop_status' => showStatus($data->hadoop_status),
);
}
return $rows[0];
}
function _datasets_rpc() {
$result = _get_datasets();
$rows = array();
foreach ($result as $record)
$rows[] = array('id' => $record->id, 'name' => $record->name);
return $rows;
}