Use function instead of =>
This commit is contained in:
@ -33,11 +33,12 @@ const oneMinuteSeconds = 60
|
|||||||
const oneHourSeconds = oneMinuteSeconds * 60
|
const oneHourSeconds = oneMinuteSeconds * 60
|
||||||
const oneDaySeconds = oneHourSeconds * 24
|
const oneDaySeconds = oneHourSeconds * 24
|
||||||
|
|
||||||
const getSeconds = (date) =>
|
function getSeconds(date) {
|
||||||
Math.floor(date.getTime() / 1000)
|
return Math.floor(date.getTime() / 1000)
|
||||||
|
}
|
||||||
|
|
||||||
const reduceResults = (memo, results) => {
|
function reduceResults(memo, results) {
|
||||||
Object.keys(results).forEach(key => {
|
Object.keys(results).forEach(function (key) {
|
||||||
const value = results[key]
|
const value = results[key]
|
||||||
|
|
||||||
if (typeof value === 'object' && value) {
|
if (typeof value === 'object' && value) {
|
||||||
@ -51,8 +52,10 @@ const reduceResults = (memo, results) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function ingestStatsForZones(zones, since, processDashboard) {
|
function ingestStatsForZones(zones, since, processDashboard) {
|
||||||
return new Promise(resolve => {
|
return new Promise(function (resolve) {
|
||||||
const zoneNames = zones.map(zone => zone.name).join(', ')
|
const zoneNames = zones.map(function (zone) {
|
||||||
|
return zone.name
|
||||||
|
}).join(', ')
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
'info: Started ingesting stats for zones %s since %d',
|
'info: Started ingesting stats for zones %s since %d',
|
||||||
@ -64,9 +67,10 @@ function ingestStatsForZones(zones, since, processDashboard) {
|
|||||||
|
|
||||||
resolve(
|
resolve(
|
||||||
Promise.all(
|
Promise.all(
|
||||||
zones.map(zone => getZoneAnalyticsDashboard(zone.id, since))
|
zones.map(function (zone) {
|
||||||
).then(
|
return getZoneAnalyticsDashboard(zone.id, since)
|
||||||
results => {
|
})
|
||||||
|
).then(function (results) {
|
||||||
const endFetchTime = Date.now()
|
const endFetchTime = Date.now()
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
@ -82,33 +86,30 @@ function ingestStatsForZones(zones, since, processDashboard) {
|
|||||||
results = results.filter(Boolean)
|
results = results.filter(Boolean)
|
||||||
|
|
||||||
return results.length ? results.reduce(reduceResults) : null
|
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(() => {
|
console.log(
|
||||||
const endProcessTime = Date.now()
|
'info: Processed zone analytics dashboards for %s since %d in %dms',
|
||||||
|
zoneNames,
|
||||||
console.log(
|
since,
|
||||||
'info: Processed zone analytics dashboards for %s since %d in %dms',
|
endProcessTime - startProcessTime
|
||||||
zoneNames,
|
)
|
||||||
since,
|
})
|
||||||
endProcessTime - startProcessTime
|
})
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -122,7 +123,7 @@ function processPerDayDashboard(dashboard) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function processPerDayTimeseries(ts) {
|
function processPerDayTimeseries(ts) {
|
||||||
return new Promise(resolve => {
|
return new Promise(function (resolve) {
|
||||||
const since = new Date(ts.since)
|
const since = new Date(ts.since)
|
||||||
const until = new Date(ts.until)
|
const until = new Date(ts.until)
|
||||||
|
|
||||||
@ -157,7 +158,7 @@ function processPerDayTimeseries(ts) {
|
|||||||
db.expireat(`stats-bandwidthFromCache-${dayKey}`, oneYearLater)
|
db.expireat(`stats-bandwidthFromCache-${dayKey}`, oneYearLater)
|
||||||
|
|
||||||
const httpStatus = ts.requests.http_status
|
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
|
return parseInt(status, 10) >= 500 ? memo + httpStatus[status] : memo
|
||||||
}, 0)
|
}, 0)
|
||||||
|
|
||||||
@ -168,7 +169,7 @@ function processPerDayTimeseries(ts) {
|
|||||||
const requestsByCountry = []
|
const requestsByCountry = []
|
||||||
const bandwidthByCountry = []
|
const bandwidthByCountry = []
|
||||||
|
|
||||||
Object.keys(ts.requests.country).forEach(country => {
|
Object.keys(ts.requests.country).forEach(function (country) {
|
||||||
const requests = ts.requests.country[country]
|
const requests = ts.requests.country[country]
|
||||||
const bandwidth = ts.bandwidth.country[country]
|
const bandwidth = ts.bandwidth.country[country]
|
||||||
|
|
||||||
@ -204,7 +205,7 @@ function processPerHourDashboard(dashboard) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function processPerHourTimeseries(ts) {
|
function processPerHourTimeseries(ts) {
|
||||||
return new Promise(resolve => {
|
return new Promise(function (resolve) {
|
||||||
const since = new Date(ts.since)
|
const since = new Date(ts.since)
|
||||||
const until = new Date(ts.until)
|
const until = new Date(ts.until)
|
||||||
|
|
||||||
@ -245,7 +246,7 @@ function processPerMinuteDashboard(dashboard) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function processPerMinuteTimeseries(ts) {
|
function processPerMinuteTimeseries(ts) {
|
||||||
return new Promise(resolve => {
|
return new Promise(function (resolve) {
|
||||||
const since = new Date(ts.since)
|
const since = new Date(ts.since)
|
||||||
const until = new Date(ts.until)
|
const until = new Date(ts.until)
|
||||||
|
|
||||||
@ -278,14 +279,17 @@ function processPerMinuteTimeseries(ts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function startZones(zones) {
|
function startZones(zones) {
|
||||||
const takePerMinuteTurn = () =>
|
function takePerMinuteTurn() {
|
||||||
ingestPerMinuteStats(zones)
|
return ingestPerMinuteStats(zones)
|
||||||
|
}
|
||||||
|
|
||||||
const takePerHourTurn = () =>
|
function takePerHourTurn() {
|
||||||
ingestPerHourStats(zones)
|
return ingestPerHourStats(zones)
|
||||||
|
}
|
||||||
|
|
||||||
const takePerDayTurn = () =>
|
function takePerDayTurn() {
|
||||||
ingestPerDayStats(zones)
|
return ingestPerDayStats(zones)
|
||||||
|
}
|
||||||
|
|
||||||
takePerMinuteTurn()
|
takePerMinuteTurn()
|
||||||
takePerHourTurn()
|
takePerHourTurn()
|
||||||
@ -296,7 +300,10 @@ function startZones(zones) {
|
|||||||
setInterval(takePerDayTurn, oneHour / 2)
|
setInterval(takePerDayTurn, oneHour / 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
Promise.all(DomainNames.map(getZones)).then(results => {
|
Promise.all(DomainNames.map(getZones)).then(function (results) {
|
||||||
const zones = results.reduce((memo, zones) => memo.concat(zones))
|
const zones = results.reduce(function (memo, zones) {
|
||||||
|
return memo.concat(zones)
|
||||||
|
})
|
||||||
|
|
||||||
startZones(zones)
|
startZones(zones)
|
||||||
})
|
})
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
const db = require('./RedisClient')
|
const db = require('./RedisClient')
|
||||||
|
|
||||||
const sumValues = (array) =>
|
function sumValues(array) {
|
||||||
array.reduce((memo, n) => memo + (parseInt(n, 10) || 0), 0)
|
return array.reduce(function (memo, n) {
|
||||||
|
return memo + (parseInt(n, 10) || 0)
|
||||||
|
}, 0)
|
||||||
|
}
|
||||||
|
|
||||||
const getKeyValues = (keys) =>
|
function getKeyValues(keys) {
|
||||||
new Promise((resolve, reject) => {
|
return new Promise(function (resolve, reject) {
|
||||||
db.mget(keys, (error, values) => {
|
db.mget(keys, function (error, values) {
|
||||||
if (error) {
|
if (error) {
|
||||||
reject(error)
|
reject(error)
|
||||||
} else {
|
} else {
|
||||||
@ -13,11 +16,13 @@ const getKeyValues = (keys) =>
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const sumKeys = (keys) =>
|
function sumKeys(keys) {
|
||||||
getKeyValues(keys).then(sumValues)
|
return getKeyValues(keys).then(sumValues)
|
||||||
|
}
|
||||||
|
|
||||||
const createScoresMap = (array) => {
|
function createScoresMap(array) {
|
||||||
const map = {}
|
const map = {}
|
||||||
|
|
||||||
for (let i = 0; i < array.length; i += 2)
|
for (let i = 0; i < array.length; i += 2)
|
||||||
@ -26,9 +31,9 @@ const createScoresMap = (array) => {
|
|||||||
return map
|
return map
|
||||||
}
|
}
|
||||||
|
|
||||||
const getScoresMap = (key, n = 10) =>
|
function getScoresMap(key, n = 10) {
|
||||||
new Promise((resolve, reject) => {
|
return new Promise(function (resolve, reject) {
|
||||||
db.zrevrange(key, 0, n, 'withscores', (error, value) => {
|
db.zrevrange(key, 0, n, 'withscores', function (error, value) {
|
||||||
if (error) {
|
if (error) {
|
||||||
reject(error)
|
reject(error)
|
||||||
} else {
|
} else {
|
||||||
@ -36,39 +41,53 @@ const getScoresMap = (key, n = 10) =>
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const createTopScores = (map) =>
|
function createTopScores(map) {
|
||||||
Object.keys(map)
|
return Object.keys(map).reduce(function (memo, key) {
|
||||||
.reduce((memo, key) => memo.concat([ [ key, map[key] ] ]), [])
|
return memo.concat([ [ key, map[key] ] ])
|
||||||
.sort((a, b) => b[1] - a[1])
|
}, []).sort(function (a, b) {
|
||||||
|
return b[1] - a[1]
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const getTopScores = (key, n) =>
|
function getTopScores(key, n) {
|
||||||
getScoresMap(key, n).then(createTopScores)
|
return getScoresMap(key, n).then(createTopScores)
|
||||||
|
}
|
||||||
|
|
||||||
const sumMaps = (maps) =>
|
function sumMaps(maps) {
|
||||||
maps.reduce((memo, map) => {
|
return maps.reduce(function (memo, map) {
|
||||||
Object.keys(map).forEach(key => {
|
Object.keys(map).forEach(function (key) {
|
||||||
memo[key] = (memo[key] || 0) + map[key]
|
memo[key] = (memo[key] || 0) + map[key]
|
||||||
})
|
})
|
||||||
|
|
||||||
return memo
|
return memo
|
||||||
}, {})
|
}, {})
|
||||||
|
}
|
||||||
|
|
||||||
const sumTopScores = (keys, n) =>
|
function sumTopScores(keys, n) {
|
||||||
Promise.all(keys.map(key => getScoresMap(key, n)))
|
return Promise.all(
|
||||||
.then(sumMaps)
|
keys.map(function (key) {
|
||||||
.then(createTopScores)
|
return getScoresMap(key, n)
|
||||||
|
})
|
||||||
|
).then(sumMaps).then(createTopScores)
|
||||||
|
}
|
||||||
|
|
||||||
const createKey = (...args) => args.join('-')
|
function createKey(...args) {
|
||||||
|
return args.join('-')
|
||||||
|
}
|
||||||
|
|
||||||
const createDayKey = (date) =>
|
function createDayKey(date) {
|
||||||
createKey(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate())
|
return createKey(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate())
|
||||||
|
}
|
||||||
|
|
||||||
const createHourKey = (date) =>
|
function createHourKey(date) {
|
||||||
createKey(createDayKey(date), date.getUTCHours())
|
return createKey(createDayKey(date), date.getUTCHours())
|
||||||
|
}
|
||||||
|
|
||||||
const createMinuteKey = (date) =>
|
function createMinuteKey(date) {
|
||||||
createKey(createHourKey(date), date.getUTCMinutes())
|
return createKey(createHourKey(date), date.getUTCMinutes())
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getKeyValues,
|
getKeyValues,
|
||||||
|
Reference in New Issue
Block a user