-
Notifications
You must be signed in to change notification settings - Fork 0
/
rt_data_parser.js
623 lines (564 loc) · 17.3 KB
/
rt_data_parser.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/** Imports */
import axios from "axios";
import cliProgress from "cli-progress";
import colors from "ansi-colors";
import { Command } from "commander";
import inquirer from "inquirer";
import he from "html-entities";
import fs from "fs";
import {
strip_html_block,
convert_date,
strip_html_tags,
} from "./lib/functions.js";
/** Setup CLI args */
const commander = new Command();
const CLI_PARAMS = commander
.requiredOption("-u, --username <username>", "RT account username")
.requiredOption("-o, --output_file <output_file>", "CSV outputfile")
.requiredOption(
"-h, --host <host>",
"RT host URL, e.g. http://localhost:8080"
)
.option(
"-i, --ticket-id <ticket_id>",
"Top-most RT ticket id to parse"
)
.option(
"-n, --numbers <numbers>",
"How many tickets to parse, from --ticket-id downards"
)
.option(
"-g, --customer_group <customer_group>",
"Optional, the customer group RT ID to export data for."
)
.parse()
.opts();
/** Setup constants */
let REQUEST_HEADERS = {
auth: {
username: CLI_PARAMS.username,
},
};
const OUTPUT_FILE = CLI_PARAMS.output_file;
const CUSTOMER_GROUP = CLI_PARAMS.customer_group;
const TOP_TICKET_ID = CLI_PARAMS.ticketId;
let HOW_MANY_TICKETS = CLI_PARAMS.numbers;
const RT_API_URL = `${CLI_PARAMS.host}/REST/2.0`;
const STREAM = fs.createWriteStream("error.log", { flags: "a" });
const password_input = [
{
type: "password",
name: "password",
message: "Enter password for user "+CLI_PARAMS.username+" at "+CLI_PARAMS.host+":",
},
];
await inquirer.prompt(password_input).then((answers) => {
REQUEST_HEADERS.auth.password = answers.password;
});
/** Setup progress bars */
const MULTIBAR = new cliProgress.MultiBar(
{
clearOnComplete: false,
hideCursor: true,
autopadding: true,
format:
colors.cyan("{bar}") +
" | {percentage}% | {message} | {value}/{total} | ETA: {eta_formatted} | Duration: {duration_formatted}",
},
cliProgress.Presets.shades_grey
);
const PROGRESS_BAR_1 = MULTIBAR.create(1, 0);
/** Main */
get_tickets_data(TOP_TICKET_ID, HOW_MANY_TICKETS);
/**
* Parses a ticket by fetching and processing data from the RT API.
*
* @param {number} ticket_id - The ID of the ticket to parse.
* @return {Promise<void>} - A Promise that resolves when the ticket has been parsed.
*/
async function parse_ticket(ticket_id) {
try {
const ticket_data = await axios
.get(`${RT_API_URL}/ticket/${ticket_id}`, REQUEST_HEADERS)
.then((response) => {
return response.data;
});
const ticket_user = await axios
.get(`${RT_API_URL}/user/${ticket_data.Creator.id}`, REQUEST_HEADERS)
.then((response) => {
return response.data;
});
const ticket_queue = await axios
.get(`${RT_API_URL}/queue/${ticket_data.Queue.id}`, REQUEST_HEADERS)
.then((response) => {
return response.data;
});
const ticket_transactions_history_data =
await get_ticket_transactions_history_data(ticket_id);
let ticket_obj = await create_ticket_obj(
ticket_data,
ticket_user,
ticket_queue,
ticket_transactions_history_data
);
return ticket_obj;
} catch (err) {
if (err.response.status === 401) {
console.log('ERROR: '+err.response.status+' '+err.response.statusText);
}
STREAM.write("[ERROR]: Ticket id: " + ticket_id + ": " + err + "\n");
}
}
async function get_customer_group_data() {
return await axios
.get(
`${RT_API_URL}/tickets?page=1&per_page=20&query=requestor=${CUSTOMER_GROUP}`,
REQUEST_HEADERS
)
.then((response) => {
return response.data;
});
}
async function process_customer_group_page(args) {
let cg_data;
if(!args.cg_data && args.next_page) {
cg_data = await axios
.get(args.next_page, REQUEST_HEADERS)
.then((response) => {
return response.data;
});
}else{
cg_data = args.cg_data;
}
let cg_promises = [];
cg_data.items.forEach((ticket) => {
const promise = parse_ticket(ticket.id).then((res) => {
PROGRESS_BAR_1.increment();
//Output csv ticket row
try {
const row_data = "\n" + Object.values(res).toString();
fs.appendFile(OUTPUT_FILE, row_data, (err) => {
if (err) {
console.error(err);
} else {
// file written successfully
}
});
} catch (err) {
STREAM.write("[ERROR]: Ticket id: " + ticket.id + ": " + err + "\n");
}
});
cg_promises.push(promise);
});
Promise.all(cg_promises).then(() => {
if (cg_data.next_page) {
process_customer_group_page({next_page: cg_data.next_page});
}else{
STREAM.end();
MULTIBAR.stop();
}
});
}
/**
* Retrieves ticket data for a specified range of ticket IDs.
*
* @return {Promise<void>} - Resolves when all ticket data has been retrieved.
*/
async function get_tickets_data() {
if(CUSTOMER_GROUP){
const cg_data = await get_customer_group_data();
HOW_MANY_TICKETS = cg_data.total;
PROGRESS_BAR_1.start(HOW_MANY_TICKETS, 0);
PROGRESS_BAR_1.update({
message: `Processing tickets...`,
});
await process_customer_group_page({cg_data: cg_data});
}else{
PROGRESS_BAR_1.start(HOW_MANY_TICKETS, 0);
PROGRESS_BAR_1.update({
message: `Processing tickets...`,
});
STREAM.write(
`[INFO]: Processing ${HOW_MANY_TICKETS} tickets from ticket id #${TOP_TICKET_ID} \n`
);
//Output CSV header columns
const headings_content = get_column_headings().join(",");
fs.writeFile(OUTPUT_FILE, headings_content, (err) => {
if (err) {
console.error(err);
} else {
// file written successfully
}
});
let promises = [];
for (let id = TOP_TICKET_ID; id > TOP_TICKET_ID - HOW_MANY_TICKETS; id--) {
const promise = parse_ticket(id).then((res) => {
PROGRESS_BAR_1.increment();
//Output csv ticket row
try {
const row_data = "\n" + Object.values(res).toString();
fs.appendFile(OUTPUT_FILE, row_data, (err) => {
if (err) {
console.error(err);
} else {
// file written successfully
}
});
} catch (err) {
STREAM.write("[ERROR]: Ticket id: " + id + ": " + err + "\n");
}
});
promises.push(promise);
}
Promise.all(promises).then(() => {
STREAM.end();
MULTIBAR.stop();
});
}
}
/**
* Retrieves the transaction history data for a given ticket.
*
* @param {string} ticket_id - The ID of the ticket.
* @return {Array} An array of transaction objects representing the ticket's history.
*/
async function get_ticket_transactions_history_data(ticket_id) {
let transactions = [];
let page = 1;
let bar2 = MULTIBAR.create(1, 0, 0, {
format:
colors.green("{bar}") +
" | {percentage}% | {message} | {value}/{total} | ETA: {eta_formatted} | Duration: {duration_formatted}",
});
const get_ticket_history_page = async (page) => {
return await axios
.get(
`${RT_API_URL}/transactions?page=${page}&query=[ { "field": "Type", "operator": "=", "value": "Comment" }, { "field": "Type", "operator": "=", "value": "Correspond" }, { "field": "Type", "operator": "=", "value": "Create" }, { "field": "ObjectId", "operator": "=", "value": ${ticket_id} } ]`,
REQUEST_HEADERS
)
.then((response) => {
return response.data;
});
};
const update_bar = (transaction_id) => {
bar2.increment();
if (transaction_id) {
bar2.update({
message: `Ticket #${ticket_id} | Parsing transaction #${transaction_id}`,
});
}
if (bar2.getProgress() == 1) {
MULTIBAR.remove(bar2);
}
};
const push_transaction = (transaction) => {
transactions.push(transaction);
update_bar(transaction.id);
};
const ticket_history = await get_ticket_history_page(page);
bar2.start(ticket_history.total, 1);
for (let i = 0; i < ticket_history.items.length; i++) {
try {
await parse_transaction(ticket_history.items[i].id).then((response) => {
push_transaction(response);
});
} catch (err) {
update_bar();
STREAM.write(
"[ERROR]: Ticket id: " +
ticket_id +
" | Transaction id: " +
ticket_history.items[i].id +
": " +
err +
"\n"
);
}
}
if (ticket_history.pages > 1) {
page++;
while (page <= ticket_history.pages) {
const ticket_history = await get_ticket_history_page(page++);
for (let i = 0; i < ticket_history.items.length; i++) {
try {
await parse_transaction(ticket_history.items[i].id).then(
(response) => {
push_transaction(response);
}
);
} catch (err) {
update_bar();
STREAM.write(
"[ERROR]: Ticket id: " +
ticket_id +
" | Transaction id: " +
ticket_history.items[i].id +
": " +
err +
"\n"
);
}
}
}
}
return transactions;
}
/**
* Retrieves and parses a transaction from the RT API.
*
* @param {string} transaction_id - The ID of the transaction to retrieve.
* @return {Promise} A promise that resolves with the parsed transaction data.
*/
async function parse_transaction(transaction_id) {
return await axios
.get(`${RT_API_URL}/transaction/${transaction_id}`, REQUEST_HEADERS)
.then((response) => {
return response.data;
});
}
/**
* Creates a ticket object based on the provided ticket data, user information, queue, and transaction history.
*
* @param {object} ticket_data - The data of the ticket.
* @param {object} ticket_user - The user associated with the ticket.
* @param {object} ticket_queue - The queue the ticket belongs to.
* @param {array} ticket_transactions_history_data - The transaction history data of the ticket.
* @return {object} - The created ticket object.
*/
async function create_ticket_obj(
ticket_data,
ticket_user,
ticket_queue,
ticket_transactions_history_data
) {
const comment_transactions =
await get_ticket_transactions_history_data_by_type(
ticket_transactions_history_data,
"Comment"
);
const create_transactions =
await get_ticket_transactions_history_data_by_type(
ticket_transactions_history_data,
"Create"
);
let first_correspondence_str = strip_html_block(
array_to_string(create_transactions),
"blockquote"
);
first_correspondence_str = strip_html_block(first_correspondence_str, "html");
// first_correspondence_str = strip_html_tags(first_correspondence_str);
const correspond_transactions =
await get_ticket_transactions_history_data_by_type(
ticket_transactions_history_data,
"Correspond"
);
let correspondence_str = strip_html_block(
array_to_string(correspond_transactions),
"blockquote"
);
correspondence_str = strip_html_block(correspondence_str, "html");
// correspondence_str = strip_html_tags(correspondence_str);
let comments_str = strip_html_tags(array_to_string(comment_transactions));
let column_headings = get_column_headings();
return {
[column_headings[0]]: ticket_data.EffectiveId.id,
[column_headings[1]]: ticket_data.EffectiveId.id,
[column_headings[2]]: 13018,
// any_comment: comments_str, #no mapping yet
[column_headings[3]]: convert_date(ticket_data.Resolved),
// created: convert_date(ticket_data.Created), #this doesnt seem to be working, openCRM sets this to time of import
// customer: ticket_data.Creator.id,
// customer_group: ticket_user.Organization,
[column_headings[4]]: convert_date(ticket_data.Resolved),
[column_headings[5]]: get_ticket_custom_field_value(
ticket_data.CustomFields,
"Outcome"
),
[column_headings[6]]: "Support",
[column_headings[7]]: ticket_queue.Name,
[column_headings[8]]: "--Please Select--",
[column_headings[9]]: 334,
[column_headings[10]]:
get_ticket_custom_field_value(
ticket_data.CustomFields,
"Security Incident"
).length == 0
? "No"
: get_ticket_custom_field_value(
ticket_data.CustomFields,
"Security Incident"
)[0],
[column_headings[11]]: "Archived", //This is hardcoded, if not: ticket_data.Status,
[column_headings[12]]: '"' + ticket_data.Subject.replace(/"/g, '\\"') + '"',
[column_headings[13]]:
'"' +
get_severity_mapping_value(
ticket_data.SLA ? ticket_data.SLA.replace(/"/g, '\\"') : ''
) +
'"',
[column_headings[14]]: get_ticket_custom_field_value(
ticket_data.CustomFields,
"TicketType"
),
[column_headings[15]]: get_user_mapping_value(ticket_data.Owner.id),
[column_headings[16]]: ticket_user.Organization,
[column_headings[17]]: first_correspondence_str,
[column_headings[18]]: correspondence_str,
};
}
/**
* Retrieves the value of a custom field from the given array of custom fields based on the field name.
*
* @param {Array} custom_fields - An array of custom field objects.
* @param {string} field_name - The name of the field to retrieve the value for.
* @return {string} - The value of the custom field, joined by commas if it contains multiple values.
*/
function get_ticket_custom_field_value(custom_fields, field_name) {
return custom_fields.find((obj) => {
return obj.name === field_name;
}).values;
}
/**
* Retrieves a list of transactions of a specific type from the ticket transactions history data.
*
* @param {Array} ticket_transactions_history_data - The array of ticket transactions history data.
* @param {string} transaction_type - The type of transaction to filter for.
* @return {string} - A JSON string representation of the filtered transactions.
*/
async function get_ticket_transactions_history_data_by_type(
ticket_transactions_history_data,
transaction_type
) {
let transactions = ticket_transactions_history_data.filter((obj) => {
return obj.Type === transaction_type;
});
let return_transactions = [];
for (let i = 0; i < transactions.length; i++) {
const hyperlinks = transactions[i]._hyperlinks;
for (let j = 0; j < hyperlinks.length; j++) {
const hyperlink = hyperlinks[j];
if (hyperlink.ref !== "attachment") {
continue;
}
try {
const response = await axios.get(hyperlink._url, REQUEST_HEADERS);
if (is_content_type_text(response.data.Headers)) {
const obj = {
created: convert_date(response.data.Created),
creator: response.data.Creator.id,
content: atob(
response.data.Content.replace(/\n+/g, "").replace(/['"]+/g, "")
),
};
return_transactions.push(obj);
}
} catch (err) {
STREAM.write(
"[ERROR]: Attachment id: " + hyperlink.id + ": " + err + "\n"
);
}
}
}
return return_transactions;
}
function array_to_string(array) {
array = array.map((member) => {
let decoded = he.decode(JSON.stringify(member));
return (
'"' +
decoded.replace(/['"]+/g, "")
.replace(/\\n/g, "\n")
.replace(/\\r/g, "\r")
.replace(/\\t/g, "\t") +
'"'
);
});
return array.join(", ");
}
/**
* Check if the given response headers indicate a text content type.
*
* @param {string} response_headers - The response headers to check.
* @return {boolean} Returns true if the response headers indicate a text content type, false otherwise.
*/
function is_content_type_text(response_headers) {
return (
response_headers.toLowerCase().includes("content-type: text/html") ||
response_headers.toLowerCase().includes("content-type: text/plain")
);
}
/**
* Retrieves the column headings for a data table.
*
* @return {Array} Array of column headings
*/
function get_column_headings() {
return [
"Imported Ticket ID",
"External ID",
"Contact ID",
"Closed On (Date)",
"Last Action Date",
"Outcome",
"Support Queue",
"System",
"Component",
"Related To",
"Security Incident",
"Status",
"Title",
"Severity",
"Type",
"Assigned User",
"Customer Code HD",
"Description",
];
}
/**
* Retrieves the severity mapping values.
*
* @return {Array} Array of severity mapping values
*/
function get_severity_mapping_value(rt_sla) {
let mapping = {
'Minor':'P3. Minor',
'Moderate': 'P2. Major',
'Severe': 'P1. Critical',
};
return rt_sla ? mapping[rt_sla] : 'P3. Minor';
}
/**
* Returns the value from the mapping corresponding to the given user.
*
* @param {string} rt_user - The user for whom the mapping value is to be retrieved
* @return {string|number} The value from the mapping for the given user
*/
function get_user_mapping_value(rt_user) {
let mapping = {
'alexander': 'Alexander',
'andrew': 'Andrew',
'aude': 'Aude',
'bernard': 'Bernard',
'david': 'David',
'fiona': 'Fiona',
'helen': 'Helen',
'jacob': 'Jacob',
'jake': 'Jake',
'janet': 'Janet',
'jonathan': 'Jonathan',
'lucy': 'Lucy',
'martin': 'Martin',
'matt': 'Matt',
'nason': 'Nason',
'Nobody': 1,
'pedro': 'Pedro',
'rachel': 'Rachel',
'rasa': 'Rasa',
'ryan': 'Ryan',
'sam': 'Sam',
'steven': 'Steven',
'val': 'Val',
};
return mapping[rt_user];
}