Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Move redrawer to static js. Apply to the remaining charts #1700

Merged
merged 1 commit into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/server/assets/apikeys/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ <h5 class="modal-title">Statistics for {{$authApp.Name}}</h5>
</main>

<script type="text/javascript">
var chartData = [];
$(() => redrawCharts(chartData, 300));

let $div = $('#apikey_stats_chart');

google.charts.load('current', {
Expand Down Expand Up @@ -194,6 +197,11 @@ <h5 class="modal-title">Statistics for {{$authApp.Name}}</h5>

let chart = new google.visualization.LineChart($div.get(0));
chart.draw(dataTable, options);
chartData.push({
chart: chart,
data: dataTable,
options: options,
});
}

function drawDeviceStats(data) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/assets/realmadmin/_stats_codes.html
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ <h5 class="modal-title">Codes issued, claimed, &amp; invalid</h5>

dashboard.bind(filter, realmChart);
dashboard.draw(dataTable);
redrawCharts.push({
chartData.push({
chart: dashboard,
data: dataTable,
});
Expand Down
8 changes: 4 additions & 4 deletions cmd/server/assets/realmadmin/_stats_keyserver.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ <h5 class="modal-title">Key server stats</h5>

let chart = new google.visualization.LineChart($div.get(0));
chart.draw(dataTable, options);
redrawCharts.push({
chartData.push({
chart: chart,
data: dataTable,
options: options,
Expand Down Expand Up @@ -224,7 +224,7 @@ <h5 class="modal-title">Key server stats</h5>

let chart = new google.visualization.ColumnChart($div.get(0));
chart.draw(dataTable, options);
redrawCharts.push({
chartData.push({
chart: chart,
data: dataTable,
options: options,
Expand Down Expand Up @@ -272,7 +272,7 @@ <h5 class="modal-title">Key server stats</h5>
tekChart = new google.visualization.ColumnChart($div.get(0));
tekData = getTEKDataTable(0);
tekChart.draw(tekData, tekOptions);
redrawCharts.push({
chartData.push({
chart: tekChart,
data: tekData,
options: tekOptions,
Expand Down Expand Up @@ -344,7 +344,7 @@ <h5 class="modal-title">Key server stats</h5>
onsetChart = new google.visualization.ColumnChart($div.get(0));
onsetData = getOnsetDataTable(0);
onsetChart.draw(onsetData, onsetOptions);
redrawCharts.push({
chartData.push({
chart: onsetChart,
data: onsetData,
options: onsetOptions,
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/assets/realmadmin/_stats_tokens.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ <h5 class="modal-title">Tokens claimed &amp; invalid</h5>

let chart = new google.visualization.LineChart($div.get(0));
chart.draw(dataTable, options);
redrawCharts.push({
chartData.push({
chart: chart,
data: dataTable,
options: options,
Expand Down
34 changes: 2 additions & 32 deletions cmd/server/assets/realmadmin/stats.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,38 +41,8 @@ <h1>Realm stats</h1>
</main>

<script type="text/javascript">
var redrawCharts = [];

$(function() {
let redrawPending = false;
let windowWidth = 0;
$(window).resize(function() {
let w = $(window).width();
if (w != windowWidth) {
windowWidth = w;
} else {
return;
}

if (!redrawPending) {
redrawPending = true;
setTimeout(function(){
redraw();
redrawPending = false;
}, 300);
}
});

function redraw(){
let c
for (c of redrawCharts) {
if (c.options) {
c.options.animation = null;
}
c.chart.draw(c.data, c.options)
}
}
});
var chartData = [];
$(() => redrawCharts(chartData, 300));
</script>
</body>
</html>
Expand Down
31 changes: 31 additions & 0 deletions cmd/server/assets/static/js/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -1021,3 +1021,34 @@ function showSuccessfulCode(request, code, line) {
$row.append($('<td/>').text(code.uuid));
$successTableBody.append($row);
}

function redrawCharts(chartsData, timeout) {
let redrawPending = false;
let windowWidth = 0;
$(window).resize(function() {
let w = $(window).width();
if (w != windowWidth) {
windowWidth = w;
} else {
return;
}

if (!redrawPending) {
redrawPending = true;
setTimeout(function() {
redraw();
redrawPending = false;
}, timeout);
}
});

function redraw() {
let c;
for (c of chartsData) {
if (c.options) {
c.options.animation = null;
}
c.chart.draw(c.data, c.options);
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline

8 changes: 8 additions & 0 deletions cmd/server/assets/users/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ <h5 class="modal-title">Codes issued by {{$user.Name}}</h5>
</main>

<script type="text/javascript">
var chartData = [];
$(() => redrawCharts(chartData, 300));

google.charts.load('current', {
packages: ['corechart'],
callback: drawRealmCharts,
Expand Down Expand Up @@ -172,6 +175,11 @@ <h5 class="modal-title">Codes issued by {{$user.Name}}</h5>

let chart = new google.visualization.LineChart($userStatsChart.get(0));
chart.draw(dataTable, options);
chartData.push({
chart: chart,
data: dataTable,
options: options,
});
})
.fail(function(xhr, status, err) {
flash.error('Failed to render realm stats: ' + err);
Expand Down