-
Notifications
You must be signed in to change notification settings - Fork 2
/
session-table.js
320 lines (306 loc) · 10.4 KB
/
session-table.js
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
import $ from './jquery.js'
import 'bootstrap'
import 'popper.js'
import 'pdfmake'
import JSZip from 'jszip'
import dataTables from 'datatables.net-bs4'
import buttons from 'datatables.net-buttons-bs4'
import columnVisibility from 'datatables.net-buttons/js/buttons.colVis.js'
import buttonsHtml5 from 'datatables.net-buttons/js/buttons.html5.js'
import buttonsPrint from 'datatables.net-buttons/js/buttons.print.js'
import colReorder from 'datatables.net-colreorder-bs4'
import fixedHeader from 'datatables.net-fixedheader-bs4'
import jsonExport from './json-export.js'
import { displayDetails, openSession } from './session-details.js'
import { formatColumnStats } from './footer-formatters.js'
import {
formatURL, formatScheme, formatPort, formatPathOrQuery, reformatDuration,
reformatTime, formatSize
} from './data-formatters.js'
import { sazStore } from './saz-store.js'
import { configuration, saveConfiguration } from './configuration.js'
import { prepareSazHelp, showHelp } from './help.js'
let tableWrapper, dataTable, columns
function destroySessionTable () {
if (dataTable) {
const oldTable = dataTable
dataTable = undefined
oldTable.destroy()
tableWrapper.html('')
}
}
function initializeSessionTable () {
tableWrapper = $('#table-wrapper')
columns = [
{ data: 'Number', title: '#' },
{ data: 'Timeline', title: 'Timeline' },
{ data: 'Method', title: 'Method' },
{ data: 'StatusCode', title: 'Status' },
{ data: 'URL', title: 'URL' },
{ data: 'Scheme', title: 'Scheme' },
{ data: 'Host', title: 'Host' },
{ data: 'Port', title: 'Port', className: 'dt-right' },
{ data: 'HostAndPort', title: 'Host+Port' },
{ data: 'Path', title: 'Path' },
{ data: 'Query', title: 'Query' },
{ data: 'PathAndQuery', title: 'Path+Query' },
{ data: 'BeginTime', title: 'Begin' },
{ data: 'EndTime', title: 'End' },
{ data: 'Duration', title: 'Duration' },
{ data: 'SendingTime', title: 'Sending' },
{ data: 'RespondingTime', title: 'Responding' },
{ data: 'ReceivingTime', title: 'Receiving' },
{ data: 'ContentType', title: 'Type' },
{
data: 'ContentLength',
title: 'Size',
className: 'dt-right',
render: function (data, type, row) {
if (type === 'display' || type === 'filter') {
return formatSize(data)
}
return data
}
},
{ data: 'Encoding', title: 'Encoding' },
{ data: 'Caching', title: 'Caching', orderable: false },
{ data: 'Process', title: 'Process' }
]
window.JSZip = JSZip
dataTables(window, $)
buttons(window, $)
columnVisibility(window, $)
buttonsHtml5(window, $)
buttonsPrint(window, $)
colReorder(window, $)
fixedHeader(window, $)
jsonExport(window, $)
}
function displaySessionTable (sessions) {
const { columns: columnSettings, order: orderSettings, search } = configuration
configureColumns(columnSettings)
const order = convertOrder(orderSettings)
const data = prepareData(sessions)
const detailRows = []
const table = $('<table class="table table-sm table-striped table-hover nowrap compact display">')
table.append('<thead></thead>')
table.append('<tbody></tbody>')
table.append('<tfoot><tr></tr></tfoot>')
dataTable = table.on('column-visibility.dt', columnVisibilityChanged)
.on('search.dt', filterChanged)
.on('order.dt', orderChanged)
.appendTo(tableWrapper)
.DataTable({
columns,
data,
order,
search: { search },
dom: '<"top"ifB>rt',
fixedHeader: true,
colReorder: true,
paging: false,
buttons: [
{
extend: 'colvis',
text: 'Columns'
},
{
extend: 'collection',
text: 'Export',
buttons: ['copy', 'print',
{
extend: 'json',
className: 'divide-at-top'
}, 'csv', 'excel', 'pdf']
}
],
rowCallback,
footerCallback,
executeSearch
})
.on('click', 'tbody tr td:not([colspan])', function (event) {
event.preventDefault()
displayDetails(dataTable, $(this), detailRows, sazStore.loaded)
})
.on('click', '[data-session]', function (event) {
event.preventDefault()
const link = $(this)
openSession(sazStore.loaded,
sazStore.loaded.Sessions[link.data().session - 1], link.attr('download'))
})
$('.dataTables_wrapper')
.find('.dataTables_info')
.addClass('form-control-sm')
.attr('data-intro', '#info')
.end()
.find('.dataTables_filter')
.attr('data-intro', '#filter')
.end()
.find('.dt-buttons')
.attr('data-intro', '#buttons')
.end()
.find('table thead th[aria-label!="#"]')
.first()
.attr('data-intro', '#column')
prepareSazHelp()
if (configuration.help.saz !== false) {
setTimeout(showHelp, 500)
}
}
function configureColumns (columnSettings) {
for (const column of columns) {
const settings = columnSettings[column.data]
column.visible = settings && settings.visible
}
}
function convertOrder (orderSettings) {
const { column: orderColumn, descending } = orderSettings
return [
columns.findIndex(({ data }) => data === orderColumn),
descending ? 'desc' : 'asc'
]
}
function prepareData (response) {
const lastTimeLine = response[response.length - 1].Timeline
const durationPrecision = lastTimeLine.startsWith('00:00')
? 6 : lastTimeLine.startsWith('00') ? 3 : 0
return response.map(session => ({
Number: session.Number,
Timeline: reformatDuration(session.Timeline, durationPrecision),
Method: session.Request.Method,
StatusCode: session.Response.StatusCode,
URL: formatURL(session.Request.URL.Full),
Scheme: formatScheme(session.Request.URL.Scheme),
Host: session.Request.URL.Host,
Port: formatPort(session.Request.URL.Port, session.Request.URL.Scheme),
HostAndPort: session.Request.URL.HostAndPort,
Path: formatPathOrQuery(session.Request.URL.Path),
Query: formatPathOrQuery(session.Request.URL.Query),
PathAndQuery: formatPathOrQuery(session.Request.URL.PathAndQuery),
BeginTime: reformatTime(session.Timers.ClientBegin),
EndTime: reformatTime(session.Timers.ClientDoneResponse),
Duration: reformatDuration(session.Timers.RequestResponseTime, durationPrecision),
SendingTime: reformatDuration(session.Timers.RequestSendTime, durationPrecision),
RespondingTime: reformatDuration(session.Timers.ServerProcessTime, durationPrecision),
ReceivingTime: reformatDuration(session.Timers.ResponseReceiveTime, durationPrecision),
ContentType: session.Response.ContentType,
ContentLength: session.Response.ContentLength,
Encoding: session.Response.Encoding,
Caching: session.Response.Caching,
Process: session.Request.Process
}))
}
function rowCallback (row, data, displayNum, displayIndex, dataIndex) {
row = $(row)
const statusCode = data.StatusCode
if (statusCode >= 400) {
return row.addClass('text-danger')
}
if (statusCode >= 300) {
return row.addClass('text-warning')
}
if (data.Method === 'CONNECT') {
return row.addClass('text-muted')
}
const contentType = data.ContentType
if (contentType.startsWith('application/json') || contentType.startsWith('application/xml') ||
contentType.startsWith('text/xml') || contentType.startsWith('text/plain')) {
return row.addClass('text-info')
}
if (contentType.startsWith('text/css') || contentType.startsWith('text/javascript') ||
contentType.startsWith('application/javascript') || contentType.startsWith('image/') ||
contentType.startsWith('font/')) {
return row.addClass('text-success')
}
}
function footerCallback (row, data, start, end, display) {
const table = this.api().table()
const headers = table.header().querySelectorAll('tr th')
const foot = $(table.footer()).html('')
const footerRow = $('<tr>').appendTo(foot)
if (display.length) {
for (let i = 0, length = headers.length; i < length; ++i) {
const header = headers[i]
const columnIndex = +header.getAttribute('data-column-index')
const column = columns[columnIndex]
const footer = header.cloneNode(true)
footer.innerHTML = `${formatColumnStats(display, column)}
<span>${column.title}</span>`
footerRow.append(footer)
}
}
}
function executeSearch (settings, data, dataString, input) {
input = input.trim()
if (!input) return true
const filters = input
.toLowerCase()
.split(/\s+/)
.map(part => {
let [column, term] = part.split(':')
if (!term) {
term = column
column = null
}
return term.startsWith('-') ? { column, exclude: true, term: term.substr(1) }
: term.startsWith('+') ? { column, term: term.substr(1) } : { column, term }
})
dataString = dataString.trim().toLowerCase()
for (const filter of filters) {
const column = filter.column
const dataContent = column ? getColumnValue(column) : dataString
const term = filter.term
if (filter.exclude) {
if (term.length) {
if (dataContent.includes(term)) return false
} else {
if (!dataContent.length) return false
}
} else {
if (term.length) {
if (!dataContent.includes(term)) return false
} else {
if (dataContent.length) return false
}
}
}
return true
function getColumnValue (column) {
for (const name in data) {
if (name.toLowerCase() === column) {
return data[name].toString().toLowerCase()
}
}
return ''
}
}
function columnVisibilityChanged (event, settings, column, state) {
if (dataTable) {
configuration.columns = settings.aoColumns.reduce((columns, column) => {
columns[column.mData] = { visible: column.bVisible }
return columns
}, {})
saveConfiguration()
}
}
function filterChanged (event, settings) {
if (dataTable) {
var search = settings.oPreviousSearch.sSearch
if (search !== configuration.search) {
configuration.search = search
saveConfiguration()
}
}
}
function orderChanged (event, settings, state) {
if (dataTable) {
const order = settings.aaSorting[0]
const newOrder = { column: columns[order[0]].data, descending: order[1] === 'desc' }
const oldOrder = configuration.order
if (newOrder.column !== oldOrder.column || newOrder.descending !== oldOrder.descending) {
configuration.order = newOrder
saveConfiguration()
}
}
}
export { initializeSessionTable, displaySessionTable, destroySessionTable }