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



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

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

.tracelistObject

The ERB template for the trace list



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

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

Instance Method Details

#content_types_providedObject



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

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



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

def encode_decisions(decisions)
  decisions.inject([]) do |list, event|
    case event[:type]
    when :decision
      # Don't produce new decisions for sub-steps in the graph
      unless event[:decision].to_s =~ /[a-z]$/
        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
    list
  end
end

#encode_trace(data) ⇒ Object



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

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



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

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

#last_modifiedObject



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

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

#produce_fileObject



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

def produce_file
  # TODO: Add support for IO objects as response bodies,
  # allowing server optimizations like sendfile or chunked
  # downloads
  open(@file, "rb") {|io| io.read }
end

#produce_listObject



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

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

#produce_traceObject



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

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

#resource_exists?Boolean

Returns:

  • (Boolean)


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

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