/
dumpstate.go
73 lines (63 loc) · 1.9 KB
/
dumpstate.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
package core
// Implementation of (*State).Dump()
import (
"encoding/json"
"time"
"github.com/neondatabase/autoscaling/pkg/api"
)
func shallowCopy[T any](ptr *T) *T {
if ptr == nil {
return nil
} else {
x := *ptr
return &x
}
}
// StateDump provides introspection into the current values of the fields of State
//
// It implements json.Marshaler.
type StateDump struct {
internal state
}
func (d StateDump) MarshalJSON() ([]byte, error) {
return json.Marshal(d.internal)
}
// Dump produces a JSON-serializable copy of the State
func (s *State) Dump() StateDump {
return StateDump{
internal: state{
Debug: s.internal.Debug,
Config: s.internal.Config,
VM: s.internal.VM,
Plugin: s.internal.Plugin.deepCopy(),
Monitor: s.internal.Monitor.deepCopy(),
NeonVM: s.internal.NeonVM.deepCopy(),
Metrics: shallowCopy[Metrics](s.internal.Metrics),
},
}
}
func (s *pluginState) deepCopy() pluginState {
return pluginState{
OngoingRequest: s.OngoingRequest,
LastRequest: shallowCopy[pluginRequested](s.LastRequest),
LastFailureAt: shallowCopy[time.Time](s.LastFailureAt),
Permit: shallowCopy[api.Resources](s.Permit),
}
}
func (s *monitorState) deepCopy() monitorState {
return monitorState{
OngoingRequest: shallowCopy[ongoingMonitorRequest](s.OngoingRequest),
RequestedUpscale: shallowCopy[requestedUpscale](s.RequestedUpscale),
DeniedDownscale: shallowCopy[deniedDownscale](s.DeniedDownscale),
Approved: shallowCopy[api.Resources](s.Approved),
DownscaleFailureAt: shallowCopy[time.Time](s.DownscaleFailureAt),
UpscaleFailureAt: shallowCopy[time.Time](s.UpscaleFailureAt),
}
}
func (s *neonvmState) deepCopy() neonvmState {
return neonvmState{
LastSuccess: shallowCopy[api.Resources](s.LastSuccess),
OngoingRequested: shallowCopy[api.Resources](s.OngoingRequested),
RequestFailedAt: shallowCopy[time.Time](s.RequestFailedAt),
}
}