Skip to content

Instantly share code, notes, and snippets.

@Alexzanderk
Last active March 22, 2020 06:16
Show Gist options
  • Select an option

  • Save Alexzanderk/01c7a07e7183b058bf5e5bd9e086579c to your computer and use it in GitHub Desktop.

Select an option

Save Alexzanderk/01c7a07e7183b058bf5e5bd9e086579c to your computer and use it in GitHub Desktop.
read dir files calculate, write into one file res
// Log incoming traffic
var metricStore = new Map();
/**
*
* @param {string} key
* @returns {void}
*/
const getCount = (key) => metricStore.has(key) ? metricStore.get(key) + 1 : 1;
const devices = {
1: 'MobileTablet',
2: 'PC',
3: 'CTV',
4: 'Phone',
5: 'Tablet',
6: 'ConnectedDevice',
7: 'SetTopBox'
};
/**
* @typedef {object} MetricControl
* @property {boolean} isWork
* @property {() => void} toogleWork
* @property {() => void} init
*/
/**
* @type {MetricControl}
*/
const metricControl = {
isWork: false,
isInit: false,
toogleWork() {
this.isWork = !this.isWork;
},
init() {
if (!this.isInit) {
try {
const workConf = JSON.parse(process.env.WORKER_CONFIG);
setTimeout(() => {
if (!this.isWork) {
this.toogleWork();
console.log(`Pid ${workConf.pid} metric work: ${this.isWork}`);
}
setTimeout(() => {
this.toogleWork();
console.log(`Pid ${workConf.pid} metric work: ${this.isWork}`);
const file =`/tmp/metrics/${workConf.pid}_METRICK.json`;
// metric.getMetricData(metric.metricStore);
files.writeFile(file, getMetricData(metricStore))
.catch(err => {
console.log(`Metric not write on pid: ${workConf.pid}`);
console.log(err);
});
}, 1 * 60 * 1000);
}, 30 * 60 * 1000);
this.isInit = true;
} catch(err) {
console.log(err);
}
}
}
};
/**
*
* @param {Bid} bid
* @returns {void}
*/
const getMetric = (bid) => {
const { device: { geo, devicetype }, app, site, imp } = bid;
const { video, native, banner } = imp[0];
const { country } = geo;
const platform = app && 'app' || site && 'site';
const adType = video && 'video' || native && 'native' || banner && 'banner';
const total = 'total';
const ContryPlatformAdformat = `${country}-${platform}-${adType}`;
const ContryPlatformDevicetypeAdformat = `${country}-${platform}-${devices[devicetype]}-${adType}`;
metricStore.set(country, getCount(country));
metricStore.set(devices[devicetype], getCount(devices[devicetype]));
metricStore.set(platform, getCount(platform));
metricStore.set(adType, getCount(adType));
metricStore.set(ContryPlatformAdformat, getCount(ContryPlatformAdformat));
metricStore.set(ContryPlatformDevicetypeAdformat, getCount(ContryPlatformDevicetypeAdformat));
metricStore.set(total, getCount(total));
};
/**
*
* @param {Map} store
*/
const getMetricData = (store) => {
const entriesArr = [...store.entries()];
return entriesArr.reduce((acc, [k,v]) => {
acc[k] = v;
return acc;
}, {});
};
const fs = require('fs');
let data = {};
const dir = `/tmp/metrics/`;
fs.readdir(dir, (err, files) => {
return new Promise((resolve, reject) => {
if (err) reject(err);
files.forEach(file => {
const content = require(`${dir}${file}`);
data = Object.keys(content).reduce((acc, key) => {
acc[key] = data[key] ? data[key] + content[key] : content[key];
return acc;
}, data);
});
resolve(data);
}).then(data => {
const csvArr = Object.entries(data).map(row => {
return row.map((val) => {
return val.toString();
});
});
const csv = `"${csvArr.join('"\n"').split(',').join('","')}"`;
fs.writeFileSync('/tmp/final.csv', csv);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment