forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.go
193 lines (165 loc) · 3.85 KB
/
manager.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
package channel
import (
"chat/globals"
"chat/utils"
"errors"
"github.com/spf13/viper"
"time"
)
var ConduitInstance *Manager
var ChargeInstance *ChargeManager
var SystemInstance *SystemConfig
var PlanInstance *PlanManager
func InitManager() {
ConduitInstance = NewChannelManager()
ChargeInstance = NewChargeManager()
SystemInstance = NewSystemConfig()
PlanInstance = NewPlanManager()
}
func NewChannelManager() *Manager {
var seq Sequence
if err := viper.UnmarshalKey("channel", &seq); err != nil {
panic(err)
}
// sort by priority
manager := &Manager{
Sequence: seq,
Models: []string{},
PreflightSequence: map[string]Sequence{},
}
manager.Load()
return manager
}
func (m *Manager) Load() {
// load channels
for _, channel := range m.Sequence {
if channel != nil {
channel.Load()
}
}
// init support models
m.Models = []string{}
for _, channel := range m.GetActiveSequence() {
for _, model := range channel.GetHitModels() {
if !utils.Contains(model, m.Models) {
m.Models = append(m.Models, model)
}
}
}
// init preflight sequence
m.PreflightSequence = map[string]Sequence{}
for _, model := range m.Models {
var seq Sequence
for _, channel := range m.GetActiveSequence() {
if channel.IsHit(model) {
seq = append(seq, channel)
}
}
seq.Sort()
m.PreflightSequence[model] = seq
}
stamp := time.Now().Unix()
globals.SupportModels = m.Models
globals.V1ListModels = globals.ListModels{
Object: "list",
Data: utils.Each(m.Models, func(model string) globals.ListModelsItem {
return globals.ListModelsItem{
Id: model,
Object: "model",
Created: stamp,
OwnedBy: "system",
}
}),
}
}
func (m *Manager) GetSequence() Sequence {
return m.Sequence
}
func (m *Manager) GetActiveSequence() Sequence {
var seq Sequence
for _, channel := range m.Sequence {
if channel.GetState() {
seq = append(seq, channel)
}
}
seq.Sort()
return seq
}
func (m *Manager) GetModels() []string {
return m.Models
}
func (m *Manager) GetPreflightSequence() map[string]Sequence {
return m.PreflightSequence
}
// HitSequence returns the preflight sequence of the model
func (m *Manager) HitSequence(model string) Sequence {
return m.PreflightSequence[model]
}
// HasChannel returns whether the channel exists
func (m *Manager) HasChannel(model string) bool {
return utils.Contains(model, m.Models)
}
func (m *Manager) GetTicker(model, group string) *Ticker {
if !m.HasChannel(model) {
return nil
}
return NewTicker(m.HitSequence(model), group)
}
func (m *Manager) Len() int {
return len(m.Sequence)
}
func (m *Manager) GetMaxId() int {
var max int
for _, channel := range m.Sequence {
if channel.Id > max {
max = channel.Id
}
}
return max
}
func (m *Manager) SaveConfig() error {
viper.Set("channel", m.Sequence)
m.Load()
return viper.WriteConfig()
}
func (m *Manager) CreateChannel(channel *Channel) error {
channel.Id = m.GetMaxId() + 1
m.Sequence = append(m.Sequence, channel)
return m.SaveConfig()
}
func (m *Manager) UpdateChannel(id int, channel *Channel) error {
for i, item := range m.Sequence {
if item.Id == id {
m.Sequence[i] = channel
return m.SaveConfig()
}
}
return errors.New("channel not found")
}
func (m *Manager) DeleteChannel(id int) error {
for i, item := range m.Sequence {
if item.Id == id {
m.Sequence = append(m.Sequence[:i], m.Sequence[i+1:]...)
return m.SaveConfig()
}
}
return errors.New("channel not found")
}
func (m *Manager) ActivateChannel(id int) error {
for i, item := range m.Sequence {
if item.Id == id {
m.Sequence[i].State = true
return m.SaveConfig()
}
}
return errors.New("channel not found")
}
func (m *Manager) DeactivateChannel(id int) error {
for i, item := range m.Sequence {
if item.Id == id {
m.Sequence[i].State = false
return m.SaveConfig()
}
}
return errors.New("channel not found")
}