Files
freeCodeCamp/utils/processingLog.js

56 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-11-12 12:28:02 -08:00
const path = require('path');
2018-11-06 23:04:43 -08:00
const fs = require('fs');
2018-11-13 02:31:51 +05:30
2018-11-20 15:31:28 -08:00
const { saveToFile } = require('./saveToFile');
2018-11-06 23:04:43 -08:00
2018-11-20 15:31:28 -08:00
class ProcessingLog {
2018-11-06 23:04:43 -08:00
constructor() {
2018-11-12 01:36:54 -08:00
this._start = null;
this._lastUpdate = null;
this._lastPRlogged = null;
this._finish = null;
this._prs = {};
2018-11-20 15:31:28 -08:00
this._logfile = path.resolve(__dirname, `../work-logs/${this.getRunType()}_open-prs-processed.json`);
2018-11-14 23:40:26 -08:00
}
getRunType() {
return process.env.PRODUCTION_RUN === 'true' ? 'production' : 'test';
2018-11-06 23:04:43 -08:00
}
import() {
return JSON.parse(fs.readFileSync(this._logfile, 'utf8'));
}
export() {
let sortedPRs = Object.keys(this._prs)
.sort((a, b) => a - b)
.map(num => ({ [num]: this._prs[num] }));
const log = {
start: this._start,
finish: this._finish,
prs: sortedPRs
};
saveToFile(this._logfile, JSON.stringify(log))
}
2018-11-11 23:52:00 -08:00
add(prNum, prop) {
this._prs[prNum] = {};
this._prs[prNum][prop] = null;
2018-11-06 23:04:43 -08:00
}
2018-11-11 23:52:00 -08:00
update(prNum, prop, value) {
this._prs[prNum][prop] = value;
2018-11-06 23:04:43 -08:00
}
start() {
this._start = new Date();
}
finish() {
this._finish = new Date();
this.export();
}
};
2018-11-20 15:31:28 -08:00
module.exports = { ProcessingLog };