Class: AppMap::Command::Upload

Inherits:
UploadStruct show all
Defined in:
lib/appmap/command/upload.rb

Constant Summary collapse

MAX_DEPTH =
12

Instance Attribute Summary collapse

Attributes inherited from UploadStruct

#config, #data, #org, #url, #user

Instance Method Summary collapse

Constructor Details

#initialize(config, data, url, user, org) ⇒ Upload

Returns a new instance of Upload.



14
15
16
17
18
19
# File 'lib/appmap/command/upload.rb', line 14

def initialize(config, data, url, user, org)
  super

  # TODO: Make this an option
  @max_depth = MAX_DEPTH
end

Instance Attribute Details

#batch_idObject

Returns the value of attribute batch_id.



12
13
14
# File 'lib/appmap/command/upload.rb', line 12

def batch_id
  @batch_id
end

Instance Method Details

#performObject



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
# File 'lib/appmap/command/upload.rb', line 21

def perform
  appmap = data.clone

  events = data.delete('events')
  class_map = data.delete('classMap') || []

  unless events.blank?
    pruned_events = []
    stack = []
    events.each do |evt|
      if evt['event'] == 'call'
        stack << evt
        stack_depth = stack.length
      else
        stack_depth = stack.length
        stack.pop
      end

      prune = stack_depth > @max_depth

      pruned_events << evt unless prune
    end

    warn "Pruned events to #{pruned_events.length}" if events.length > pruned_events.length

    appmap[:events] = pruned_events
    appmap[:classMap] = prune(class_map, events: pruned_events)
  else
    appmap[:events] = []
    appmap[:classMap] = prune(class_map)
  end

  upload_file = { user: user, org: org, data: appmap }.compact

  conn = Faraday.new(url: url)
  response = conn.post do |req|
    req.url '/api/scenarios'
    req.headers['Content-Type'] = 'application/json'
    req.headers[AppMap::BATCH_HEADER_NAME] = @batch_id if @batch_id
    req.body = JSON.generate(upload_file)
  end

  unless response.body.blank?
    message = begin
                JSON.parse(response.body)
              rescue JSON::ParserError => e
                warn "Response is not valid JSON (#{e.message})"
                nil
              end
  end

  unless response.success?
    error = [ 'Upload failed' ]
    error << ": #{message}" if message
    raise error.join
  end

  batch_id = @batch_id || response.headers[AppMap::BATCH_HEADER_NAME]

  uuid = message['uuid']
  UploadResponse.new(batch_id, uuid)
end