Skip to content

Commit c6fdf32

Browse files
authored
Merge pull request #18 from Astylodon/extend-dashboard-front
Add missing info to dashboard
2 parents d42a2e6 + cd25db4 commit c6fdf32

File tree

6 files changed

+278
-22
lines changed

6 files changed

+278
-22
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ jobs:
77
runs-on: ubuntu-latest
88

99
steps:
10-
- uses: actions/checkout@v3
11-
- uses: actions/setup-node@v3
10+
- uses: actions/checkout@v4
11+
- uses: actions/setup-node@v4
1212
with:
1313
node-version: 18
1414

@@ -18,7 +18,7 @@ jobs:
1818
- name: Build
1919
run: npm run build
2020

21-
- uses: actions/upload-artifact@v3
21+
- uses: actions/upload-artifact@v4
2222
with:
2323
name: script
2424
path: |
@@ -30,20 +30,20 @@ jobs:
3030
runs-on: ubuntu-latest
3131

3232
steps:
33-
- uses: actions/checkout@v3
33+
- uses: actions/checkout@v4
3434

3535
- name: Validate composer.json and composer.lock
3636
run: composer validate --strict
3737

3838
- name: Install dependencies
3939
run: composer install --prefer-dist --no-progress
4040

41-
- uses: actions/download-artifact@v3
41+
- uses: actions/download-artifact@v4
4242
with:
4343
name: script
4444
path: public
4545

46-
- uses: actions/upload-artifact@v3
46+
- uses: actions/upload-artifact@v4
4747
with:
4848
name: build
4949
path: .

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ vendor/
33
*.mmdb
44
node_modules/
55
public/shika.js
6+
public/shika.js.map
67
public/build
78
.env

assets/js/graphs.js

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,42 @@
1-
import { Chart, BarController, BarElement, CategoryScale, LinearScale, Colors, Tooltip } from "chart.js"
1+
import { Chart, BarController, PieController, BarElement, ArcElement, CategoryScale, LinearScale, Colors, Tooltip, Legend } from "chart.js"
2+
import { ChoroplethController, ProjectionScale, ColorScale, GeoFeature, topojson } from 'chartjs-chart-geo';
3+
import countries50m from "world-atlas/countries-50m.json" with { type: "json" };
24

35
// Bundle optimization
4-
Chart.register(BarController, BarElement, CategoryScale, LinearScale, Colors, Tooltip)
6+
Chart.register(BarController, PieController, ChoroplethController, BarElement, ArcElement, CategoryScale, LinearScale, ProjectionScale, ColorScale, GeoFeature, Colors, Tooltip, Legend)
57

68
// Current charts
7-
let pagesChart = null
8-
let referrersChart = null
9+
let charts = []
910

1011
async function displayData(site, time) {
1112
// Destroy the old charts
12-
if (pagesChart !== null) pagesChart.destroy()
13-
if (referrersChart !== null) referrersChart.destroy()
13+
for (let chart of charts) {
14+
chart.destroy()
15+
}
16+
charts = []
1417

1518
// Calculate the from time
1619
const from = (Date.now() / 1000) - time
1720

1821
// Fetch the numbers
1922
const pages = await fetch(`/api/sites/${site}/pages?from=${from}`).then(x => x.json())
2023
const referrers = await fetch(`/api/sites/${site}/referrers?from=${from}`).then(x => x.json())
24+
const browsers = await fetch(`/api/sites/${site}/browsers?from=${from}`).then(x => x.json())
25+
const systems = await fetch(`/api/sites/${site}/operating-systems?from=${from}`).then(x => x.json())
26+
const devices = await fetch(`/api/sites/${site}/device-types?from=${from}`).then(x => x.json())
27+
const countries = await fetch(`/api/sites/${site}/countries?from=${from}`).then(x => x.json())
2128

2229
// Display the charts
23-
pagesChart = displayLineChart("pages", pages.map(x => [x.path, x.count]))
24-
referrersChart = displayLineChart("referrers", referrers.map(x => [x.referrer, x.count]))
30+
const region = new Intl.DisplayNames(['en'], { type: 'region' })
31+
charts.push(displayLineChart("pages", pages.map(x => [x.path, x.count])))
32+
charts.push(displayLineChart("referrers", referrers.map(x => [x.referrer, x.count])))
33+
charts.push(displayPieChart("browsers", browsers.map(x => [x.browser, x.count])))
34+
charts.push(displayPieChart("systems", systems.map(x => [x.operating_system, x.count])))
35+
charts.push(displayPieChart("devices", devices.map(x => [x.device_type, x.count])))
36+
charts.push(displayCountryChart("countries", Object.fromEntries(countries.map(x => [ region.of(x.country), x.count ]))));
2537
}
2638

27-
function displayLineChart(id, values) {
28-
const options = {
29-
indexAxis: "y"
30-
}
31-
39+
function displayChartInternal(type, options, id, values) {
3240
// Map the data
3341
const data = {
3442
labels: values.map(x => x[0]),
@@ -42,14 +50,60 @@ function displayLineChart(id, values) {
4250

4351
// Display the chart
4452
const chart = new Chart(document.getElementById(id), {
45-
type: "bar",
53+
type: type,
4654
data,
4755
options
4856
})
4957

5058
return chart
5159
}
5260

61+
function displayCountryChart(id, values) {
62+
const countries = topojson.feature(countries50m, countries50m.objects.countries).features;
63+
64+
const chart = new Chart(document.getElementById(id).getContext("2d"), {
65+
type: 'choropleth',
66+
data: {
67+
labels: countries.map((d) => d.properties.name),
68+
datasets: [{
69+
label: 'Countries',
70+
data: countries.map((d) => {
71+
const country = d.properties.name;
72+
return ({feature: d, value: values[country] ? values[country] : 0})
73+
}),
74+
}]
75+
},
76+
options: {
77+
scales: {
78+
projection: {
79+
axis: 'x',
80+
projection: 'equalEarth'
81+
}
82+
}
83+
}
84+
});
85+
86+
return chart;
87+
}
88+
89+
function displayLineChart(id, values) {
90+
const options = {
91+
indexAxis: "y"
92+
}
93+
return displayChartInternal("bar", options, id, values)
94+
}
95+
96+
function displayPieChart(id, values) {
97+
const options = {
98+
plugins: {
99+
legend: {
100+
position: 'top'
101+
}
102+
}
103+
}
104+
return displayChartInternal("pie", options, id, values)
105+
}
106+
53107
function startDisplayData() {
54108
// Get the site and time
55109
const site = document.getElementById("site-select").value

package-lock.json

Lines changed: 156 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
},
77
"dependencies": {
88
"bootstrap": "^5.3.2",
9-
"chart.js": "^4.4.1"
9+
"chart.js": "^4.4.1",
10+
"chartjs-chart-geo": "^4.3.4",
11+
"world-atlas": "^2.0.2"
1012
},
1113
"devDependencies": {
1214
"esbuild": "^0.19.10",

0 commit comments

Comments
 (0)