forked from Fankserver/spaceengineers-metrics
/
torch.go
277 lines (240 loc) · 5.84 KB
/
torch.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"encoding/json"
"fmt"
"net/http"
"sync"
"time"
"github.com/pkg/errors"
)
type TorchMetrics struct {
client *http.Client
clientLock sync.Mutex
host string
}
func NewTrochMetrics(host string) (*TorchMetrics, error) {
return &TorchMetrics{
client: &http.Client{},
host: host,
}, nil
}
type TorchMetricServer struct {
Version string
ServerName string
WorldName string
IsReady bool
SimSpeed float64
SimulationCpuLoad float64
TotalTime int64
Players int8
UsedPCU int
MaxPlayers int
MaxFactionsCount int
MaxFloatingObjects int
MaxGridSize int
MaxBlocksPerPlayer int
BlockLimitEnabled string
TotalPCU int
}
func (t *TorchMetrics) Server() (*TorchMetricServer, error) {
res, err := t.client.Get(fmt.Sprintf("%s/metrics/v1/server", t.host))
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.New(res.Status)
}
var server TorchMetricServer
err = json.NewDecoder(res.Body).Decode(&server)
if err != nil {
return nil, err
}
return &server, nil
}
type TorchMetricsProcess struct {
PrivateMemorySize64 int64
VirtualMemorySize64 int64
WorkingSet64 int64
NonpagedSystemMemorySize64 int64
PagedMemorySize64 int64
PagedSystemMemorySize64 int64
PeakPagedMemorySize64 int64
PeakVirtualMemorySize64 int64
PeakWorkingSet64 int64
}
func (t *TorchMetrics) Process() (*TorchMetricsProcess, error) {
res, err := t.client.Get(fmt.Sprintf("%s/metrics/v1/process", t.host))
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.New(res.Status)
}
var server TorchMetricsProcess
err = json.NewDecoder(res.Body).Decode(&server)
if err != nil {
return nil, err
}
return &server, nil
}
type TorchMetricsEvent struct {
Type string
Text string
Tags []string
Occurred time.Time
}
func (t *TorchMetrics) Events() ([]TorchMetricsEvent, error) {
res, err := t.client.Get(fmt.Sprintf("%s/metrics/v1/events", t.host))
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.New(res.Status)
}
var grids []TorchMetricsEvent
err = json.NewDecoder(res.Body).Decode(&grids)
if err != nil {
return nil, err
}
return grids, nil
}
type TorchMetricsSessionGrid struct {
DisplayName string
EntityId int64
GridSize string
BlocksCount int
Mass float64
Position struct {
X float64
Y float64
Z float64
}
LinearSpeed float64
DistanceToPlayer float64
OwnerSteamID int64 `json:"OwnerSteamId"`
OwnerDisplayName string
OwnerFactionTag string
OwnerFactionName string
IsPowered bool
PCU int
IsConcealed bool
DampenersEnabled bool
IsStatic bool
}
func (t *TorchMetrics) SessionGrids() ([]TorchMetricsSessionGrid, error) {
res, err := t.client.Get(fmt.Sprintf("%s/metrics/v1/session/grids", t.host))
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.New(res.Status)
}
var grids []TorchMetricsSessionGrid
err = json.NewDecoder(res.Body).Decode(&grids)
if err != nil {
return nil, err
}
return grids, nil
}
type TorchMetricsSessionAsteroidOrPlanet struct {
DisplayName string
EntityId int64
Position struct {
X float64
Y float64
Z float64
}
}
func (t *TorchMetrics) SessionAsteroids() ([]TorchMetricsSessionAsteroidOrPlanet, error) {
res, err := t.client.Get(fmt.Sprintf("%s/metrics/v1/session/asteroids", t.host))
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.New(res.Status)
}
var grids []TorchMetricsSessionAsteroidOrPlanet
err = json.NewDecoder(res.Body).Decode(&grids)
if err != nil {
return nil, err
}
return grids, nil
}
func (t *TorchMetrics) SessionPlanets() ([]TorchMetricsSessionAsteroidOrPlanet, error) {
res, err := t.client.Get(fmt.Sprintf("%s/metrics/v1/session/planets", t.host))
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.New(res.Status)
}
var grids []TorchMetricsSessionAsteroidOrPlanet
err = json.NewDecoder(res.Body).Decode(&grids)
if err != nil {
return nil, err
}
return grids, nil
}
type TorchMetricsSessionFloatingObject struct {
DisplayName string
EntityId int64
Kind string
Mass float64
Position struct {
X float64
Y float64
Z float64
}
LinearSpeed float64
DistanceToPlayer float64
}
func (t *TorchMetrics) SessionFloatingObjects() ([]TorchMetricsSessionFloatingObject, error) {
res, err := t.client.Get(fmt.Sprintf("%s/metrics/v1/session/floatingObjects", t.host))
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.New(res.Status)
}
var grids []TorchMetricsSessionFloatingObject
err = json.NewDecoder(res.Body).Decode(&grids)
if err != nil {
return nil, err
}
return grids, nil
}
type TorchMetricsSessionFaction struct {
AcceptHumans bool
AutoAcceptMember bool
AutoAcceptPeace bool
EnableFriendlyFire bool
FactionId int64
FounderId int64
MemberCount int
Name string
Tag string
NPCOnly bool
}
func (t *TorchMetrics) SessionFactions() ([]TorchMetricsSessionFaction, error) {
res, err := t.client.Get(fmt.Sprintf("%s/metrics/v1/session/factions", t.host))
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.New(res.Status)
}
var grids []TorchMetricsSessionFaction
err = json.NewDecoder(res.Body).Decode(&grids)
if err != nil {
return nil, err
}
return grids, nil
}