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"
|
|
|
|
v-model="search"
|
|
|
|
ref="search"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div v-if="this.loading" class="my-4 animate fadein">
|
|
|
|
<div class="text-center h-16">
|
|
|
|
<span class="spinner spinner-xl"></span>
|
|
|
|
</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
|
|
|
|
v-for="(server, index) in servers.models"
|
|
|
|
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-03 05:23:54 +00:00
|
|
|
import { DateTime } from 'luxon';
|
2018-05-28 21:11:23 +00:00
|
|
|
import { ServerCollection } from '../../models/server';
|
2018-05-27 06:17:02 +00:00
|
|
|
import _ from 'lodash';
|
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-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-03 05:23:54 +00:00
|
|
|
backgroundedAt: DateTime.local(),
|
|
|
|
documentVisible: true,
|
2018-05-28 23:28:36 +00:00
|
|
|
loading: true,
|
2018-05-27 06:17:02 +00:00
|
|
|
search: '',
|
2018-05-28 21:11:23 +00:00
|
|
|
servers: new ServerCollection,
|
2018-05-27 06:17:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-06-03 02:08:53 +00:00
|
|
|
/**
|
|
|
|
* Start loading the servers before the DOM $.el is created.
|
|
|
|
*/
|
|
|
|
created: function () {
|
2018-05-27 06:17:02 +00:00
|
|
|
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
|
|
|
/**
|
|
|
|
* Once the page is mounted set a function to run every 5 seconds that will
|
|
|
|
* iterate through the visible servers and fetch their resource usage.
|
|
|
|
*/
|
|
|
|
mounted: function () {
|
2018-06-03 05:23:54 +00:00
|
|
|
this._iterateServerResourceUse();
|
2018-06-03 02:08:53 +00:00
|
|
|
},
|
|
|
|
|
2018-05-27 06:17:02 +00:00
|
|
|
methods: {
|
|
|
|
/**
|
|
|
|
* Load the user's servers and render them onto the dashboard.
|
|
|
|
*
|
|
|
|
* @param {string} query
|
|
|
|
*/
|
|
|
|
loadServers: function (query = '') {
|
2018-05-28 23:28:36 +00:00
|
|
|
this.loading = true;
|
2018-05-28 20:23:40 +00:00
|
|
|
window.axios.get(this.route('api.client.index'), {
|
2018-05-27 06:17:02 +00:00
|
|
|
params: { query },
|
|
|
|
})
|
2018-05-28 23:28:36 +00:00
|
|
|
.finally(() => {
|
|
|
|
this.clearFlashes();
|
|
|
|
})
|
2018-05-28 21:11:23 +00:00
|
|
|
.then(response => {
|
|
|
|
this.servers = new ServerCollection;
|
|
|
|
response.data.data.forEach(obj => {
|
2018-06-03 02:08:53 +00:00
|
|
|
this.getResourceUse(
|
|
|
|
this.servers.add(obj.attributes)
|
|
|
|
);
|
2018-05-28 20:23:40 +00:00
|
|
|
});
|
2018-05-28 23:28:36 +00:00
|
|
|
|
|
|
|
if (this.servers.models.length === 0) {
|
|
|
|
this.info(this.$t('dashboard.index.no_matches'));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
const response = err.response;
|
|
|
|
if (response.data && _.isObject(response.data.errors)) {
|
|
|
|
response.data.errors.forEach(function (error) {
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
onChange: _.debounce(function () {
|
|
|
|
this.loadServers(this.$data.search);
|
|
|
|
}, 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);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
},
|
2018-06-03 05:23:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterates over all of the active servers and gets their resource usage.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_iterateServerResourceUse: function (initialTimeout = 5000) {
|
|
|
|
window.setTimeout(() => {
|
|
|
|
if (this.documentVisible) {
|
|
|
|
return window.setTimeout(this._iterateServerResourceUse(), 5000);
|
|
|
|
}
|
|
|
|
}, initialTimeout);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle changes to document visibilty to keep server statuses updated properly.
|
|
|
|
*
|
|
|
|
* @param {Boolean} isVisible
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_handleDocumentVisibilityChange: function (isVisible) {
|
|
|
|
if (!isVisible) {
|
|
|
|
this.backgroundedAt = DateTime.local();
|
|
|
|
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 = DateTime.local().diff(this.backgroundedAt, 'seconds');
|
|
|
|
this._iterateServerResourceUse(diff.seconds > 30 ? 1 : 5000);
|
|
|
|
},
|
2018-05-27 06:17:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|