Add top packages to stats summary script
This commit is contained in:
parent
3ea2ac210e
commit
7742b9bd02
|
@ -72,6 +72,7 @@
|
||||||
"promise": "7.1.1",
|
"promise": "7.1.1",
|
||||||
"react-dev-utils": "^0.5.2",
|
"react-dev-utils": "^0.5.2",
|
||||||
"style-loader": "0.13.1",
|
"style-loader": "0.13.1",
|
||||||
|
"text-table": "^0.2.0",
|
||||||
"url-loader": "0.5.7",
|
"url-loader": "0.5.7",
|
||||||
"webpack": "1.14.0",
|
"webpack": "1.14.0",
|
||||||
"webpack-dev-server": "1.16.2",
|
"webpack-dev-server": "1.16.2",
|
||||||
|
|
|
@ -2,6 +2,7 @@ const redis = require('redis')
|
||||||
const subDays = require('date-fns/sub_days')
|
const subDays = require('date-fns/sub_days')
|
||||||
const prettyBytes = require('pretty-bytes')
|
const prettyBytes = require('pretty-bytes')
|
||||||
const invariant = require('invariant')
|
const invariant = require('invariant')
|
||||||
|
const table = require('text-table')
|
||||||
|
|
||||||
const RedisURL = process.env.REDIS_URL
|
const RedisURL = process.env.REDIS_URL
|
||||||
|
|
||||||
|
@ -10,6 +11,9 @@ invariant(
|
||||||
'Missing the $REDIS_URL environment variable'
|
'Missing the $REDIS_URL environment variable'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const sumValues = (array) =>
|
||||||
|
array.reduce((memo, n) => memo + (parseInt(n, 10) || 0), 0)
|
||||||
|
|
||||||
const db = redis.createClient(RedisURL)
|
const db = redis.createClient(RedisURL)
|
||||||
|
|
||||||
const getKeyValues = (keys) =>
|
const getKeyValues = (keys) =>
|
||||||
|
@ -23,7 +27,39 @@ const getKeyValues = (keys) =>
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
const sumKeys = (keys) => getKeyValues(keys).then(sumValues)
|
const sumKeys = (keys) =>
|
||||||
|
getKeyValues(keys).then(sumValues)
|
||||||
|
|
||||||
|
const createScoresMap = (array) => {
|
||||||
|
const map = {}
|
||||||
|
|
||||||
|
for (let i = 0; i < array.length; i += 2)
|
||||||
|
map[array[i]] = parseInt(array[i + 1], 10)
|
||||||
|
|
||||||
|
return map
|
||||||
|
}
|
||||||
|
|
||||||
|
const getTopScores = (key, n = 10) =>
|
||||||
|
new Promise((resolve, reject) => {
|
||||||
|
db.zrevrange(key, 0, n, 'withscores', (error, value) => {
|
||||||
|
if (error) {
|
||||||
|
reject(error)
|
||||||
|
} else {
|
||||||
|
resolve(createScoresMap(value))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
const sumTopScores = (keys, n) =>
|
||||||
|
Promise.all(keys.map(key => getTopScores(key, n))).then(values => {
|
||||||
|
return values.reduce((memo, map) => {
|
||||||
|
Object.keys(map).forEach(key => {
|
||||||
|
memo[key] = (memo[key] || 0) + map[key]
|
||||||
|
})
|
||||||
|
|
||||||
|
return memo
|
||||||
|
}, {})
|
||||||
|
})
|
||||||
|
|
||||||
const createRange = (start, end) => {
|
const createRange = (start, end) => {
|
||||||
const range = []
|
const range = []
|
||||||
|
@ -40,9 +76,6 @@ const createDayKey = (date) =>
|
||||||
const createHourKey = (date) =>
|
const createHourKey = (date) =>
|
||||||
`${createDayKey(date)}-${date.getUTCHours()}`
|
`${createDayKey(date)}-${date.getUTCHours()}`
|
||||||
|
|
||||||
const sumValues = (array) =>
|
|
||||||
array.reduce((memo, n) => memo + (parseInt(n, 10) || 0), 0)
|
|
||||||
|
|
||||||
const now = new Date
|
const now = new Date
|
||||||
|
|
||||||
const createPastDays = (n) =>
|
const createPastDays = (n) =>
|
||||||
|
@ -51,38 +84,46 @@ const createPastDays = (n) =>
|
||||||
const pastSevenDays = createPastDays(7)
|
const pastSevenDays = createPastDays(7)
|
||||||
const pastThirtyDays = createPastDays(30)
|
const pastThirtyDays = createPastDays(30)
|
||||||
|
|
||||||
sumKeys(
|
Promise.all([
|
||||||
pastSevenDays.map(date => `stats-requests-${createHourKey(date)}`)
|
sumKeys(pastSevenDays.map(date => `stats-requests-${createDayKey(date)}`)),
|
||||||
).then(total => {
|
sumKeys(pastSevenDays.map(date => `stats-bandwidth-${createDayKey(date)}`)),
|
||||||
|
sumKeys(pastThirtyDays.map(date => `stats-requests-${createDayKey(date)}`)),
|
||||||
|
sumKeys(pastThirtyDays.map(date => `stats-bandwidth-${createDayKey(date)}`))
|
||||||
|
]).then(results => {
|
||||||
|
console.log('\n## Summary')
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
'Requests this week: %s',
|
'Requests this week: %s',
|
||||||
total.toLocaleString()
|
results[0].toLocaleString()
|
||||||
)
|
)
|
||||||
})
|
|
||||||
|
|
||||||
sumKeys(
|
|
||||||
pastSevenDays.map(date => `stats-bandwidth-${createHourKey(date)}`)
|
|
||||||
).then(total => {
|
|
||||||
console.log(
|
console.log(
|
||||||
'Bandwidth this week: %s',
|
'Bandwidth this week: %s',
|
||||||
prettyBytes(total)
|
prettyBytes(results[1])
|
||||||
)
|
)
|
||||||
})
|
|
||||||
|
|
||||||
sumKeys(
|
|
||||||
pastThirtyDays.map(date => `stats-requests-${createDayKey(date)}`)
|
|
||||||
).then(total => {
|
|
||||||
console.log(
|
console.log(
|
||||||
'Requests this month: %s',
|
'Requests this month: %s',
|
||||||
total.toLocaleString()
|
results[2].toLocaleString()
|
||||||
)
|
)
|
||||||
})
|
|
||||||
|
|
||||||
sumKeys(
|
|
||||||
pastThirtyDays.map(date => `stats-bandwidth-${createDayKey(date)}`)
|
|
||||||
).then(total => {
|
|
||||||
console.log(
|
console.log(
|
||||||
'Bandwidth this month: %s',
|
'Bandwidth this month: %s',
|
||||||
prettyBytes(total)
|
prettyBytes(results[3])
|
||||||
)
|
)
|
||||||
|
|
||||||
|
sumTopScores(pastSevenDays.map(date => `stats-packageRequests-${createDayKey(date)}`)).then(results => {
|
||||||
|
console.log('\n## Top Packages This Week')
|
||||||
|
|
||||||
|
const topPackages = Object.keys(results).sort((a, b) => results[b] - results[a])
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
table(topPackages.map(packageName => [
|
||||||
|
packageName,
|
||||||
|
results[packageName].toLocaleString()
|
||||||
|
]))
|
||||||
|
)
|
||||||
|
|
||||||
|
process.exit()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -5140,7 +5140,7 @@ test-exclude@^3.3.0:
|
||||||
read-pkg-up "^1.0.1"
|
read-pkg-up "^1.0.1"
|
||||||
require-main-filename "^1.0.1"
|
require-main-filename "^1.0.1"
|
||||||
|
|
||||||
text-table@~0.2.0:
|
text-table@^0.2.0, text-table@~0.2.0:
|
||||||
version "0.2.0"
|
version "0.2.0"
|
||||||
resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4"
|
resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue