forked from newrelic/el-dorado-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathel_dorado.rb
527 lines (464 loc) · 13.6 KB
/
el_dorado.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
# Copyright 2017 New Relic, Inc. Licensed under the Apache License, version 2.0 (the "License");you may not use this
# file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/
# LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, either express or implied. require 'open3'
require 'open3'
class ElDorado < Sinatra::Base
configure :development do
register Sinatra::Reloader
end
get '/' do
haml :welcome
end
get '/catalog' do
@yaml = yaml
haml :catalog
end
get '/catalog/:category' do |category|
nested = {}; nest = {}
yaml.each do |k, v|
if v['query'].nil?
nest = nested[k] = {k => v}
else
nest[k] = v
end
end
@yaml = nested[category] || yaml
haml :catalog
end
get '/about' do
haml :about
end
get '/label_name_detail' do
query = queries['label_name_detail']
query['alias'] = {'Name' => params['label'].downcase }
run 'label_name_detail', query
haml :query
end
get '/search' do
comp = params['comp']||''
regex = comp.downcase.gsub(/^|$| /, '.*')
regex += "|#{comp.downcase.split('').join('.*[ _-]')}.*" if comp =~ /^[A-Z]+$/
redirect "/schema_with_sources?regex=#{regex}"
end
get '/run' do
tip = 'Ad hoc query and graph. With power comes responsibility.
See docs [https://neo4j.com/docs/cypher-refcard/current/ cypher],
[http://www.graphviz.org/content/dot-language dot]'
unless params['query']
body '400: No Query Provided'
halt 400
end
query = deyamlify(params['query']).merge({'title' => 'Run Query', 'description' => tip})
run 'run', query
dotplot query
visplot query
haml :query
end
get '/smoketest' do
@report = []
count = 0
queries.each do |slug, query|
count += 1
if inputs(query).length == 0 || !query['args'].nil? || !query['sample'].nil?
start = Time.now
run slug, query, eval_sample(query['sample'])||query['args']||{}
# run slug, query
duration = Time.now - start
errors = @error ||
(slug=='errors' ? @results.count!=0 && 'found' : @results.count==0 && 'no rows') ||
(slug!='errors' && has_empty_column? && 'empty column') ||
duration>1.5 && 'slow' ||
''
@report << {
slug: slug,
query: query,
duration: duration,
rows: @results.count,
columns: @columns,
error: errors
}
else
@report << {
slug: slug,
query: query,
error: 'missing sample args or sample query'
}
end
# break if count > 3
end
haml :smoketest
end
get '/download' do
query = deyamlify(params['query']).merge({'title' => 'Run Query'})
run 'run', query
content_type :json
JSON.pretty_generate decypher @results
end
get '/download/:query' do |slug|
if queries[slug]
run slug
else
body "404: query not found"
halt 404
end
content_type :json
JSON.pretty_generate decypher @results
end
get '/csv' do
query = deyamlify(params['query']).merge({'title' => 'Run Query'})
run 'run', query
content_type :csv
csv_generate decypher @results
end
get '/csv/:query' do |slug|
if queries[slug]
run slug
else
body "404: query not found"
halt 404
end
content_type :csv
csv_generate decypher @results
end
get '/dot' do
query = deyamlify(params['query']).merge({'title' => 'Run Query'})
run 'run', query
dotify query['dot']
end
get '/dot/:query' do |slug|
if (query = queries[slug]) && (dot = query['dot'])
run slug
else
body "404: query not found"
halt 404
end
dotify dot
end
post '/cypher' do
request.body.rewind
query = JSON.parse request.body.read
query['params'] = {} if query['params'].nil?
run 'api', query, query['params']
content_type :json
if @error.nil?
JSON.pretty_generate decypher @results
else
status 400
JSON.pretty_generate({error: @error})
end
end
get '/:query' do |slug|
if queries[slug]
run slug
dotplot queries[slug]
else
body "<center><h1>404: query not found</h1>See <a href=/catalog>Catalog</a>"
halt 404
end
haml :query
end
helpers do
def eval_sample match
return nil unless match
result = neo4j.query(match)
return_value = {}
result.first.to_h.each do |k,v|
return_value[k.to_s] = v
end
return return_value
end
def yamldef query, field
return '' unless query[field]
"#{field}: |\n" +
query[field].split(/\n/).map{|line|" #{line}\n"}.join('')
end
def yamlify query
yamldef(query, 'query') +
yamldef(query, 'dot')
end
def deyamlify string
if string.split("\n")[0] == 'query: |'
YAML.load(string)
else
{'query' => string}
end
end
def csv_generate table
cols = table[0].keys
puts cols.inspect
CSV.generate do |csv|
csv << cols
table.each do |row|
csv << cols.map{|col|row[col]}
end
end
end
def decypher results
results.to_a.map do |struct|
decypher_row struct.to_h
end
end
def decypher_row elem
case elem
when Hash then elem.inject({}) {|hash,(key, value)| hash[key] = decypher_row(value); hash}
when Array then elem.map {|value| decypher_row(value)}
when Neo4j::Server::CypherNode then elem.props
when Neo4j::Server::CypherRelationship then elem.props
else elem
end
end
def quote string
"\"#{string.gsub(/[ _-]+/,'\n')}\""
end
def dotsub buffer, key, value
buffer
.gsub("\"{#{key}}\"",quote(value))
.gsub("{#{key}}",value.gsub("&", "&"))
end
def interpolate_kv(buffer, key, val)
if val.is_a? Array
return val.map do |elem|
interpolate_kv(buffer, key, elem)
end.join("\n")
elsif val.is_a? Hash
val.each do |subkey,subval|
buffer = interpolate_kv(buffer, "#{key}.#{subkey}", subval)
end
return buffer
else
return dotsub(buffer, key, val.to_s)
end
end
def add_base_to_urls(template)
template.gsub(/URL="/, %(URL="#{request.base_url}))
end
def dotify template
columns = @results.first.members
output = []
add_base_to_urls(template).lines.each do |line|
line = line.gsub(/&/, '&') # http://www.graphviz.org/doc/info/lang.html
if (keywords = columns.select {|key| line.include? "{#{key}}"}).any?
@results.each do |row|
next unless keywords.all? {|key| row[key]}
buffer = line.dup
keywords.each do |key|
buffer = interpolate_kv(buffer, key, row[key])
end
output << buffer
end
else
output << line
end
end
output.join("\n")
end
def dotplot query
if @results.any? && (dot = query['dot'])
@svg = pipe('dot -Tsvg', dotify(dot))
.gsub(/<a /,'<a target="_top" ')
end
end
def vistime string
return Time.now.to_s if string.nil?
return string.to_i if string.match /^\d+$/
return string
end
def visplot query
if @results.any?
columns = @results.first.members.to_a
puts columns.inspect
return unless start = columns.find_index(:Start)
return unless stop = columns.find_index(:Stop)
return unless content = columns.find_index(:Content)
@timeline = {data: [], groups: nil, options: {}}
if group = columns.find_index(:Group)
@timeline[:groups] = []
@results.each do |row|
id = row[group] || 'N/A'
@timeline[:groups].push({id: id}) unless @timeline[:groups].include?(({id: id}))
end
end
id = 0
@timeline['data'] = @results.map do |row|
id += 1
{
id: id,
content: row[content],
start: vistime(row[start]),
end: vistime(row[stop]),
group: (group ? row[group] : 'N/A')
}
end
end
end
def has_empty_column?
@columns.each_with_index do |value, col|
return true unless @results.map{|row|row[col]}.any?
end
false
end
def inputs(query)
query['query'].scan(/\{([a-z]+)\}/).flatten.sort.uniq
end
def variations(query)
(query['variations']||{}).keys
end
def outputs(query)
query['query'].scan(/as (\w+)\b/).flatten.map{|key|label(query,key)}.sort.uniq
end
def label(query, col)
if @query['alias'] && @query['alias'][col.to_s]
@query['alias'][col.to_s].downcase
else
col.to_s.downcase
end
end
def json text
JSON.parse text
rescue JSON::ParserError => e
nil
end
def cypher(query)
cypher = @query['query']
(@query['variations']||[]).each do |k, v|
if @params.include? k
cypher.gsub! v['replace'], v['with']
end
end
cypher
end
def run slug, query=nil, args=nil
@query = query || queries[slug]
@error = nil
unless slug == 'api'
# Prepare expected URL params
expected = inputs(@query) + variations(@query)
@params = (args||params).select{ |k,_| expected.include? k }
@params.each { |k,v| @params[k] = v.sub(/-aMp-/,'&') }
else
@params = args
end
# Run the query variation
begin
@results = neo4j.query(cypher(@query), @params)
rescue Exception => e
@results = []
@error = e.message
end
# Select targets for clicks by column
if @results.first
@columns = @results.first.members
@targets = find_best_target slug
else
@columns = []
end
end
def sample_args(query)
URI.encode_www_form eval_sample(query['sample'])||query['args']||[]
end
def formatted_value(value, column, row=nil)
if value.nil?
''
elsif value.kind_of?(Array)
value.map{|v| formatted_value(v, column, row)}.join("<br/>")
elsif value.kind_of?(String) && value[0]=='[' and json(value)
formatted_value json(value), column
elsif value.kind_of?(String) && value.match(/^https?:/)
link_to_url(value)
elsif value.kind_of?(Hash) && value[:type] == "link"
link_to_url(value)
elsif value.kind_of?(Hash)
preserve("<pre>#{value.map{|k,v|"#{k}: #{v}"}.join("\n")}</pre>")
elsif value.kind_of? Neo4j::Server::CypherNode
# http://www.rubydoc.info/github/neo4jrb/neo4j-core/Neo4j/Server/CypherNode
preserve("<pre>#{value.props.map{|k,v|"#{k}: #{v}"}.join("\n")}</pre>")
elsif value.kind_of?(Neo4j::Server::CypherRelationship)
# http://www.rubydoc.info/github/neo4jrb/neo4j-core/Neo4j/Server/CypherNode
preserve("<pre>#{value.props.map{|k,v|"#{k}: #{v}"}.join("\n")}</pre>")
elsif @targets && !(@targets[column].nil? || @targets[column].empty?)
link_to_targets(value, label(@query,column), @targets[column].keys.first)
elsif column == :Node && !row.nil? && @columns.include?(:Label)
link_to_label_detail(value, row[:Label])
else
value
end
end
def link_to_url(value)
target = value
kind = case value
when Hash then
target = value[:href]
value[:text]
when /README/ then 'readme'
when /docs.google.com/ then 'docs'
when /\/wiki\// then 'wiki'
when /\.md/ then 'doc'
else 'site'
end
"<a href=\"#{target}\" target=_blank>#{kind}</a>"
end
def link_to_targets(value, label, target)
%Q|<a href="/#{target}?#{label.downcase}=#{enc value}">#{value}</a>|
end
def link_to_label_detail(value, label)
%Q|<a href="/label_name_detail?label=#{enc label.upcase}&name=#{enc value}">#{value}</a>|
end
def find_best_target slug
places = queries.keys
here = places.index slug
forward = places.rotate here||0
@columns.inject({}) do |res, col|
want = label(@query,col)
there = forward.index do |key|
q = inputs(queries[key])
(q.size == 1 and q.first == want) or
(q.size == 0 and variations(queries[key]).include?(want))
end
if there
slug_there = forward[there]
targets = {slug_there => queries[slug_there]}
res.update(col => targets)
else
res
end
end
end
def recommended
if want = @params['type'] || @params['label']
count = 0
queries.each do |slug, query|
if query['query'].match /:#{want}\b/ and inputs(query).empty?
yield slug, query
count += 1
end
break if count >= 3
end
end
end
def yaml
@yaml ||= YAML.load_file('config/queries.yml')
end
def queries
@queries ||= yaml.select{ |s,q| q['query']}
end
def neo4j
@neo4j ||= Neo4j::Session.open(:server_db, ENV['GRAPHDBURL']||'http://neo4j:password@localhost:7474/')
end
def pipe cmd, input
result = nil
Open3.popen3(cmd) do |i, o, e, t|
i.write input
i.close
# STDERR.puts e.read
result = o.read
end
result
end
def enc(thing)
return "" if thing.nil?
return thing.collect { |c| URI.encode(c) } if thing.kind_of?(Array)
CGI.escape(thing.to_s)
end
end
end