Class: Webmachine::Trace::TraceResource

Inherits:
Resource
  • Object
show all
Defined in:
lib/webmachine/trace/trace_resource.rb

Overview

Implements the user-interface of the visual debugger. This includes serving the static files (the PNG flow diagram, CSS and JS for the UI) and the HTML for the individual traces.

Constant Summary collapse

MAP_EXTERNAL =
%w[static map.png]
MAP_FILE =
File.expand_path('../static/http-headers-status-v3.png', __FILE__)
SCRIPT_EXTERNAL =
%w[static wmtrace.js]
SCRIPT_FILE =
File.expand_path("../#{SCRIPT_EXTERNAL.join "/"}", __FILE__)
STYLE_EXTERNAL =
%w[static wmtrace.css]
STYLE_FILE =
File.expand_path("../#{STYLE_EXTERNAL.join "/"}", __FILE__)
TRACELIST_ERB =
File.expand_path('../static/tracelist.erb', __FILE__)
TRACE_ERB =
File.expand_path('../static/trace.erb', __FILE__)

Instance Attribute Summary

Attributes inherited from Resource

#request, #response

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

new, run

Methods included from Resource::Tracing

#trace?

Methods included from Resource::EntityTags

#weak_etag

Methods included from Resource::Encodings

#encode_deflate, #encode_gzip, #encode_identity

Methods included from Resource::Callbacks

#allow_missing_post?, #allowed_methods, #base_uri, #charsets_provided, #content_types_accepted, #create_path, #delete_completed?, #delete_resource, #encodings_provided, #finish_request, #forbidden?, #generate_etag, #handle_exception, #is_authorized?, #is_conflict?, #known_content_type?, #known_methods, #language_chosen, #languages_provided, #malformed_request?, #moved_permanently?, #moved_temporarily?, #multiple_choices?, #options, #post_is_create?, #previously_existed?, #process_post, #service_available?, #uri_too_long?, #valid_content_headers?, #valid_entity_length?, #validate_content_checksum, #variances

Class Method Details

.traceObject

The ERB template for a single trace



25
26
27
# File 'lib/webmachine/trace/trace_resource.rb', line 25

def self.trace
  @@trace ||= ERB.new(File.read(TRACE_ERB))
end

.tracelistObject

The ERB template for the trace list



20
21
22
# File 'lib/webmachine/trace/trace_resource.rb', line 20

def self.tracelist
  @@tracelist ||= ERB.new(File.read(TRACELIST_ERB))
end

Instance Method Details

#content_types_providedObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/webmachine/trace/trace_resource.rb', line 29

def content_types_provided
  case request.path_tokens
  when []
    [['text/html', :produce_list]]
  when MAP_EXTERNAL
    [['image/png', :produce_file]]
  when SCRIPT_EXTERNAL
    [['text/javascript', :produce_file]]
  when STYLE_EXTERNAL
    [['text/css', :produce_file]]
  else
    [['text/html', :produce_trace]]
  end
end

#encode_decisions(decisions) ⇒ Object



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
# File 'lib/webmachine/trace/trace_resource.rb', line 101

def encode_decisions(decisions)
  decisions.each_with_object([]) do |event, list|
    case event[:type]
    when :decision
      # Don't produce new decisions for sub-steps in the graph
      unless /[a-z]$/.match?(event[:decision].to_s)
        list << {'d' => event[:decision], 'calls' => []}
      end
    when :attempt
      list.last['calls'] << {
        'call' => event[:name],
        'source' => event[:source],
        'input' => event[:args] && event[:args].inspect
      }
    when :result
      list.last['calls'].last['output'] = event[:value].inspect
    when :exception
      list.last['calls'].last['exception'] = {
        'class' => event[:class],
        'backtrace' => event[:backtrace].join("\n"),
        'message' => event[:message]
      }
    end
  end
end

#encode_trace(data) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/webmachine/trace/trace_resource.rb', line 91

def encode_trace(data)
  data = data.dup
  # Request is first, response is last
  treq = data.shift.dup
  tres = data.pop.dup
  treq.delete :type
  tres.delete :type
  [MultiJson.dump(treq), MultiJson.dump(tres), MultiJson.dump(encode_decisions(data))]
end

#expiresObject



67
68
69
# File 'lib/webmachine/trace/trace_resource.rb', line 67

def expires
  (Time.now + 30 * 86400).utc if @file
end

#last_modifiedObject



63
64
65
# File 'lib/webmachine/trace/trace_resource.rb', line 63

def last_modified
  File.mtime(@file) if @file
end

#produce_fileObject



71
72
73
74
75
76
# File 'lib/webmachine/trace/trace_resource.rb', line 71

def produce_file
  # TODO: Add support for IO objects as response bodies,
  # allowing server optimizations like sendfile or chunked
  # downloads
  File.binread(@file)
end

#produce_listObject



78
79
80
81
82
# File 'lib/webmachine/trace/trace_resource.rb', line 78

def produce_list
  base = request.uri.path.chomp('/')
  traces = Trace.traces.map { |t| [t, "#{base}/#{t}"] }
  self.class.tracelist.result(binding)
end

#produce_traceObject



84
85
86
87
88
89
# File 'lib/webmachine/trace/trace_resource.rb', line 84

def produce_trace
  data = Trace.fetch(@trace)
  treq, tres, trace = encode_trace(data)
  name = @trace
  self.class.trace.result(binding)
end

#resource_exists?Boolean



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/webmachine/trace/trace_resource.rb', line 44

def resource_exists?
  case request.path_tokens
  when []
    true
  when MAP_EXTERNAL
    @file = MAP_FILE
    File.exist?(MAP_FILE)
  when SCRIPT_EXTERNAL
    @file = SCRIPT_FILE
    File.exist?(SCRIPT_FILE)
  when STYLE_EXTERNAL
    @file = STYLE_FILE
    File.exist?(STYLE_FILE)
  else
    @trace = request.path_tokens.first
    Trace.traces.include? @trace
  end
end