Last active
March 22, 2020 06:15
-
-
Save Alexzanderk/0fd9af207a4137fe6af664963b211ca5 to your computer and use it in GitHub Desktop.
get metric
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}, {}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment