Class: MassiveImport::DashboardServer

Inherits:
Object
  • Object
show all
Defined in:
lib/massive-import/dashboard_server.rb

Instance Method Summary collapse

Instance Method Details

#batches_table(batches) ⇒ Object



131
132
133
134
135
136
137
138
139
140
# File 'lib/massive-import/dashboard_server.rb', line 131

def batches_table(batches)
  return '<p>No batches yet.</p>' if batches.empty?

  rows = batches.map do |b|
    started = b.started_at ? Time.at(b.started_at.to_i).strftime('%Y-%m-%d %H:%M:%S') : '-'
    ["##{b.id}", b.attempt, h(b.status), b.start_id, b.end_id, started]
  end

  table(['Batch', 'Attempt', 'Status', 'Start ID', 'End ID', 'Started'], rows)
end

#dispatch(request, response, logger) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/massive-import/dashboard_server.rb', line 35

def dispatch(request, response, logger)
  unless request.request_method == 'GET'
    respond(response, 405, layout(request, 'Method not allowed', '<h1>405 Method Not Allowed</h1>'))
    return
  end

  title, content = yield

  if content
    respond(response, 200, layout(request, title, content))
  else
    respond(response, 404, layout(request, 'Not found', '<h1>404 Not Found</h1><p><a href="/">Back to imports</a></p>'))
  end
rescue => e
  logger.info("Unhandled exception: message=#{e.message}")
  logger.info("Unhandled exception: trace=#{e.backtrace}")
  respond(response, 500, layout(request, 'Error', "<h1>500 Internal Server Error</h1><p><a href=\"/\">Back to imports</a></p>"))
end

#h(text) ⇒ Object



156
157
158
# File 'lib/massive-import/dashboard_server.rb', line 156

def h(text)
  CGI.escapeHTML(text.to_s)
end

#home(request) ⇒ Object



54
55
56
# File 'lib/massive-import/dashboard_server.rb', line 54

def home(request)
  render_imports if request.path_info == '/'
end

#import(request) ⇒ Object



58
59
60
# File 'lib/massive-import/dashboard_server.rb', line 58

def import(request)
  render_import($1.to_i) if request.path_info =~ %r{\A/(\d+)\z}
end

#layout(request, title, content) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/massive-import/dashboard_server.rb', line 160

def layout(request, title, content)
  refresh = request.query['refresh'].to_i
  refresh_html = +''
  refresh_html << %(<meta http-equiv="refresh" content="#{refresh}">) if refresh > 0

  <<~HTML
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      #{refresh_html}
      <title>#{h(title)}</title>
    </head>
    <body>
      #{content}
    </body>
    </html>
  HTML
end

#render_import(id) ⇒ Object



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
# File 'lib/massive-import/dashboard_server.rb', line 89

def render_import(id)
  imp = Import.find_by(id: id)
  return ['Not found', '<h1>Import not found</h1><p><a href="/">Back to imports</a></p>'] unless imp

  batch_stats  = Batch.where(import_id: imp.id).group(:status).count
  record_stats = Record.where(import_id: imp.id).group(:status).count
  batches = Batch.where(import_id: imp.id).order(id: :desc).limit(100).to_a

  summary_headers = ['Processor', 'Attempt', 'Total records', 'Batched records', 'Concurrency', 'Batch size']
  summary_values = [
    h(imp.processor_class),
    "#{imp.attempt} / #{imp.max_attempts}",
    imp.total_records,
    "#{imp.batch_records} / #{imp.total_records}",
    "#{imp.current_batch_concurrency} / #{imp.max_batch_concurrency}",
    imp.batch_size
  ]

  content = +''
  content << %(<p><a href="/">&larr; All imports</a></p>)
  content << "<h1>Import ##{imp.id} (#{h(imp.status)})</h1>"
  content << table(summary_headers, [summary_values])

  content << '<h2>Batches by status</h2>'
  content << stats_table(batch_stats)
  content << '<h2>Records by status</h2>'
  content << stats_table(record_stats)
  content << '<h2>Recent 100 batches</h2>'
  content << batches_table(batches)

  ["Import ##{imp.id}", content]
end

#render_importsObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/massive-import/dashboard_server.rb', line 68

def render_imports
  imports = Import.order(id: :desc).to_a
  return ['Imports', '<h1>Imports</h1><p>No imports found.</p>'] if imports.empty?

  headers = ['ID', 'Status', 'Processor', 'Attempt', 'Total', 'Batched', 'Concurrency', 'Batch size']
  rows = imports.map do |imp|
    [
      %(<a href="/imports/#{imp.id}">##{imp.id}</a>),
      h(imp.status),
      h(imp.processor_class),
      "#{imp.attempt} / #{imp.max_attempts}",
      imp.total_records,
      "#{imp.batch_records} / #{imp.total_records}",
      "#{imp.current_batch_concurrency} / #{imp.max_batch_concurrency}",
      imp.batch_size
    ]
  end

  ['Imports', "<h1>Imports</h1>#{table(headers, rows)}"]
end

#respond(response, status, body) ⇒ Object



62
63
64
65
66
# File 'lib/massive-import/dashboard_server.rb', line 62

def respond(response, status, body)
  response.status = status
  response['Content-Type'] = 'text/html; charset=utf-8'
  response.body = body
end

#start(**options) ⇒ Object



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
# File 'lib/massive-import/dashboard_server.rb', line 6

def start(**options)
  port = options.fetch(:dashboard_port, (ENV["dashboard_port"] || 9292).to_i)
  host = options.fetch(:dashboard_host, ENV["dashboard_host"] || "127.0.0.1")

  logger = WEBrick::Log.new($stdout, WEBrick::Log::INFO)
  server = WEBrick::HTTPServer.new(
    BindAddress: host,
    Port: port,
    Logger: logger,
    AccessLog: []
  )

  server.mount_proc('/') do |request, response|
    dispatch(request, response, logger) do
      home(request)
    end
  end

  server.mount_proc('/imports') do |request, response|
    dispatch(request, response, logger) do
      import(request)
    end
  end

  %w[INT TERM].each { |signal| trap(signal) { server.shutdown } }

  server.start
end

#stats_table(stats) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/massive-import/dashboard_server.rb', line 122

def stats_table(stats)
  return '<p>None.</p>' if stats.empty?

  rows = stats.map { |status, count| [h(status), count] }
  rows << ['<strong>Total</strong>', "<strong>#{stats.values.sum}</strong>"]

  table(['Status', 'Count'], rows)
end

#table(headers, rows) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/massive-import/dashboard_server.rb', line 142

def table(headers, rows)
  head = headers.empty? ? '' : "<thead><tr>#{headers.map { |header| "<th>#{header}</th>" }.join}</tr></thead>"
  body = rows.map do |cells|
    "<tr>#{cells.map { |cell| "<td>#{cell}</td>" }.join}</tr>"
  end.join

  <<~TABLE
    <table border="1" cellpadding="4" cellspacing="0">
      #{head}
      <tbody>#{body}</tbody>
    </table>
  TABLE
end