Project

General

Profile

Hacking Workbench » History » Version 19

Tom Clegg, 11/25/2014 12:25 AM

1 1 Tom Clegg
h1. Hacking Workbench
2
3
{{toc}}
4
5
h2. Source tree layout
6
7
Everything is in @/apps/workbench@.
8
9
Key pieces to know about before going much further:
10
11
|/|Usual Rails project layout|
12
|/app/controllers/application_controller.rb|Controller superclass with authentication setup, error handling, and generic CRUD actions|
13
|/app/controllers/*.rb|Actions other than generic CRUD (users#activity, jobs#generate_provenance, ...)|
14
|/app/models/arvados_base.rb|Default Arvados model behavior and ActiveRecord-like accessors and introspection features|
15
|/app/models/arvados_resource_list.rb|ActiveRelation-like class (what you get from Model.where() etc.)|
16
17 12 Tom Clegg
h2. Background resources
18
19
Workbench is a Rails 4 application.
20
21
* "Getting started with Rails":http://guides.rubyonrails.org/getting_started.html at rubyonrails.org
22
* "AJAX in Rails 3.1":http://blog.madebydna.com/all/code/2011/12/05/ajax-in-rails-3.html blog post (still relevant in Rails 4)
23
24 1 Tom Clegg
h2. Unlike a typical Rails project...
25
26 7 Peter Amstutz
* ActiveRecord in Workbench doesn't talk to the database directly, but instead queries the Arvados API as REST client.
27
* The Arvados query API is somewhat limited and doesn't accept SQL statements, so Workbench has to work harder to get what it needs.
28 1 Tom Clegg
* Workbench itself only has the privileges of the Workbench user: when making Arvados API calls, it uses the API token provided by the user.
29
30
h2. Unlike what you might expect...
31
32
* Workbench doesn't use the Ruby SDK. It uses a sort of baked-in Rails SDK.
33
** TODO: move it out of Workbench into a gem.
34
** TODO: use the Ruby SDK under the hood.
35
36
h2. Running in development mode
37
38 2 Misha Zatsman
h3. SSL certificates
39
40 4 Tom Clegg
You can get started quickly with SSL by generating a self-signed certificate:
41 1 Tom Clegg
42
 openssl req -new -x509 -nodes -out ~/self-signed.pem -keyout ~/self-signed.key -days 3650 -subj '/CN=arvados.example.com'
43
44
Alternatively, download a set from the bottom of the [[API server]] page.
45 2 Misha Zatsman
46
h3. Download and configure
47 1 Tom Clegg
48 2 Misha Zatsman
Follow "these instructions":http://doc.arvados.org/install/install-workbench-app.html to download the source and configure your workbench instance.
49 3 Misha Zatsman
50 4 Tom Clegg
h3. Start the server
51 1 Tom Clegg
52 4 Tom Clegg
Save something like the following at @~/bin/workbench@, make it executable[1], make sure @~/bin@ is in your path[2]:
53 1 Tom Clegg
54
 #!/bin/sh
55
set -e
56
cd ~/arvados/apps/workbench
57
export RAILS_ENV=development
58 5 Tom Clegg
bundle install --path=vendor/bundle
59 4 Tom Clegg
exec bundle exec passenger start -p 3031 --ssl --ssl-certificate ~/self-signed.pem --ssl-certificate-key ~/self-signed.key
60 1 Tom Clegg
61
The first time you run the above it will take a while to install all the ruby gems. In particular @Installing nokogiri@ takes a while
62
63
Once you see:
64
65
 =============== Phusion Passenger Standalone web server started ===============
66
67
You can visit your server at:
68
69 4 Tom Clegg
 @https://{ip-or-host}:3031/@
70
71 6 Misha Zatsman
You can kill your server with @ctrl-C@ but if you get disconnected from the terminal, it will continue running. You can kill it by running
72
73
 @ps x |grep nginx |grep master@
74
75
And then
76
77
 @kill ####@
78
79
Replacing #### with the number in the left column returned by ps
80
81 4 Tom Clegg
fn1. @chmod +x ~/bin/workbench@
82
83
fn2. In Debian systems, the default .profile adds ~/bin to your path, but only if it exists when you log in. If you just created ~/bin, doing @exec bash -login@ or @source .profile@ should make ~/bin appear in your path.
84
85
h2. Running tests
86
87
The test suite brings up an API server in test mode, and runs browser tests with Firefox.
88
89 17 Tom Clegg
Make sure API server has its dependencies in place and its database schema up-to-date.
90 4 Tom Clegg
91 1 Tom Clegg
<pre>
92 17 Tom Clegg
(
93
 set -e
94
 cd ../../services/api
95
 RAILS_ENV=test bundle install --path=vendor/bundle
96
 RAILS_ENV=test bundle exec rake db:migrate
97
)
98 4 Tom Clegg
</pre>
99
100
Install headless testing tools.
101
102
<pre>
103
sudo apt-get install xvfb iceweasel
104
</pre>
105
106
(Install firefox instead of iceweasel if you're not using Debian.)
107
108 10 Tom Clegg
Install phantomjs. (See http://phantomjs.org/download.html for latest version.)
109
110
<pre>
111 16 Tom Clegg
wget -P /tmp https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.8-linux-x86_64.tar.bz2
112
sudo tar -C /usr/local -xjf /tmp/phantomjs-1.9.8-linux-x86_64.tar.bz2
113
sudo ln -s ../phantomjs-1.9.8-linux-x86_64/bin/phantomjs /usr/local/bin/
114 10 Tom Clegg
</pre>
115
116 4 Tom Clegg
Run the test suite.
117
118
<pre>
119
RAILS_ENV=test bundle exec rake test
120 1 Tom Clegg
</pre>
121 9 Tom Clegg
122 13 Tom Clegg
h3. When tests fail...
123
124
When an integration test fails (or skips) a screenshot is automatically saved in @arvados/apps/workbench/tmp/workbench-fail-1.png@, etc.
125
126 16 Tom Clegg
By default, @rake test@ just shows F when a test fails (and E when a test crashes) and doesn't tell you which tests had problems until the entire test suite is done. During development it makes more sense to use @TESTOPTS=-v@. This reports after each test the test class and name, outcome, and elapsed time:
127 13 Tom Clegg
* <pre>
128 1 Tom Clegg
$ RAILS_ENV=test bundle exec rake test TESTOPTS=-v
129 13 Tom Clegg
[...]
130
ApplicationControllerTest#test_links_for_object = 0.10 s = .
131
[...]
132
Saved ./tmp/workbench-fail-2.png
133 16 Tom Clegg
CollectionsTest#test_combine_selected_collection_files_into_new_collection = 10.89 s = F
134 13 Tom Clegg
[...]
135
</pre>
136 9 Tom Clegg
137 19 Tom Clegg
h3. Iterating on a single test
138
139
Sometimes you want to poke at the code and re-run a single test to confirm that you made it pass. You don't want to reboot everything just to make Minitest notice that you edited your test.
140
141
Here's how:
142
143
<pre>
144
arvados/apps/workbench$ RAILS_ENV=test bundle exec irb -Ilib:test
145
146
load 'test/integration/pipeline_instances_test.rb'
147
148
>>> Minitest.run(%w(-v -n test_Create_and_run_a_pipeline))
149
150
...
151
PipelineInstancesTest#test_Create_and_run_a_pipeline = 14.72 s = E
152
...
153
154
(Edit test/integration/pipeline_instances_test.rb here)
155
156
>>> begin
157
      Object.send :remove_const, :PipelineInstancesTest
158
      ::Minitest::Runnable.runnables.reject! { true }
159
      load 'test/integration/pipeline_instances_test.rb'
160
      Minitest.run(%w(-v -n test_Create_and_run_a_pipeline))
161
    end
162
163
...
164
PipelineInstancesTest#test_Create_and_run_a_pipeline = 38.54 s = .
165
...
166
</pre>
167
168
169 1 Tom Clegg
h2. Loading state from API into models
170
171
If your model makes an API call that returns the new state of an object, load the new attributes into the local model with @private_reload@:
172
173
<pre><code class="ruby">
174
  api_response = $arvados_api_client.api(...)
175
  private_reload api_response
176
</code></pre>
177
178
h2. Features
179
180
h3. Authentication
181
182
ApplicationController uses an around_filter to make sure the user is logged in, redirect to Arvados to complete the login procedure if not, and store the user's API token in Thread.current[:arvados_api_token] if so.
183
184
The @current_user@ helper returns User.current if the user is logged in, otherwise nil. (Generally, only special pages like "welcome" and "error" get displayed to users who aren't logged in.)
185
186
h3. Default filter behavior
187
188
@before_filter :find_object_by_uuid@
189
190
* This is enabled by default, @except :index, :create@.
191
* It renames the @:id@ param to @:uuid@. (The Rails default routing rules use @:id@ to accept params in path components, but @params[:uuid]@ makes more sense everywhere else in our code.)
192
* If you define a collection method (where there's no point looking up an object with the :id supplied in the request), skip this.
193
194
<pre><code class="ruby">
195
  skip_before_filter :find_object_by_uuid, only: [:action_that_takes_no_uuid_param]
196
</code></pre>
197
198
h3. Error handling
199
200
ApplicationController has a render_error method that shows a standard error page. (It's not very good, but it's better than a default Rails stack trace.)
201
202
In a controller you get there like this
203
204
<pre><code class="ruby">
205
  @errors = ['I could not achieve what you wanted.']
206
  render_error status: 500
207
</code></pre>
208
209
You can also do this, anywhere
210
211
<pre><code class="ruby">
212
  raise 'My spoon is too big.'
213
</code></pre>
214
215
The @render_error@ method sends JSON or HTML to the client according to the Accept header in the request (it sends JSON if JavaScript was requested), so reasonable things happen whether or not the request is AJAX.
216
217
h2. Development patterns
218
219
h3. Add a model
220
221
Currently, when the API provides a new model, we need to generate a corresponding model in Workbench: it's not smart enough to pick up the list of models from the API server's discovery document.
222
223
_(Need to fill in details here)_
224
# @rails generate model ....@
225
# Delete migration
226 8 Peter Amstutz
# Change base class to ArvadosBase
227
# @rails generate controller ...@ 
228 1 Tom Clegg
229
Model _attributes_, on the other hand, are populated automatically.
230
231
h3. Add a configuration knob
232
233
Same situation as API server. See [[Hacking API Server]].
234
235
h3. Add an API method
236
237
Workbench is not yet smart enough to look in the discovery document for supported API methods. You need to add a method to the appropriate model class before you can use it in the Workbench app.
238
239
h3. Writing tests
240
241 18 Tom Clegg
In integration tests, this makes your tests flaky because the result depends on whether the page has finished loading:
242
* <pre><code class="ruby">
243
assert page.has_selector?('a', text: 'foo')  # Danger!
244
</code></pre>
245
* Instead, do this:
246
* <pre><code class="ruby">
247
assert_selector('a', text: 'foo')
248
</code></pre>
249
* This lets Capybara wait for the selector to appear.
250
251 1 Tom Clegg
252
h3. AJAX using Rails UJS (remote:true with JavaScript response)
253
254
This pattern is the best way to make a button/link that invokes an asynchronous action on the Workbench server side, i.e., before/without navigating away from the current page.
255
256
# Add <code class="ruby">remote: true</code> to a link or button. This makes Rails put a <code class="html">data-remote="true"</code> attribute in the HTML element. Say, in @app/views/fizz_buzzes/index.html.erb@:
257
<pre><code class="ruby">
258
<%= link_to "Blurfl", blurfl_fizz_buzz_url(id: @object.uuid), {class: 'btn btn-primary', remote: true} %>
259
</code></pre>
260
# Ensure the targeted action responds appropriately to both "js" and "html" requests. At minimum:
261
<pre><code class="ruby">
262
class FizzBuzzesController
263
  #...
264
  def blurfl
265
    @howmany = 1
266
    #...
267
    respond_to do |format|
268
      format.js
269
      format.html
270
    end
271
  end
272
end
273
</code></pre>
274
# The @html@ view is used if this is a normal page load (presumably this means the client has turned off JS).
275
#* @app/views/fizz_buzz/blurfl.html.erb@
276
<pre><code>
277
<p>I am <%= @howmany %></p>
278
</code></pre>
279
# The @js@ view is used if this is an AJAX request. It renders as JavaScript code which will be executed in the browser. Say, in @app/views/fizz_buzz/blurfl.js.erb@:
280
<pre><code class="javascript">
281
window.alert('I am <%= @howmany %>');
282
</code></pre>
283
# The browser opens an alert box:
284
<pre>
285
I am 1
286
</pre>
287
# A common task is to render a partial and use it to update part of the page. Say the partial is in @app/views/fizz_buzz/_latest_news.html.erb@:
288
<pre><code class="javascript">
289
var new_content = "<%= escape_javascript(render partial: 'latest_news') %>";
290
if ($('div#latest-news').html() != new_content)
291
   $('div#latest-news').html(new_content);
292
</code></pre>
293
294
*TODO: error handling*
295
296
h3. AJAX invoked from custom JavaScript (JSON response)
297
298
(and error handling)
299
300
h3. Add JavaScript triggers and fancy behavior
301
302
Some guidelines for implementing stuff nicely in JavaScript:
303
* Don't rely on the DOM being loaded before your script is loaded.
304
** If you need to inspect/alter the DOM as soon as it's loaded, make a setup function that fires on "document ready" and "ajax:complete".
305
** jQuery's delegated event pattern can help keep your code clean. See http://api.jquery.com/on/
306
<pre><code class="javascript">
307
// worse:
308
$('table.fizzbuzzer tr').
309
    on('mouseover', function(e, xhr) {
310
        console.log("This only works if the table exists when this setup script is executed.");
311
    });
312
// better:
313
$(document).
314
    on('mouseover', 'table.fizzbuzzer tr', function(e, xhr) {
315
        console.log("This works even if the table appears (or has the fizzbuzzer class added) later.");
316
    });
317
</code></pre>
318
319
* If your code really only makes sense for a particular view, rather than embedding @<script>@ tags in the middle of the page,
320
** use this:
321
<pre><code class="ruby">
322
<% content_for :js do %>
323
console.log("hurray, this goes in HEAD");
324
<% end %>
325
</code></pre>
326
** or, if your code should run after [most of] the DOM is loaded:
327
<pre><code class="ruby">
328
<% content_for :footer_js do %>
329
console.log("hurray, this runs at the bottom of the BODY element in the default layout.");
330
<% end %>
331
</code></pre>
332
333
* Don't just write JavaScript on the @fizz_buzzes/blurfl@ page and rely on the fact that the only @table@ element on the page is the one you want to attach your special behavior to. Instead, add a class to the table, and use a jQuery selector to attach special behavior to it.
334
** In @app/views/fizz_buzzes/blurfl.html.erb@
335
<pre>
336
<table class="fizzbuzzer">
337
 <tr>
338
  <td>fizz</td><td>buzz</td>
339
 </tr>
340
</table>
341
</pre>
342
** In @app/assets/javascripts/fizz_buzzes.js@
343
<pre><code class="javascript">
344
<% content_for :js do %>
345
$(document).on('mouseover', 'table.fizzbuzzer tr', function() {
346
    console.log('buzz');
347
});
348
<% end %>
349
</code></pre>
350
** Advantage: You can reuse the special behavior in other tables/pages/classes
351
** Advantage: The JavaScript can get compiled, minified, cached in the browser, etc., instead of being rendered with every page view
352
** Advantage: The JavaScript code is available regardless of how the content got into the DOM (regular page view, partial update with AJAX)
353
354 14 Phil Hodgson
* If the result of clicking on some link invokes Javascript that will ultimately change the content of the current page using @window.location.href=@ then it is advisable to add to the link the @force-cache-reload@ CSS class. By doing so, when a user uses the browser-back button to return to the original page, it will be forced to reload itself from the server, thereby reflecting the updated content. (Ref: https://arvados.org/issues/3634)
355
356 11 Tom Clegg
h3. Invoking chooser
357
358
Example from @app/views/projects/_show_contents.html.erb@:
359
360
<pre>
361
    <%= link_to(
362
          choose_collections_path(
363
            title: 'Add data to project:',
364
            multiple: true,
365
            action_name: 'Add',
366
            action_href: actions_path(id: @object.uuid),
367
            action_method: 'post',
368
            action_data: {selection_param: 'selection[]', copy_selections_into_project: @object.uuid, success: 'page-refresh'}.to_json),
369
          { class: "btn btn-primary btn-sm", remote: true, method: 'get', data: {'event-after-select' => 'page-refresh'} }) do %>
370
      <i class="fa fa-fw fa-plus"></i> Add data...
371
    <% end %>
372
</pre>
373
374
Tour:
375 1 Tom Clegg
376
(TODO)
377
378 15 Tom Clegg
h3. Infinite scroll
379
380
When showing a list that might be too long to render up front in its entirety, use the infinite-scroll feature.
381
382
Links/buttons that flip to page 1, 2, 3, etc. (e.g., <code class="ruby">render partial: "paging"</code>) are deprecated.
383
384
The comments that should be at the top of source:apps/workbench/app/assets/javascripts/infinite_scroll.js, when we write them, will tell you how to do it.
385
386
387
h3. Filtering lists
388
389
When a list is displayed, and the user might want to filter them by selecting a category or typing a search string, use @class="filterable"@. It's easy!
390
391
The comments at the top of source:apps/workbench/app/assets/javascripts/filterable.js tell you how to do it.
392
393 1 Tom Clegg
h3. Tabs/panes on index & show pages
394
395
(TODO)
396
397
h3. User notifications
398
399
(TODO)
400
401
h3. Customizing breadcrumbs
402
403
(TODO)
404
405
h3. Making a page accessible before login
406
407
(TODO)
408
409
h3. Making a page accessible to non-active users
410
411
(TODO)