/
DmdrReportGenerator.js
337 lines (308 loc) · 11.5 KB
/
DmdrReportGenerator.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//DMDR Report Generator was created by staff of the U.S. Securities and Exchange Commission.
//Data and content created by government employees within the scope of their employment
//are not subject to domestic copyright protection. 17 U.S.C. 105.
const NS = ''; /* null string */
const WS = ' '; /* one whitespace */
const ZWSP = '\u200B'; /* zero width space */
const NL = '\n';
const SEMI = ';'; /* one semicolon */
const AUTO = 'auto';
const TEXTSTYLE = 'textStyle';
const TABLEVALUE = 'tableValue';
const COMPRESS = false; /* For debugging, set false so as to inspect pdf output. */
const FILLCOLOR = '#CCE6FF'; /* blue */
const FAILCOLOR = '#FFCCE6'; /* pink */
const GRAYCOLOR = '#CCCCCC'; /* gray */
var basename = null;
var xsdContent, xmlDoc, matrAspectsArr, detailData;
var hasTimeStamp = false;
/******* d m d r ******* based on b1 because it is kinda similar but without a top level split in three ****/
function createDailyMarketDataReportPDF() { /* d m d r */
memoryStatInitialize();
var docStyles = {
header : { fontSize : 16, bold : true, alignment : 'center' },
header3 : { fontSize : 10, bold : true, alignment : 'center', lineHeight: 1.2 },
header4 : { fontSize : 8, bold : true, alignment : 'center', lineHeight: 1.2 },
sectionHeader : { fontSize : 12, bold : true, alignment : 'left', lineHeight: 1.2 },
subSectionHeader : { fontSize : 10, bold : true, alignment : 'left', lineHeight: 1.2 },
textStyle : { fontSize : 8 },
tableHeader : { fontSize : 7, bold : false, alignment : 'center', fillColor : FILLCOLOR },
tableValue : { fontSize : 6, alignment : 'left' },
tableNameValue : { fontSize : 7, alignment : 'left' },
failHeader: { fontSize: 9, bold: true, alignment : 'center', fillColor : FAILCOLOR }
}
var body = getSbsData();
var hasoutput = body.length > 0; /* not right */
if (!hasoutput) {
alert('No transactions in '+basename.name+', no output files.');
}
const outname = ((basename == null) ? 'DailyMarketData' : removeExtension(basename.name));
const title = 'Daily Market Data Report';
const docDefinition = {info:
{title: title,
PageLayout: 'OneColumn'},
content: body,
pageOrientation : 'landscape',
styles: docStyles};
const pdf = pdfMake.createPdf(docDefinition);
pdf.download(outname + '.pdf')
}
const dmdrFirstPage = [
[ 'sbsef', 'left', 'SBSEF Name'],
[ 'date', 'left', 'Market Data for Date'],
[ 'generated', 'left', 'Generated']
];
const dmdrFirstPageElts = [];
for (i=0;i<dmdrFirstPage.length;i++) { dmdrFirstPageElts.push(dmdrFirstPage[i][0]); }
const dmdrFirstPageAlign = [];
for (i=0;i<dmdrFirstPage.length;i++) { dmdrFirstPageAlign.push(dmdrFirstPage[i][1]); }
const dmdrFirstPageHeaders = [];
for (i=0;i<dmdrFirstPage.length;i++) { dmdrFirstPageHeaders.push(dmdrFirstPage[i][2]); }
const dmdrLastPage = [
[ 'mtdUsedToDtrmnNmnlPrc', 'left', 'Method used to determine nominal prices'],
[ 'mtdUsedToDtrmnSttlmPrc', 'left', 'Method used to determine settlement prices'],
[ 'dscrtnExpltn', 'left', 'Explanation that discretion may be employed'],
[ 'dscrtnMnnrDesc', 'left', 'Description of the manner in which discretion may be employed']
];
const dmdrLastPageElts = [];
for (i=0;i<dmdrLastPage.length;i++) { dmdrLastPageElts.push(dmdrLastPage[i][0]); }
const dmdrLastPageAlign = [];
for (i=0;i<dmdrLastPage.length;i++) { dmdrLastPageAlign.push(dmdrLastPage[i][1]); }
const dmdrLastPageHeaders = [];
for (i=0;i<dmdrLastPage.length;i++) { dmdrLastPageHeaders.push(dmdrLastPage[i][2]); }
const dmdrTable = [ /* xml name, alignment, width, pretty name. ORDER is significant. 570. */
/* pretty name has newlines because package does not support vertical-alignment. */
[ 'sbsNm', 'left', 72, NL.repeat(6) + 'Name'],
[ 'upi', 'left', 45, NL.repeat(6) + 'UPI'],
[ 'tenor', 'left', 33, NL.repeat(6) + 'Tenor'],
[ 'ccy', 'left', 20, NL.repeat(5) + 'Cur-' + NL + 'rency'],
[ 'prcFrmt', 'left', 40, NL.repeat(5) + 'Price' + NL + 'Format'],
[ 'tradCnt', 'right', 40, NL.repeat(5) + 'Trade' + NL + 'Count'],
[ 'ttlNtnlAmtTraddUsd', 'right', 70, NL.repeat(5) + 'Total Notional Amount Traded (USD)'],
[ 'ttlNtnlAmtBlckTradsUsd', 'right', 70, NL.repeat(4) + 'Total Notional Amount of Block Trades (USD)'],
[ 'opngPrc', 'right', 25, NL.repeat(5) + 'Open Price'],
[ 'clsgPrc', 'right', 25, NL.repeat(5) + 'Close Price'],
[ 'sttlmPrc', 'right', 25, NL.repeat(4) + 'Settle-' + NL + 'ment Price'],
[ 'lwstPrc', 'right', 25, NL.repeat(5) + 'Lowest Price'],
[ 'lwstPrcTyp', 'center', 25, NL.repeat(4) + 'Low Price Type'],
[ 'hgstPrc', 'right', 25, NL.repeat(5) + 'Highest Price'],
[ 'hgstPrcTyp', 'center', 25, NL.repeat(4) + 'High Price Type'],
[ 'dscrtnryAuthrtyApld', 'center', 25, NL.repeat(2) + 'Discre-' + NL + 'tionary Auth-' + NL + 'ority Applied'],
[ 'dscrtnryAuthrtyApldFtnt', 'left', 25, NL.repeat(5) + 'Foot-' + NL + 'note']
];
const sbsCols = dmdrTable.length;
const sbsElts = [];
for (i=0;i<dmdrTable.length;i++) { sbsElts.push(dmdrTable[i][0]); }
const sbsAlign = [];
for (i=0;i<dmdrTable.length;i++) { sbsAlign.push(dmdrTable[i][1]); }
const sbsWidths = [];
for (i=0;i<dmdrTable.length;i++) { sbsWidths.push(dmdrTable[i][2]); }
const sbsHeaders = [];
for (i=0;i<dmdrTable.length;i++) { sbsHeaders.push(dmdrTable[i][3]); }
function getSbsData() { /* dmdr */
preamble = [];
preamble.push({text: "Daily Market Data Report",
style: 'header',
tags: ['TITLE','/TITLE'] });
$.each(dmdrFirstPage, function(index, val) {
var tag = val[0];
var align = val[1];
var label = val[2];
var elts = xmlDoc.getElementsByTagName(tag);
if (elts.length > 0) {
var content = elts[0].textContent.trim();
if (index == 0 || tag == 'sbsef') {
preamble.push({
text: content, style: 'header', tags: ['H','/H']});
} else if (dmdrFirstPageElts.indexOf(elts[0].localName) >= 0) {
preamble.push({
text: label, style: 'sectionHeader', tags: ['H2','/H2']});
var content = elts[0].textContent.trim();
preamble.push({
text: content, style: 'textStyle', tags: ['P', '/P']});
}
}})
const lastCol = sbsCols - 1;
var body = [];
var row = [];
const sbsRows = xmlDoc.getElementsByTagName('sbsTnr');
const lastRow = sbsRows.length - 1;
$.each(sbsHeaders, function(i, val) {
isFirstCol = (i == 0);
isLastCol = (i == lastCol);
row.push({
text: val,
tags: []
.concat(isFirstCol ? ['Table','TR'] : [])
.concat(['TH','/TH'])
.concat(isLastCol ? ['/TR'] : [] ),
style: 'tableHeader', unbreakable:true
});
});
body.push(row);
$.each(sbsRows, function (j, sbsRow) {
row = [];
isFirstRow = (j == 0);
isLastRow = (j == lastRow);
$.each(sbsElts, function(i, local_name) {
isFirstCol = (i == 0);
isLastCol = (i == lastCol);
content = WS; /* really empty cells aren't handled by the tagging code */
/* columns might be empty so first see if there is a value in the XML */
nodes = sbsRow.getElementsByTagName(local_name);
if (nodes.length > 0) {
child = nodes[0]
content = child.textContent.trim();
if (sbsAlign[i] == 'right') {
content = addCommas(content);
}
}
row.push({
text: content,
tags: []
.concat(isFirstCol ? ['TR'] : [])
.concat(['TD','/TD'])
.concat(isLastCol ? ['/TR'] : [] )
.concat(isLastCol && isLastRow ? ['/Table'] : [] ),
style: 'tableValue',
unbreakable:true,
alignment: sbsAlign[i]
});
});
body.push(row);
});
postamble = [];
$.each(dmdrLastPage, function(index, val) {
var tag = val[0];
var align = val[1];
var label = val[2];
var elts = xmlDoc.getElementsByTagName(tag);
if (elts.length > 0) {
var content = elts[0].textContent.trim();
if (dmdrLastPageElts.indexOf(elts[0].localName) >= 0) {
postamble.push({
text: label, style: 'sectionHeader', tags: ['H2','/H2']});
var content = elts[0].textContent.trim();
postamble.push({
text: content, style: 'textStyle', tags: ['P', '/P']});
}
}})
return [preamble, NL,
{table: { body : body,
headerRows : 1,
dontBreakRows: true,
widths : sbsWidths
}
},
NL, postamble]
}
/**** *****/
/****** utilities ******/
function formatDate(timestamp) {
var date = new Date(timestamp);
return date.toString();
}
function removeExtension(filename){
var lastDotPosition = filename.lastIndexOf('.');
if (lastDotPosition === -1) return filename;
else return filename.substr(0, lastDotPosition);
}
function loadXML() {
var oFiles = document.getElementById('xmlFile').files;
var isValid;
try {
var reader = new FileReader();
basename = oFiles[0];
reader.readAsText(oFiles[0]);
} catch (err) {
alert(err);
}
reader.onloadend = function() {
try {
if (xsdContent != null) {
isValid = validateXMLContent(reader.result, oFiles[0].name);
}
if (isValid) {
if (window.DOMParser) {
var parser = new DOMParser();
xmlDoc = $.parseXML(reader.result);
} else if (window.ActiveXObject) {
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false;
xmlDoc.loadXML(reader.result);
}
if (xmlDoc.getElementsByTagName('timestamp')[0] != null) {
hasTimeStamp = true;
}
if (xmlDoc.getElementsByTagName('notHeldOrderHandlingCustomerReport')[0] != null) {
/* b3 */
createNotHeldOrderHandlingCustomerReportPDF({});
} else if (xmlDoc.getElementsByTagName('heldOrderRoutingPublicReport')[0] != null) {
// a1
createHeldOrderRoutingPublicReportPDF();
} else if (xmlDoc.getElementsByTagName('heldExemptNotHeldOrderRoutingCustomerReport')[0] != null) {
// b1
createHeldExemptNotHeldOrderRoutingCustomerReport();
} else if (xmlDoc.getElementsByTagName('dailyMarketDataReport')[0] != null) {
// d m d r
createDailyMarketDataReportPDF();
} else {
alert('NO MATCH');
}
}
document.getElementById('rule606Form').reset();
} catch (err) {
alert(err);
}
}
}
function validateXMLContent(xmlContent, xmlFileName) {
var isValid = true;
var Module = {
xml : xmlContent,
schema : xsdContent,
arguments : [ '--noout', '--schema', 'oh-20160630.xsd', xmlFileName ]
};
var validationMessage = validateXML(Module);
if (validationMessage.indexOf('fails') >= 0) {
isValid = false;
alert(validationMessage);
}
return isValid;
}
function loadXSD() {
var oFiles = document.getElementById('xsdFile').files;
try {
var reader = new FileReader();
reader.readAsText(oFiles[0]);
reader.onloadend = function() {xsdContent = reader.result;};
} catch (err) {
alert(err);
}
}
function addCommas(number) {
var x = number.split('.');
var y = x[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,');
if (x[1] != null) {
y = y + '.' + x[1];
}
return y;
}
/**** memory metering ****/
var memoryMessages = [{used:0}];
function memoryStatInitialize() {
memoryMessages = [{used:0}];
}
function memoryStat(msg,popup) {
if (window.performance == undefined || window.performance.memory == undefined) return;
var memory = window.performance.memory;
var used = Math.round(memory.usedJSHeapSize/(1024*1024));
var total = Math.round(memory.totalJSHeapSize/(1024*1024));
var previous = memoryMessages[memoryMessages.length-1]
var change = used - previous.used;
var record = {msg:msg,used:used,total:total,change:change};
memoryMessages.push(record);
if (popup) {
alert(JSON.stringify(record));
}
}