Skip to content

Commit 8a71e47

Browse files
committed
Added ability to specify stats period
1 parent 40d5a63 commit 8a71e47

File tree

1 file changed

+45
-30
lines changed

1 file changed

+45
-30
lines changed

packages/assets/stats/index.html

+45-30
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ <h1>WebXR Input Profile Asset Stats</h1>
1616
broader trends in device usage on the web.
1717
</p>
1818

19+
<form id='args' method='GET'>
20+
<label for='period'>Period:</label>
21+
<input name='period' type='text' placeholder='YYYY-MM' />
22+
<input type='submit' value='Refresh'/>
23+
</form>
24+
1925
<h2>Profile List requests</h2>
2026
<p>
2127
Almost all uses of the library fetch this list prior to requesting specific assets, so it serves as a rough
@@ -39,14 +45,26 @@ <h2>Profile requests</h2>
3945
<canvas id='profilesPieChart'></canvas>
4046

4147
<script>
42-
const JSDELIVR_API_ROOT = 'https://data.jsdelivr.com/v1/package/npm/@webxr-input-profiles/assets'; //@1.0.0/stats';
48+
const JSDELIVR_API_ROOT = 'https://data.jsdelivr.com/v1/stats/packages/npm/%40webxr-input-profiles%2Fassets'
4349

4450
const profileListCtx = document.getElementById('profileListChart').getContext('2d');
4551
const profilesCtx = document.getElementById('profilesChart').getContext('2d');
4652
const profilesPieCtx = document.getElementById('profilesPieChart').getContext('2d');
4753

48-
async function processVersions() {
49-
let response = await fetch(JSDELIVR_API_ROOT);
54+
let profileListChart;
55+
let profilesChart;
56+
let profilesPieChart;
57+
58+
const argsForm = document.getElementById('args');
59+
argsForm.addEventListener('submit', (ev) => {
60+
ev.preventDefault();
61+
const args = new FormData(argsForm);
62+
processVersions(args);
63+
});
64+
65+
async function processVersions(args = new FormData()) {
66+
let queryArgs = new URLSearchParams(args).toString();
67+
let response = await fetch(`${JSDELIVR_API_ROOT}/versions?${queryArgs}`);
5068
let versionJson = await response.json();
5169

5270
let stats = {
@@ -57,8 +75,8 @@ <h2>Profile requests</h2>
5775
};
5876

5977
let statPromises = [];
60-
for (let version of versionJson.versions) {
61-
statPromises.push(processVersionStats(version, stats));
78+
for (let version of versionJson) {
79+
statPromises.push(processVersionStats(version.version, stats, args));
6280
}
6381
await Promise.all(statPromises);
6482

@@ -73,30 +91,24 @@ <h2>Profile requests</h2>
7391
processStats(stats);
7492
}
7593

76-
async function processVersionStats(version, stats) {
77-
let response = await fetch(`${JSDELIVR_API_ROOT}@${version}/stats`);
94+
async function processVersionStats(version, stats, args) {
95+
let queryArgs = new URLSearchParams(args).toString();
96+
let response = await fetch(`${JSDELIVR_API_ROOT}@${version}/files?${queryArgs}`);
7897
let statsJson = await response.json();
7998

80-
let profileListStats = statsJson.files['/dist/profiles/profilesList.json'];
81-
82-
if (!profileListStats) {
83-
return;
84-
}
85-
86-
for (let date in profileListStats.dates) {
87-
if (!stats.profileList.dates[date]) {
88-
stats.profileList.dates[date] = 0;
89-
}
90-
stats.profileList.dates[date] += profileListStats.dates[date];
91-
}
92-
93-
for(let key in statsJson.files) {
94-
if(/.json$/.test(key) && key != '/dist/profiles/profilesList.json') {
95-
let profile = statsJson.files[key];
96-
let total = profile.total;
99+
for(let file of statsJson) {
100+
if (file.name === '/dist/profiles/profilesList.json') {
101+
for (let date in file.hits.dates) {
102+
if (!stats.profileList.dates[date]) {
103+
stats.profileList.dates[date] = 0;
104+
}
105+
stats.profileList.dates[date] += file.hits.dates[date];
106+
}
107+
} else if(/.json$/.test(file.name) && file.name != '/dist/profiles/profilesList.json') {
108+
let total = file.hits.total;
97109
if (total == 0) continue;
98110

99-
let label = key.replace('/dist/profiles/', '').replace('/profile.json', '');
111+
let label = file.name.replace('/dist/profiles/', '').replace('/profile.json', '');
100112
if (!stats.profiles[label]) {
101113
stats.profiles[label] = {
102114
total: 0,
@@ -105,11 +117,11 @@ <h2>Profile requests</h2>
105117
}
106118
stats.profiles[label].total += total;
107119

108-
for (let date in profile.dates) {
120+
for (let date in file.hits.dates) {
109121
if (!stats.profiles[label].dates[date]) {
110122
stats.profiles[label].dates[date] = 0;
111123
}
112-
stats.profiles[label].dates[date] += profile.dates[date];
124+
stats.profiles[label].dates[date] += file.hits.dates[date];
113125
}
114126
}
115127
}
@@ -118,7 +130,8 @@ <h2>Profile requests</h2>
118130
function processStats(stats) {
119131
let dateLabels = Object.keys(stats.profileList.dates);
120132

121-
let profileListChart = new Chart(profileListCtx, {
133+
if (profileListChart) { profileListChart.destroy(); }
134+
profileListChart = new Chart(profileListCtx, {
122135
type: 'line',
123136
data: {
124137
labels: dateLabels,
@@ -188,15 +201,17 @@ <h2>Profile requests</h2>
188201
});
189202
}
190203

191-
let profilesChart = new Chart(profilesCtx, {
204+
if (profilesChart) { profilesChart.destroy(); }
205+
profilesChart = new Chart(profilesCtx, {
192206
type: 'line',
193207
data: {
194208
labels: dateLabels,
195209
datasets: profileDatasets
196210
},
197211
});
198212

199-
let profilesPieChart = new Chart(profilesPieCtx, {
213+
if (profilesPieChart) { profilesPieChart.destroy(); }
214+
profilesPieChart = new Chart(profilesPieCtx, {
200215
type: 'doughnut',
201216
data: {
202217
labels: profileLabels,

0 commit comments

Comments
 (0)