-
Notifications
You must be signed in to change notification settings - Fork 4
/
load_query.go
48 lines (44 loc) · 1.06 KB
/
load_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
package psutilsql
import (
"github.com/noborus/trdsql"
"github.com/shirou/gopsutil/load"
)
// LoadAvgReader returns load.Avg result as trdsql.SliceReader.
func LoadAvgReader() (*trdsql.SliceReader, error) {
v, err := load.Avg()
if err != nil {
return nil, err
}
return trdsql.NewSliceReader(psLoadAvg, v), nil
}
// LoadMiscReader returns load.Misc result as trdsql.SliceReader.
func LoadMiscReader() (*trdsql.SliceReader, error) {
v, err := load.Misc()
if err != nil {
return nil, err
}
return trdsql.NewSliceReader(psLoadMisc, v), nil
}
// LoadQuery executes SQL on Load.Avg or Load.Misc.
func LoadQuery(misc bool, query string, w trdsql.Writer) error {
if misc {
defaultQuery := "SELECT * FROM " + psLoadMisc
if query == "" {
query = defaultQuery
}
reader, err := LoadMiscReader()
if err != nil {
return err
}
return readerExec(reader, query, w)
}
defaultQuery := "SELECT * FROM " + psLoadAvg
if query == "" {
query = defaultQuery
}
reader, err := LoadAvgReader()
if err != nil {
return err
}
return readerExec(reader, query, w)
}