Skip to content

Commit df2aaab

Browse files
jrainvilleiurimatias
authored andcommitted
refactor(@embark/transaction-logger): change log storage and read
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.
1 parent d0d584a commit df2aaab

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

packages/cockpit/ui/src/actions/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export const processLogs = {
173173
export const CONTRACT_LOGS = createRequestTypes('CONTRACT_LOGS');
174174
export const contractLogs = {
175175
request: () => action(CONTRACT_LOGS[REQUEST]),
176-
success: (contractLogs) => action(CONTRACT_LOGS[SUCCESS], {contractLogs}),
176+
success: (contractLogs) => action(CONTRACT_LOGS[SUCCESS], {contractLogs: contractLogs ? contractLogs.reverse() : []}),
177177
failure: (error) => action(CONTRACT_LOGS[FAILURE], {error, name: 'contractLogs'})
178178
};
179179

packages/plugins/transaction-logger/src/index.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ export default class TransactionLogger {
6363
this.writeLogFile = async.cargo((tasks, callback) => {
6464
let appendThis = '';
6565
tasks.forEach(task => {
66-
appendThis += `"${new Date().getTime()}":${JSON.stringify(task, getCircularReplacer())},\n`;
66+
// Write each line to a JSON string. The replacer is to avoid circular dependencies
67+
// Add a comma at the end to be able to make an array off of it when reading
68+
appendThis += `${JSON.stringify(task, getCircularReplacer())},\n`;
6769
});
6870
this.fs.appendFile(this.logFile, appendThis, (err) => {
6971
if (err) {
@@ -250,8 +252,7 @@ export default class TransactionLogger {
250252
apiRoute,
251253
(ws, _req) => {
252254
this.events.on('contracts:log', (log) => {
253-
ws.send(JSON.stringify(log), () => {
254-
});
255+
ws.send(JSON.stringify(log), () => {});
255256
});
256257
}
257258
);
@@ -260,39 +261,37 @@ export default class TransactionLogger {
260261
'get',
261262
apiRoute,
262263
async (req, res) => {
263-
res.send(JSON.stringify(await this._getLogs()));
264+
res.send(await this._readLogs(true));
264265
}
265266
);
266267
}
267268

268-
async _getLogs() {
269-
const data = await this._readLogs();
270-
return Object.values(data).reverse();
271-
}
272-
273269
_saveLog(log) {
274270
this.writeLogFile.push(log);
275271
}
276272

277-
async _readLogs() {
273+
async _readLogs(asString = false) {
278274
try {
279275
await this.fs.ensureFile(this.logFile);
280276
let data = await this.fs.readFile(this.logFile);
281277

282278
data = data.toString();
283279

284280
if (!data) {
285-
return {};
281+
return asString ? '[]' : [];
286282
}
287283

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

291290
return JSON.parse(data);
292291
} catch (error) {
293292
this.logger.error('Error reading contract log file', error.message);
294293
this.logger.trace(error.trace);
295-
return {};
294+
return asString ? '[]' : [];
296295
}
297296
}
298297
}

0 commit comments

Comments
 (0)