Rename StatsServer => StatsAPI

This commit is contained in:
MICHAEL JACKSON 2017-11-08 11:07:48 -08:00
parent b614f8646d
commit a8ab15e49d
5 changed files with 66 additions and 71 deletions

View File

@ -5,39 +5,39 @@ const CloudflareEmail = process.env.CLOUDFLARE_EMAIL
const CloudflareKey = process.env.CLOUDFLARE_KEY const CloudflareKey = process.env.CLOUDFLARE_KEY
const RayID = process.argv[2] const RayID = process.argv[2]
invariant( invariant(CloudflareEmail, 'Missing the $CLOUDFLARE_EMAIL environment variable')
CloudflareEmail,
'Missing the $CLOUDFLARE_EMAIL environment variable'
)
invariant( invariant(CloudflareKey, 'Missing the $CLOUDFLARE_KEY environment variable')
CloudflareKey,
'Missing the $CLOUDFLARE_KEY environment variable'
)
invariant( invariant(
RayID, RayID,
'Missing the RAY_ID argument; use `heroku run node show-log.js RAY_ID`' 'Missing the RAY_ID argument; use `heroku run node show-log.js RAY_ID`'
) )
const getZones = (domain) => function getZones(domain) {
fetch(`https://api.cloudflare.com/client/v4/zones?name=${domain}`, { return fetch(`https://api.cloudflare.com/client/v4/zones?name=${domain}`, {
method: 'GET', method: 'GET',
headers: { headers: {
'X-Auth-Email': CloudflareEmail, 'X-Auth-Email': CloudflareEmail,
'X-Auth-Key': CloudflareKey 'X-Auth-Key': CloudflareKey
} }
}).then(res => res.json()) })
.then(res => res.json())
.then(data => data.result) .then(data => data.result)
}
const getLog = (zoneId, rayId) => function getLog(zoneId, rayId) {
fetch(`https://api.cloudflare.com/client/v4/zones/${zoneId}/logs/requests/${rayId}`, { return fetch(
method: 'GET', `https://api.cloudflare.com/client/v4/zones/${zoneId}/logs/requests/${rayId}`,
headers: { {
'X-Auth-Email': CloudflareEmail, method: 'GET',
'X-Auth-Key': CloudflareKey headers: {
'X-Auth-Email': CloudflareEmail,
'X-Auth-Key': CloudflareKey
}
} }
}).then(res => res.status === 404 ? 'NOT FOUND' : res.json()) ).then(res => (res.status === 404 ? 'NOT FOUND' : res.json()))
}
getZones('unpkg.com').then(zones => { getZones('unpkg.com').then(zones => {
getLog(zones[0].id, RayID).then(entry => { getLog(zones[0].id, RayID).then(entry => {

View File

@ -1,67 +1,57 @@
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 table = require('text-table') const table = require('text-table')
const {
sumKeys,
sumTopScores,
createDayKey
} = require('../server/StatsServer')
const now = new Date const StatsAPI = require('../server/StatsAPI')
const now = new Date()
const createRange = (start, end) => { function createRange(start, end) {
const range = [] const range = []
while (start < end) range.push(start++)
while (start < end)
range.push(start++)
return range return range
} }
const createPastDays = (n) => function createPastDays(n) {
createRange(1, n + 1).map(days => subDays(now, days)).reverse() return createRange(1, n + 1)
.map(days => subDays(now, days))
.reverse()
}
const pastSevenDays = createPastDays(7) const pastSevenDays = createPastDays(7)
const pastThirtyDays = createPastDays(30) const pastThirtyDays = createPastDays(30)
Promise.all([ Promise.all([
sumKeys(pastSevenDays.map(date => `stats-requests-${createDayKey(date)}`)), StatsAPI.sumKeys(
sumKeys(pastSevenDays.map(date => `stats-bandwidth-${createDayKey(date)}`)), pastSevenDays.map(date => `stats-requests-${StatsAPI.createDayKey(date)}`)
sumKeys(pastThirtyDays.map(date => `stats-requests-${createDayKey(date)}`)), ),
sumKeys(pastThirtyDays.map(date => `stats-bandwidth-${createDayKey(date)}`)) StatsAPI.sumKeys(
pastSevenDays.map(date => `stats-bandwidth-${StatsAPI.createDayKey(date)}`)
),
StatsAPI.sumKeys(
pastThirtyDays.map(date => `stats-requests-${StatsAPI.createDayKey(date)}`)
),
StatsAPI.sumKeys(
pastThirtyDays.map(date => `stats-bandwidth-${StatsAPI.createDayKey(date)}`)
)
]).then(results => { ]).then(results => {
console.log('\n## Summary') console.log('\n## Summary')
console.log('Requests this week: %s', results[0].toLocaleString())
console.log('Bandwidth this week: %s', prettyBytes(results[1]))
console.log('Requests this month: %s', results[2].toLocaleString())
console.log('Bandwidth this month: %s', prettyBytes(results[3]))
console.log( StatsAPI.sumTopScores(
'Requests this week: %s', pastSevenDays.map(
results[0].toLocaleString() date => `stats-packageRequests-${StatsAPI.createDayKey(date)}`
) )
).then(topPackages => {
console.log(
'Bandwidth this week: %s',
prettyBytes(results[1])
)
console.log(
'Requests this month: %s',
results[2].toLocaleString()
)
console.log(
'Bandwidth this month: %s',
prettyBytes(results[3])
)
sumTopScores(pastSevenDays.map(date => `stats-packageRequests-${createDayKey(date)}`)).then(topPackages => {
console.log('\n## Top Packages This Week') console.log('\n## Top Packages This Week')
topPackages.forEach(result => { topPackages.forEach(result => {
result[1] = result[1].toLocaleString() result[1] = result[1].toLocaleString()
}) })
console.log( console.log(table(topPackages))
table(topPackages)
)
process.exit() process.exit()
}) })

View File

@ -2,28 +2,32 @@ const express = require('express')
const subDays = require('date-fns/sub_days') const subDays = require('date-fns/sub_days')
const startOfDay = require('date-fns/start_of_day') const startOfDay = require('date-fns/start_of_day')
const startOfSecond = require('date-fns/start_of_second') const startOfSecond = require('date-fns/start_of_second')
const StatsServer = require('./StatsServer') const StatsAPI = require('./StatsAPI')
function serveArbitraryStats(req, res) { function serveArbitraryStats(req, res) {
const now = startOfSecond(new Date()) const now = startOfSecond(new Date())
const since = req.query.since ? new Date(req.query.since) : subDays(now, 30) const since = req.query.since ? new Date(req.query.since) : subDays(now, 30)
const until = req.query.until ? new Date(req.query.until) : now const until = req.query.until ? new Date(req.query.until) : now
if (isNaN(since.getTime())) if (isNaN(since.getTime())) {
return res.status(403).send({ error: '?since is not a valid date' }) return res.status(403).send({ error: '?since is not a valid date' })
}
if (isNaN(until.getTime())) if (isNaN(until.getTime())) {
return res.status(403).send({ error: '?until is not a valid date' }) return res.status(403).send({ error: '?until is not a valid date' })
}
if (until <= since) if (until <= since) {
return res return res
.status(403) .status(403)
.send({ error: '?until date must come after ?since date' }) .send({ error: '?until date must come after ?since date' })
}
if (until > now) if (until > now) {
return res.status(403).send({ error: '?until must be a date in the past' }) return res.status(403).send({ error: '?until must be a date in the past' })
}
StatsServer.getStats(since, until, function(error, stats) { StatsAPI.getStats(since, until, (error, stats) => {
if (error) { if (error) {
console.error(error) console.error(error)
res.status(500).send({ error: 'Unable to fetch stats' }) res.status(500).send({ error: 'Unable to fetch stats' })
@ -42,7 +46,7 @@ function servePastDaysStats(days, req, res) {
const until = startOfDay(new Date()) const until = startOfDay(new Date())
const since = subDays(until, days) const since = subDays(until, days)
StatsServer.getStats(since, until, function(error, stats) { StatsAPI.getStats(since, until, (error, stats) => {
if (error) { if (error) {
console.error(error) console.error(error)
res.status(500).send({ error: 'Unable to fetch stats' }) res.status(500).send({ error: 'Unable to fetch stats' })

View File

@ -3,9 +3,10 @@ const startOfDay = require('date-fns/start_of_day')
const addDays = require('date-fns/add_days') const addDays = require('date-fns/add_days')
const validateNPMPackageName = require('validate-npm-package-name') const validateNPMPackageName = require('validate-npm-package-name')
const parsePackageURL = require('./utils/parsePackageURL') const parsePackageURL = require('./utils/parsePackageURL')
const cf = require('./CloudflareAPI') const CloudflareAPI = require('./CloudflareAPI')
const StatsAPI = require('./StatsAPI')
const db = require('./RedisClient') const db = require('./RedisClient')
const { createDayKey } = require('./StatsServer')
/** /**
* Domains we want to analyze. * Domains we want to analyze.
@ -55,7 +56,7 @@ function computeCounters(stream) {
const nextDay = startOfDay(addDays(date, 1)) const nextDay = startOfDay(addDays(date, 1))
const sevenDaysLater = getSeconds(addDays(nextDay, 7)) const sevenDaysLater = getSeconds(addDays(nextDay, 7))
const thirtyDaysLater = getSeconds(addDays(nextDay, 30)) const thirtyDaysLater = getSeconds(addDays(nextDay, 30))
const dayKey = createDayKey(date) const dayKey = StatsAPI.createDayKey(date)
const clientRequest = entry.clientRequest const clientRequest = entry.clientRequest
const edgeResponse = entry.edgeResponse const edgeResponse = entry.edgeResponse
@ -138,7 +139,7 @@ function ingestLogs(zone, startSeconds, endSeconds) {
const startFetchTime = Date.now() const startFetchTime = Date.now()
resolve( resolve(
cf.getLogs(zone.id, startSeconds, endSeconds).then(stream => { CloudflareAPI.getLogs(zone.id, startSeconds, endSeconds).then(stream => {
const endFetchTime = Date.now() const endFetchTime = Date.now()
console.log( console.log(
@ -226,7 +227,7 @@ function startZone(zone) {
takeATurn() takeATurn()
} }
Promise.all(DomainNames.map(cf.getZones)).then(results => { Promise.all(DomainNames.map(CloudflareAPI.getZones)).then(results => {
const zones = results.reduce((memo, zones) => { const zones = results.reduce((memo, zones) => {
return memo.concat(zones) return memo.concat(zones)
}) })