Skip to content

Commit be5a910

Browse files
committed
Dont refresh server statuses unless the page is active
Reduces the number of polling requests happening on the server by only sending those requests if the user is actively viewing the dashboard. There was no point in updating the resource usage if no one is viewing it. After 30 seconds away from the window when a user comes back it will update instantenously, otherwise it'll just update after 5 seconds.
1 parent 02b29a6 commit be5a910

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"jquery": "^3.3.1",
2727
"jwt-decode": "^2.2.0",
2828
"lodash": "^4.17.5",
29+
"luxon": "^1.2.1",
2930
"postcss": "^6.0.21",
3031
"postcss-import": "^11.1.0",
3132
"postcss-preset-env": "^3.4.0",
@@ -35,6 +36,7 @@
3536
"vue": "^2.5.7",
3637
"vue-axios": "^2.1.1",
3738
"vue-devtools": "^3.1.9",
39+
"vue-feather-icons": "^4.7.1",
3840
"vue-loader": "^14.2.2",
3941
"vue-mc": "^0.2.4",
4042
"vue-router": "^3.0.1",

resources/assets/scripts/components/dashboard/Dashboard.vue

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
</template>
2626

2727
<script>
28+
import { DateTime } from 'luxon';
2829
import { ServerCollection } from '../../models/server';
2930
import _ from 'lodash';
3031
import Flash from '../Flash';
@@ -35,6 +36,8 @@
3536
components: { ServerBox, Flash },
3637
data: function () {
3738
return {
39+
backgroundedAt: DateTime.local(),
40+
documentVisible: true,
3841
loading: true,
3942
search: '',
4043
servers: new ServerCollection,
@@ -46,16 +49,19 @@
4649
*/
4750
created: function () {
4851
this.loadServers();
52+
53+
document.addEventListener('visibilitychange', () => {
54+
this.documentVisible = document.visibilityState === 'visible';
55+
this._handleDocumentVisibilityChange(this.documentVisible);
56+
});
4957
},
5058
5159
/**
5260
* Once the page is mounted set a function to run every 5 seconds that will
5361
* iterate through the visible servers and fetch their resource usage.
5462
*/
5563
mounted: function () {
56-
setInterval(() => {
57-
this.servers.each(this.getResourceUse)
58-
}, 10000);
64+
this._iterateServerResourceUse();
5965
},
6066
6167
methods: {
@@ -124,6 +130,37 @@
124130
console.error(err);
125131
});
126132
},
133+
134+
/**
135+
* Iterates over all of the active servers and gets their resource usage.
136+
*
137+
* @private
138+
*/
139+
_iterateServerResourceUse: function (initialTimeout = 5000) {
140+
window.setTimeout(() => {
141+
if (this.documentVisible) {
142+
return window.setTimeout(this._iterateServerResourceUse(), 5000);
143+
}
144+
}, initialTimeout);
145+
},
146+
147+
/**
148+
* Handle changes to document visibilty to keep server statuses updated properly.
149+
*
150+
* @param {Boolean} isVisible
151+
* @private
152+
*/
153+
_handleDocumentVisibilityChange: function (isVisible) {
154+
if (!isVisible) {
155+
this.backgroundedAt = DateTime.local();
156+
return;
157+
}
158+
159+
// If it has been more than 30 seconds since this window was put into the background
160+
// lets go ahead and refresh all of the listed servers so that they have fresh stats.
161+
const diff = DateTime.local().diff(this.backgroundedAt, 'seconds');
162+
this._iterateServerResourceUse(diff.seconds > 30 ? 1 : 5000);
163+
},
127164
}
128165
};
129166
</script>

resources/assets/scripts/components/dashboard/ServerBox.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</div>
2525
<div class="inline-block border border-grey-lighter border-l-0 border-r-0 p-4 flex-1">
2626
<span class="font-bold text-xl">{{ memory > 0 ? memory : '&mdash;' }}</span>
27-
<span class="font-light text-sm">Mb</span>
27+
<span class="font-light text-sm">MB</span>
2828
</div>
2929
</div>
3030
<div class="flex items-center">

yarn.lock

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,10 @@ babel-helper-replace-supers@^6.24.1:
505505
babel-traverse "^6.24.1"
506506
babel-types "^6.24.1"
507507

508+
babel-helper-vue-jsx-merge-props@^2.0.2:
509+
version "2.0.3"
510+
resolved "https://registry.yarnpkg.com/babel-helper-vue-jsx-merge-props/-/babel-helper-vue-jsx-merge-props-2.0.3.tgz#22aebd3b33902328e513293a8e4992b384f9f1b6"
511+
508512
babel-helpers@^6.24.1:
509513
version "6.24.1"
510514
resolved "https://registry.yarnpkg.com/babel-helpers/-/babel-helpers-6.24.1.tgz#3471de9caec388e5c850e597e58a26ddf37602b2"
@@ -3424,6 +3428,10 @@ lru-cache@^4.0.1, lru-cache@^4.1.1:
34243428
pseudomap "^1.0.2"
34253429
yallist "^2.1.2"
34263430

3431+
luxon@^1.2.1:
3432+
version "1.2.1"
3433+
resolved "https://registry.yarnpkg.com/luxon/-/luxon-1.2.1.tgz#5c4948d141939c2b2820f0c3a99276932245efb1"
3434+
34273435
macaddress@^0.2.8:
34283436
version "0.2.8"
34293437
resolved "https://registry.yarnpkg.com/macaddress/-/macaddress-0.2.8.tgz#5904dc537c39ec6dbefeae902327135fa8511f12"
@@ -5985,6 +5993,12 @@ vue-devtools@^3.1.9:
59855993
version "3.1.9"
59865994
resolved "https://registry.yarnpkg.com/vue-devtools/-/vue-devtools-3.1.9.tgz#283b458c6853f569a987da0092e7c52e8243a436"
59875995

5996+
vue-feather-icons@^4.7.1:
5997+
version "4.7.1"
5998+
resolved "https://registry.yarnpkg.com/vue-feather-icons/-/vue-feather-icons-4.7.1.tgz#d8c55fbee7c9ad59689ebbaf07ad1e2f1e5c37da"
5999+
dependencies:
6000+
babel-helper-vue-jsx-merge-props "^2.0.2"
6001+
59886002
vue-hot-reload-api@^2.2.0:
59896003
version "2.3.0"
59906004
resolved "https://registry.yarnpkg.com/vue-hot-reload-api/-/vue-hot-reload-api-2.3.0.tgz#97976142405d13d8efae154749e88c4e358cf926"

0 commit comments

Comments
 (0)