-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
495 lines (440 loc) · 17.8 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GitHub Traffic Dashboard</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.0/chart.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.3.0/papaparse.min.js"></script>
<script defer data-domain="piebro.github.io/github-repo-traffic-stats" src="https://plausible.io/js/script.js"></script>
<style>
html {
font-size: 14px;
}
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f0f0f0;
text-align: center;
}
.charts-grid {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 20px;
max-width: 1500px;
margin: 0 auto;
}
.chart-container {
flex: 1 1 calc(50% - 20px);
min-width: 300px;
max-width: 700px;
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
padding: 20px;
}
h1, h2 {
color: #333;
}
select {
margin: 10px 5px;
}
.preview-grid {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 20px;
max-width: 1500px;
margin: 0 auto;
}
.preview-item {
flex: 1 1 700px;
max-width: 700px;
cursor: pointer;
}
.preview-item img {
width: 100%;
height: auto;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
#dashboardView {
display: none;
}
#backButton {
position: absolute;
top: 20px;
left: 20px;
padding: 10px 15px;
background-color: #f0f0f0;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
#backButton:hover {
background-color: #e0e0e0;
}
.dashboard-header {
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 20px;
}
.dashboard-header h1 {
margin-right: 20px;
}
#timeRange {
font-size: 16px;
padding: 8px 12px;
border-radius: 5px;
border: 1px solid #ccc;
}
.dashboard-header {
display: flex;
flex-direction: column;
align-items: flex-start;
padding: 20px;
position: relative;
}
#backButton {
position: static;
margin-bottom: 10px;
}
.repo-title {
margin-right: 20px;
word-break: break-word;
}
@media (min-width: 768px) {
.dashboard-header {
flex-direction: row;
align-items: center;
}
#backButton {
position: absolute;
top: 20px;
left: 20px;
margin-bottom: 0;
}
.repo-title {
margin-left: 60px;
}
}
</style>
</head>
<body>
<h1 id="mainHeader"><a href="https://github.com/piebro">github.com/piebro</a> traffic dashboard</h1>
<div id="previewView">
<div class="preview-grid">
<div class="preview-item"><img src="preview_plots/factorio-blueprint-visualizer.webp"/></div>
<div class="preview-item"><img src="preview_plots/openstreetmap-statistics.webp"/></div>
<div class="preview-item"><img src="preview_plots/deutsche-bahn-statistics.webp"/></div>
<div class="preview-item"><img src="preview_plots/deutsche-bahn-data.webp"/></div>
<div class="preview-item"><img src="preview_plots/plotting-maps.webp"/></div>
<div class="preview-item"><img src="preview_plots/spotify-statistics.webp"/></div>
<div class="preview-item"><img src="preview_plots/substitution-system.webp"/></div>
<div class="preview-item"><img src="preview_plots/simple-website-change-detection.webp"/></div>
<div class="preview-item"><img src="preview_plots/plotting-architecture.webp"/></div>
<div class="preview-item"><img src="preview_plots/map2svg.webp"/></div>
<div class="preview-item"><img src="preview_plots/piebro.github.io.webp"/></div>
<div class="preview-item"><img src="preview_plots/generativeart.webp"/></div>
<div class="preview-item"><img src="preview_plots/js-map-generator.webp"/></div>
<div class="preview-item"><img src="preview_plots/plotting-ribbons.webp"/></div>
<div class="preview-item"><img src="preview_plots/physarum-latent-walk.webp"/></div>
<div class="preview-item"><img src="preview_plots/simple-image-classification-labeling-website.webp"/></div>
</div>
</div>
<div id="dashboardView">
<div class="dashboard-header">
<button id="backButton" onclick="showPreview()">← Back</button>
<h1 class="repo-title">GitHub Traffic Dashboard - <a id="repoLink" href="#" target="_blank"></a></h1>
<select id="timeRange" onchange="updateCharts()">
<option value="total">Total</option>
<option value="7">Last 7 days</option>
<option value="30">Last 30 days</option>
<option value="365">Last 365 days</option>
</select>
</div>
<div class="charts-grid">
<div class="chart-container">
<h2>GitHub Views</h2>
<canvas id="viewsChart"></canvas>
</div>
<div class="chart-container">
<h2>GitHub Clones</h2>
<canvas id="clonesChart"></canvas>
</div>
<div class="chart-container">
<h2>Approximate Top GitHub Referrers</h2>
<canvas id="referrersChart"></canvas>
</div>
<div class="chart-container">
<h2>Approximate Top GitHub Paths</h2>
<canvas id="pathsChart"></canvas>
</div>
</div>
</div>
<p>
The code of this project can be found here: <a href="https://github.com/piebro/github-repo-traffic-stats">github.com/piebro/github-repo-traffic-stats</a>
</p>
<script>
const PIE_CHART_COLORS = [
'rgba(255, 99, 132, 0.6)',
'rgba(54, 162, 235, 0.6)',
'rgba(255, 206, 86, 0.6)',
'rgba(75, 192, 192, 0.6)',
'rgba(153, 102, 255, 0.6)',
'rgba(255, 159, 64, 0.6)',
'rgba(199, 199, 199, 0.6)',
'rgba(83, 102, 255, 0.6)',
'rgba(255, 99, 255, 0.6)',
'rgba(127, 127, 127, 0.6)',
'rgba(255, 0, 0, 0.6)',
'rgba(0, 255, 0, 0.6)',
'rgba(0, 0, 255, 0.6)',
'rgba(128, 0, 128, 0.6)',
'rgba(255, 165, 0, 0.6)',
'rgba(0, 128, 128, 0.6)'
]
// Function to fetch and parse CSV
async function fetchCSV(url) {
const response = await fetch(url);
const text = await response.text();
return Papa.parse(text, { header: true, dynamicTyping: true }).data;
}
// Function to create a chart
function createChart(elementId, type, data, options) {
return new Chart(document.getElementById(elementId), {
type: type,
data: data,
options: options
});
}
let clonesChart, pathsChart, referrersChart, viewsChart;
let allClonesData, allPathsData, allReferrersData, allViewsData;
function addMissingDates(data, dateField) {
if (data.length === 0) return [];
const startDate = new Date(data[0][dateField]);
const endDate = new Date();
const dateRange = getDateRange(startDate, endDate);
// Create a lookup object for quick access
const dataLookup = {};
data.forEach(entry => {
dataLookup[entry[dateField]] = entry;
});
const filledData = dateRange.map(date => {
return dataLookup[date] || {
[dateField]: date,
count: 0,
uniques: 0
};
});
return filledData;
}
function getDateRange(startDate, endDate) {
const dateList = [];
for (let d = new Date(startDate); d <= endDate; d.setDate(d.getDate() + 1)) {
dateList.push(d.toISOString().split('T')[0]);
}
return dateList;
}
function filterLastNEntries(data, n) {
return n === 'total' ? data : data.slice(-n);
}
let currentDataSource = 'factorio-blueprint-visualizer';
// Modified loadDataAndCreateCharts function
async function loadDataAndCreateCharts() {
// Load all data
allClonesData = addMissingDates(await fetchCSV(`data/github_clones/${currentDataSource}.csv`), 'date');
allPathsData = addMissingDates(await fetchCSV(`data/github_paths/${currentDataSource}.csv`), 'date');
allReferrersData = addMissingDates(await fetchCSV(`data/github_referrers/${currentDataSource}.csv`), 'date');
allViewsData = addMissingDates(await fetchCSV(`data/github_views/${currentDataSource}.csv`), 'date');
// Initial chart creation
updateCharts();
}
// Function to update charts based on selected time range
function updateCharts() {
const timeRange = document.getElementById('timeRange').value;
const days = timeRange === 'total' ? 'total' : parseInt(timeRange);
const clonesData = filterLastNEntries(allClonesData, days);
const pathsData = filterLastNEntries(allPathsData, days);
const referrersData = filterLastNEntries(allReferrersData, days);
const viewsData = filterLastNEntries(allViewsData, days);
updateClonesChart(clonesData);
updatePathsChart(pathsData);
updateReferrersChart(referrersData);
updateViewsChart(viewsData);
}
// Functions to update individual charts
function updateClonesChart(clonesData) {
if (clonesChart) clonesChart.destroy();
// Calculate total clones and unique clones
const totalClones = clonesData.reduce((sum, row) => sum + row.count, 0);
const totalUniqueClones = clonesData.reduce((sum, row) => sum + row.uniques, 0);
// Update the chart container's heading
const clonesChartContainer = document.getElementById('clonesChart').closest('.chart-container');
clonesChartContainer.querySelector('h2').innerHTML = `GitHub Clones (Total: ${totalClones}, Unique: ${totalUniqueClones})`;
clonesChart = createChart('clonesChart', 'line', {
labels: clonesData.map(row => row.date),
datasets: [
{
label: 'Count',
data: clonesData.map(row => row.count),
borderColor: 'rgb(75, 192, 192)',
tension: 0.1
},
{
label: 'Uniques',
data: clonesData.map(row => row.uniques),
borderColor: 'rgb(255, 99, 132)',
tension: 0.1
}
]
});
}
function updatePathsChart(pathsData) {
if (pathsChart) pathsChart.destroy();
const accumulatedPaths = {};
// Accumulate paths from all dates
pathsData.forEach(row => {
for (let i = 1; i <= 10; i++) {
if (row[`path_${i}`] && row[`path_${i}_count`]) {
let path = row[`path_${i}`];
path = path.replace(new RegExp(`^/piebro/${currentDataSource}`), '') || '/';
const count = parseInt(row[`path_${i}_count`]);
accumulatedPaths[path] = (accumulatedPaths[path] || 0) + count;
}
}
});
// Sort paths by count
const sortedPaths = Object.entries(accumulatedPaths)
.sort((a, b) => b[1] - a[1]);
// Prepare data for the chart
let pathLabels, pathCounts;
if (sortedPaths.length > 15) {
pathLabels = sortedPaths.slice(0, 15).map(([path]) => path);
pathLabels.push('Other');
// the data is divided by 13, because the paths and referrers are a snapshot each day of the last 14 days
pathCounts = sortedPaths.slice(0, 15).map(([_, count]) => Math.ceil(count / 13));
const otherPaths = Math.ceil(sortedPaths.slice(15).reduce((sum, [_, count]) => sum + count, 0) / 13);
pathCounts.push(otherPaths);
} else {
pathLabels = sortedPaths.map(([path]) => path);
pathCounts = sortedPaths.map(([_, count]) => Math.ceil(count / 13));
}
pathsChart = createChart('pathsChart', 'pie', {
labels: pathLabels,
datasets: [{
label: 'Path Views',
data: pathCounts,
backgroundColor: PIE_CHART_COLORS
}]
});
}
function updateReferrersChart(referrersData) {
if (referrersChart) referrersChart.destroy();
const accumulatedReferrers = {};
// Accumulate referrers from all dates
referrersData.forEach(row => {
for (let i = 1; i <= 10; i++) {
if (row[`ref_${i}`] && row[`ref_${i}_uniques`]) {
const referrer = row[`ref_${i}`];
const uniques = parseInt(row[`ref_${i}_uniques`]);
accumulatedReferrers[referrer] = (accumulatedReferrers[referrer] || 0) + uniques;
}
}
});
// Sort referrers by unique count
const sortedReferrers = Object.entries(accumulatedReferrers)
.sort((a, b) => b[1] - a[1]);
// Prepare data for the chart
let referrerLabels, referrerCounts;
if (sortedReferrers.length > 15) {
referrerLabels = sortedReferrers.slice(0, 15).map(([referrer]) => referrer);
referrerLabels.push('Other');
referrerCounts = sortedReferrers.slice(0, 15).map(([_, count]) => Math.ceil(count / 14));
const otherReferrers = Math.ceil(sortedReferrers.slice(15).reduce((sum, [_, count]) => sum + count, 0) / 14);
referrerCounts.push(otherReferrers);
} else {
referrerLabels = sortedReferrers.map(([referrer]) => referrer);
referrerCounts = sortedReferrers.map(([_, count]) => Math.ceil(count / 14));
}
referrersChart = createChart('referrersChart', 'pie', {
labels: referrerLabels,
datasets: [{
label: 'Unique Referrers',
data: referrerCounts,
backgroundColor: PIE_CHART_COLORS
}]
});
}
function updateViewsChart(viewsData) {
if (viewsChart) viewsChart.destroy();
// Calculate total views and unique views
const totalViews = viewsData.reduce((sum, row) => sum + row.count, 0);
const totalUniqueViews = viewsData.reduce((sum, row) => sum + row.uniques, 0);
// Update the chart container's heading
const viewsChartContainer = document.getElementById('viewsChart').closest('.chart-container');
viewsChartContainer.querySelector('h2').innerHTML = `GitHub Views (Total: ${totalViews}, Unique: ${totalUniqueViews})`;
viewsChart = createChart('viewsChart', 'line', {
labels: viewsData.map(row => row.date),
datasets: [
{
label: 'Views',
data: viewsData.map(row => row.count),
borderColor: 'rgb(75, 192, 192)',
tension: 0.1
},
{
label: 'Unique Views',
data: viewsData.map(row => row.uniques),
borderColor: 'rgb(255, 99, 132)',
tension: 0.1
}
]
});
}
let previewScrollPosition = 0;
function showDashboard(repo) {
previewScrollPosition = window.scrollY;
document.getElementById('previewView').style.display = 'none';
document.getElementById('dashboardView').style.display = 'block';
document.getElementById('mainHeader').style.display = 'none';
currentDataSource = repo;
const repoLink = document.getElementById('repoLink');
repoLink.textContent = repo;
repoLink.href = `https://github.com/piebro/${repo}`;
loadDataAndCreateCharts();
history.pushState({view: 'dashboard', repo: repo}, '', `#${repo}`);
window.scrollTo(0, 0);
}
function showPreview() {
document.getElementById('previewView').style.display = 'flex';
document.getElementById('dashboardView').style.display = 'none';
document.getElementById('mainHeader').style.display = 'block';
history.pushState({view: 'preview'}, '', '#preview');
window.scrollTo(0, previewScrollPosition);
}
window.addEventListener('popstate', (event) => {
if (event.state && event.state.view === 'dashboard') {
showDashboard(event.state.repo);
} else {
showPreview();
}
});
// Initialize the page
document.querySelectorAll('.preview-item').forEach(item => {
item.addEventListener('click', () => {
const imgSrc = item.querySelector('img').src;
const filename = imgSrc.split('/').pop();
const dataSource = filename.replace('.webp', '');
this.showDashboard(dataSource);
});
});
showPreview();
loadDataAndCreateCharts();
</script>
</body>
</html>