Combine stats from npmcdn.com + unpkg.com

This commit is contained in:
Michael Jackson 2016-09-12 17:07:42 -07:00
parent 855988865d
commit 469102ab52
2 changed files with 30 additions and 11 deletions

View File

@ -48,3 +48,28 @@ export const getZoneAnalyticsDashboard = (zone, since, until) =>
commonStack,
createRangeQuery(since, until)
)(`/zones/${zone.id}/analytics/dashboard`)
export const getAnalyticsDashboards = (domainNames, since, until) =>
Promise.all(
domainNames.map(domainName => getZones(domainName))
).then(
domainZones => domainZones.reduce((memo, zones) => memo.concat(zones))
).then(
zones => Promise.all(zones.map(zone => getZoneAnalyticsDashboard(zone, since, until)))
).then(
results => results.reduce(reduceResults)
)
const reduceResults = (target, results) => {
Object.keys(results).forEach(key => {
const value = results[key]
if (typeof value === 'object' && value) {
target[key] = reduceResults(target[key] || {}, value)
} else if (typeof value === 'number') {
target[key] = (target[key] || 0) + results[key]
}
})
return target
}

View File

@ -1,6 +1,6 @@
import React from 'react'
import { renderToStaticMarkup } from 'react-dom/server'
import { getZones, getZoneAnalyticsDashboard } from './CloudFlare'
import { getAnalyticsDashboards } from './CloudFlare'
import HomePage from './components/HomePage'
const OneMinute = 1000 * 60
@ -11,17 +11,11 @@ const fetchStats = (callback) => {
if (process.env.NODE_ENV === 'development') {
callback(null, require('./CloudFlareStats.json'))
} else {
getZones('unpkg.com')
.then(zones => {
const zone = zones[0]
const since = new Date(Date.now() - ThirtyDays)
const until = new Date(Date.now() - OneMinute)
const since = new Date(Date.now() - ThirtyDays)
const until = new Date(Date.now() - OneMinute)
return getZoneAnalyticsDashboard(zone, since, until).then(result => {
callback(null, result)
})
})
.catch(callback)
getAnalyticsDashboards([ 'npmcdn.com', 'unpkg.com' ], since, until)
.then(result => callback(null, result), callback)
}
}