forked from aebruno/asteroid-top
/
sysinfo.go
66 lines (57 loc) · 1.84 KB
/
sysinfo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2017 Andrew E. Bruno
//
// This file is part of asteroid-top.
//
// asteroid-top is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// asteroid-top is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with asteroid-top. If not, see <http://www.gnu.org/licenses/>.
package model
import (
"fmt"
"time"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
"github.com/therecipe/qt/core"
)
//go:generate qtmoc
type SystemInfoModel struct {
core.QObject
_ string `property:"kernelVersion"`
_ float64 `property:"usedMemory"`
_ float64 `property:"usedDisk"`
_ float64 `property:"usedCpu"`
_ string `property:"loadAvg"`
_ func() `signal:"refresh"`
_ func() `constructor:"init"`
}
// Initialize model
func (model *SystemInfoModel) init() {
// Connect to refresh signal
model.ConnectRefresh(model.refresh)
kernel, _ := host.KernelVersion()
model.SetKernelVersion(kernel)
model.refresh()
}
// Refresh model and update system info properties
func (model *SystemInfoModel) refresh() {
v, _ := mem.VirtualMemory()
d, _ := disk.Usage("/")
l, _ := load.Avg()
c, _ := cpu.Percent(500*time.Millisecond, false)
model.SetUsedMemory(v.UsedPercent)
model.SetUsedDisk(d.UsedPercent)
model.SetUsedCpu(c[0])
model.SetLoadAvg(fmt.Sprintf("%.1f, %.1f, %.1f", l.Load1, l.Load5, l.Load15))
}