Skip to content

Commit

Permalink
refactor(@embark/transaction-logger): change log storage and read
Browse files Browse the repository at this point in the history
Changes the way the logs are stored to straight up be logged as an
array and then reads it as such. It also removes the reverse from
the read and puts it in the UI instead since it's the UI that needs
it reversed.
  • Loading branch information
jrainville committed Jan 20, 2020
1 parent e0ac539 commit 1e78826
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/cockpit/ui/src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export const processLogs = {
export const CONTRACT_LOGS = createRequestTypes('CONTRACT_LOGS');
export const contractLogs = {
request: () => action(CONTRACT_LOGS[REQUEST]),
success: (contractLogs) => action(CONTRACT_LOGS[SUCCESS], {contractLogs}),
success: (contractLogs) => action(CONTRACT_LOGS[SUCCESS], {contractLogs: contractLogs ? contractLogs.reverse() : []}),
failure: (error) => action(CONTRACT_LOGS[FAILURE], {error, name: 'contractLogs'})
};

Expand Down
23 changes: 11 additions & 12 deletions packages/plugins/transaction-logger/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ export default class TransactionLogger {
this.writeLogFile = async.cargo((tasks, callback) => {
let appendThis = '';
tasks.forEach(task => {
appendThis += `"${new Date().getTime()}":${JSON.stringify(task, getCircularReplacer())},\n`;
// Write each line to a JSON string. The replacer is to avoid circular dependencies
// Add a comma at the end to be able to make an array off of it when reading
appendThis += `${JSON.stringify(task, getCircularReplacer())},\n`;
});
this.fs.appendFile(this.logFile, appendThis, (err) => {
if (err) {
Expand Down Expand Up @@ -250,8 +252,7 @@ export default class TransactionLogger {
apiRoute,
(ws, _req) => {
this.events.on('contracts:log', (log) => {
ws.send(JSON.stringify(log), () => {
});
ws.send(JSON.stringify(log), () => {});
});
}
);
Expand All @@ -260,21 +261,16 @@ export default class TransactionLogger {
'get',
apiRoute,
async (req, res) => {
res.send(JSON.stringify(await this._getLogs()));
res.send(await this._readLogs(true));
}
);
}

async _getLogs() {
const data = await this._readLogs();
return Object.values(data).reverse();
}

_saveLog(log) {
this.writeLogFile.push(log);
}

async _readLogs() {
async _readLogs(asString = false) {
try {
await this.fs.ensureFile(this.logFile);
let data = await this.fs.readFile(this.logFile);
Expand All @@ -285,8 +281,11 @@ export default class TransactionLogger {
return {};
}

// remove last comma and add braces around
data = `{${data.substring(0, data.length - 2)}}`;
// remove last comma and add brackets around to make it an array of object logs
data = `[${data.substring(0, data.length - 2)}]`;
if (asString) {
return data;
}

return JSON.parse(data);
} catch (error) {
Expand Down

0 comments on commit 1e78826

Please sign in to comment.