-
Notifications
You must be signed in to change notification settings - Fork 4
/
cpu_query.go
72 lines (65 loc) · 1.75 KB
/
cpu_query.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
67
68
69
70
71
72
package psutilsql
import (
"github.com/noborus/trdsql"
"github.com/shirou/gopsutil/cpu"
)
// CPUTimeReader returns cpu.Times result as trdsql.SliceReader.
func CPUTimeReader(total bool) (*trdsql.SliceReader, error) {
v, err := cpu.Times(!total)
if err != nil {
return nil, err
}
return trdsql.NewSliceReader(psCPUTime, v), nil
}
// CPUInfoReader returns cpu.Info result as trdsql.SliceReader.
func CPUInfoReader() (*trdsql.SliceReader, error) {
v, err := cpu.Info()
if err != nil {
return nil, err
}
return trdsql.NewSliceReader(psCPUInfo, v), nil
}
// CPUPercentReader returns cpu.Percent result as trdsql.SliceReader.
func CPUPercentReader(total bool) (*trdsql.SliceReader, error) {
v, err := cpu.Percent(0, !total)
if err != nil {
return nil, err
}
return trdsql.NewSliceReader(psCPUPercent, v), nil
}
// CPUTimeQuery executes SQL on cpu.Time.
func CPUTimeQuery(total bool, query string, w trdsql.Writer) error {
reader, err := CPUTimeReader(total)
if err != nil {
return err
}
defaultQuery := "SELECT * FROM " + psCPUTime + " ORDER BY cpu"
if query == "" {
query = defaultQuery
}
return readerExec(reader, query, w)
}
// CPUInfoQuery executes SQL on cpu.Info.
func CPUInfoQuery(query string, w trdsql.Writer) error {
reader, err := CPUInfoReader()
if err != nil {
return err
}
defaultQuery := "SELECT * FROM " + psCPUInfo + " ORDER BY cpu"
if query == "" {
query = defaultQuery
}
return readerExec(reader, query, w)
}
// CPUPercentQuery executes SQL on cpu.Percent.
func CPUPercentQuery(total bool, query string, w trdsql.Writer) error {
reader, err := CPUPercentReader(total)
if err != nil {
return err
}
defaultQuery := "SELECT * FROM " + psCPUPercent
if query == "" {
query = defaultQuery
}
return readerExec(reader, query, w)
}