2018-02-19 22:56:39 +00:00
|
|
|
<template>
|
|
|
|
<div class="cpu-memory">
|
2018-06-09 02:29:50 +00:00
|
|
|
<svg :viewBox="`0 0 ${ viewBoxX } ${ viewBoxY }`">
|
2018-02-19 22:56:39 +00:00
|
|
|
<defs>
|
|
|
|
<linearGradient :id="cpuGradientId" x1="0" x2="0" y1="1" y2="0">
|
|
|
|
<stop offset="0%" stop-color="hsl(180, 80%, 70%)"></stop>
|
|
|
|
<stop offset="100%" stop-color="hsl(0, 80%, 70%)"></stop>
|
|
|
|
</linearGradient>
|
|
|
|
<mask :id="cpuMaskId" x="0" y="0" :width="viewBoxX" :height="viewBoxY">
|
|
|
|
<polygon
|
|
|
|
:points="cpuPolygonPoints"
|
|
|
|
fill="#fff"
|
|
|
|
fill-opacity="0.5"/>
|
|
|
|
<polyline
|
|
|
|
:points="cpuPolylinePoints"
|
|
|
|
fill="none"
|
|
|
|
stroke="#fff"
|
|
|
|
stroke-width="1"/>
|
2018-06-09 02:29:50 +00:00
|
|
|
<circle
|
|
|
|
:cx="cpuHeadX"
|
|
|
|
:cy="cpuHeadY"
|
|
|
|
r="1.5"
|
|
|
|
fill="#fff"/>
|
2018-02-19 22:56:39 +00:00
|
|
|
</mask>
|
|
|
|
</defs>
|
|
|
|
<rect
|
2018-06-09 02:29:50 +00:00
|
|
|
x="-2" y="-2"
|
|
|
|
:width="viewBoxX + 4" :height="viewBoxY + 4"
|
2018-02-19 22:56:39 +00:00
|
|
|
:style="`stroke: none; fill: url(#${ cpuGradientId }); mask: url(#${ cpuMaskId })`"/>
|
|
|
|
<text x="1" y="5">CPU <tspan>{{ cpuP }}%</tspan></text>
|
|
|
|
</svg>
|
2018-06-09 02:29:50 +00:00
|
|
|
<svg :viewBox="`0 0 ${ viewBoxX } ${ viewBoxY }`">
|
2018-02-19 22:56:39 +00:00
|
|
|
<defs>
|
|
|
|
<linearGradient :id="memGradientId" x1="0" x2="0" y1="1" y2="0">
|
|
|
|
<stop offset="0%" stop-color="hsl(180, 80%, 70%)"></stop>
|
|
|
|
<stop offset="100%" stop-color="hsl(0, 80%, 70%)"></stop>
|
|
|
|
</linearGradient>
|
|
|
|
<mask :id="memMaskId" x="0" y="0" :width="viewBoxX" :height="viewBoxY">
|
|
|
|
<polygon
|
|
|
|
:points="memPolygonPoints"
|
|
|
|
fill="#fff"
|
|
|
|
fill-opacity="0.5"/>
|
|
|
|
<polyline
|
|
|
|
:points="memPolylinePoints"
|
|
|
|
fill="none"
|
|
|
|
stroke="#fff"
|
|
|
|
stroke-width="1"/>
|
2018-06-09 02:29:50 +00:00
|
|
|
<circle
|
|
|
|
:cx="memHeadX"
|
|
|
|
:cy="memHeadY"
|
|
|
|
r="1.5"
|
|
|
|
fill="#fff"/>
|
2018-02-19 22:56:39 +00:00
|
|
|
</mask>
|
|
|
|
</defs>
|
|
|
|
<rect
|
2018-06-09 02:29:50 +00:00
|
|
|
x="-2" y="-2"
|
|
|
|
:width="viewBoxX + 4" :height="viewBoxY + 4"
|
2018-02-19 22:56:39 +00:00
|
|
|
:style="`stroke: none; fill: url(#${ memGradientId }); mask: url(#${ memMaskId })`"/>
|
|
|
|
<text x="1" y="5">MEM <tspan>{{ memP }}%</tspan></text>
|
|
|
|
</svg>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
import Vue from 'vue';
|
2018-02-21 15:07:37 +00:00
|
|
|
import * as uuid from 'uuid';
|
2018-02-19 22:56:39 +00:00
|
|
|
|
|
|
|
export default Vue.extend({
|
|
|
|
props: ['connection'],
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
viewBoxX: 50,
|
|
|
|
viewBoxY: 30,
|
|
|
|
stats: [],
|
|
|
|
cpuGradientId: uuid(),
|
|
|
|
cpuMaskId: uuid(),
|
|
|
|
memGradientId: uuid(),
|
|
|
|
memMaskId: uuid(),
|
|
|
|
cpuPolylinePoints: '',
|
|
|
|
memPolylinePoints: '',
|
|
|
|
cpuPolygonPoints: '',
|
|
|
|
memPolygonPoints: '',
|
2018-06-09 02:29:50 +00:00
|
|
|
cpuHeadX: null,
|
|
|
|
cpuHeadY: null,
|
|
|
|
memHeadX: null,
|
|
|
|
memHeadY: null,
|
2018-02-19 22:56:39 +00:00
|
|
|
cpuP: '',
|
|
|
|
memP: ''
|
|
|
|
};
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.connection.on('stats', this.onStats);
|
2018-06-08 16:45:25 +00:00
|
|
|
this.connection.on('statsLog', this.onStatsLog);
|
2018-10-09 06:08:31 +00:00
|
|
|
this.connection.send('requestLog', {
|
2018-06-08 16:45:25 +00:00
|
|
|
id: Math.random().toString()
|
|
|
|
});
|
2018-02-19 22:56:39 +00:00
|
|
|
},
|
|
|
|
beforeDestroy() {
|
|
|
|
this.connection.off('stats', this.onStats);
|
2018-06-08 16:45:25 +00:00
|
|
|
this.connection.off('statsLog', this.onStatsLog);
|
2018-02-19 22:56:39 +00:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
onStats(stats) {
|
|
|
|
this.stats.push(stats);
|
|
|
|
if (this.stats.length > 50) this.stats.shift();
|
|
|
|
|
2018-06-09 02:29:50 +00:00
|
|
|
const cpuPolylinePoints = this.stats.map((s, i) => [this.viewBoxX - ((this.stats.length - 1) - i), (1 - s.cpu_usage) * this.viewBoxY]);
|
|
|
|
const memPolylinePoints = this.stats.map((s, i) => [this.viewBoxX - ((this.stats.length - 1) - i), (1 - (s.mem.used / s.mem.total)) * this.viewBoxY]);
|
|
|
|
this.cpuPolylinePoints = cpuPolylinePoints.map(xy => `${xy[0]},${xy[1]}`).join(' ');
|
|
|
|
this.memPolylinePoints = memPolylinePoints.map(xy => `${xy[0]},${xy[1]}`).join(' ');
|
2018-02-19 22:56:39 +00:00
|
|
|
|
2018-07-27 10:12:16 +00:00
|
|
|
this.cpuPolygonPoints = `${this.viewBoxX - (this.stats.length - 1)},${this.viewBoxY} ${this.cpuPolylinePoints} ${this.viewBoxX},${this.viewBoxY}`;
|
|
|
|
this.memPolygonPoints = `${this.viewBoxX - (this.stats.length - 1)},${this.viewBoxY} ${this.memPolylinePoints} ${this.viewBoxX},${this.viewBoxY}`;
|
2018-02-19 22:56:39 +00:00
|
|
|
|
2018-06-09 02:29:50 +00:00
|
|
|
this.cpuHeadX = cpuPolylinePoints[cpuPolylinePoints.length - 1][0];
|
|
|
|
this.cpuHeadY = cpuPolylinePoints[cpuPolylinePoints.length - 1][1];
|
|
|
|
this.memHeadX = memPolylinePoints[memPolylinePoints.length - 1][0];
|
|
|
|
this.memHeadY = memPolylinePoints[memPolylinePoints.length - 1][1];
|
|
|
|
|
2018-02-19 22:56:39 +00:00
|
|
|
this.cpuP = (stats.cpu_usage * 100).toFixed(0);
|
|
|
|
this.memP = (stats.mem.used / stats.mem.total * 100).toFixed(0);
|
2018-06-08 16:45:25 +00:00
|
|
|
},
|
|
|
|
onStatsLog(statsLog) {
|
2018-08-18 18:32:01 +00:00
|
|
|
statsLog.reverse().forEach(stats => this.onStats(stats));
|
2018-02-19 22:56:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="stylus" scoped>
|
2018-09-28 02:18:56 +00:00
|
|
|
.cpu-memory
|
2018-02-19 22:56:39 +00:00
|
|
|
> svg
|
|
|
|
display block
|
|
|
|
padding 10px
|
|
|
|
width 50%
|
|
|
|
float left
|
|
|
|
|
|
|
|
&:first-child
|
|
|
|
padding-right 5px
|
|
|
|
|
|
|
|
&:last-child
|
|
|
|
padding-left 5px
|
|
|
|
|
|
|
|
> text
|
|
|
|
font-size 5px
|
2018-09-28 02:18:56 +00:00
|
|
|
fill var(--chartCaption)
|
2018-02-19 22:56:39 +00:00
|
|
|
|
|
|
|
> tspan
|
|
|
|
opacity 0.5
|
|
|
|
|
|
|
|
&:after
|
|
|
|
content ""
|
|
|
|
display block
|
|
|
|
clear both
|
2018-04-19 22:45:37 +00:00
|
|
|
|
2018-02-19 22:56:39 +00:00
|
|
|
</style>
|