Significantly less atrocious resource checking for servers...

This commit is contained in:
Dane Everitt 2018-08-18 21:02:58 -07:00
parent dc52e238ac
commit 68b23de55d
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
2 changed files with 95 additions and 82 deletions

View file

@ -28,9 +28,7 @@
</template>
<script>
import Server from '../../models/server';
import debounce from 'lodash/debounce';
import differenceInSeconds from 'date-fns/difference_in_seconds';
import Flash from '../Flash';
import ServerBox from './ServerBox';
import Navigation from '../core/Navigation';
@ -56,11 +54,6 @@
if (this.servers.length === 0) {
this.loadServers();
}
document.addEventListener('visibilitychange', () => {
this.documentVisible = document.visibilityState === 'visible';
this._handleDocumentVisibilityChange(this.documentVisible);
});
},
/**
@ -69,10 +62,6 @@
*/
mounted: function () {
this.$refs.search.focus();
window.setTimeout(() => {
this._iterateServerResourceUse();
}, 5000);
},
computed: {
@ -123,66 +112,6 @@
onChange: debounce(function () {
this.loadServers();
}, 500),
/**
* Get resource usage for an individual server for rendering purposes.
*
* @param {Server} server
*/
getResourceUse: function (server) {
window.axios.get(this.route('api.client.servers.resources', { server: server.identifier }))
.then(response => {
if (!(response.data instanceof Object)) {
throw new Error('Received an invalid response object back from status endpoint.');
}
window.events.$emit(`server:${server.uuid}::resources`, response.data.attributes);
});
},
/**
* Iterates over all of the active servers and gets their resource usage.
*
* @private
*/
_iterateServerResourceUse: function (loop = true) {
// Try again in 10 seconds, window is not in the foreground.
if (!this.documentVisible && loop) {
window.setTimeout(() => {
this._iterateServerResourceUse();
}, 10000);
}
this.servers.forEach(server => {
this.getResourceUse(server);
});
if (loop) {
window.setTimeout(() => {
this._iterateServerResourceUse();
}, 10000);
}
},
/**
* Handle changes to document visibilty to keep server statuses updated properly.
*
* @param {Boolean} isVisible
* @private
*/
_handleDocumentVisibilityChange: function (isVisible) {
if (!isVisible) {
this.backgroundedAt = new Date();
return;
}
// If it has been more than 30 seconds since this window was put into the background
// lets go ahead and refresh all of the listed servers so that they have fresh stats.
const diff = differenceInSeconds(new Date(), this.backgroundedAt);
if (diff > 30) {
this._iterateServerResourceUse(false);
}
},
}
};
</script>