2018-05-27 06:17:02 +00:00
|
|
|
<template>
|
|
|
|
<div>
|
2018-06-03 06:15:10 +00:00
|
|
|
<navigation/>
|
|
|
|
<div class="container">
|
|
|
|
<flash container="mt-4"/>
|
|
|
|
<div class="server-search animate fadein">
|
|
|
|
<input type="text"
|
|
|
|
:placeholder="$t('dashboard.index.search')"
|
|
|
|
@input="onChange"
|
2018-07-16 00:53:40 +00:00
|
|
|
v-model="searchTerm"
|
2018-06-03 06:15:10 +00:00
|
|
|
ref="search"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div v-if="this.loading" class="my-4 animate fadein">
|
|
|
|
<div class="text-center h-16">
|
2018-07-19 05:48:19 +00:00
|
|
|
<span class="spinner spinner-xl spinner-thick blue"></span>
|
2018-06-03 06:15:10 +00:00
|
|
|
</div>
|
2018-05-28 23:28:36 +00:00
|
|
|
</div>
|
2018-06-03 06:15:10 +00:00
|
|
|
<transition-group class="w-full m-auto mt-4 animate fadein sm:flex flex-wrap content-start" v-else>
|
|
|
|
<server-box
|
2018-06-06 06:42:34 +00:00
|
|
|
v-for="(server, index) in servers"
|
2018-06-03 06:15:10 +00:00
|
|
|
v-bind:key="index"
|
|
|
|
v-bind:server="server"
|
|
|
|
/>
|
|
|
|
</transition-group>
|
2018-05-28 23:28:36 +00:00
|
|
|
</div>
|
2018-05-27 06:17:02 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2018-06-06 06:00:01 +00:00
|
|
|
import Server from '../../models/server';
|
2018-06-16 22:05:36 +00:00
|
|
|
import debounce from 'lodash/debounce';
|
2018-06-16 21:11:58 +00:00
|
|
|
import differenceInSeconds from 'date-fns/difference_in_seconds';
|
2018-05-28 23:28:36 +00:00
|
|
|
import Flash from '../Flash';
|
2018-06-03 00:41:06 +00:00
|
|
|
import ServerBox from './ServerBox';
|
2018-06-03 06:15:10 +00:00
|
|
|
import Navigation from '../core/Navigation';
|
2018-07-16 00:53:40 +00:00
|
|
|
import isObject from 'lodash/isObject';
|
|
|
|
import {mapState} from 'vuex';
|
2018-05-27 06:17:02 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'dashboard',
|
2018-06-03 06:15:10 +00:00
|
|
|
components: { Navigation, ServerBox, Flash },
|
2018-05-27 06:17:02 +00:00
|
|
|
data: function () {
|
|
|
|
return {
|
2018-06-16 21:11:58 +00:00
|
|
|
backgroundedAt: new Date(),
|
2018-06-03 05:23:54 +00:00
|
|
|
documentVisible: true,
|
2018-07-16 00:53:40 +00:00
|
|
|
loading: false,
|
2018-05-27 06:17:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-06-03 02:08:53 +00:00
|
|
|
/**
|
2018-07-16 00:53:40 +00:00
|
|
|
* Start loading the servers before the DOM $.el is created. If we already have servers
|
|
|
|
* stored in vuex shows those and don't fire another API call just to load them again.
|
2018-06-03 02:08:53 +00:00
|
|
|
*/
|
|
|
|
created: function () {
|
2018-07-16 00:53:40 +00:00
|
|
|
if (this.servers.length === 0) {
|
|
|
|
this.loadServers();
|
|
|
|
}
|
2018-06-03 05:23:54 +00:00
|
|
|
|
|
|
|
document.addEventListener('visibilitychange', () => {
|
|
|
|
this.documentVisible = document.visibilityState === 'visible';
|
|
|
|
this._handleDocumentVisibilityChange(this.documentVisible);
|
|
|
|
});
|
2018-05-27 06:17:02 +00:00
|
|
|
},
|
|
|
|
|
2018-06-03 02:08:53 +00:00
|
|
|
/**
|
2018-07-16 01:11:29 +00:00
|
|
|
* Once the page is mounted set a function to run every 10 seconds that will
|
2018-06-03 02:08:53 +00:00
|
|
|
* iterate through the visible servers and fetch their resource usage.
|
|
|
|
*/
|
|
|
|
mounted: function () {
|
2018-07-22 05:43:16 +00:00
|
|
|
this.$refs.search.focus();
|
|
|
|
|
2018-07-16 01:11:29 +00:00
|
|
|
window.setTimeout(() => {
|
|
|
|
this._iterateServerResourceUse();
|
|
|
|
}, 5000);
|
2018-06-03 02:08:53 +00:00
|
|
|
},
|
|
|
|
|
2018-07-16 00:53:40 +00:00
|
|
|
computed: {
|
|
|
|
...mapState('dashboard', ['servers']),
|
|
|
|
searchTerm: {
|
|
|
|
get: function () {
|
|
|
|
return this.$store.getters['dashboard/getSearchTerm'];
|
|
|
|
},
|
|
|
|
set: function (value) {
|
|
|
|
this.$store.dispatch('dashboard/setSearchTerm', value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-05-27 06:17:02 +00:00
|
|
|
methods: {
|
|
|
|
/**
|
|
|
|
* Load the user's servers and render them onto the dashboard.
|
|
|
|
*/
|
2018-07-16 00:53:40 +00:00
|
|
|
loadServers: function () {
|
2018-05-28 23:28:36 +00:00
|
|
|
this.loading = true;
|
2018-07-16 00:53:40 +00:00
|
|
|
this.$store.dispatch('dashboard/loadServers')
|
2018-05-28 23:28:36 +00:00
|
|
|
.finally(() => {
|
|
|
|
this.clearFlashes();
|
|
|
|
})
|
2018-07-16 00:53:40 +00:00
|
|
|
.then(() => {
|
2018-06-06 06:00:01 +00:00
|
|
|
if (this.servers.length === 0) {
|
2018-05-28 23:28:36 +00:00
|
|
|
this.info(this.$t('dashboard.index.no_matches'));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
const response = err.response;
|
2018-07-16 00:53:40 +00:00
|
|
|
if (response.data && isObject(response.data.errors)) {
|
2018-06-12 03:42:01 +00:00
|
|
|
response.data.errors.forEach(error => {
|
2018-05-28 23:28:36 +00:00
|
|
|
this.error(error.detail);
|
|
|
|
});
|
|
|
|
}
|
2018-05-27 06:17:02 +00:00
|
|
|
})
|
2018-05-28 23:28:36 +00:00
|
|
|
.finally(() => {
|
|
|
|
this.loading = false;
|
2018-05-27 06:17:02 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a search for servers but only call the search function every 500ms
|
|
|
|
* at the fastest.
|
|
|
|
*/
|
2018-06-16 22:05:36 +00:00
|
|
|
onChange: debounce(function () {
|
2018-07-16 00:53:40 +00:00
|
|
|
this.loadServers();
|
2018-05-27 06:17:02 +00:00
|
|
|
}, 500),
|
2018-06-03 02:08:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
});
|
|
|
|
},
|
2018-06-03 05:23:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterates over all of the active servers and gets their resource usage.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
2018-07-16 01:11:29 +00:00
|
|
|
_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);
|
|
|
|
}
|
2018-06-03 05:23:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle changes to document visibilty to keep server statuses updated properly.
|
|
|
|
*
|
|
|
|
* @param {Boolean} isVisible
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_handleDocumentVisibilityChange: function (isVisible) {
|
|
|
|
if (!isVisible) {
|
2018-06-16 21:11:58 +00:00
|
|
|
this.backgroundedAt = new Date();
|
2018-06-03 05:23:54 +00:00
|
|
|
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.
|
2018-06-16 21:11:58 +00:00
|
|
|
const diff = differenceInSeconds(new Date(), this.backgroundedAt);
|
2018-07-16 01:11:29 +00:00
|
|
|
if (diff > 30) {
|
|
|
|
this._iterateServerResourceUse(false);
|
|
|
|
}
|
2018-06-03 05:23:54 +00:00
|
|
|
},
|
2018-05-27 06:17:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|