Consolidate stats module
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
import { subDays, startOfDay, startOfSecond } from 'date-fns';
|
||||
|
||||
import { getStats } from '../utils/stats';
|
||||
import getStats from './utils/getStats.js';
|
||||
|
||||
export default function serveStats(req, res) {
|
||||
let since, until;
|
||||
|
72
modules/actions/utils/cloudflare.js
Normal file
72
modules/actions/utils/cloudflare.js
Normal file
@ -0,0 +1,72 @@
|
||||
import fetch from 'isomorphic-fetch';
|
||||
import invariant from 'invariant';
|
||||
|
||||
const cloudflareURL = 'https://api.cloudflare.com/client/v4';
|
||||
const cloudflareEmail = process.env.CLOUDFLARE_EMAIL;
|
||||
const cloudflareKey = process.env.CLOUDFLARE_KEY;
|
||||
|
||||
invariant(
|
||||
cloudflareEmail,
|
||||
'Missing the $CLOUDFLARE_EMAIL environment variable'
|
||||
);
|
||||
|
||||
invariant(cloudflareKey, 'Missing the $CLOUDFLARE_KEY environment variable');
|
||||
|
||||
function get(path, headers) {
|
||||
return fetch(`${cloudflareURL}${path}`, {
|
||||
headers: Object.assign({}, headers, {
|
||||
'X-Auth-Email': cloudflareEmail,
|
||||
'X-Auth-Key': cloudflareKey
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
function getJSON(path, headers) {
|
||||
return get(path, headers)
|
||||
.then(res => {
|
||||
return res.json();
|
||||
})
|
||||
.then(data => {
|
||||
if (!data.success) {
|
||||
console.error(`cloudflare.getJSON failed at ${path}`);
|
||||
console.error(data);
|
||||
throw new Error('Failed to getJSON from Cloudflare');
|
||||
}
|
||||
|
||||
return data.result;
|
||||
});
|
||||
}
|
||||
|
||||
export function getZones(domains) {
|
||||
return Promise.all(
|
||||
(Array.isArray(domains) ? domains : [domains]).map(domain =>
|
||||
getJSON(`/zones?name=${domain}`)
|
||||
)
|
||||
).then(results => results.reduce((memo, zones) => memo.concat(zones)));
|
||||
}
|
||||
|
||||
function reduceResults(target, values) {
|
||||
Object.keys(values).forEach(key => {
|
||||
const value = values[key];
|
||||
|
||||
if (typeof value === 'object' && value) {
|
||||
target[key] = reduceResults(target[key] || {}, value);
|
||||
} else if (typeof value === 'number') {
|
||||
target[key] = (target[key] || 0) + values[key];
|
||||
}
|
||||
});
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
export function getZoneAnalyticsDashboard(zones, since, until) {
|
||||
return Promise.all(
|
||||
(Array.isArray(zones) ? zones : [zones]).map(zone => {
|
||||
return getJSON(
|
||||
`/zones/${
|
||||
zone.id
|
||||
}/analytics/dashboard?since=${since.toISOString()}&until=${until.toISOString()}`
|
||||
);
|
||||
})
|
||||
).then(results => results.reduce(reduceResults));
|
||||
}
|
42
modules/actions/utils/getStats.js
Normal file
42
modules/actions/utils/getStats.js
Normal file
@ -0,0 +1,42 @@
|
||||
import * as cloudflare from './cloudflare.js';
|
||||
|
||||
function extractPublicInfo(data) {
|
||||
return {
|
||||
since: data.since,
|
||||
until: data.until,
|
||||
requests: {
|
||||
all: data.requests.all,
|
||||
cached: data.requests.cached,
|
||||
country: data.requests.country,
|
||||
status: data.requests.http_status
|
||||
},
|
||||
bandwidth: {
|
||||
all: data.bandwidth.all,
|
||||
cached: data.bandwidth.cached,
|
||||
country: data.bandwidth.country
|
||||
},
|
||||
threats: {
|
||||
all: data.threats.all,
|
||||
country: data.threats.country
|
||||
},
|
||||
uniques: {
|
||||
all: data.uniques.all
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const DomainNames = ['unpkg.com', 'npmcdn.com'];
|
||||
|
||||
export default async function getStats(since, until) {
|
||||
const zones = await cloudflare.getZones(DomainNames);
|
||||
const dashboard = await cloudflare.getZoneAnalyticsDashboard(
|
||||
zones,
|
||||
since,
|
||||
until
|
||||
);
|
||||
|
||||
return {
|
||||
timeseries: dashboard.timeseries.map(extractPublicInfo),
|
||||
totals: extractPublicInfo(dashboard.totals)
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user