-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiler.rb
executable file
·528 lines (421 loc) · 14.3 KB
/
profiler.rb
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#!/usr/bin/env ruby
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
require 'capybara/poltergeist'
require 'faker'
require 'httparty'
Capybara.run_server = false
Capybara.current_driver = :poltergeist
Capybara.default_max_wait_time = 20
Capybara.app_host = ARGV[0]
module DalphiProfiler
class Authentification
include Capybara::DSL
def initialize(email: nil, password: nil)
page.driver.browser.js_errors = false
email ||= Faker::Internet.safe_email
password ||= Faker::Internet.password
@email = email
@password = password
rescue
puts "error in Authentification#initialize"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
def login_admin
visit '/auth/admins/sign_in'
fill_in 'admin_email', with: @email
fill_in 'admin_password', with: @password
click_button 'Sign in'
rescue
puts "error in Authentification#login_admin"
save_screenshot("#{Dir.pwd}/public/system/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
def login_annotator
visit '/auth/annotators/sign_in'
fill_in 'annotator_email', with: @email
fill_in 'annotator_password', with: @password
click_button 'Sign in'
rescue
puts "error in Authentification#login_annotator"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
def logout_admin
logout
rescue
puts "error in Authentification#logout_admin"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
def logout_annotator
logout
rescue
puts "error in Authentification#logout_annotator"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
def logout
visit '/'
find(:css, '.sign-out').trigger('click')
end
end
class Registration
include Capybara::DSL
def initialize(email: nil, password: nil)
page.driver.browser.js_errors = false
email ||= Faker::Internet.safe_email
password ||= Faker::Internet.password
@email = email
@password = password
rescue
puts "error in Registration#initialize"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
def register_admin
visit('/auth/admins/sign_up')
fill_in 'admin_email', with: @email
fill_in 'admin_password', with: @password
fill_in 'admin_password_confirmation', with: @password
click_button 'Sign up'
{ email: @email, password: @password }
rescue
puts "error in Registration#register_admin"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
def unregister_admin
visit '/auth/admins/edit'
click_on 'Cancel My Account'
rescue
puts "error in Registration#unregister_admin"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
end
class Service
include Capybara::DSL
def initialize(url: nil, title: nil)
@url = url
@title = title
rescue
puts "error in Service#initialize"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
def register
/(?<protocol>http|https):\/\/(?<uri>.*)/ =~ @url
visit "/services/new?protocol=#{protocol}&uri=#{URI.escape(uri)}"
fill_in 'service[title]', with: @title if @title
find(:css, '.btn-primary').trigger('click')
end
def unregister
visit '/services'
click_on @url
find(:css, '.btn-danger').trigger('click')
end
end
class Interface
include Capybara::DSL
def initialize(title: nil, interface_type_name: nil, associated_problem_identifiers: nil, html: nil, coffee: nil, scss: nil, dalphi_storage: nil, profiler_storage: nil)
@dalphi_storage ||= '/usr/src/app/public/system'
@profiler_storage ||= '/usr/src/app/public/system'
@title = title
@interface_type_name = interface_type_name
@associated_problem_identifiers = associated_problem_identifiers
@html = html
@coffee = coffee
@scss = scss
end
def create
visit '/interfaces/new'
fill_in 'interface[title]', with: @title
fill_in 'interface[interface_type][name]', with: @interface_type_name
page.execute_script("$('#interface_associated_problem_identifiers').attr('value', 'ner')")
find(:css, '.btn-primary').trigger('click')
path = find(:css, '.pro-tip .input-group:nth-of-type(1) input').value.gsub(@dalphi_storage, @profiler_storage)
File.write path, @html
path = find(:css, '.pro-tip .input-group:nth-of-type(2) input').value.gsub(@dalphi_storage, @profiler_storage)
File.write path, @coffee
path = find(:css, '.pro-tip .input-group:nth-of-type(3) input').value.gsub(@dalphi_storage, @profiler_storage)
File.write path, @scss
click_on 'Refresh'
end
end
class Project
include Capybara::DSL
def initialize(title: nil, description: nil, iterate_service: nil, merge_service: nil, interfaces: nil)
page.driver.browser.js_errors = false
title ||= "#{Faker::Hacker.adjective} #{Faker::Hacker.noun} #{Faker::Hacker.ingverb}"
description ||= Faker::Hacker.say_something_smart
iterate_service ||= 'MaxEnt NER Iterator (synchronous)'
merge_service ||= 'RawDatum replacer (synchronous)'
interfaces ||= { 'ner_complete' => 'NER complete' }
@title = title
@description = description
@iterate_service = iterate_service
@merge_service = merge_service
@interfaces = interfaces
rescue
puts "error in Project#initialize"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
def create
visit '/projects/new'
fill_in 'title', with: @title
fill_in 'description', with: @description
select @iterate_service, from: 'project_iterate_service'
select @merge_service, from: 'project_merge_service'
find(:css, '.btn-primary').trigger('click')
click_on 'Dashboard'
click_on 'Edit project'
@interfaces.each do |interface_type, interface|
within(:css, ".project_interfaces.#{interface_type}") do
select interface, from: 'project_interfaces'
end
end
find(:css, '.btn-primary').trigger('click')
rescue
puts "error in Project#create"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
def destroy
visit '/projects'
while !page.has_content?(@title) && page.has_css?('a.next_page')
find(:css, 'a.next_page').trigger('click')
end
click_on @title
click_on 'Edit project'
find(:css, '.btn-danger').trigger('click')
rescue
puts "error in Project#destroy"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
end
class Annotator
include Capybara::DSL
def initialize(name: nil, email: nil, password: nil)
page.driver.browser.js_errors = false
name ||= Faker::Name.name
email ||= Faker::Internet.safe_email
password ||= Faker::Internet.password
@name = name
@email = email
@password = password
rescue
puts "error in Annotator#initialize"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
def create
visit '/annotators'
fill_in 'annotator[name]', with: @name
fill_in 'annotator[email]', with: @email
fill_in 'annotator[password]', with: @password
click_on 'New annotator'
rescue
puts "error in Annotator#create"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
def destroy
visit '/annotators'
while !page.has_content?(@name) && page.has_css?('a.next_page')
find(:css, 'a.next_page').trigger('click')
end
click_on @name
click_on 'Delete'
rescue
puts "error in Annotator#destroy"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
def assign_to_project(project_title: nil)
visit '/projects'
while !page.has_content?(project_title) && page.has_css?('a.next_page')
find(:css, 'a.next_page').trigger('click')
end
click_on project_title
within :css, '.nav-tabs' do
click_on 'Annotators'
end
select "#{@name} (#{@email})", from: 'project_annotator'
click_on 'Add annotator'
rescue
puts "error in Annotator#assign_to_project"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
def unassign_from_project(project_title: nil)
visit '/projects'
while !page.has_content?(project_title) && page.has_css?('a.next_page')
find(:css, 'a.next_page').trigger('click')
end
click_on project_title
within :css, '.nav-tabs' do
click_on 'Annotators'
end
while !page.has_content?(@name) && page.has_css?('a.next_page')
find(:css, 'a.next_page').trigger('click')
end
click_on @name
click_on 'Unassign annotator from project'
rescue
puts "error in Annotator#unassign_to_project"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
end
class RawDatum
include Capybara::DSL
def initialize(project_title: nil, files: nil)
page.driver.browser.js_errors = false
unless files
file = Tempfile.new(['', '.json'])
file.write(
{
character: Faker::StarWars.character,
droid: Faker::StarWars.droid,
planet: Faker::StarWars.planet,
quote: Faker::StarWars.quote,
specie: Faker::StarWars.specie,
vehicle: Faker::StarWars.vehicle,
wookie_sentence: Faker::StarWars.wookie_sentence
}.to_json
)
file.rewind
files = [file]
end
@project_title = project_title
@files = files
rescue
puts "error in RawDatum#initialize"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
def create
visit '/projects'
while !page.has_content?(@project_title) && page.has_css?('a.next_page')
find(:css, 'a.next_page').trigger('click')
end
click_on @project_title
click_on 'Raw Data'
click_on 'New raw datum'
page.evaluate_script '$("#raw_datum_data").removeAttr("accept")'
attach_file 'raw_datum_data', @files
find(:css, '.btn-primary').trigger('click')
rescue
puts "error in RawDatum#create"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
def destroy_all
visit '/projects'
while !page.has_content?(@project_title) && page.has_css?('a.next_page')
find(:css, 'a.next_page').trigger('click')
end
click_on @project_title
click_on 'Raw Data'
click_on 'Delete all'
rescue
puts "error in RawDatum#destroy_all"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
end
class AnnotationDocument
include Capybara::DSL
def initialize(project_title: nil)
page.driver.browser.js_errors = false
@project_title = project_title
rescue
puts "error in AnnotationDocument#initialize"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
def create
visit '/projects'
while !page.has_content?(@project_title) && page.has_css?('a.next_page')
find(:css, 'a.next_page').trigger('click')
end
click_on @project_title
click_on 'Annotation Documents'
click_on 'Generate annotation documents'
rescue
puts "error in AnnotationDocument#create"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
def destroy_all
visit '/projects'
while !page.has_content?(@project_title) && page.has_css?('a.next_page')
find(:css, 'a.next_page').trigger('click')
end
click_on @project_title
click_on 'Annotation Documents'
click_on 'Delete all'
rescue
puts "error in AnnotationDocument#destroy_all"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
end
class Annotation
include Capybara::DSL
def initialize(project_title: nil)
page.driver.browser.js_errors = false
@project_title = project_title
rescue
puts "error in Annotation#initialize"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
def annotate(label_words: nil)
visit '/projects'
while !page.has_content?(@project_title) && page.has_css?('a.next_page')
find(:css, 'a.next_page').trigger('click')
end
click_on @project_title
click_on 'Annotation Documents'
visit "#{page.find("[href*='annotate']")[:href]}&synchronous_request=true"
label_words.each do |label, words|
page.find(".#{label}").trigger('click')
words.each do |word|
page.all('span', text: word).each do |token|
token.trigger('click')
end
end
end if label_words
click_on 'Save annotation'
rescue
puts "error in Annotation#annotate"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
def merge
visit '/projects'
while !page.has_content?(@project_title) && page.has_css?('a.next_page')
find(:css, 'a.next_page').trigger('click')
end
click_on @project_title
click_on 'Annotation Documents'
click_on 'Merge'
rescue
puts "error in Annotation#merge"
save_screenshot("#{Dir.pwd}/error-#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%N')}.png")
raise
end
end
end
session = Capybara::Session.new(:poltergeist)
eval(
File.open(
File.expand_path(ARGV[1])
).read
)