Use function instead of =>

This commit is contained in:
MICHAEL JACKSON 2017-08-16 11:58:01 -07:00
parent f542cdafcd
commit 65c2aa7357
2 changed files with 102 additions and 76 deletions

View File

@ -33,11 +33,12 @@ const oneMinuteSeconds = 60
const oneHourSeconds = oneMinuteSeconds * 60
const oneDaySeconds = oneHourSeconds * 24
const getSeconds = (date) =>
Math.floor(date.getTime() / 1000)
function getSeconds(date) {
return Math.floor(date.getTime() / 1000)
}
const reduceResults = (memo, results) => {
Object.keys(results).forEach(key => {
function reduceResults(memo, results) {
Object.keys(results).forEach(function (key) {
const value = results[key]
if (typeof value === 'object' && value) {
@ -51,8 +52,10 @@ const reduceResults = (memo, results) => {
}
function ingestStatsForZones(zones, since, processDashboard) {
return new Promise(resolve => {
const zoneNames = zones.map(zone => zone.name).join(', ')
return new Promise(function (resolve) {
const zoneNames = zones.map(function (zone) {
return zone.name
}).join(', ')
console.log(
'info: Started ingesting stats for zones %s since %d',
@ -64,9 +67,10 @@ function ingestStatsForZones(zones, since, processDashboard) {
resolve(
Promise.all(
zones.map(zone => getZoneAnalyticsDashboard(zone.id, since))
).then(
results => {
zones.map(function (zone) {
return getZoneAnalyticsDashboard(zone.id, since)
})
).then(function (results) {
const endFetchTime = Date.now()
console.log(
@ -82,33 +86,30 @@ function ingestStatsForZones(zones, since, processDashboard) {
results = results.filter(Boolean)
return results.length ? results.reduce(reduceResults) : null
}).then(function (dashboard) {
if (dashboard == null) {
console.warn(
'warning: Missing dashboards for %s since %d',
zoneNames,
since
)
return
}
).then(
dashboard => {
if (dashboard == null) {
console.warn(
'warning: Missing dashboards for %s since %d',
zoneNames,
since
)
return
}
const startProcessTime = Date.now()
const startProcessTime = Date.now()
return processDashboard(dashboard).then(function () {
const endProcessTime = Date.now()
return processDashboard(dashboard).then(() => {
const endProcessTime = Date.now()
console.log(
'info: Processed zone analytics dashboards for %s since %d in %dms',
zoneNames,
since,
endProcessTime - startProcessTime
)
})
}
)
console.log(
'info: Processed zone analytics dashboards for %s since %d in %dms',
zoneNames,
since,
endProcessTime - startProcessTime
)
})
})
)
})
}
@ -122,7 +123,7 @@ function processPerDayDashboard(dashboard) {
}
function processPerDayTimeseries(ts) {
return new Promise(resolve => {
return new Promise(function (resolve) {
const since = new Date(ts.since)
const until = new Date(ts.until)
@ -157,7 +158,7 @@ function processPerDayTimeseries(ts) {
db.expireat(`stats-bandwidthFromCache-${dayKey}`, oneYearLater)
const httpStatus = ts.requests.http_status
const errors = Object.keys(httpStatus).reduce((memo, status) => {
const errors = Object.keys(httpStatus).reduce(function (memo, status) {
return parseInt(status, 10) >= 500 ? memo + httpStatus[status] : memo
}, 0)
@ -168,7 +169,7 @@ function processPerDayTimeseries(ts) {
const requestsByCountry = []
const bandwidthByCountry = []
Object.keys(ts.requests.country).forEach(country => {
Object.keys(ts.requests.country).forEach(function (country) {
const requests = ts.requests.country[country]
const bandwidth = ts.bandwidth.country[country]
@ -204,7 +205,7 @@ function processPerHourDashboard(dashboard) {
}
function processPerHourTimeseries(ts) {
return new Promise(resolve => {
return new Promise(function (resolve) {
const since = new Date(ts.since)
const until = new Date(ts.until)
@ -245,7 +246,7 @@ function processPerMinuteDashboard(dashboard) {
}
function processPerMinuteTimeseries(ts) {
return new Promise(resolve => {
return new Promise(function (resolve) {
const since = new Date(ts.since)
const until = new Date(ts.until)
@ -278,14 +279,17 @@ function processPerMinuteTimeseries(ts) {
}
function startZones(zones) {
const takePerMinuteTurn = () =>
ingestPerMinuteStats(zones)
function takePerMinuteTurn() {
return ingestPerMinuteStats(zones)
}
const takePerHourTurn = () =>
ingestPerHourStats(zones)
function takePerHourTurn() {
return ingestPerHourStats(zones)
}
const takePerDayTurn = () =>
ingestPerDayStats(zones)
function takePerDayTurn() {
return ingestPerDayStats(zones)
}
takePerMinuteTurn()
takePerHourTurn()
@ -296,7 +300,10 @@ function startZones(zones) {
setInterval(takePerDayTurn, oneHour / 2)
}
Promise.all(DomainNames.map(getZones)).then(results => {
const zones = results.reduce((memo, zones) => memo.concat(zones))
Promise.all(DomainNames.map(getZones)).then(function (results) {
const zones = results.reduce(function (memo, zones) {
return memo.concat(zones)
})
startZones(zones)
})

View File

@ -1,11 +1,14 @@
const db = require('./RedisClient')
const sumValues = (array) =>
array.reduce((memo, n) => memo + (parseInt(n, 10) || 0), 0)
function sumValues(array) {
return array.reduce(function (memo, n) {
return memo + (parseInt(n, 10) || 0)
}, 0)
}
const getKeyValues = (keys) =>
new Promise((resolve, reject) => {
db.mget(keys, (error, values) => {
function getKeyValues(keys) {
return new Promise(function (resolve, reject) {
db.mget(keys, function (error, values) {
if (error) {
reject(error)
} else {
@ -13,11 +16,13 @@ const getKeyValues = (keys) =>
}
})
})
}
const sumKeys = (keys) =>
getKeyValues(keys).then(sumValues)
function sumKeys(keys) {
return getKeyValues(keys).then(sumValues)
}
const createScoresMap = (array) => {
function createScoresMap(array) {
const map = {}
for (let i = 0; i < array.length; i += 2)
@ -26,9 +31,9 @@ const createScoresMap = (array) => {
return map
}
const getScoresMap = (key, n = 10) =>
new Promise((resolve, reject) => {
db.zrevrange(key, 0, n, 'withscores', (error, value) => {
function getScoresMap(key, n = 10) {
return new Promise(function (resolve, reject) {
db.zrevrange(key, 0, n, 'withscores', function (error, value) {
if (error) {
reject(error)
} else {
@ -36,39 +41,53 @@ const getScoresMap = (key, n = 10) =>
}
})
})
}
const createTopScores = (map) =>
Object.keys(map)
.reduce((memo, key) => memo.concat([ [ key, map[key] ] ]), [])
.sort((a, b) => b[1] - a[1])
function createTopScores(map) {
return Object.keys(map).reduce(function (memo, key) {
return memo.concat([ [ key, map[key] ] ])
}, []).sort(function (a, b) {
return b[1] - a[1]
})
}
const getTopScores = (key, n) =>
getScoresMap(key, n).then(createTopScores)
function getTopScores(key, n) {
return getScoresMap(key, n).then(createTopScores)
}
const sumMaps = (maps) =>
maps.reduce((memo, map) => {
Object.keys(map).forEach(key => {
function sumMaps(maps) {
return maps.reduce(function (memo, map) {
Object.keys(map).forEach(function (key) {
memo[key] = (memo[key] || 0) + map[key]
})
return memo
}, {})
}
const sumTopScores = (keys, n) =>
Promise.all(keys.map(key => getScoresMap(key, n)))
.then(sumMaps)
.then(createTopScores)
function sumTopScores(keys, n) {
return Promise.all(
keys.map(function (key) {
return getScoresMap(key, n)
})
).then(sumMaps).then(createTopScores)
}
const createKey = (...args) => args.join('-')
function createKey(...args) {
return args.join('-')
}
const createDayKey = (date) =>
createKey(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate())
function createDayKey(date) {
return createKey(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate())
}
const createHourKey = (date) =>
createKey(createDayKey(date), date.getUTCHours())
function createHourKey(date) {
return createKey(createDayKey(date), date.getUTCHours())
}
const createMinuteKey = (date) =>
createKey(createHourKey(date), date.getUTCMinutes())
function createMinuteKey(date) {
return createKey(createHourKey(date), date.getUTCMinutes())
}
module.exports = {
getKeyValues,