From 7742b9bd024f8cae9035e512471a6b7f23fcde23 Mon Sep 17 00:00:00 2001 From: MICHAEL JACKSON Date: Thu, 25 May 2017 16:12:44 -0700 Subject: [PATCH] Add top packages to stats summary script --- package.json | 1 + scripts/show-stats.js | 87 +++++++++++++++++++++++++++++++------------ yarn.lock | 2 +- 3 files changed, 66 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 5783cf5..d26c44d 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,7 @@ "promise": "7.1.1", "react-dev-utils": "^0.5.2", "style-loader": "0.13.1", + "text-table": "^0.2.0", "url-loader": "0.5.7", "webpack": "1.14.0", "webpack-dev-server": "1.16.2", diff --git a/scripts/show-stats.js b/scripts/show-stats.js index aa8eefe..3766a01 100644 --- a/scripts/show-stats.js +++ b/scripts/show-stats.js @@ -2,6 +2,7 @@ const redis = require('redis') const subDays = require('date-fns/sub_days') const prettyBytes = require('pretty-bytes') const invariant = require('invariant') +const table = require('text-table') const RedisURL = process.env.REDIS_URL @@ -10,6 +11,9 @@ invariant( '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 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 range = [] @@ -40,9 +76,6 @@ const createDayKey = (date) => const createHourKey = (date) => `${createDayKey(date)}-${date.getUTCHours()}` -const sumValues = (array) => - array.reduce((memo, n) => memo + (parseInt(n, 10) || 0), 0) - const now = new Date const createPastDays = (n) => @@ -51,38 +84,46 @@ const createPastDays = (n) => const pastSevenDays = createPastDays(7) const pastThirtyDays = createPastDays(30) -sumKeys( - pastSevenDays.map(date => `stats-requests-${createHourKey(date)}`) -).then(total => { +Promise.all([ + sumKeys(pastSevenDays.map(date => `stats-requests-${createDayKey(date)}`)), + 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( 'Requests this week: %s', - total.toLocaleString() + results[0].toLocaleString() ) -}) -sumKeys( - pastSevenDays.map(date => `stats-bandwidth-${createHourKey(date)}`) -).then(total => { console.log( 'Bandwidth this week: %s', - prettyBytes(total) + prettyBytes(results[1]) ) -}) -sumKeys( - pastThirtyDays.map(date => `stats-requests-${createDayKey(date)}`) -).then(total => { console.log( 'Requests this month: %s', - total.toLocaleString() + results[2].toLocaleString() ) -}) -sumKeys( - pastThirtyDays.map(date => `stats-bandwidth-${createDayKey(date)}`) -).then(total => { console.log( '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() + }) }) diff --git a/yarn.lock b/yarn.lock index 0372940..adda351 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5140,7 +5140,7 @@ test-exclude@^3.3.0: read-pkg-up "^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" resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4"