-
Notifications
You must be signed in to change notification settings - Fork 4
/
query.go
107 lines (101 loc) · 2.53 KB
/
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package psutilsql
import (
"strings"
"github.com/noborus/trdsql"
)
const (
psCPUTime = "cputime"
psCPUInfo = "cpuinfo"
psCPUPercent = "cpupercent"
psDiskPartition = "diskpartition"
psDiskUsage = "diskusage"
psDocker = "docker"
psHostInfo = "hostinfo"
psHostUser = "hostuser"
psHostTemperature = "hosttemperature"
psLoadAvg = "loadavg"
psLoadMisc = "loadmisc"
psVirtualMemory = "virtualmemory"
psSwapMemory = "swapmemory"
psNet = "net"
psProcess = "process"
psProcessEx = "processex"
psWinservices = "winservices"
)
func psutilReader(tableName string) Reader {
var err error
var reader Reader
switch strings.ToLower(tableName) {
case psCPUTime:
reader, err = CPUTimeReader(false)
case psCPUInfo:
reader, err = CPUInfoReader()
case psCPUPercent:
reader, err = CPUPercentReader(false)
case psDiskPartition:
reader, err = DiskPartitionReader(true)
case psDiskUsage:
reader, err = DiskUsageReader("/")
case psDocker:
reader, err = DockerReader()
case psHostInfo:
reader, err = HostInfoReader()
case psHostUser:
reader, err = HostUsersReader()
case psHostTemperature:
reader, err = HostTemperatureReader()
case psLoadAvg:
reader, err = LoadAvgReader()
case psLoadMisc:
reader, err = LoadMiscReader()
case psVirtualMemory:
reader, err = VirtualMemoryReader()
case psSwapMemory:
reader, err = SwapMemoryReader()
case psNet:
reader, err = NetReader()
case psProcess:
reader, err = NewProcessReader(false)
case psProcessEx:
reader, err = NewProcessReader(true)
case "pstable":
reader, err = TableReader()
default:
reader = nil
}
if err != nil {
return nil
}
return reader
}
func readerExec(reader Reader, query string, writer trdsql.Writer) error {
importer, err := NewMultiImporter(reader)
if err != nil {
return err
}
trd := trdsql.NewTRDSQL(importer, trdsql.NewExporter(writer))
err = trd.Exec(query)
return err
}
// QueryExec actually executes the passed query and writes it to the writer.
func QueryExec(query string, writer trdsql.Writer) error {
parsedQuery := trdsql.SQLFields(query)
tables, _ := trdsql.TableNames(parsedQuery)
if tables == nil {
return nil
}
var readers []Reader
for _, table := range tables {
reader := psutilReader(table)
if reader != nil {
readers = append(readers, reader)
}
}
importer, err := NewMultiImporter(readers...)
if err != nil {
return err
}
trd := trdsql.NewTRDSQL(importer, trdsql.NewExporter(writer))
err = trd.Exec(query)
return err
}