Skip to content

Commit

Permalink
Fix error handling and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ammopt committed Jan 16, 2024
1 parent ddba5ed commit 1c8b7e5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules
error.txt
error.log
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@ Fetch data from [RT](https://github.com/bestpractical/rt) using [REST 2](https:/

![demo](https://github.com/PTFS-Europe/rt-data-parser/blob/master/tickets.jpg?raw=true)

# Installation
## Installation
```
yarn install
```

# CLI Usage
## CLI Usage
```
node rt_data_parser.js -u ${RT_USER} -p ${RT_PASS} -h ${RT_HOST} -i ${TICKET_ID} -n ${NUMBER} > data.csv
```

## Example
### Example
Parse ticket #49504 and 20 tickets below that, down to ticket #49484
```
node rt_data_parser.js -u user -p pass -h https://helpdesk.yoursite.com -i 49504 -n 20 > data.csv
```

## Error logging
Errors with ticket/transaction information are logged to ./error.log

## Tests
```
npm test
Expand Down
32 changes: 15 additions & 17 deletions rt_data_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import colors from "ansi-colors";
import { Command } from "commander";
import he from "html-entities";
import fs from "fs";
import { strip_html_block } from"./lib/functions.js";
import { strip_html_block } from "./lib/functions.js";

const commander = new Command();
/** Setup CLI args */
Expand Down Expand Up @@ -53,8 +53,7 @@ const REQUEST_HEADERS = {
const TOP_TICKET_ID = CLI_PARAMS.ticketId;
const HOW_MANY_TICKETS = CLI_PARAMS.numbers;
const RT_API_URL = `${CLI_PARAMS.host}/REST/2.0`;
const STREAM = fs.createWriteStream("error.txt", { flags: "a" });
STREAM.write("hi: \n");
const STREAM = fs.createWriteStream("error.log", { flags: "a" });

/** Main */
get_tickets_data(TOP_TICKET_ID, HOW_MANY_TICKETS);
Expand Down Expand Up @@ -95,8 +94,8 @@ async function parse_ticket(ticket_id) {
ticket_transactions_history_data
);
return ticket_obj;
} catch (error) {
// console.log(error);
} catch (err) {
STREAM.write("Ticket id: " + ticket_id + ": " + err + "\n");
}
}

Expand All @@ -118,23 +117,21 @@ async function get_tickets_data(TOP_TICKET_ID, HOW_MANY_TICKETS) {
"id,all_other_correspondence,any_comment,closed,created,customer,customer_group,first_correspondence,last_correspondence,outcome,owner,queue,security_incident,status,subject,tickettype"
);


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 {
console.log(Object.values(res).toString());
} catch (error) {
// console.log(error);
STREAM.write("Ticket id: "+id + ": " + error + "\n");
} catch (err) {
STREAM.write("Ticket id: " + id + ": " + err + "\n");
}
});
promises.push(promise);
}
STREAM.end();
Promise.all(promises).then(() => {
STREAM.end();
MULTIBAR.stop();
});
}
Expand Down Expand Up @@ -191,9 +188,10 @@ async function get_ticket_transactions_history_data(ticket_id) {
push_transaction(response);
});
} catch (err) {
// console.log(err);
STREAM.write("Transaction id: "+ticket_history.items[i].id + ": " + error + "\n");
update_bar();
STREAM.write(
"Transaction id: " + ticket_history.items[i].id + ": " + err + "\n"
);
}
}

Expand All @@ -209,9 +207,10 @@ async function get_ticket_transactions_history_data(ticket_id) {
}
);
} catch (err) {
// console.log(err);
STREAM.write("Transaction id: "+ticket_history.items[i].id + ": " + error + "\n");
update_bar();
STREAM.write(
"Transaction id: " + ticket_history.items[i].id + ": " + err + "\n"
);
}
}
}
Expand Down Expand Up @@ -380,9 +379,8 @@ async function get_ticket_transactions_history_data_by_type(
};
return_transactions.push(obj);
}
} catch (error) {
// console.log(err);
STREAM.write("Attachment id: " + hyperlinks[j].id + ": " + error + "\n" );
} catch (err) {
STREAM.write("Attachment id: " + hyperlink.id + ": " + err + "\n");
}
}
}
Expand Down

0 comments on commit 1c8b7e5

Please sign in to comment.