0.0.7 • Published 1 year ago
alertacoches-logger v0.0.7
Using Logger
First install Logger
from npm using npm i alertacoches-logger
.
Then import Logger
into importer.js
:
const { Logger } = require('alertacoches-logger');
Extract the Logs
model from mongoose.models
.
const { Search: searchModel, Logs: logModel } = mongoose.models;
Be sure to pass the logModel
to new Logger
as the second argument.
Create a log object in the construtor as seen below, then add the start
, setTotalExpectedSearches
and end
methods to the import
method:
class Importer {
constructor(sourceId) {
this.log = new Logger(sourceId, logModel);
}
async import() {
const searches = await this.getSearches(this.sourceId);
let numOfActualSearches = 0;
let totalAdsFound = 0;
const total = searches.length;
this.log.setTotalExpectedSearches(total);
for (const search of searches) {
this.logProgress(
`Getting ad ${numOfActualSearches} of ${total}\r`,
Boolean(process.env.DEBUG),
);
try {
const source = await this.getSource(this.sourceId, {
make: search.make,
model: search.model,
fueltype: search.fuel,
});
numOfActualSearches += 1;
if (source) {
let adsFound = 0;
const ads = await this.getAdsFromSource(source.source_ep);
for (const item of ads) {
adsFound += 1;
const ad = this.parseToAd(
item,
search.make,
search.model,
search.fuel,
);
this.saveAd(ad);
}
this.logProgress(`IMPORT ADS OK (${adsFound})`);
await this.updateSearch(search._id);
totalAdsFound += adsFound;
}
} catch (err) {
console.error('IMPORT ADS KO: ' + err);
continue;
}
}
this.log.end(numOfActualSearches, totalAdsFound);
this.logProgress('Import FINISHED');
}
}
The model
Be sure to create a model in src/mongo/models
like this one in your worker:
class Logs {
constructor({ mongoose }) {
const Schema = new mongoose.Schema({
workerName: String,
sourceId: Number,
numberAdsSaved: Number,
numberAdsFound: Number,
startTimestamp: { type: Date, default: new Date() },
endTimestamp: { type: Date, default: new Date() },
totalExecutionTime: Number,
totalNumOfExpectedSearches: Number,
totalNumOfActualSearches: Number,
});
this.model = mongoose.model('Logs', Schema);
this.name = 'Logs';
}
}
module.exports = Logs;