Skip to content

Instantly share code, notes, and snippets.

@Robgd
Last active May 29, 2018 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Robgd/faf0cf6ef866c883f44f5762736e4973 to your computer and use it in GitHub Desktop.
Save Robgd/faf0cf6ef866c883f44f5762736e4973 to your computer and use it in GitHub Desktop.
'use strict';
const {promisify} = require('util');
const fs = require('fs');
const glob = require("glob");
const path = require('path');
const _ = require('lodash');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const readFileAsync = promisify(fs.readFile);
glob(path.join(__dirname, 'test/*.log'), (er, files) => {
// _.forEach(files, file => {
return readFileAsync(files[0], "utf8")
.then(data => {
let jsonString = data.replace(/\r?\n|\r/g, ',');
jsonString = jsonString.substr(0, jsonString.length - 1);
jsonString = `[${jsonString}]`;
const json = JSON.parse(jsonString);
let output;
output = _.filter(json, {
message: 'Send SMS message sucessfully'
});
output = output.reduce((acc, log) => {
acc.push({
dateTime: log['@timestamp'].substr(0, 10),
source: log.ctx_message_source,
countryId: log.ctx_country_code
});
return acc;
}, []);
output = _.groupBy(output, 'source');
const csvWriter = createCsvWriter({
path: path.join(__dirname, `/stats/${files[0]}`),
header: ['DateTime', 'Count', 'Sources', 'CountryId']
});
_.forEach(output, (stat, key) => {
const country = _.groupBy(stat, 'dateTime');
console.log(country);
//console.log('STAT', stat, key);
});
})
.catch(err => {
throw err;
console.log(err);
});
// })
});
// glob(path.join(__dirname, 'logs/*.log'), (er, files) => {
// let stats = [];
// _.forEach(files, file => {
// const data = fs.readFileSync(file, "utf8");
//
// let jsonString = data.replace(/\r?\n|\r/g, ',');
// jsonString = jsonString.substr(0, jsonString.length - 1);
// jsonString = `[${jsonString}]`;
// const json = JSON.parse(jsonString);
//
// let output;
// output = _.filter(json, {
// message: 'Send SMS message sucessfully'
// });
//
// output = output.reduce((acc, log) => {
// acc.push({
// dateTime: log['@timestamp'],
// source: log.ctx_message_source,
// countryId: log.ctx_country_code
// });
//
// return acc;
// }, []);
//
// stats = _.concat(stats, output);
// });
//
// stats = _.groupBy(stats, 'source');
// //console.log('STATS', stats);
//
// _.forEach(stats, (stat, key) => {
// console.log('STAT', stat, key);
// })
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment