-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server: update metrics and traffic page
- Loading branch information
Showing
8 changed files
with
222 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<div class="container project-container py-4" id="traffic" data-x-account="{{user.id}}"> | ||
<div> | ||
<h6 class="mb-0">Global Traffic</h6> | ||
<p class="fs-6 text-body-tertiary mb-0">Aggregated all projects traffic in recently.</p> | ||
</div> | ||
<div class="row g-4 py-4"> | ||
<div class="col-6"> | ||
<div class="border rounded p-4"> | ||
<div> | ||
<p class="text-body-tertiary text-uppercase mb-2">Request counts</p> | ||
<p class="fw-bold mb-1" id="requests-total"></p> | ||
<div id="requests-loading-spinner" class="spinner-grow spinner-grow-sm" role="status"> | ||
<span class="visually-hidden">Loading...</span> | ||
</div> | ||
</div> | ||
<div id="requests-chart" style="height: 200px;"></div> | ||
</div> | ||
</div> | ||
<div class="col-6">222</div> | ||
</div> | ||
</div> | ||
<script src="https://cdn.jsdelivr.net/npm/echarts@5.5.0/dist/echarts.min.js" | ||
integrity="sha256-QvgynZibb2U53SsVu98NggJXYqwRL7tg3FeyfXvPOUY=" crossorigin="anonymous"></script> | ||
<script src="/static/js/traffic.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
|
||
(async () => { | ||
let d = document.getElementById("requests-loading-spinner"); | ||
let acc = document.getElementById("traffic").getAttribute("data-x-account"); | ||
let resp = await fetch("/traffic/requests?account=" + acc) | ||
if (!resp.ok) { | ||
// TODO: show error message | ||
return | ||
} | ||
// hide loading spinner | ||
d.classList.add("d-none"); | ||
let series = await resp.json(); | ||
document.getElementById("requests-total").innerText = friendly_bytesize(series.total, false); | ||
let chart = echarts.init(document.getElementById('requests-chart')); | ||
let option = { | ||
title: { | ||
show: false, | ||
}, | ||
tooltip: { | ||
trigger: "axis", | ||
formatter: function (params) { | ||
let t = params[0].axisValueLabel; | ||
let v = params[0].value[1]; | ||
return `<span>${t}<br/><span style="color:#AC3B2A">REQUESTS</span> <strong>${v}</strong></span>` | ||
} | ||
}, | ||
xAxis: { | ||
type: 'time', | ||
axisLabel: { | ||
formatter: function (value, index) { | ||
return unix2hour(value) // js use milliseconds | ||
}, | ||
color: "#AAA", | ||
}, | ||
splitNumber: 3, | ||
}, | ||
yAxis: { | ||
show: false | ||
}, | ||
grid: { | ||
top: 0, | ||
left: 0, | ||
right: 0, | ||
bottom: 20, | ||
}, | ||
series: [ | ||
{ | ||
type: 'line', | ||
data: series.values || [], | ||
smooth: true, | ||
symbol: "none", | ||
lineStyle: { | ||
normal: { | ||
color: "#AC3B2A", | ||
width: 2, | ||
} | ||
}, | ||
areaStyle: { | ||
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [ | ||
{ | ||
offset: 0, | ||
color: '#AC3B2A' | ||
}, | ||
{ | ||
offset: 1, | ||
color: '#FFF' | ||
} | ||
]) | ||
}, | ||
} | ||
] | ||
}; | ||
chart.setOption(option); | ||
})() | ||
|
||
/// convert unixtimestamp to hour and minute, HH:MM | ||
function unix2hour(v) { | ||
const dateObj = new Date(v) | ||
const hours = dateObj.getHours() >= 10 ? dateObj.getHours() : '0' + dateObj.getHours() | ||
const minutes = dateObj.getMinutes() < 10 ? dateObj.getMinutes() + '0' : dateObj.getMinutes() | ||
const UnixTimeToDate = hours + ':' + minutes | ||
return UnixTimeToDate | ||
} | ||
|
||
function friendly_bytesize(v, is_bytes) { | ||
let bytes_units = ['iB', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']; | ||
let units = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']; | ||
let i = 0; | ||
while (v > 1000) { | ||
v /= 1000; | ||
i++; | ||
} | ||
v = v.toFixed(2); | ||
let u = is_bytes ? bytes_units[i] : units[i]; | ||
return `${v}${u}` | ||
} |