Combine stats from npmcdn.com + unpkg.com
This commit is contained in:
parent
855988865d
commit
469102ab52
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue