2017-04-15 04:01:23 +00:00
|
|
|
import React from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
2017-03-26 06:53:54 +00:00
|
|
|
import formatBytes from 'pretty-bytes'
|
2016-05-20 18:58:58 +00:00
|
|
|
import formatDate from 'date-fns/format'
|
|
|
|
import parseDate from 'date-fns/parse'
|
2017-08-22 15:31:33 +00:00
|
|
|
import formatNumber from './utils/formatNumber'
|
|
|
|
import formatPercent from './utils/formatPercent'
|
|
|
|
|
|
|
|
import { continents, countries } from 'countries-list'
|
|
|
|
|
|
|
|
const getCountriesByContinent = (continent) =>
|
|
|
|
Object.keys(countries).filter(country => countries[country].continent === continent)
|
|
|
|
|
|
|
|
const sumKeyValues = (hash, keys) =>
|
|
|
|
keys.reduce((n, key) => n + (hash[key] || 0), 0)
|
|
|
|
|
|
|
|
const sumValues = (hash) =>
|
|
|
|
Object.keys(hash).reduce((memo, key) => memo + hash[key], 0)
|
2016-05-20 18:58:58 +00:00
|
|
|
|
|
|
|
class Stats extends React.Component {
|
2016-07-20 19:26:15 +00:00
|
|
|
static propTypes = {
|
2017-08-22 15:31:33 +00:00
|
|
|
data: PropTypes.object
|
2016-05-20 18:58:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
state = {
|
2017-08-22 16:12:57 +00:00
|
|
|
minPackageRequests: 1000000,
|
2017-08-22 15:31:33 +00:00
|
|
|
minCountryRequests: 1000000
|
2017-03-26 06:53:54 +00:00
|
|
|
}
|
2017-04-15 04:01:23 +00:00
|
|
|
|
2017-03-26 06:53:54 +00:00
|
|
|
render() {
|
2017-08-22 15:31:33 +00:00
|
|
|
const { data } = this.props
|
|
|
|
|
|
|
|
if (data == null)
|
|
|
|
return null
|
|
|
|
|
|
|
|
const totals = data.totals
|
2016-05-20 18:58:58 +00:00
|
|
|
|
|
|
|
// Summary data
|
2017-08-22 15:31:33 +00:00
|
|
|
const since = parseDate(totals.since)
|
|
|
|
const until = parseDate(totals.until)
|
|
|
|
|
|
|
|
// Packages
|
|
|
|
const packageRows = []
|
|
|
|
|
|
|
|
Object.keys(totals.requests.package).sort((a, b) => {
|
|
|
|
return totals.requests.package[b] - totals.requests.package[a]
|
|
|
|
}).forEach(packageName => {
|
|
|
|
const requests = totals.requests.package[packageName]
|
|
|
|
const bandwidth = totals.bandwidth.package[packageName]
|
|
|
|
|
|
|
|
if (requests >= this.state.minPackageRequests) {
|
|
|
|
packageRows.push(
|
|
|
|
<tr key={packageName}>
|
|
|
|
<td><a href={`https://npmjs.org/package/${packageName}`} title={`${packageName} on npm`}>{packageName}</a></td>
|
|
|
|
<td>{formatNumber(requests)} ({formatPercent(requests / totals.requests.all)}%)</td>
|
|
|
|
{bandwidth
|
|
|
|
? <td>{formatBytes(bandwidth)} ({formatPercent(bandwidth / totals.bandwidth.all)}%)</td>
|
|
|
|
: <td>-</td>
|
|
|
|
}
|
|
|
|
</tr>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
2016-05-20 18:58:58 +00:00
|
|
|
|
2017-08-22 15:31:33 +00:00
|
|
|
// Regions
|
|
|
|
const regionRows = []
|
2016-05-20 18:58:58 +00:00
|
|
|
|
2017-08-22 15:31:33 +00:00
|
|
|
const continentsData = Object.keys(continents).reduce((memo, continent) => {
|
|
|
|
const localCountries = getCountriesByContinent(continent)
|
2016-05-20 18:58:58 +00:00
|
|
|
|
|
|
|
memo[continent] = {
|
2017-08-22 15:31:33 +00:00
|
|
|
countries: localCountries,
|
|
|
|
requests: sumKeyValues(totals.requests.country, localCountries),
|
|
|
|
bandwidth: sumKeyValues(totals.bandwidth.country, localCountries)
|
2016-05-20 18:58:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return memo
|
|
|
|
}, {})
|
|
|
|
|
2017-08-22 15:31:33 +00:00
|
|
|
const topContinents = Object.keys(continentsData).sort((a, b) => {
|
|
|
|
return continentsData[b].requests - continentsData[a].requests
|
2016-05-20 18:58:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
topContinents.forEach(continent => {
|
2017-08-22 15:31:33 +00:00
|
|
|
const continentName = continents[continent]
|
|
|
|
const continentData = continentsData[continent]
|
2016-05-20 18:58:58 +00:00
|
|
|
|
2017-08-22 15:31:33 +00:00
|
|
|
if (continentData.requests > this.state.minCountryRequests && continentData.bandwidth !== 0) {
|
2016-05-20 18:58:58 +00:00
|
|
|
regionRows.push(
|
|
|
|
<tr key={continent} className="continent-row">
|
2016-07-20 19:26:15 +00:00
|
|
|
<td>{continentName}</td>
|
2017-08-22 15:31:33 +00:00
|
|
|
<td>{formatNumber(continentData.requests)} ({formatPercent(continentData.requests / totals.requests.all)}%)</td>
|
|
|
|
<td>{formatBytes(continentData.bandwidth)} ({formatPercent(continentData.bandwidth / totals.bandwidth.all)}%)</td>
|
2016-05-20 18:58:58 +00:00
|
|
|
</tr>
|
|
|
|
)
|
|
|
|
|
2017-08-22 15:31:33 +00:00
|
|
|
const topCountries = continentData.countries.sort((a, b) => {
|
|
|
|
return totals.requests.country[b] - totals.requests.country[a]
|
|
|
|
})
|
2016-05-20 18:58:58 +00:00
|
|
|
|
|
|
|
topCountries.forEach(country => {
|
2017-08-22 15:31:33 +00:00
|
|
|
const countryRequests = totals.requests.country[country]
|
|
|
|
const countryBandwidth = totals.bandwidth.country[country]
|
2016-05-20 18:58:58 +00:00
|
|
|
|
2017-08-22 15:31:33 +00:00
|
|
|
if (countryRequests > this.state.minCountryRequests) {
|
2016-05-20 18:58:58 +00:00
|
|
|
regionRows.push(
|
|
|
|
<tr key={continent + country} className="country-row">
|
2017-08-22 15:31:33 +00:00
|
|
|
<td className="country-name">{countries[country].name}</td>
|
|
|
|
<td>{formatNumber(countryRequests)} ({formatPercent(countryRequests / totals.requests.all)}%)</td>
|
|
|
|
<td>{formatBytes(countryBandwidth)} ({formatPercent(countryBandwidth / totals.bandwidth.all)}%)</td>
|
2016-05-20 18:58:58 +00:00
|
|
|
</tr>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-08-22 17:14:21 +00:00
|
|
|
// Protocols
|
|
|
|
const protocolRows = Object.keys(totals.requests.protocol).sort((a, b) => {
|
|
|
|
return totals.requests.protocol[b] - totals.requests.protocol[a]
|
|
|
|
}).map(protocol => {
|
|
|
|
const requests = totals.requests.protocol[protocol]
|
|
|
|
|
|
|
|
return (
|
|
|
|
<tr key={protocol}>
|
|
|
|
<td>{protocol}</td>
|
|
|
|
<td>{formatNumber(requests)} ({formatPercent(requests / sumValues(totals.requests.protocol))}%)</td>
|
|
|
|
</tr>
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2016-05-20 18:58:58 +00:00
|
|
|
return (
|
|
|
|
<div className="wrapper">
|
2017-08-22 15:31:33 +00:00
|
|
|
<p>From <strong>{formatDate(since, 'MMM D')}</strong> to <strong>{formatDate(until, 'MMM D')}</strong> unpkg served <strong>{formatNumber(totals.requests.all)}</strong> requests and a total of <strong>{formatBytes(totals.bandwidth.all)}</strong> of data to <strong>{formatNumber(totals.uniques.all)}</strong> unique visitors, <strong>{formatPercent(totals.requests.cached / totals.requests.all, 0)}%</strong> of which were served from the cache.</p>
|
|
|
|
|
|
|
|
<h3>Packages</h3>
|
|
|
|
|
2017-08-22 16:12:57 +00:00
|
|
|
<p>The table below shows the most popular packages served by unpkg from <strong>{formatDate(since, 'MMM D')}</strong> to <strong>{formatDate(until, 'MMM D')}</strong>. Only the top {Object.keys(totals.requests.package).length} packages are shown.</p>
|
|
|
|
|
2017-08-22 15:31:33 +00:00
|
|
|
<p className="table-filter">Include only packages that received at least <select
|
|
|
|
value={this.state.minPackageRequests}
|
|
|
|
onChange={event => this.setState({ minPackageRequests: parseInt(event.target.value, 10) })}
|
|
|
|
>
|
|
|
|
<option value="0">0</option>
|
|
|
|
<option value="1000">1,000</option>
|
|
|
|
<option value="10000">10,000</option>
|
|
|
|
<option value="100000">100,000</option>
|
|
|
|
<option value="1000000">1,000,000</option>
|
2017-08-22 16:30:57 +00:00
|
|
|
<option value="10000000">10,000,000</option>
|
2017-08-22 15:31:33 +00:00
|
|
|
</select> requests.
|
|
|
|
</p>
|
2016-05-20 18:58:58 +00:00
|
|
|
|
2017-08-22 15:31:33 +00:00
|
|
|
<table cellSpacing="0" cellPadding="0" style={{ width: '100%' }}>
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>Package</th>
|
|
|
|
<th>Requests (% of total)</th>
|
|
|
|
<th>Bandwidth (% of total)</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{packageRows}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
2016-05-20 18:58:58 +00:00
|
|
|
|
2017-08-22 15:31:33 +00:00
|
|
|
<h3>Regions</h3>
|
|
|
|
|
2017-08-22 16:12:57 +00:00
|
|
|
<p>The table below breaks down requests to unpkg from <strong>{formatDate(since, 'MMM D')}</strong> to <strong>{formatDate(until, 'MMM D')}</strong> by geographic region.</p>
|
|
|
|
|
2017-08-22 15:31:33 +00:00
|
|
|
<p className="table-filter">Include only countries that made at least <select
|
|
|
|
value={this.state.minCountryRequests}
|
|
|
|
onChange={event => this.setState({ minCountryRequests: parseInt(event.target.value, 10) })}
|
|
|
|
>
|
|
|
|
<option value="0">0</option>
|
|
|
|
<option value="100000">100,000</option>
|
|
|
|
<option value="1000000">1,000,000</option>
|
|
|
|
<option value="10000000">10,000,000</option>
|
|
|
|
<option value="100000000">100,000,000</option>
|
|
|
|
</select> requests.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<table cellSpacing="0" cellPadding="0" style={{ width: '100%' }} className="regions-table">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>Region</th>
|
2016-05-20 18:58:58 +00:00
|
|
|
<th>Requests (% of total)</th>
|
|
|
|
<th>Bandwidth (% of total)</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{regionRows}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
2017-08-22 17:14:21 +00:00
|
|
|
|
|
|
|
<h3>Protocols</h3>
|
|
|
|
|
|
|
|
<p>The table below breaks down requests to unpkg from <strong>{formatDate(since, 'MMM D')}</strong> to <strong>{formatDate(until, 'MMM D')}</strong> by HTTP protocol.</p>
|
|
|
|
|
|
|
|
<table cellSpacing="0" cellPadding="0" style={{ width: '100%' }}>
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>Protocol</th>
|
|
|
|
<th>Requests (% of total)</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{protocolRows}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
|
2016-05-20 18:58:58 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Stats
|